在上篇中小白采用附着在用户任务上的边界时间事件间隔触发TIME_FIRED事件,那么接下来,我们需要在流程中定义一个全局的事件监听器,用来捕获具体事件【TIME_FIRED】,并进行业务处理【超时邮件提醒】;
从Activiti5.15版本开始添加了全局事件监听器,可以用来监听一个引擎实例的所有流程或者作用于一个流程实例的所有发生事件包括TIME_FIRED,触发了定时器;它采用的是典型的监听者模式,既可在引擎配置对象中配置事件处理器,也可在流程模型文件中配置,这里以流程模型文件中定义为例;
<process id="XXXApply" name="XXX" isExecutable="true"> <extensionElements> <activiti:eventListener delegateExpression="${globalEventListenerImpl}"></activiti:eventListener> </extensionElements> <!--其他代码忽略--> </process>sendMailImpl是由Spring容器管理的Bean,它必须实现ActivitiEventListener才能起到事件捕获作用;
package com.hlmc.webapp.listener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.activiti.engine.TaskService; import org.activiti.engine.delegate.event.ActivitiEvent; import org.activiti.engine.delegate.event.ActivitiEventListener; import org.activiti.engine.delegate.event.ActivitiEventType; import org.activiti.engine.task.IdentityLink; import org.activiti.engine.task.Task; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.MailException; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; import com.hlmc.webapp.entity.BusinessStatus; import com.hlmc.webapp.service.admin.business.BusinessStatusService; import com.hlmc.webapp.service.admin.business.CommonWorkflowService; import com.hlmc.webapp.service.admin.system.UserService; @Component("globalEventListenerImpl") public class GlobalEventListenerImpl implements ActivitiEventListener { private final Logger logger = LogManager.getLogger (this.getClass().getName()); @Autowired private MailServiceImpl mailServiceImpl; @Autowired private TaskService taskService; @Autowired private BusinessStatusService businessStatusService; @Autowired private CommonWorkflowService commonWorkflowService; @Autowired private UserService userService; @Override public void onEvent(ActivitiEvent event) { //定时器事件 if(event.getType()==ActivitiEventType.TIMER_FIRED) { logger.info("-----启动定时事件-------"); //1、获取办理人或候选人的个人信息包括邮箱地址 //2、发送邮件 } } @Override public boolean isFailOnException() { // TODO Auto-generated method stub return false; } }(更多有关activiti的项目开发内容请等下次更新,欢迎点评,共同进步哦!)