Total Pageviews

2017/07/07

[webMethods] How to delete threads in webMethods

Problem
假設在 OpcTest 此 package 中會執行以下幾個 flow services

若希望在 package 被 disable 時,能把這幾個 flow services 的 threads 砍掉,該如何做?


How-To
解決方式如下,共有兩個步驟:
Step 1. 建立一個 Java Service,source code 如下:
public static final void terminationService(IData pipeline) throws ServiceException {
        IDataCursor pipelineCursor = pipeline.getCursor();
        String threadNameToKill = IDataUtil.getString(pipelineCursor, "threadNameToKill");
        pipelineCursor.destroy();
        
        if(threadNameToKill == null)
        {
            // Fill in the toppest folder name.
            // Take the OpcTest package for example, it should be "group1".
            threadNameToKill = "group1";
        }
        
        Thread current = Thread.currentThread();
        ThreadGroup root = current.getThreadGroup();
                
        while (root.getParent() != null) {
            root = root.getParent();
        }
        
        Thread [] threads = new Thread[1000];
        int count = root.enumerate(threads, true);
        
        for(int i = 0 ; i < count ; i++)
        {
            if(threads[i] instanceof ServerThread)
            {
                ServerThread temp = (ServerThread) threads[i];
                
                String threadName = temp.getName();
                
                logger( "[ Thread Name ]: " + threadName);
                if(threadName.contains(threadNameToKill))
                {
                    logger( "[ Stopping ]: " + threadName);
                    threads[i].stop();
                }
        
            }
        }       
            
    }

Step 2. 將此 Java Service 設定為 shutdown service,如此一來,當 package 被 disable 或 integration server 被 shutdown 時,都會執行此 shutdown service





No comments: