Tuesday, February 9, 2010

WLI processes terminator!

a very similar utility has already been published here
http://javamonamour.blogspot.com/2009/11/querying-and-terminating-wli-instances.html


package com.acme.integration.tools;


import java.util.Iterator;
import java.util.Set;

import javax.management.MBeanException;
import javax.naming.Context;
import javax.naming.NamingException;

import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;

import com.bea.wli.bpm.runtime.ProcessInstanceNotFoundException;
import com.bea.wli.bpm.runtime.ProcessNotFoundException;
import com.bea.wli.bpm.runtime.ProcessStatus;
import com.bea.wli.knex.runtime.core.bean.ConversationNotFoundException;
import com.bea.wli.management.runtime.ProcessInstanceInfo;
import com.bea.wli.management.runtime.ProcessInstanceQuery;
import com.bea.wli.management.runtime.ProcessInstanceQueryResult;
import com.bea.wli.management.runtime.ProcessRuntimeMBean;

public class WLITerminator {
   String SERVICE_URI = "url/to/your/process.jpd";

    private static final ProcessStatus[] processStatusToTerminate = new ProcessStatus[] {
        ProcessStatus.ABORTED, ProcessStatus.COMPLETED, ProcessStatus.FROZEN, ProcessStatus.RUNNING};
   
    public static void main(String[] args) throws NamingException {
        terminateAll("weblogic", "weblogic", "t3://localhost:7001");       
    }

    private static void terminateAll(String username, String password, String url) throws NamingException {
        Environment env = new Environment();
        env.setSecurityPrincipal(username);
        env.setSecurityCredentials(password);
        env.setProviderUrl(url);
        Context ctx = env.getInitialContext();
        MBeanHome home = (MBeanHome)ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
        System.out.println("Got the Server-specific MBeanHome: " + home);
        Set s = home.getMBeansByType("ProcessRuntime");
        Iterator it = s.iterator();   
        try {
            if (it.hasNext()) {
                ProcessRuntimeMBean bean = (ProcessRuntimeMBean)it.next();
                ProcessInstanceQuery query = new ProcessInstanceQuery();
                //query.setServiceURI(SERVICE_URI);
                for (ProcessStatus p : processStatusToTerminate) {
                    query.setStatus(p);
                    terminateInstances(bean, query);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }   
       
    }

    private static void terminateInstances(ProcessRuntimeMBean bean,
            ProcessInstanceQuery query) throws MBeanException,
            ConversationNotFoundException, ProcessNotFoundException,
            ProcessInstanceNotFoundException {
        ProcessInstanceQueryResult info = bean.getProcessInstances(query);
        int count = info.getResultCard();
        String[] instances = info.getInstanceIds();
        System.out.println("found " + instances.length + " instances");
        for (int i = 0 ; i <  instances.length; i++) {
            String instanceId = instances[i];
            ProcessInstanceInfo pi = bean.getProcessInstanceInfo(instanceId, false);
            System.out.println(pi.getServiceURI() + " " + instanceId);
            bean.terminate(pi.getServiceURI(), instanceId);
        }
    }
}


and here are the .classpath entries necessary:

classpathentry kind="lib" path="C:/beawli/wli_10.3/lib/jpdpublic.jar"  
classpathentry kind="lib" path="C:/beawli/wlserver_10.3/server/lib/wls-api.jar"  
classpathentry kind="lib" path="C:/beawli/modules/com.bea.core.weblogic.security.identity_1.1.0.0.jar"
classpathentry kind="lib" path="C:/beawli/modules/com.bea.core.weblogic.workmanager_1.4.0.0.jar"
classpathentry kind="lib" path="C:/beawli/wlserver_10.3/server/lib/weblogic.jar"

No comments: