⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asyncexecutiondbtest.java

📁 jbpm demo 是一款非常不错的开源工作流,简单易用,适合扩张开发!
💻 JAVA
字号:
/*
 * 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.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.quit();
    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 {
    long timeout = 10000L;
    // wait till the receiver has received all the messages of the senders
    long start = System.currentTimeMillis();
    long deadline = start + timeout;
    boolean isDone = false; 
    while (! isDone) {
      if (deadline<System.currentTimeMillis()) {
        log.debug("messages couldn't be processed within "+timeout+" milliseconds");
        commandExecutor.getStackTrace();
        log.debug("command executor state: "+commandExecutor.getState());
        log.debug("command executor stack trace: ");
        StackTraceElement[] commandExecutorStackTrace = commandExecutor.getStackTrace();
        for( int i = 0; i<commandExecutorStackTrace.length; i++) {
          log.debug("command executor stack trace: "+commandExecutorStackTrace[i]);
        }
        fail("messages couldn't be processed within "+timeout+" milliseconds");
      }
      Thread.sleep(200);
      newTransaction();
      
      DbMessageService dbMessageService = (DbMessageService) jbpmContext.getServices().getMessageService();
      if (! dbMessageService.hasMessages(Command.DEFAULT_CMD_DESTINATION)) {
        isDone = true;
      }
    }
  }
  
  private static Log log = LogFactory.getLog(AsyncExecutionDbTest.class);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -