JBPM6入门资料: Spring4 + Hibernate4 + JBPM6整合

参考资料:
========================================
Eclipse的link方式安装JBPM6插件 http://blog.csdn.net/dcz1001/article/details/19905035
Eclipse的link方式安装JBPM6 [bpmn2 Process Editor]插件 http://blog.csdn.net/dcz1001/article/details/19905035 但是这种方法无法安装bpmn2 diagram Editor,需要到http://eclipse.org/bpmn2-modeler/downloads.php这个地址来安装,但还需要配置bpmn2,jbpm,drools的环境才能正常使用(eclipse配置和工程属性的配置都要)。
安装JBPM6运行环境(JBPM6学习之二) http://blog.csdn.net/dcz1001/article/details/19910039
创建第一个JBPM6项目并且运行自带的helloword例子(JBPM6学习之三) http://blog.csdn.net/dcz1001/article/details/19909201
Spring JBPM6整理例子代码: https://github.com/kylinsoong/jbpm-6-examples/tree/master/spring-integration
jBPM 5/jBPM 6 基本概念,示例介绍,深入理解,工作流应用开发指南 http://blog.csdn.net/kylinsoong/article/details/17262605
这里是以前配置 JBPM5的资料: http://panyongzheng.iteye.com/blog/1865507
jBPM6与BPMN2.0 http://www.open-open.com/doc/view/471d5cd3ce15481d9203d0158f6611a3最后面的代码有用
JPA的persistence.xml文件的Hibernate配置: http://www.cnblogs.com/luxh/archive/2012/05/24/2516282.html
Spring Hibernate Integration Using Java Persistence API http://www.studytrails.com/frameworks/spring/spring-hibernate-jpa.jsp
Hibernate 4.3 and JPA 2.1 Example http://www.roseindia.net/hibernate/hibernate4.3/Hibernate-4.3-and-JPA-2.1-Example.shtml



WorkItemHandler,类似于自定义Task。
   人工任务节点在JBPM5中是一个异步WorItem,JBPM5中已经带有一个WorkItemHandler的人工任务的实现类是WSHumanTaskHandler,这个handler能同一个Task Service通信(由jBPM5提供),并在其中创建一个新的任务。然后,它将等待别人(使用人工客户端API)来完成/取消该任务。
   在jbpm5中你可以实现WorkItemHandler接口, 不使用默认实现,根据你需要进行。
   JBPM6提供了一个默认的WorkItemHandler的实现org.jbpm.services.task.wih.LocalHTWorkItemHandler。
jBPM6 - Custom Work Item: Hello Process Example https://community.jboss.org/people/bpmn2user/blog/2013/12/29/jbpm6--custom-work-item-hello-process-example
How to Customize Work Item Handlers in jBPM 6 http://simplesassim.wordpress.com/2014/01/21/how-to-customize-work-item-handlers-in-jbpm-6/





一些有用的例子:
jbpm5通过Claim实现动态指定审批人的过程 http://my.oschina.net/remind2010/blog/131491
jBPM 6 Deploy a process from Java code https://community.jboss.org/thread/234075?start=0&tstart=0
JBPM5 Example -  Human Task Forms with Variables https://community.jboss.org/people/bpmn2user/blog/2011/02/21/jbpm5-example-for-forms-with-variables
JBPM6 - RuleTask Example Using a Rule File https://community.jboss.org/people/bpmn2user/blog/2013/12/20/jbpm6--ruletask-example-using-a-rule-file
JBPM6 - RuleTask Example Using an Excel Decision Table https://community.jboss.org/people/bpmn2user/blog/2013/12/21/jbpm6--ruletask-example-using-an-excel-decision-table
JBoss 系列三十七:jBPM5示例之 Rule Task http://blog.csdn.net/kylinsoong/article/details/13631273


流程图介绍:
==================================
如何避免业务流程建模中的常见陷阱 http://tech.ddvip.com/2009-10/1256223495136207_3.html
BPMN业务流程建模标注  http://sapblog.org/archives/446.html
BPMN这点事-BPMN基本元素(上) http://blog.csdn.net/ronghao100/article/details/6707276
BPMN这点事-BPMN基本元素(下) http://blog.csdn.net/ronghao100/article/details/6711779
JBPM6入门资料: Spring4 + Hibernate4 + JBPM6整合_第1张图片
网关的一些说明: XOR,OR,AND需要和Diverging(分流),Converging(汇聚)组合使用
可选分支用 exclusive (XOR) gateway 建立,它显示为一个菱形,也可以用“X”标记。只能选择一条路径,每个出口用布尔表达式来控制将走两条路径中的哪一条
包含分支用 inclusive (OR) gateway 建立,它显示为一个内含圆形的菱形。
并行分支用 parallel (AND) gateway 建立,它显示为一个包含“+”的菱形。也称为"与"网关,把顺序流分成并行的分支


