JBoss 系列三十六:jBPM5示例之 Email

在实际应用的BPM流程中经常需要发送邮件,比如订单流程中当商品缺货时需要发送邮件通知相关负责人,jBPM5中的Email Service Tasks可以完成这一任务。Email 流程示意图如下

JBoss 系列三十六:jBPM5示例之 Email_第1张图片

如图Logging流程就包括一个Service Tasks节点Email,用来发送邮件,在设计流程时我们实际发送邮件的发件人为[email protected];邮件的收件人为[email protected];[email protected];邮件的主题为Notification for pass the exam;邮件的内容为Congratuations, you have pass the exam。

  • Email 流程运行的代码:https://github.com/kylinsoong/.../MailStart.java
  • Email 流程:https://github.com/kylinsoong/.../mail.bpmn
运行org.jbpm.quickstarts.mail.MailStart可运行 Email  流程发送邮件,MailStart代码如下:

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();
	}

}

如上我们注册WorkItemManager来在运行时完成日志记录工作,jBPM设计的一大特点就是灵活性,本示例我们通过自己实现的日志记录Hander(MailWorkItemHandler)来完成运行时日志记录,MailWorkItemHandler内容如下:

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());
	}

}

如上我们自定义的 MailWorkItemHandler实现了WorkItemHandler接口,日志记录是在executeWorkItem方法中完成,我们这里只是读取出设计时设计的邮件发送者地址,邮件接收者地址,邮件主题,邮件内容,并将这些信息输出,实际应用中我们可以使用邮件发送API来完成发送邮件的工作。运行MailWorkItemHandler会有如下输出:

To: [email protected];[email protected]
From: [email protected]
Subject: Notification for pass the exam
Body: Congratuations, you have pass the exam


你可能感兴趣的:(jboss,jbpm,email,BPM,JBPM5)