这篇文章主要记录流程监听器的部分用法,整个环境实在集成在spring下,ssm环境:
package com.abc.activiti.listenner;
import org.activiti.engine.delegate.DelegateExecution;
public class JavaExpressionActivitiListenner {
public void expression(DelegateExecution execution) throws Exception {
System.out.println("executionId:" + execution.getId() + " ActivitiListenner" + this.toString());
}
}
package com.abc.activiti.listenner;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener;
public class ActivitiListenner implements TaskListener, ExecutionListener {
/**
*
*/
private static final long serialVersionUID = -3759054058055401826L;
@Override
public void notify(DelegateExecution execution) throws Exception {
System.out.println("xml流程:" + execution.getId() + " ActivitiListenner" + this.toString());
}
@Override
public void notify(DelegateTask delegateTask) {
System.out.println("xml任务:" + delegateTask.getId() + " ActivitiListenner" + this.toString());
}
}
package com.abc.activiti.listenner;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
public class JavaDelegateActivitiListenner implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println("executionId:" + execution.getId() + " ActivitiListenner" + this.toString());
}
}
class="com.abc.activiti.listenner.xxxListenner">
class="com.abc.activiti.listenner.xxxListenner">
targetRef="usertask1">
class="com.abc.activiti.listenner.xxxListenner">
2017-09-16 10:36:25.816 [http-nio-8080-exec-3] ERROR c.a.a.controller.ActivitiController.exception - 处理请求http://localhost:8080/abc-master/activiti/start?null 时出错。
完整异常栈堆信息
org.activiti.engine.ActivitiIllegalArgumentException: com.abc.activiti.listenner.JavaExpressionActivitiListenner doesn't implement interface org.activiti.engine.delegate.ExecutionListener nor interface org.activiti.engine.delegate.JavaDelegate
启动流程时报错,说明通过javaClass配置activiti的流程监听器必须实现org.activiti.engine.delegate.ExecutionListener或者org.activiti.engine.delegate.JavaDelegate接口xml流程:7f7aa2be-9a89-11e7-9d02-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.ActivitiListenner@39ca6dcd
xml流程:7f7aa2be-9a89-11e7-9d02-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.ActivitiListenner@306a75c6
xml流程:7f7aa2be-9a89-11e7-9d02-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.ActivitiListenner@342664da
测试表明,通过加载java class的方式去加载实现了ExecutionListener接口的bean,可以顺利实现流程监听,但是每次获取的监听器都是一个新对象;
executionId:eebefb34-9a89-11e7-b1f2-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.JavaDelegateActivitiListenner@26d8afc4
executionId:eebefb34-9a89-11e7-b1f2-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.JavaDelegateActivitiListenner@38835b92
executionId:eebefb34-9a89-11e7-b1f2-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.JavaDelegateActivitiListenner@21dcaa6e
不出所料,其结果与B类监听一致;
package com.abc.activiti.listenner;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.abc.activiti.service.ActivitiService;
@Service
public class JavaDelegateActivitiListenner implements JavaDelegate {
@Autowired
private ActivitiService activitiService;
@Override
public void execute(DelegateExecution execution) throws Exception {
System.out.println("executionId:" + execution.getId() + " ActivitiListenner" + this.toString()
+ " spring容器注入bean:" + activitiService.toString());
}
}
2017-09-16 10:57:01.760 [http-nio-8080-exec-3] ERROR c.a.a.controller.ActivitiController.exception - 处理请求http://localhost:8080/abc-master/activiti/start?null 时出错。
完整异常栈堆信息
java.lang.NullPointerException: null
at com.abc.activiti.listenner.JavaDelegateActivitiListenner.execute(JavaDelegateActivitiListenner.java:19) ~[JavaDelegateActivitiListenner.class:na]
at org.activiti.engine.impl.delegate.JavaDelegateInvocation.invoke(JavaDelegateInvocation.java:34) ~[activiti-engine-5.20.0.jar:na]
package com.abc.activiti.listenner;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.abc.activiti.service.ActivitiService;
@Service
public class ActivitiListenner implements TaskListener, ExecutionListener {
/**
*
*/
private static final long serialVersionUID = -3759054058055401826L;
@Autowired
private ActivitiService activitiService;
@Override
public void notify(DelegateExecution execution) throws Exception {
System.out.println("xml流程:" + execution.getId() + " ActivitiListenner" + this.toString());
System.out.println("activitiService: " + activitiService.toString());
}
@Override
public void notify(DelegateTask delegateTask) {
System.out.println("xml任务:" + delegateTask.getId() + " ActivitiListenner" + this.toString());
}
}
xml流程:03ec2a11-9a8b-11e7-903c-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.ActivitiListenner@5de11ac2
2017-09-16 10:59:11.541 [http-nio-8080-exec-3] ERROR c.a.a.controller.ActivitiController.exception - 处理请求http://localhost:8080/abc-master/activiti/start?null 时出错。
完整异常栈堆信息
java.lang.NullPointerException: null
at com.abc.activiti.listenner.ActivitiListenner.notify(ActivitiListenner.java:26) ~[ActivitiListenner.class:na]
at org.activiti.engine.impl.delegate.ExecutionListenerInvocation.invoke(ExecutionListenerInvocation.java:34) ~[activiti-engine-5.20.0.jar:na]
以上两组结果表明,spring只是将其纳入管理,但并成功注入到activitiEngine中。
2017-09-16 11:27:29.420 [http-nio-8080-exec-3] ERROR c.a.a.controller.ActivitiController.exception - 处理请求http://localhost:8080/abc-master/activiti/start?null 时出错。
完整异常栈堆信息
org.activiti.engine.ActivitiException: Unknown property used in expression: ${javaExpressionActivitiListenner.expression(execution)}
at org.activiti.engine.impl.el.JuelExpression.getValue(JuelExpression.java:53) ~[activiti-engine-5.20.0.jar:na]
......
Caused by: org.activiti.engine.impl.javax.el.PropertyNotFoundException: Cannot resolve identifier 'javaExpressionActivitiListenner'
at org.activiti.engine.impl.juel.AstIdentifier.eval(AstIdentifier.java:83) ~[activiti-engine-5.20.0.jar:5.20.0]
监听器改造:
package com.abc.activiti.listenner;
import org.activiti.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;
@Component
public class JavaExpressionActivitiListenner {
public void expression(DelegateExecution execution) throws Exception {
System.out.println("executionId:" + execution.getId() + " ActivitiListenner" + this.toString());
}
}
测试结果如下:
executionId:ddd3f07e-9a8d-11e7-9fc5-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.JavaExpressionActivitiListenner@420a38c6
executionId:ddd3f07e-9a8d-11e7-9fc5-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.JavaExpressionActivitiListenner@420a38c6
executionId:ddd3f07e-9a8d-11e7-9fc5-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.JavaExpressionActivitiListenner@420a38c6
测试结果:
xml流程:2175ef09-9a8e-11e7-9af4-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.ActivitiListenner@2f68610c
xml流程:2175ef09-9a8e-11e7-9af4-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.ActivitiListenner@2f68610c
xml流程:2175ef09-9a8e-11e7-9af4-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.ActivitiListenner@2f68610c
测试结果:
executionId:719dc30a-9a8d-11e7-9ac9-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.JavaDelegateActivitiListenner@7374d21e
executionId:719dc30a-9a8d-11e7-9ac9-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.JavaDelegateActivitiListenner@7374d21e
executionId:719dc30a-9a8d-11e7-9ac9-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.JavaDelegateActivitiListenner@7374d21e
xml流程:2b882f40-9a91-11e7-9325-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.ActivitiListenner@42e9857e
activitiService: com.abc.activiti.service.ActivitiService@2f9b021f
xml流程:2b882f40-9a91-11e7-9325-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.ActivitiListenner@42e9857e
activitiService: com.abc.activiti.service.ActivitiService@2f9b021f
xml流程:2b882f40-9a91-11e7-9325-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.ActivitiListenner@42e9857e
activitiService: com.abc.activiti.service.ActivitiService@2f9b021f
此处仅提供A类监听测试结果,其余测试结果均一致,以上测试结果表明,使用expression表达式江容器中的bean作为监听器注入到activitiEngine中是一种非常灵活强大的方式。
A类监听测试:
2017-09-16 12:21:54.134 [http-nio-8080-exec-3] ERROR c.a.a.controller.ActivitiController.exception - 处理请求http://localhost:8080/abc-master/activiti/start?null 时出错。
完整异常栈堆信息
org.activiti.engine.ActivitiIllegalArgumentException: Delegate expression ${javaExpressionActivitiListenner} did not resolve to an implementation of interface org.activiti.engine.delegate.ExecutionListener nor interface org.activiti.engine.delegate.JavaDelegate
at org.activiti.engine.impl.bpmn.listener.DelegateExpressionExecutionListener.notify(DelegateExpressionExecutionListener.java:57) ~[activiti-engine-5.20.0.jar:5.20.0]
at org.activiti.engine.impl.pvm.runtime.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:42) ~[activiti-engine-5.20.0.jar:na]
B类监听测试:
xml流程:fcc6c8ba-9a95-11e7-826d-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.ActivitiListenner@71425d05
activitiService: com.abc.activiti.service.ActivitiService@72a765d5
xml流程:fcc6c8ba-9a95-11e7-826d-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.ActivitiListenner@71425d05
activitiService: com.abc.activiti.service.ActivitiService@72a765d5
xml流程:fcc6c8ba-9a95-11e7-826d-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.ActivitiListenner@71425d05
activitiService: com.abc.activiti.service.ActivitiService@72a765d5
C类监听测试:
executionId:c96567e2-9a95-11e7-b7e3-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.JavaDelegateActivitiListenner@6f660921 spring容器注入bean:com.abc.activiti.service.ActivitiService@1dc6b66a
executionId:c96567e2-9a95-11e7-b7e3-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.JavaDelegateActivitiListenner@6f660921 spring容器注入bean:com.abc.activiti.service.ActivitiService@1dc6b66a
executionId:c96567e2-9a95-11e7-b7e3-b4b6761cb4ac ActivitiListennercom.abc.activiti.listenner.JavaDelegateActivitiListenner@6f660921 spring容器注入bean:com.abc.activiti.service.ActivitiService@1dc6b66a
以上测试结果表明: