activiti 全局流程监听ActivitiEventListener,…
2020-02-08 16:05:04来源:博客园 阅读 ()
activiti 全局流程监听ActivitiEventListener,实现监听不同类型事件,不需要在acitivit中配置任务监听,非常方便
如果我们像给任务配置监听,按照常规的做法是这样的
一个个配置,比较麻烦。
现在利用ActivitiEventListener,监听全局事件,并且可以判断不同的事件类型,进而执行不同的业务逻辑。
1.定义事件处理handler接口
用接口来定义统一约定
public interface EventHandler {
void handle(ActivitiEvent event);
}
2.实现不同事件
任务结束事件
*/
public class TaskCompleteListener implements EventHandler {
protected Logger logger = LoggerFactory.getLogger(getClass());
@Override
public void handle(ActivitiEvent event) {
ActivitiEntityEventImpl eventImpl = (ActivitiEntityEventImpl) event;
TaskEntity taskEntity = (TaskEntity) eventImpl.getEntity();
}
}
任务创建事件
public class TaskCreateListener implements EventHandler {
protected Logger logger = LoggerFactory.getLogger(getClass());
@Override
public void handle(ActivitiEvent event) {
ActivitiEntityEventImpl eventImpl = (ActivitiEntityEventImpl) event;
TaskEntity taskEntity = (TaskEntity) eventImpl.getEntity();
}
}
还有很多事件,比如TASK_ASSIGNED,PROCESS_STARTED,PROCESS_COMPLETED
3.实现ActivitiEventListener
public class GlobalEventListener implements ActivitiEventListener {
protected Logger logger = LoggerFactory.getLogger(getClass());
/**
* 各类 Event 的处理器
*/
private Map<ActivitiEventType, EventHandler> handlers = new HashMap<ActivitiEventType, EventHandler>();
@Override
public void onEvent(ActivitiEvent event) {
EventHandler eventHandler = handlers.get(event.getType());
if(eventHandler!=null){
eventHandler.handle(event);
}
}
@Override
public boolean isFailOnException() {
return false;
}
public Map<ActivitiEventType, EventHandler> getHandlers() {
return handlers;
}
public void setHandlers(Map<ActivitiEventType, EventHandler> handlers) {
this.handlers = handlers;
}
}
4.注册事件监听
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource"/>
<property name="transactionManager" ref="transactionManager"/>
<property name="databaseSchemaUpdate" value="true"/>
<property name="jobExecutorActivate" value="false"/>
<property name="history" value="full"/>
<property name="processDefinitionCacheLimit" value="10"/>
<!-- 生成流程图的字体 -->
<property name="activityFontName" value="宋体"/>
<property name="labelFontName" value="宋体"/>
<property name="annotationFontName" value="宋体"/>
<!-- 自动部署 -->
<property name="deploymentResources">
<list>
<value>classpath*:*</value>
</list>
</property>
<!-- <property name="idGenerator" ref="idGenerator"/>-->
<property name="eventListeners">
<list>
<ref bean="globalEventListener"/>
</list>
</property>
<property name="customFormTypes">
<bean class="org.activiti.engine.impl.form.DateFormType">
<constructor-arg value="yyyy-MM-dd HH:mm:ss"/>
</bean>
</property>
</bean>
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration"/>
</bean>
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>
<bean id="formService" factory-bean="processEngine" factory-method="getFormService"/>
<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService"/>
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService"/>
<!--流程全局事件处理器-->
<bean id="globalEventListener" class="GlobalEventListener">
<property name="handlers">
<map>
<entry key="TASK_CREATED">
<bean class="TaskCreateListener"/>
</entry>
<entry key="TASK_COMPLETED">
<bean class="TaskCompleteListener"/>
</entry>
</map>
</property>
</bean>
优点
- 这样代码清晰,业务逻辑解耦,单一职责
- 省去在bpmn.xml中配置,方便。
原文链接:https://www.cnblogs.com/qingmiaokeji/p/12285173.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:Java面向对象
- Java学习之第二天 2020-06-11
- Spring Cloud Gateway 全局通用异常处理 2020-06-08
- 架构设计 | 异步处理流程,多种实现模式详解 2020-06-04
- Java必备主流技术流程图,写得非常好! 2020-05-29
- 微服务项目持续集成部署流程简介 2020-05-27
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash