/*
* JBoss, Home of Professional Open Source
* Copyright 2005, 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.jbpm.msg.command;
import java.util.ArrayList;
import java.util.List;
import org.jbpm.command.Command;
import org.jbpm.db.AbstractDbTestCase;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.def.Node;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.msg.db.DbMessageService;
public class AsyncExecutionDbTest extends AbstractDbTestCase {
static List nodes = new ArrayList();
public static class RecordNode implements ActionHandler {
private static final long serialVersionUID = 1L;
public void execute(ExecutionContext executionContext) throws Exception {
Node node = executionContext.getNode();
nodes.add(node.getName());
node.leave(executionContext);
}
}
CommandExecutorThread commandExecutor = null;
public void setUp() throws Exception {
super.setUp();
commandExecutor = new CommandExecutorThread(jbpmConfiguration);
commandExecutor.start();
}
public void tearDown() throws Exception {
commandExecutor.interrupt();
commandExecutor.join();
}
public void testAsyncExecution() throws Exception {
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
"<process-definition>" +
" <start-state>" +
" <transition to='one' />" +
" </start-state>" +
" <node async='true' name='one'>" +
" <action class='org.jbpm.msg.command.AsyncExecutionDbTest$RecordNode' />" +
" <transition to='two' />" +
" </node>" +
" <node async='true' name='two'>" +
" <action class='org.jbpm.msg.command.AsyncExecutionDbTest$RecordNode' />" +
" <transition to='three' />" +
" </node>" +
" <node async='true' name='three'>" +
" <action class='org.jbpm.msg.command.AsyncExecutionDbTest$RecordNode' />" +
" <transition to='end' />" +
" </node>" +
" <end-state name='end' />" +
"</process-definition>"
);
processDefinition = saveAndReload(processDefinition);
ProcessInstance processInstance = new ProcessInstance(processDefinition);
processInstance.signal();
jbpmContext.save(processInstance);
assertEquals(processDefinition.getNode("one"), processInstance.getRootToken().getNode());
newTransaction();
waitTillMsgProcessed();
processDefinition = graphSession.loadProcessDefinition(processDefinition.getId());
processInstance = graphSession.loadProcessInstance(processInstance.getId());
assertTrue(processInstance.hasEnded());
assertEquals(processDefinition.getNode("end"), processInstance.getRootToken().getNode());
}
public void testAsyncTaskInstanceCreation() throws Exception {
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
"<process-definition>" +
" <start-state>" +
" <transition to='one' />" +
" </start-state>" +
" <task-node async='true' name='one'>" +
" <task name='first task' />" +
" <transition to='two' />" +
" </task-node>" +
" <task-node async='true' name='two'>" +
" <task name='second task' />" +
" <transition to='end' />" +
" </task-node>" +
" <end-state name='end' />" +
"</process-definition>"
);
processDefinition = saveAndReload(processDefinition);
ProcessInstance processInstance = new ProcessInstance(processDefinition);
processInstance.signal();
jbpmContext.save(processInstance);
assertEquals(processDefinition.getNode("one"), processInstance.getRootToken().getNode());
newTransaction();
waitTillMsgProcessed();
}
private void waitTillMsgProcessed() throws InterruptedException {
// wait till the receiver has received all the messages of the senders
boolean isDone = false;
while (! isDone) {
Thread.sleep(200);
newTransaction();
DbMessageService dbMessageService = (DbMessageService) jbpmContext.getServices().getMessageService();
if (! dbMessageService.hasMessages(Command.DEFAULT_CMD_DESTINATION)) {
isDone = true;
}
}
}
}