数据
1.读取和设定bpmn的变量一定要使用:kcontext.get/setVariable();
============================================
jBPM 6 中 Process Variable 和 Task Variable 以及它们之间的Mapping模式 http://blog.csdn.net/kylinsoong/article/details/17998427
工作流的变量:JBPM5 Setting process variables at startup http://www.mastertheboss.com/jbpm5/jbpm5-setting-process-variables-at-startup
定义bpmn变量,点击bpmn空白,看属性,增加三个变量
[var1,var2]
//传输数据到bpmn文件的变量
//把传值给工作流里面的变量
Map<String, Object> inputParams = new HashMap<String, Object>();
inputParams.put("var1", "This is a test message.");
inputParams.put("var2", 100);
ksession1.startProcess(proId, inputParams);
//在bpmn的script task里面处理java传进来的数据
System.out.println("++++++++++++++++++++++++++++++++++++++++++ScriptTask1");
System.out.println("var1="+var1); 
System.out.println("var2="+var2); 
kcontext.setVariable("var1", "Script Task1 access to Var1");
kcontext.setVariable("var2", 10);

User Task跟java互交参数的例子: https://community.jboss.org/thread/235848
Process Variable位于Process 上下文中,Task Variable 位于Task 上下文,Task Variable 被 Task WorkItem使用,Task 通过Input Mapping获取Process 上下文中的变量,Task 通过Output Mapping将自己的变量传输给Process 上下文中。
从java或者Script Task把数据传给User Task
1. 定义bpmn变量,点击bpmn空白,看属性,增加三个变量
[age,content,name]
2. 设定bpmn的user task
paramterMapping: 从其他任务传进人工任务,或者从java读取人工任务。
input_age:age
input_content:content
input_name:name
resultMapping: 从其他任务输出人工任务,或者从java传入人工任务。
output_age:age
output_content:content
output_name:name
3. 使用, 注意,一定要在ksession.startProcess()的第二个参数传入包含变量的Map传入,或者在流程的第一个script task做初始化操作kcontext.setVariable("message",""),否则input_和output_的变量是无法读取和访问的
//java=>Script Task   设定[age,content,name]
Map<String, Object> params = new HashMap<String, Object>();
params.put("name", "krisv");
params.put("age", 35);
params.put("content", "Yearly performance evaluation");
ksession.startProcess("org.jbpm.demo.parameters", params);

//taskService->InternalTaskService  读取之前设定的[age,content,name]
//user task开始之前。通过这个获得paramterMapping的信息
Map<String, Object> content = taskService.getTaskContent(Long.parseLong(taskId));

//java=>User Task   修改[age,content,name]
Map<String, Object> results = new HashMap<String, Object>();
results.put("output_name", "Kylin");
results.put("output_age", 29);
results.put("output_content", "performance evaluation finish");
taskService.complete(task.getId(), "krisv", results);

//在bpmn文件读取和操作[age,content,name]
String name_ = (String)kcontext.getVariable("name"); 
Integer age_ = (Integer)kcontext.getVariable("age"); 
String content_ = (String)kcontext.getVariable("content"); 



实现类:
====================================
任务的定义:workflow metadata (jbpm6) https://community.jboss.org/thread/233802?start=0&tstart=0
FormProviderServiceImpl.getFormDisplayTask (jbpm6) https://community.jboss.org/thread/233872?start=0&tstart=0
jbpm-kie-services,很多service都在里面,给程序提供很多帮助,比如官方 http://docs.jboss.org/jbpm/v6.0.1/userguide/jBPMTaskService.html后面提到的TaskQueryService taskQueryService;  TaskContentService taskContentService;都能在里面找到。
Task taskById = taskQueryService.getTaskInstanceById(Long.parseLong(taskId));
        Content contentById = taskContentService.getContentById(taskById.getTaskData().getDocumentContentId());
        ContentMarshallerContext context = taskContentService.getMarshallerContext(taskById);
        Object unmarshalledObject = ContentMarshallerHelper.unmarshall(contentById.getContent(), context.getEnvironment(), context.getClassloader());
        if (!(unmarshalledObject instanceof Map)) {
            throw new IllegalStateException(" The Task Content Needs to be a Map in order to use this method and it was: "+unmarshalledObject.getClass());

        }
        Map<String, Object> content1 = (Map<String, Object>) unmarshalledObject;


