在实际应用的BPM流程中经常需要发送邮件,比如订单流程中当商品缺货时需要发送邮件通知相关负责人,jBPM5中的Email Service Tasks可以完成这一任务。Email 流程示意图如下
如图Logging流程就包括一个Service Tasks节点Email,用来发送邮件,在设计流程时我们实际发送邮件的发件人为[email protected];邮件的收件人为[email protected];[email protected];邮件的主题为Notification for pass the exam;邮件的内容为Congratuations, you have pass the exam。
package org.jbpm.quickstarts.mail; import org.drools.runtime.StatefulKnowledgeSession; import org.jbpm.quickstarts.QuickStartBase; public class MailStart extends QuickStartBase { public void test() { StatefulKnowledgeSession ksession = createKnowledgeSession("quickstarts/mail.bpmn"); ksession.getWorkItemManager().registerWorkItemHandler("Email", new MailWorkItemHandler()); ksession.startProcess("org.jbpm.quickstarts.mail"); } public static void main(String[] args) { new MailStart().test(); } }
package org.jbpm.quickstarts.mail; import java.util.Map; import org.drools.runtime.process.WorkItem; import org.drools.runtime.process.WorkItemHandler; import org.drools.runtime.process.WorkItemManager; public class MailWorkItemHandler implements WorkItemHandler { public void executeWorkItem(WorkItem workItem, WorkItemManager manager) { Map<String, Object> map = workItem.getParameters(); System.out.println("To: " + map.get("To")); System.out.println("From: " + map.get("From")); System.out.println("Subject: " + map.get("Subject")); System.out.println("Body: " + map.get("Body")); //TODO use javax.mail send email manager.completeWorkItem(workItem.getId(), null); } public void abortWorkItem(WorkItem workItem, WorkItemManager manager) { manager.abortWorkItem(workItem.getId()); } }
To: [email protected];[email protected] From: [email protected] Subject: Notification for pass the exam Body: Congratuations, you have pass the exam