Activiti中邮件任务一般用于发送邮件。
*后台服务基于Springboot2 + Activiti6,整合文章请参考:https://blog.csdn.net/yy756127197/article/details/101211510 不需要流程设计器就排除3,4步骤 *
1. 邮件任务
1.1 流程设计
设置
说明
charset:设置编码格式
流程图
流程文件bpmn
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/processdef">
<process id="mailTask" name="邮件任务" isExecutable="true">
<documentation>script</documentation>
<startEvent id="startEvent1"></startEvent>
<sequenceFlow id="sid-8FB9551F-B3A0-4F90-A6FF-EB18A7401952" sourceRef="startEvent1" targetRef="sid-3F1C24D4-C145-4E4F-932E-B532BA1ECB0A"></sequenceFlow>
<endEvent id="sid-C6D808B0-9D9E-4A64-8AD0-C65E440DD29A"></endEvent>
<serviceTask id="sid-3F1C24D4-C145-4E4F-932E-B532BA1ECB0A" name="发送邮件" activiti:type="mail">
<extensionElements>
<activiti:field name="to">
<activiti:string><![CDATA[gaha_hero@163.com]]></activiti:string>
</activiti:field>
<activiti:field name="from">
<activiti:string><![CDATA[756127197@qq.com]]></activiti:string>
</activiti:field>
<activiti:field name="subject">
<activiti:string><![CDATA[Activiti测试邮件]]></activiti:string>
</activiti:field>
<activiti:field name="text">
<activiti:string><![CDATA[测试邮件测试邮件测试邮件测试邮件]]></activiti:string>
</activiti:field>
</extensionElements>
</serviceTask>
<sequenceFlow id="sid-E85675E5-0A4F-4210-B667-A8F7A7BAFB5C" sourceRef="sid-3F1C24D4-C145-4E4F-932E-B532BA1ECB0A" targetRef="sid-C6D808B0-9D9E-4A64-8AD0-C65E440DD29A"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_mailTask">
<bpmndi:BPMNPlane bpmnElement="mailTask" id="BPMNPlane_mailTask">
<bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
<omgdc:Bounds height="30.0" width="30.0" x="180.0" y="160.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-C6D808B0-9D9E-4A64-8AD0-C65E440DD29A" id="BPMNShape_sid-C6D808B0-9D9E-4A64-8AD0-C65E440DD29A">
<omgdc:Bounds height="28.0" width="28.0" x="495.0" y="161.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sid-3F1C24D4-C145-4E4F-932E-B532BA1ECB0A" id="BPMNShape_sid-3F1C24D4-C145-4E4F-932E-B532BA1ECB0A">
<omgdc:Bounds height="80.0" width="100.0" x="300.0" y="135.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sid-8FB9551F-B3A0-4F90-A6FF-EB18A7401952" id="BPMNEdge_sid-8FB9551F-B3A0-4F90-A6FF-EB18A7401952">
<omgdi:waypoint x="210.0" y="175.0"></omgdi:waypoint>
<omgdi:waypoint x="300.0" y="175.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sid-E85675E5-0A4F-4210-B667-A8F7A7BAFB5C" id="BPMNEdge_sid-E85675E5-0A4F-4210-B667-A8F7A7BAFB5C">
<omgdi:waypoint x="400.0" y="175.0"></omgdi:waypoint>
<omgdi:waypoint x="495.0" y="175.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
1.2 引入pom
<!-- 邮件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
1.3 邮件配置
# activiti配置
activiti:
database-schema-update: true
# 记录级别,默认audit
history-level: full
# 自动部署验证设置:true-开启(默认)、false-关闭
check-process-definitions: false
# 默认流程定义文件存放目录
process-definition-location-prefix: classpath:/processes/
# 邮件
mail-server-host: smtp.qq.com
mail-server-port: 465
mail-server-default-from: 756127197@qq.com
mail-server-user-name: 756127197@qq.com
mail-server-password: (你的邮箱授权码)
mail-server-use-ssl: true
我是用的是QQ邮件,要使用QQ邮箱需要以下几步
- 开启SMTP服务
- 获取授权码
1.4 测试类
MailTaskTest .java
import com.it.cloud.modules.activiti.service.IActReModelService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* 邮件任务测试
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class MailTaskTest {
@Autowired
private IActReModelService actReModelService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private RepositoryService repositoryService;
@Autowired
private TaskService taskService;
@Autowired
private HistoryService historyService;
/**
* 部署流程定义
*/
@Test
public void deploy() {
Deployment deployment = repositoryService.createDeployment() // 创建部署
.addClasspathResource("diagrams/mailTask.bpmn20.xml") // 加载流程资源文件
.name("mailTask流程") // 流程名称
.deploy(); // 部署
System.out.println("流程部署ID:" + deployment.getId());
System.out.println("流程部署Name:" + deployment.getName());
}
/**
* 启动流程实例
*/
@Test
public void start() {
ProcessInstance pi = runtimeService.startProcessInstanceByKey("mailTask"); // 流程定义表的KEY字段值
System.out.println("流程实例ID:" + pi.getId());
System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
}
}
1.5 运行
运行deploy()
1.5.1 启动任务
运行start()
效果:
源码地址
IT-CLOUD-ACTIVITI6
开源项目,持续更新中,喜欢请 Star~
项目推荐
IT-CLOUD :IT服务管理平台,集成基础服务,中间件服务,监控告警服务等。
开源项目,持续更新中,喜欢请 Star~