activiti实战七(通过监听器发邮件)

1、使用spring的邮件封装类

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>xxxcom</value>
</property>
<property name="port">
<value>25</value>
</property>
<property name="protocol" value="smtp"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
<property name="username">
<value>xxx</value> <!-- 发送者用户名 -->
</property>
<property name="password">
<value>xxx</value> <!-- 发送者密码 -->
</property>
</bean>

2、在<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">中添加属性

<property name="eventListeners">
<list>
<bean
class="xxx.activitimgmt.activitideploy.listeners.MyEventListener">
</bean>
</list>
</property>

3、创建监听器,实现ActivitiEventListener接口

public class MyEventListener implements ActivitiEventListener {

@Autowired
private MailWrapper mailWrapper;//用于发邮件的类

@Override
public void onEvent(ActivitiEvent event) {
// System.out.println("Event received: " + event.getType());
if (event.getType() == ActivitiEventType.TASK_CREATED) {// 任务创建,当流程走到一个任务节点时,会进入这个条件
ActivitiEntityEvent entityEvent = (ActivitiEntityEvent) event;
TaskEntity taskEntity = (TaskEntity) entityEvent.getEntity();
// taskEntity.getAssignee(),它是ID
Set<IdentityLink> candidates = taskEntity.getCandidates();//这个是获取候选的
for (IdentityLink identityLink : candidates) {
System.out.println(identityLink.getGroupId());// 根据identityLink.getGroupId(),这个GroupId就是流程图中activiti:candidateGroups对应的值。用它可以查询 该组的所有用户,然后发邮件,让其去审批。

}
}
if (event.getType() == ActivitiEventType.TASK_ASSIGNED) {// 任务认领,按照谁先登录谁先认领的原则,认领后会给该认领的用户发邮件
ActivitiEntityEvent entityEvent = (ActivitiEntityEvent) event;
TaskEntity taskEntity = (TaskEntity) entityEvent.getEntity();//认领后我们主要用taskEntity.getAssignee()来查询当前用户信息,并发邮件
mailWrapper.sendTaskMail(taskEntity.getOwner(), taskEntity.getAssignee(), taskEntity.getName(),
event.getType());
}

if (event.getType() == ActivitiEventType.TASK_COMPLETED) {//审批完成,首先会进入下一节点,并给下一节点的用户发邮件,之后会通过历史流程去找到流程发起者,并将审批信息通过邮件发送给发起者。这里要注意,该类不能注入historyService,因为它先于historyService存在,必须通过entityEvent.getEngineServices();去获取。
ActivitiEntityEvent entityEvent = (ActivitiEntityEvent) event;


TaskEntity taskEntity = (TaskEntity) entityEvent.getEntity();// 这个任务是当前做完的任务,可以理解为历史任务了
EngineServices engineServices = entityEvent.getEngineServices();
HistoryService historyService = engineServices.getHistoryService();
TaskService taskService = engineServices.getTaskService();
ProcessInstance processInstance = taskEntity.getProcessInstance();
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
.processInstanceId(processInstance.getId()).singleResult();//根据流程ID获取历史流程
String startUserId = historicProcessInstance.getStartUserId();//获取流程的发起人
List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
.processInstanceId(processInstance.getId()).list();//获取历史历史流程的所有任务,包括下一节点的任务,不包括下下节点的任务
String message = "";
String userId = "";
for (HistoricTaskInstance historicTaskInstance : list) {
if (historicTaskInstance.getId().equals(taskEntity.getId())) {//找出和当前完成任务ID一致的历史任务,并从中获取审批信息
List<Comment> taskComments = taskService.getTaskComments(historicTaskInstance.getId());
for (Comment comment : taskComments) {
message = comment.getFullMessage();// 审批信息
userId = comment.getUserId();// 审批人
}
}
}
mailWrapper.sendTaskMail(message, startUserId, userId, event.getType());//不要太在意这个方法,随便写的
}

}

@Override
public boolean isFailOnException() {
return true;//true表示打印错误信息,并抛异常,否则任务会往下执行
}
}

MailWrapper中主要还是

@Autowired
private MailSender mailSender;

public void sendTaskMail(String xxx, String assignee, String xxx, ActivitiEventType eventType) {

SimpleMailMessage msg = new SimpleMailMessage();
String toEmailAddress = "
xxx@xxx.com";

msg.setSubject("hello world");
msg.setTo(toEmailAddress);
msg.setText(userName + "," + xxx+ ",来自" + userName2 + "的审批");
msg.setFrom("xxx2@xxxcom");

try {
mailSender.send(msg);
} catch (MailException e) {
e.printStackTrace();
}

}

当然我们可以使用邮件模板velocity来做

发布了91 篇原创文章 · 获赞 9 · 访问量 12万+
展开阅读全文
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览