经过JBPM的安装,我们对JBPM有了初步的认识,现在我们来做一个简单的HelloWorld流程,strart-->my-->end。
1、创建一个Dynamic web project
2、把基本的jar包COPY至WEB-INF/lib 下:
antlr-2.7.6.jar
bsh-2.0b1.jar
commosn-collections-3.1.jar
commons-io-1.3.2.jar
commons-logging-api-1.1.jar
dom4j.jar
hibernate-cglib-repack-2.1_3.jar
hibernate3.jar
javassist-3.4.GA.JAR
jbpm-identity.jar
jbpm-jpdl.jar (3.2.3)
jta-1.1.jar
mysql-connector-java-5.1.1-rc-bin.jar
slf4j-api-1.5.2.jar
slf4j-nop-1.5.2.jar
3、创建 example.deployProcess.class
代码如下:
package example;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
public class deployProcess {
/**
* @param args
*/
public static void main(String[] args) {
//定义流程helloworld 的XML
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
"<process-definition name='helloworld'> " +
" <start-state name='start'> "+
" <transition to='my' /> "+
" </start-state> "+
" <state name='my'> "+
" <transition to='end' />" +
" </state> "+
" <end-state name='end' /> "+
"</process-definition>"
);
JbpmConfiguration config=JbpmConfiguration.getInstance();
JbpmContext context=config.getCurrentJbpmContext();
if(context == null)
{
context = config.createJbpmContext();
}
//部署流程
context.deployProcessDefinition(processDefinition);
context.close();
config.close();
}
}
运行后,我们可以进入mysql,查看流程是否发布成功:
SELECT * FROM jbpm.jbpm_processdefinition j;
可以查询出刚才发布的流程。
我们也可以创建一个类来执行流程:
package example;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
public class ExecuteFlow {
/**
* @param args
*/
public static void main(String[] args) {
JbpmConfiguration config=JbpmConfiguration.getInstance();
JbpmContext context=config.getCurrentJbpmContext();
if(context == null)
{
context = config.createJbpmContext();
}
//读取最近发布的名称为helloworld的流程
ProcessDefinition pdf = context.getGraphSession().findLatestProcessDefinition(
"helloworld");
ProcessInstance pi = new ProcessInstance(pdf);
//根令牌
Token token = pi.getRootToken();
//执行开始活动
token.signal();
//进入"my"活动,输出当前等待执行的活动名称
System.out.println(token.getNode().getName());
//继续执行至结束
token.signal();
context.close();
config.close();
}
}