jBPM 6 Web application example http://www.mastertheboss.com/jbpm6/jbpm-6-web-application-example
jBPM 6 with spring http://planet.jboss.org/post/jbpm_6_with_spring
kie-spring https://github.com/droolsjbpm/droolsjbpm-integration/tree/master/kie-spring
JBPM6 Spring Persistence and Runtime Manager https://community.jboss.org/thread/231759
论坛一个帖子spring+jbpm6 https://community.jboss.org/thread/235092
How to Customize Work Item Handlers in jBPM 6 https://simplesassim.wordpress.com/tag/jbpm/
jbpm6 mysql http://www.mastertheboss.com/jbpm6/introduction-to-jbpm-6
UserTask selected actors problem in jbpm6 https://community.jboss.org/thread/240319

两个有用的整合例子源码 https://community.jboss.org/thread/235092
sjhweb.tar.gz这个文件有Spring+Hibernate+Mysql的整合,但有问题,可是I have changed hibernate4.3.4 to 4.2.0.SP1 and jbpm6.0.1 to 6.1.0.Beta1. Everything works well now.
test-spring-ht-jta.zip里面的persistence.xml比较完整,可以参考
Hibernate persistence.xml examples http://www.xinotes.net/notes/note/1214/



org.kie.spring.factorybeans.RuntimeManagerFactoryBean被org.jbpm.runtime.manager.impl.RuntimeManagerFactoryImpl替换 http://mswiderski.blogspot.com/2013/09/jbpm6-samples-with-runtimemanager.html


几个个UserGroupCallback
1. 自己实现 org.kie.internal.task.api.UserGroupCallback接口; https://github.com/jsvitak/jbpm-6-examples/blob/master/rewards-basic/src/main/java/org/jbpm/examples/util/RewardsUserGroupCallback.java
2. 使用JBossUserGroupCallbackImpl
RuntimeEnvironmentBuilder builder = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().entityManagerFactory(emf).userGroupCallback(new JBossUserGroupCallbackImpl("classpath:/usergroups.properties")).knowledgeBase(kbase);  

3. MVELUserGroupCallbackImpl


官方User Task的文档: http://docs.jboss.org/jbpm/v6.0/userguide/jBPMTaskService.html
User Task的生命周期
JBPM6入门资料: Spring4 + Hibernate4 + JBPM6整合_第2张图片
当一个流程实例的人工任务结点被触发,人工任务实例将被创建。当创建完成后,人工任务进入Created 状态。
当人工任务的Wait for completion 属性为 False时,流程会立即继续执行,否则要等待任务完成或终止。

进入Created 状态,任务将显示所有负责执行的actors。等待他们来获取任务。一旦有一个actor 获取任务,任务进入 Reserved 状态。用户决定开始执行任务,此时任务进入 InProgress 状态。任务开始执行,执行完成后,用户必须完成任务。此时用户进入 Completed 状态。用户也可以决定任务已经失败,此时任务进入 Failed 状态。
上面介绍的正常的生命周期,其它包括:
Delegating /forwarding  任务, 分配任务给另外一个 actor 来处理
Revoking  回滚任务
Temporarly suspending and resuming  临时挂起或继续任务
Stopping a task in progress 停止正在执行的任务
Skipping   跳过(不执行)

一些 状态的定义:
jbpm-human-task-core-6.1.0.Beta3.jar!\operations-dsl.mvel

