|
|
使用JBPM工作流引擎测试的一个例子
|
|
|
|
本文提供使用jBPM工作流引擎测试的一个例子。
提供一个Persistence,用于存储全局的变量值,方便存储和获取
- public class Persistence {
-
private static Map variables = null;
-
private static String tmpfile = System.getProperty("java.io.tmpdir") + "/temp.object";
-
static{
-
- try{
- if(new File(tmpfile).exists()){
- FileInputStream in = new FileInputStream(tmpfile);
- ObjectInputStream s = new ObjectInputStream(in);
- variables = (Map)s.readObject();
- }
- if(variables == null){
- variables = new HashMap();
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
-
-
public static void setVariable(String name,Serializable object){
- if(variables != null){
- variables.put(name, object);
- }
- try {
- FileOutputStream fos = new FileOutputStream(tmpfile);
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- oos.writeObject(variables);
- oos.flush();
- oos.close();
- fos.flush();
- fos.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
public static Serializable getVariable(String name){
- if(variables != null){
-
return (Serializable)variables.get(name);
- }
-
return null;
- }
- }
-
1.首先使用流程设计器,创建一个简单的流程规则
2.创建数据库表,创建流程定义对象,并部署流程定义
-
-
public class Jbpm_01_CreateTable extends TestCase {
-
public void testCreateTable(){
- JbpmConfiguration.getInstance().createSchema();
- }
- }
-
-
public class Jbpm_02_DeployProcessDefinition extends TestCase {
-
static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
-
public void testDeployProcessDefinition(){
-
- ProcessDefinition processDefinition = ProcessDefinition.parseXmlResource("test01/processdefinition.xml");
-
- Persistence.setVariable("processName", processDefinition.getName());
-
- JbpmContext context = jbpmConfiguration.createJbpmContext();
- try{
- context.deployProcessDefinition(processDefinition);
- }catch(Exception e){
- e.printStackTrace();
- context.setRollbackOnly();
- }finally{
- context.close();
- }
- }
- }
-
3.定义公文Doucment 及其映射文件Doucment.hbm.xml
- public class Document {
-
private int id;
-
private String title;
-
private String content;
-
private Long processInstanceId;
-
public String getContent() {
-
return content;
- }
-
public void setContent(String content) {
- this.content = content;
- }
-
public int getId() {
-
return id;
- }
-
public void setId(int id) {
- this.id = id;
- }
-
public Long getProcessInstanceId() {
-
return processInstanceId;
- }
-
public void setProcessInstanceId(Long processInstanceId) {
- this.processInstanceId = processInstanceId;
- }
-
public String getTitle() {
-
return title;
- }
-
public void setTitle(String title) {
- this.title = title;
- }
- }
-
- <hibernate-mapping>
- <class table="tbl_document" name="Document">
- <id name="id">
- <generator class="native"/>
- </id>
- <property name="title"/>
- <property name="content"/>
- <property name="processInstanceId"/>
- </class>
- </hibernate-mapping>
-
4.创建公文并与流程实例绑定
- public class Jbpm_03_CreateDocument extends TestCase {
-
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
-
- public void testCreateDocument(){
-
-
-
- JbpmContext context = jbpmConfiguration.createJbpmContext();
-
- try{
-
- Document doc = new Document();
-
- doc.setTitle("测试公文"+new Random().nextInt(9999));
-
- context.getSession().save(doc);
-
- Persistence.setVariable("docId", doc.getId());
-
- }catch(Exception e){
-
- e.printStackTrace();
-
- context.setRollbackOnly();
-
- }finally{
-
- context.close();
-
- }
-
- }
-
- }
-
5.提交公文到流程,触发流程流转
- public class Jbpm_05_SubmitDocument extends TestCase {
-
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
-
-
-
- public void testSubmitDocument(){
-
-
-
- JbpmContext context = jbpmConfiguration.createJbpmContext();
-
- try{
-
-
-
- int docId = (Integer)Persistence.getVariable("docId");
-
- Document doc = (Document)context.getSession().load(Document.class, docId);
-
- long processInstanceId = doc.getProcessInstanceId();
-
- ProcessInstance processInstance = context.getProcessInstance(processInstanceId);
-
-
-
- processInstance.signal();
-
- }catch(Exception e){
-
- e.printStackTrace();
-
- context.setRollbackOnly();
-
- }finally{
-
- context.close();
-
- }
-
- }
-
- }
-
6.查看公文所处的当前任务节点
- public class Jbpm_06_CurrentNode extends TestCase {
-
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
-
-
-
- public void testCurrentNode(){
-
-
-
- JbpmContext context = jbpmConfiguration.createJbpmContext();
-
- try{
-
-
-
- int docId = (Integer)Persistence.getVariable("docId");
-
- Document doc = (Document)context.getSession().load(Document.class, docId);
-
- long processInstanceId = doc.getProcessInstanceId();
-
- ProcessInstance processInstance = context.getProcessInstance(processInstanceId);
-
- String currentNode = processInstance.getRootToken().getNode().getName();
-
- System.err.println("公文【"+doc.getTitle()+"】当前所处的环节" +
-
-
"是:"+currentNode+",流程实例是否已结束?"+processInstance.hasEnded());
-
- }catch(Exception e){
-
- e.printStackTrace();
-
- context.setRollbackOnly();
-
- }finally{
-
- context.close();
-
- }
-
- }
-
- }
-
7.获取流转个某个参与者处待处理的任务列表
- public class Jbpm_07_SearchMyTaskList extends TestCase {
-
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
-
-
-
- public void testSearchMyTaskList(){
-
-
-
- JbpmContext context = jbpmConfiguration.createJbpmContext();
-
- try{
-
- printTask(context,"张三");
-
- printTask(context,"李四");
-
- printTask(context,"王五");
-
- }catch(Exception e){
-
- e.printStackTrace();
-
- context.setRollbackOnly();
-
- }finally{
-
- context.close();
-
- }
-
- }
-
- private void printTask(JbpmContext context,String actorId){
-
- List taskInstances = context.getTaskMgmtSession().findTaskInstances(actorId);
-
- for (Iterator iter = taskInstances.iterator(); iter.hasNext();) {
-
- TaskInstance ti = (TaskInstance) iter.next();
-
- Integer docId = (Integer)ti.getProcessInstance().getContextInstance().getVariable("documnt");
-
- Document doc = (Document)context.getSession().load(Document.class, docId);
-
- System.err.println("正在等待【"+actorId+"】审批的公文是:"+doc.getTitle());
-
- }
-
- }
-
- }
-
8.参与者执行审批操作,触发流程流转到下一个环节
- public class Jbpm_08_NextNode extends TestCase {
-
- static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
-
-
-
- public void testNextNode(){
-
-
-
- JbpmContext context = jbpmConfiguration.createJbpmContext();
-
- try{
-
-
-
-
-
-
-
-
-
- Integer docId = (Integer)Persistence.getVariable("docId");
-
-
-
- nextStep(context,"张三",docId);
-
- nextStep(context,"李四",docId);
-
- nextStep(context,"王五",docId);
-
-
-
- }catch(Exception e){
-
- e.printStackTrace();
-
- context.setRollbackOnly();
-
- }finally{
-
- context.close();
-
- }
-
- }
-
- private void nextStep(JbpmContext context,String actorId,Integer docId){
-
- List taskInstances = context.getTaskMgmtSession().findTaskInstances(actorId);
-
- for (Iterator iter = taskInstances.iterator(); iter.hasNext();) {
-
- TaskInstance ti = (TaskInstance) iter.next();
-
- Integer _docId = (Integer)ti.getProcessInstance().getContextInstance().getVariable("documnt");
-
-
-
- if(docId.equals(_docId)){
-
- Document doc = (Document)context.getSession().load(Document.class, docId);
-
-
-
-
-
- ti.end();
-
- System.err.println("公文【"+doc.getTitle()+"】已被【"+actorId+"】审批完成,已提交到下一个环节");
-
- }
-
- }
-
- }
-
- }
-
|
|
|
|
|
|