[jboss-svn-commits] JBL Code SVN: r18032 - labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actionhandlers.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Jan 22 10:23:15 EST 2008
Author: kurt.stam at jboss.com
Date: 2008-01-22 10:23:15 -0500 (Tue, 22 Jan 2008)
New Revision: 18032
Added:
labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actionhandlers/AsyncTest.java
labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actionhandlers/DummyAction.java
labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actionhandlers/Wait2SecondsAction.java
Log:
JBESB-1444, demonstrate how to make signaling asynchronous, by adding the async="true" attribute to node's jDPL.
Added: labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actionhandlers/AsyncTest.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actionhandlers/AsyncTest.java (rev 0)
+++ labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actionhandlers/AsyncTest.java 2008-01-22 15:23:15 UTC (rev 18032)
@@ -0,0 +1,167 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.soa.esb.services.jbpm.actionhandlers;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import junit.framework.JUnit4TestAdapter;
+
+import org.apache.log4j.Logger;
+import org.jbpm.JbpmConfiguration;
+import org.jbpm.JbpmContext;
+import org.jbpm.graph.def.Node;
+import org.jbpm.graph.def.ProcessDefinition;
+import org.jbpm.graph.exe.ProcessInstance;
+import org.jbpm.graph.exe.Token;
+import org.junit.Before;
+import org.junit.Test;
+/**
+ * Tests the capabilities of jBPM async, which we are using.
+ *
+ * @author kstam
+ *
+ */
+public class AsyncTest
+{
+ private static String PROCESS_DEF_XML_ASYNC = "testAsync.xml";
+ private static String PROCESS_DEF_XML_SYNC = "testSync.xml";
+ private static Logger logger = Logger.getLogger(AsyncTest.class);
+
+ /**
+ * If the node *after* myNode is set to async="true", then the signal
+ * command will return immediately, and run in a different transaction.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void sync() throws Exception
+ {
+ logger.info("Setting up jBPM");
+ JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
+
+ jbpmConfiguration.getJobExecutor().start();
+ JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
+ //Extract a process definition from the processdefinition.xml file.
+ ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource(PROCESS_DEF_XML_SYNC);
+ assertNotNull(processDefinition);
+ //Create an instance of the process definition.
+ jbpmContext.deployProcessDefinition(processDefinition);
+ ProcessInstance processInstance = jbpmContext.newProcessInstance("testSync");
+ long processInstanceId = processInstance.getId();
+ assertTrue(
+ "Instance is in start state",
+ "start".equals(processInstance.getRootToken().getNode().getName()));
+ jbpmContext.close();
+
+ jbpmConfiguration = JbpmConfiguration.getInstance();
+ jbpmContext = jbpmConfiguration.createJbpmContext();
+ processInstance = jbpmContext.loadProcessInstance(processInstanceId);
+
+ Token token = processInstance.getRootToken();
+ processInstance.signal();
+ assertEquals("mynode",token.getNode().getName());
+ jbpmContext.close();
+
+ long startTime = System.currentTimeMillis();
+// Send mynode a signal such that it goes to wait2seconds
+ jbpmContext = jbpmConfiguration.createJbpmContext();
+ processInstance = jbpmContext.loadProcessInstance(processInstanceId);
+ processInstance.signal();
+ jbpmContext.save(processInstance);
+ jbpmContext.close();
+
+ jbpmContext = jbpmConfiguration.createJbpmContext();
+ processInstance = jbpmContext.loadProcessInstance(processInstanceId);
+ token = processInstance.getRootToken();
+// Check that we are now in the end node.
+ String currentNode = token.getNode().getName();
+ long endTime = System.currentTimeMillis();
+ assertEquals("end",currentNode);
+ jbpmContext.close();
+ long elapsedMillies = endTime - startTime;
+ System.out.println("async elapsedMillies=" + elapsedMillies);
+ assertTrue(elapsedMillies > 2000);
+ }
+
+ /**
+ * If the node *after* myNode is set to async="true", then the signal
+ * command will return immediately, and run in a different transaction.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void async() throws Exception
+ {
+ logger.info("Setting up jBPM");
+ JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
+
+ jbpmConfiguration.getJobExecutor().start();
+ JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
+ //Extract a process definition from the processdefinition.xml file.
+ ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource(PROCESS_DEF_XML_ASYNC);
+ assertNotNull(processDefinition);
+ //Create an instance of the process definition.
+ jbpmContext.deployProcessDefinition(processDefinition);
+ ProcessInstance processInstance = jbpmContext.newProcessInstance("testAsync");
+ long processInstanceId = processInstance.getId();
+ assertTrue(
+ "Instance is in start state",
+ "start".equals(processInstance.getRootToken().getNode().getName()));
+ jbpmContext.close();
+
+ jbpmConfiguration = JbpmConfiguration.getInstance();
+ jbpmContext = jbpmConfiguration.createJbpmContext();
+ processInstance = jbpmContext.loadProcessInstance(processInstanceId);
+
+ Token token = processInstance.getRootToken();
+ processInstance.signal();
+ assertEquals("mynode",token.getNode().getName());
+ jbpmContext.close();
+
+ long startTime = System.currentTimeMillis();
+// Send mynode a signal such that it goes to wait2seconds
+ jbpmContext = jbpmConfiguration.createJbpmContext();
+ processInstance = jbpmContext.loadProcessInstance(processInstanceId);
+ processInstance.signal();
+ jbpmContext.save(processInstance);
+ jbpmContext.close();
+
+ jbpmContext = jbpmConfiguration.createJbpmContext();
+ processInstance = jbpmContext.loadProcessInstance(processInstanceId);
+ token = processInstance.getRootToken();
+// Check that we are now in the end node.
+ String currentNode = token.getNode().getName();
+ long endTime = System.currentTimeMillis();
+ assertEquals("wait2seconds",currentNode);
+ jbpmContext.close();
+ long elapsedMillies = endTime - startTime;
+ System.out.println("async elapsedMillies=" + elapsedMillies);
+ assertTrue(elapsedMillies < 2000);
+ }
+
+ public static junit.framework.Test suite(){
+ return new JUnit4TestAdapter(AsyncTest.class);
+ }
+
+}
Property changes on: labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actionhandlers/AsyncTest.java
___________________________________________________________________
Name: svn:eol-style
+ native
Added: labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actionhandlers/DummyAction.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actionhandlers/DummyAction.java (rev 0)
+++ labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actionhandlers/DummyAction.java 2008-01-22 15:23:15 UTC (rev 18032)
@@ -0,0 +1,22 @@
+/**
+ *
+ */
+package org.jboss.soa.esb.services.jbpm.actionhandlers;
+
+import org.jbpm.graph.def.ActionHandler;
+import org.jbpm.graph.exe.ExecutionContext;
+
+/**
+ * @author kstam
+ *
+ */
+public class DummyAction implements ActionHandler
+{
+ private static final long serialVersionUID = 1L;
+
+ public void execute (ExecutionContext executionContext) throws Exception
+ {
+ System.out.println("DummyAction fired.");
+ }
+
+}
Property changes on: labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actionhandlers/DummyAction.java
___________________________________________________________________
Name: svn:eol-style
+ native
Added: labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actionhandlers/Wait2SecondsAction.java
===================================================================
--- labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actionhandlers/Wait2SecondsAction.java (rev 0)
+++ labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actionhandlers/Wait2SecondsAction.java 2008-01-22 15:23:15 UTC (rev 18032)
@@ -0,0 +1,31 @@
+/**
+ *
+ */
+package org.jboss.soa.esb.services.jbpm.actionhandlers;
+
+import org.jbpm.graph.def.ActionHandler;
+import org.jbpm.graph.exe.ExecutionContext;
+
+/**
+ * @author kstam
+ *
+ */
+
+public class Wait2SecondsAction implements ActionHandler{
+
+ private static final long serialVersionUID = 1L;
+
+ public void execute (ExecutionContext executionContext) throws Exception
+ {
+ for (int i=0; i<2; i++){
+ try {
+ Thread.sleep(1000);
+ System.out.println("Waited " + (i+1) + " seconds");
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ System.out.println("Done Waiting");
+ }
+
+}
Property changes on: labs/jbossesb/branches/JBESB_4_2_1_GA_CP/product/services/jbpm/src/test/java/org/jboss/soa/esb/services/jbpm/actionhandlers/Wait2SecondsAction.java
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the jboss-svn-commits
mailing list