自定义节点custom
调用用户代码,实现一个自定义的活动行为。
Demo:
流程定义文件:custom.jpdl.xml
<?xml version="1.0" encoding="UTF-8"?>
<process name="custom" xmlns="http://jbpm.org/4.4/jpdl">
<start name="start1" g="26,125,48,48">
<transition name="to printDots" to="printDots" g="-35,-16"/>
</start>
<custom name="printDots" g="152,125,92,52" class="com.itstudio.jbpm.custom.demo.PrintDots">
<transition name="to end1" to="end1" g="-16,-17"/>
</custom>
<end name="end1" g="308,128,48,48"/>
</process>
自定义行为类PrintDots.java
package com.itstudio.jbpm.custom.demo;
import java.util.Map;
import org.jbpm.api.activity.ActivityExecution;
import org.jbpm.api.activity.ExternalActivityBehaviour;
public class PrintDots implements ExternalActivityBehaviour{
private static final String NEWLINE = System.getProperty("line.separator");
public void signal(ActivityExecution execution, String signalName, Map<String, ?> parameters)
throws Exception {
execution.take(signalName);
}
@Override
public void execute(ActivityExecution execution) throws Exception {
String executionId = execution.getId();
String dots =
" "+".::::. " + NEWLINE +
" " +" " +" .:::::" +":::. " +
NEWLINE + " ::::::::::: " + NEWLINE +
" " + " ':::::::::::.. "
+ NEWLINE +" ::::" +":::::::::::' " + NEWLINE +" ':::::::::::. " + NEWLINE +
" .::::::::" +"::::::' " + NEWLINE +" " +".:::::::::::... " + NEWLINE +
" ::::::::::::::''" +
" " + NEWLINE +" .:::. '::::::::'':::: " + NEWLINE +
" .::::::::. " +
" ':::::' ':::: " + NEWLINE +" "
+".::::':::::::. " +" ::::: '::::. " + NEWLINE +" .:::::' '::::"
+":::::. ::::: ':::. " + NEWLINE +" .:::::' ':::::::::.::::: " +" '::. " +
NEWLINE +" .::::'' ':::::::::::::: '::. " + NEWLINE +" .::'' ':::::"
+"::::::: :::... " + NEWLINE +" ..:::: ':::::::::' " +
" .:' '''' " + NEWLINE +" ..''''':' ':::::.' " + NEWLINE;
System.out.println(dots);
execution.waitForSignal();
}
}
测试类:CustomTest.java
package com.itstudio.jbpm.custom.demo;
import org.jbpm.api.Configuration;
import org.jbpm.api.Execution;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
public class CustomTest {
public final Configuration configuration = new Configuration();
public final ProcessEngine processEngine=configuration.buildProcessEngine();
public final RepositoryService repositoryService= processEngine.getRepositoryService();
public final ExecutionService executionService=processEngine.getExecutionService();
public final TaskService taskService=processEngine.getTaskService();
private void deploy() {
repositoryService.createDeployment().
addResourceFromClasspath("com/itstudio/jbpm/custom/demo/custom.jpdl.xml").deploy();
System.out.println("发布成功");
}
private void test() {
ProcessInstance processInstance = executionService
.startProcessInstanceByKey("custom");
System.out.println("当前节点:" + processInstance.findActiveActivityNames());
Execution executionInPrintDots = processInstance
.findActiveExecutionIn("printDots");
String executionId = executionInPrintDots.getId();
// 调用下行方法,应该是调用自定义节点的signal方法,从而调用take方法离开该活动,转到结束
executionService.signalExecutionById(executionId, "to end1");
// 但发现结果是流程未结束,当前激活的活动还是printDots
System.out.println("进行到:" + processInstance.findActiveActivityNames());
System.out.println("是否结束:" + processInstance.isEnded());
}
public static void main(String[] args) {
CustomTest test = new CustomTest();
test.deploy();
test.test();
}
}
By Mr.Z