[jboss-user] [JBoss jBPM] - Re: Stateless Process Engine executor threading issues

jorgemoralespou_2 do-not-reply at jboss.com
Thu Mar 1 13:16:34 EST 2007


Hi Ronald, I'm posting here the code.

Web service

  | @WebService(name = "ProcessTestService", targetNamespace = "http://test/ws", serviceName = "ProcessTestService")
  | @SOAPBinding(style = SOAPBinding.Style.RPC)
  | public class ProcessTestService {
  | 
  |    public String testInTransaction(String param1) throws Exception {
  |       Map request = new HashMap();
  |       StatelessEngine engine = new StatelessEngine();
  |       Object o = engine.executeProcessInstance("test-som-process", request);
  |       return "result";
  |    }
  |    
  |    @WebMethod
  |    public String test(String param1) throws Exception {
  |       String retValue = null;
  |       UserTransaction tx = getUserTransaction();
  |       tx.begin();
  |       try {
  |          retValue = testInTransaction(param1);
  |          tx.commit();
  |       } catch (Exception e) {
  |          tx.rollback();
  |          throw e;
  |       }
  |       return retValue;
  |    }
  |    
  |    private UserTransaction getUserTransaction() {
  |       UserTransaction ut = null;
  |       try {
  |          InitialContext ctx = new InitialContext();
  |          ut = (UserTransaction) ctx.lookup("UserTransaction");
  |       } catch (NamingException e) {
  |          appLog.error("Error obtaining the UserTransaction", e);
  |       }
  |       return ut;
  |    }
  | }
  | 

 The engine 

  | import java.util.Map;
  | import org.jbpm.JbpmContext;
  | import org.jbpm.graph.def.ProcessDefinition;
  | import org.jbpm.graph.exe.ProcessInstance;
  | import org.jbpm.graph.node.EndState;
  | import com.hp.som.bpm.SomJbpmEngineConfigurationMBean;
  | import javax.naming.Context;
  | import javax.naming.InitialContext;
  | import org.jbpm.JbpmConfiguration;
  | 
  | public class StatelessEngine{
  |   
  |    public Object executeProcessInstance(String processName, Map request) throws Exception{
  |       JbpmContext ctx = null;
  |       try{
  |          ctx = getJbpmConfiguration().createJbpmContext();
  |          
  |          // Get process definition
  |          ProcessDefinition processDefiniton = ctx.getGraphSession().findLatestProcessDefinition(processName);
  |          if (processDefiniton == null) {
  |             throw new Exception("Process definition not found");
  |          }
  |          
  |          ProcessInstance instance = new ProcessInstance(processDefiniton);
  |          instance.getContextInstance().setTransientVariable("REQUEST", request);
  |          
  |          // Execute process
  |          do{
  |             instance.getRootToken().signal();
  |          }while (!instance.getRootToken().hasEnded());
  |          
  |          // Check that execution is on a end state. if not, throw exception
  |          if (!EndState.class.isAssignableFrom(instance.getRootToken()
  |          .getNode().getClass())) {
  |             throw new Exception("Process finished not in an End Node");
  |          }
  |          
  |          Object response = instance.getContextInstance().getTransientVariable("RESPONSE");
  |          return response;
  |       } finally {
  |          if (ctx != null) ctx.close();
  |       }
  |    }
  |    
  |    
  |    private static JbpmConfiguration conf = null;
  |    
  |    protected JbpmConfiguration getJbpmConfiguration() throws Exception {
  |       if (this.conf == null){
  |          JbpmConfiguration config = null;
  |          String jndiName = null;
  |          Context ctx = new InitialContext();
  |          
  |          // Get jndi name for jbpm configuration
  |          jndiName = "java:/jbpm/JbpmConfiguration";
  |          config = (JbpmConfiguration)ctx.lookup(jndiName);
  |          this.conf = config;
  |       }
  |       return this.conf;
  |    }
  |    
  | }
  | 

 The test process

  | <process-definition 
  |   xmlns=""
  |   name="test-som-process">
  |    <start-state name="start">
  |       <transition name="" to="node1"></transition>
  |    </start-state>
  |    <end-state name="end">
  |    </end-state>
  |    <node name="node1">
  |       <action class="com.hp.som.test.TestActionHandler"></action>
  |       <transition name="" to="node3"></transition>
  |    </node>
  |    <node name="node3">
  |       <action class="com.hp.som.test.TestActionHandler"></action>
  |       <transition name="" to="node4"></transition>
  |    </node>
  |    <node name="node4">
  |       <action class="com.hp.som.test.TestActionHandler"></action>
  |       <transition name="" to="node5"></transition>
  |    </node>
  |    <node name="node5">
  |       <action class="com.hp.som.test.TestActionHandler"></action>
  |       <transition name="" to="node6"></transition>
  |    </node>
  |    <node name="node6">
  |       <action class="com.hp.som.test.TestActionHandler"></action>
  |       <transition name="" to="node8"></transition>
  |    </node>
  |    <node name="node8">
  |       <action class="com.hp.som.test.TestActionHandler"></action>
  |       <transition name="" to="end"></transition>
  |    </node>
  | </process-definition>
  | 

And finally test handler

  | public class TestActionHandler  implements ActionHandler {
  |    public void execute(ExecutionContext context) throws Exception {
  |       try {
  |          // Create a process logger whith process name
  |          customExecute(context);
  |          // The action have been executed correctly
  |          context.getToken().signal();
  |       } catch (Exception e) {
  |          e.printStackTrace();
  |          throw e;
  |       }
  |    }
  |    
  |    protected void customExecute(ExecutionContext context) throws Exception {
  |       String param = "Do nothing important";
  |    }
  |    
  | }
  | 

When you load test the web service, you can see how response time doubles as client doubles, but cpu stays low.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4024414#4024414

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4024414



More information about the jboss-user mailing list