------------------------------------------------ TaskService的方法:
//-------------------处理任务
void claim(long taskId, String userId); //从group抢到一个任务来处理
void start( long taskId, String userId );  //当sctor存在的时候使用它来启动自己的任务,跟claim不一样
void stop( long taskId, String userId ); //停止任务
void release( long taskId, String userId ); //释放和发布任务
void suspend( long taskId, String userId );//暂停任务
void resume( long taskId, String userId );//继续暂停的任务
void skip( long taskId, String userId );//忽略任务
void delegate(long taskId, String userId, String targetUserId);//代理任务
void complete( long taskId, String userId, Map<String, Object> results );//完成任务
//.........更多见文档
//----------------------查询任务
Task getTaskByWorkItemId(long workItemId);
Task getTaskById(long taskId); //根据taskId获得任务
List<TaskSummary> getTasksAssignedAsBusinessAdministrator(String userId, String language);//获得任务
List<TaskSummary> getTasksAssignedAsPotentialOwner(String userId, String language);//获得任务
List<TaskSummary> getTasksAssignedAsPotentialOwnerByStatus(String userId, List<Status> status, String language);//获得任务
List<TaskSummary> getTasksOwned(String userId, String language);//获得任务
List<TaskSummary> getTasksOwnedByStatus(String userId, List<Status> status, String language);//获得任务
List<TaskSummary> getTasksByStatusByProcessInstanceId(long processInstanceId, List<Status> status, String language);//获得任务
List<Long> getTasksByProcessInstanceId(long processInstanceId);//获得任务ID
List<TaskSummary> getTasksByVariousFields( List<Long> workItemIds, List<Long> taskIds, List<Long> procInstIds, List<String> busAdmins, List<String> potOwners, List<String> taskOwners, List<Status> status, List<String> language, boolean union);//获得任务
List<TaskSummary> getTasksByVariousFields(Map <String, List<?>> parameters, boolean union);//获得任务
//.........更多见文档




例子简单源码:
版本号:
Spring 4.0.5.RELEASE, Hibernate 4.3.5.Final, JBPM 6.0.1.Final
但是无法在xml配置TaskService, 解决能在aplicationContext.xml配置TaskService的问题:
https://community.jboss.org/thread/235092最后面得到提示,并综合 http://mswiderski.blogspot.com/2014/01/jbpm-6-with-spring.html
修改pom.xml, 升级JBPM的版本到 6.1.0.Beta1以上, 就可以解决问题


如果有RuleTask的话一定要使用fireAllRules(),否则RuleTask无法被执行
ksession1.startProcess(proId, inputParams);
ksession1.fireAllRules();




问题和解决:
1. Hibernate 4.3.x ==> GetProcessInstanceIdByCorrelation
https://github.com/droolsjbpm/jbpm/commit/75a8bc8ae4cb8607f6eff91d324b79fe2048c85a
在这里看到的: https://github.com/droolsjbpm/jbpm/blob/master/jbpm-persistence-jpa/src/main/resources/META-INF/JBPMorm.xml
select 
      key.processInstanceId 
  from 
     CorrelationKeyInfo key left join key.properties props 
     CorrelationKeyInfo key 
     left join key.properties props 
  where 
     size(key.properties) = cast(:elem_count as integer) and 
     cast(:elem_count as integer) = 
 	(select count(id) from CorrelationPropertyInfo cpi where cpi.correlationKey.id = key.id) and 
      props.value in :properties 
      group by key.id,key.processInstanceId 
  having count(key.id) = :elem_count


2. Error in named query: TasksAssignedAsPotentialOwner
META-INF/persistence.xml增加
<class>org.jbpm.services.task.impl.model.TaskImpl</class>
可能还需要增加更多的类, https://community.jboss.org/thread/235092帖子里面的源码test-spring-ht-jta.zip里面的persistence.xml比较完整,可以参考

3. Could not find work item handler for Human Task
http://docs.jboss.org/jbpm/v6.0.1/userguide/jBPMTaskService.html最后面提到If you use this approach, there is no need to register the Task Service with the Process Engine. The Runtime Manager will do that for you automatically. If you don't use the Runtime Manager, you will be responsible for setting the LocalHTWorkItemHandler in the session in order to get the Task Service notifying the Process Engine when a task is completed, or the Process Engine notifying that a task has been created.
https://community.jboss.org/thread/234126里面提到
protected WorkItemHandler getHTWorkItemHandler(RuntimeEngine runtime) {  
         
        RuntimeManager manager = ((RuntimeEngineImpl)runtime).getManager();  
        taskListener.addMappedManger(manager.getIdentifier(), manager);  
         
        LocalHTWorkItemHandler humanTaskHandler = new LocalHTWorkItemHandler();  
        humanTaskHandler.setRuntimeManager(manager);  
  
  
        return humanTaskHandler;  
    }


4. Unknown entity: org.jbpm.services.task.audit.impl.model.TaskEventImpl
在persistence.xml增加
<class>org.jbpm.services.task.audit.impl.model.TaskEventImpl</class>

你可能感兴趣的:(Hibernate4)