JBoss 系列七十六: jBPM 6 示例之 rewards-basic

概述

首先我们明确jBPM 6 示例之 rewards-basic与前一篇jBPM 6 示例之 rewards的异同,相同点它们都使用同样的流程,即如下:


不同之处有两个:

  1. rewards-basic演示如何在 JEE 6环境中整合使用 jBPM 6,而前一篇(jBPM 6 示例之 rewards)是在J2SE环境下运行rewards示例
  2. rewards-basic使用Key/Value缓存解决方案Infinispan存储每个节点运行的信息,而前一篇(jBPM 6 示例之 rewards)是将每个节点运行的信息直接输出。
本示例主要演示如下方面:

  • Human tasks - 如何在JEE6环境下运行 jBPM 6 Human tasks 
  • Persistence - jBPM 6 使用JPA/Hibernate存数运行状态数据
  • Transactions - EJB TransactionManagement来细化事务控制
  • Singleton session manager - jBPM 6 RuntimeManager提供的策略之一
  • Context dependency injection - 如何将jBPM 6与JEE6 CDI 整合
  • Infinispan - 流程运行时节点执行情况在Infinispan中保存
  • Maven - 通过Maven控制项目

运行 rewards-basic

最简单运行rewards-basic方法如下面两步:

1. 从http://sourceforge.net/projects/jbpm6examples/files/deployments/下载jbpm-demo-rewards-basic.war

2. 启动 JBoss 7(如软件安装及资料下载所示),部署jbpm-demo-rewards-basic.war(如使用4种方式部署应用到JBoss7/WildFly所示),部署完成既可以通过Web页面运行rewards-basic流程

另为我们也可以从源代码的方式编译生成jbpm-demo-rewards-basic.war,rewards-basic代码位于:https://github.com/kylinsoong/jbpm-6-examples/tree/master/rewards-basic,同样如软件安装及资料下载中描述的方法从github上克隆rewards-basic,使用Maven编译生成jbpm-demo-rewards-basic.war,

运行示例如下面几步:

1. 开始一个流程,在(http://localhost:8080/rewards-basic/)页面点击 Start如下:

JBoss 系列七十六: jBPM 6 示例之 rewards-basic_第1张图片

2. 点击John's Task,完成approve操作,如下:

JBoss 系列七十六: jBPM 6 示例之 rewards-basic_第2张图片

3. 点击Mary's Task,完成approve操作,如下:

JBoss 系列七十六: jBPM 6 示例之 rewards-basic_第3张图片

4. 点击Audit,查看流程节点执行情况,如下:

JBoss 系列七十六: jBPM 6 示例之 rewards-basic_第4张图片

jBPM 6 服务CDI注入

org.jbpm.demo.rewards.ejb.ProcessBean中我们注入RuntimeManager,如下所示:

@Inject
    @Singleton
    private RuntimeManager singletonManager;

org.jbpm.demo.rewards.ejb.TaskBean中我们注入TaskService,如下所示:

@Inject
    TaskService taskService;

流程执行事务控制

org.jbpm.demo.rewards.ejb.ProcessBean中我们注入UserTransaction如下所示:

@Resource
    private UserTransaction ut;

在startProcess()方法中,我们使用事务控制流程启动,如下:

ut.begin();

        try {
            // start a new process instance
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("recipient", recipient);
            ProcessInstance processInstance = ksession.startProcess("org.jbpm.demo.rewards-basic", params);

            processInstanceId = processInstance.getId() ;
            
            System.out.println("Process started ... : processInstanceId = " + processInstanceId);

            ut.commit();
        } catch (Exception e) {

使用JPA存储流程运行状态数据

jbpm-demo-rewards-basic.war的classes/META-INF/persistence.xml定义的内容如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence version="2.0"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                                 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd
                                 http://java.sun.com/xml/ns/persistence/orm 
                                 http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
             xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/persistence">

  <persistence-unit name="org.jbpm.demo.rewards" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:jboss/datasources/jBPMRewardsDemotDS</jta-data-source>
        <mapping-file>META-INF/JBPMorm.xml</mapping-file>
        <mapping-file>META-INF/Taskorm.xml</mapping-file>
        <class>org.drools.persistence.info.SessionInfo</class>
        <class>org.jbpm.persistence.processinstance.ProcessInstanceInfo</class>
        <class>org.drools.persistence.info.WorkItemInfo</class>
        <class>org.jbpm.persistence.correlation.CorrelationKeyInfo</class>
        <class>org.jbpm.persistence.correlation.CorrelationPropertyInfo</class>
        
        <class>org.jbpm.process.audit.ProcessInstanceLog</class>
        <class>org.jbpm.process.audit.NodeInstanceLog</class>
        <class>org.jbpm.process.audit.VariableInstanceLog</class>
        
        <!-- manager -->
        <class>org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo</class>

        <class>org.jbpm.services.task.impl.model.AttachmentImpl</class>
        <class>org.jbpm.services.task.impl.model.ContentImpl</class>
        <class>org.jbpm.services.task.impl.model.BooleanExpressionImpl</class>
        <class>org.jbpm.services.task.impl.model.CommentImpl</class>
        <class>org.jbpm.services.task.impl.model.DeadlineImpl</class>
        <class>org.jbpm.services.task.impl.model.DelegationImpl</class>
        <class>org.jbpm.services.task.impl.model.EscalationImpl</class>
        <class>org.jbpm.services.task.impl.model.GroupImpl</class>
        <class>org.jbpm.services.task.impl.model.I18NTextImpl</class>
        <class>org.jbpm.services.task.impl.model.NotificationImpl</class>
        <class>org.jbpm.services.task.impl.model.EmailNotificationImpl</class>
        <class>org.jbpm.services.task.impl.model.EmailNotificationHeaderImpl</class>
        <class>org.jbpm.services.task.impl.model.PeopleAssignmentsImpl</class>
        <class>org.jbpm.services.task.impl.model.ReassignmentImpl</class>
    
        <class>org.jbpm.services.task.impl.model.TaskImpl</class>
        <class>org.jbpm.services.task.impl.model.TaskDataImpl</class>
        <class>org.jbpm.services.task.impl.model.UserImpl</class>
    
        <!--BAM for task service -->
        <class>org.jbpm.services.task.impl.model.BAMTaskSummaryImpl</class>
    
        <!-- Event Classes -->
        <class>org.jbpm.services.task.audit.TaskEventImpl</class>
        
        <exclude-unlisted-classes>true</exclude-unlisted-classes>

    <properties>
      <property name="hibernate.max_fetch_depth" value="3"/>
      <property name="hibernate.hbm2ddl.auto" value="update" />
      <property name="hibernate.show_sql" value="false" />	
      <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
	  <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
      <property name="hibernate.id.new_generator_mappings" value="false"/>
    </properties>        
  </persistence-unit>
   
</persistence>

使用Infinispan存储流程节点运行的数据

我们在设计流程时在Script部分除了系统输出外,还添加使用Infinispan来存储节点运行数据,如下为开始节点Script内容:

long id = kcontext.getProcessInstance().getId();
String name = (String)kcontext.getVariable("recipient");
String log = "process " + id + " started by " + name;
System.out.println(log);
AuditDAO.Factory.get().addAudit(new Long(id), log);


你可能感兴趣的:(jta,jpa,jbpm,6,human,Tasks,infinispan)