JBPM 4.3 Email与Notification事例
第一步:jpdl.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<process key="moves" name="moves" xmlns="http://jbpm.org/4/jpdl">
<start g="372,12,49,73" name="start1">
<transition to="Request_New_Move"/>
</start>
<task assignee="sanshu" form="request_move.xsl" g="312,72,169,52" name="Request_New_Move">
<notification>
<to addresses="${toEmail}" />
<cc addresses="${lmEmail}" />
<subject>${task.name}</subject>
<text>
<![CDATA[Hi ${task.assignee},
Task "${task.name}" has been assigned to you.
Sent by JBoss jBPM
]]>
</text>
</notification>
<transition g="-43,-5" name="submit" to="Line_Manager_review"/>
</task>
<task assignee="${reqLoginName}" g="312,288,169,52" name="Line_Manager_review">
<transition g="-57,-4" name="approve" to="Send_approval_email"/>
<transition g="18,-5" name="reject" to="Send_rejection_email"/>
</task>
<mail g="240,408,133,40" name="Send_approval_email">
<to addresses="somebody@office.com"/>
<cc addresses="nobody@office.com" />
<subject>Your move request is approved!</subject>
<text>Employee name ${employeeName} and request details
${requestDetails}</text>
<transition to="end1"/>
</mail>
<mail g="432,408,133,37" name="Send_rejection_email">
<to addresses="somebody@office.com"/>
<subject>Your move request is rejected!</subject>
<text>Employee name ${employeeName} and request details ${requestDetails}</text>
<transition to="end1"/>
</mail>
<end g="384,480,48,48" name="end1"/>
</process>
第二步:
jbpm.mial.properties配置文件,配置邮件属性
mail.smtp.host localhost
mail.smtp.port 2525
mail.from shusanzhan@163.com
##mail.smtps.starttls.enable=true
##mail.smtps.auth=true
##mail.smtp.port=465
#mail.transport.protocol=smtps
#mail.from=myemail@gmail.com
#mail.user=myemail
##mail.smtp.quitwait=false
#mail.debug=true
第三步:
jbpm.mail.template.example.xml配置邮件模板等
<?xml version="1.0" encoding="UTF-8"?>
<jbpm-configuration>
<process-engine-context>
<mail-template name="rectify-template">
<to addresses="${addresses}" />
<cc users="shusanzhan2010" groups="home" />
<bcc groups="friend" />
<subject>rectify ${newspaper}</subject>
<text>${newspaper} ${date} ${details}</text>
</mail-template>
<business-calendar>
<monday hours="9:00-12:00 and 12:30-17:00"/>
<tuesday hours="9:00-12:00 and 12:30-17:00"/>
<wednesday hours="9:00-12:00 and 12:30-17:00"/>
<thursday hours="9:00-12:00 and 12:30-17:00"/>
<friday hours="9:00-12:00 and 12:30-17:00"/>
<holiday period="01/07/2008 - 31/08/2008"/>
</business-calendar>
<mail-template name='task-notification'>
<to users="${task.assignee}"/>
<subject>${task.name}</subject>
<text><![CDATA[Hi ${task.assignee},
Task "${task.name}" has been assigned to you.
${task.description}
Sent by JBoss jBPM
]]></text>
</mail-template>
<mail-template name='task-reminder'>
<to users="${task.assignee}"/>
<subject>${task.name}</subject>
<text><![CDATA[Hey ${task.assignee},
Do not forget about task "${task.name}".
${task.description}
Sent by JBoss jBPM
]]></text>
</mail-template>
<transaction-context>
<repository-session />
<db-session />
<message-session />
<timer-session />
<history-session />
<mail-session>
<mail-server>
<session-properties resource="jbpm.mail.properties" />
</mail-server>
</mail-session>
</transaction-context>
</process-engine-context>
</jbpm-configuration>
其它配置文件不用修改
第四 测试程序java程序:
package com.mail.notification;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jms.MessageEOFException;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage.RecipientType;
import org.jboss.util.platform.PID;
import org.jbpm.api.Execution;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.task.Task;
import org.jbpm.pvm.internal.email.impl.MailSessionImpl;
import org.jbpm.pvm.internal.email.spi.MailSession;
import org.jbpm.pvm.internal.history.events.ProcessInstanceEnd;
import org.jbpm.test.JbpmTestCase;
import org.subethamail.wiser.Wiser;
import org.subethamail.wiser.WiserMessage;
public class NotificationTest extends JbpmTestCase {
Wiser wiser=new Wiser();
protected void setUp() throws Exception{
super.setUp();
String deploymentId=repositoryService.createDeployment().
addResourceFromClasspath("com/mail/notification/move.jpdl.xml").deploy();
registerDeployment(deploymentId);
wiser.setPort(2525);
wiser.start();
}
protected void tearDown()throws Exception {
wiser.stop();
super.tearDown();
}
public void testNotificationTest() throws MessageEOFException,IOException, MessagingException{
Map<String,Object> variables=new HashMap<String, Object>();
String lmEmail="shusanzhan24@163.com";
String rqLoginName="shusanzhan";
String employeeName="zhangsan";
String requestDetails="Must to do id";
String toEail="shusanzhan2010@163.com";
variables.put("lmEmail", lmEmail);
variables.put("reqLoginName", rqLoginName);
variables.put("employeeName", employeeName);
variables.put("requestDetails", requestDetails);
variables.put("toEmail",toEail);
ProcessInstance processInstance=executionService.startProcessInstanceByKey("moves", variables);
String piId=processInstance.getId();
List<Task> listTasks=taskService.findPersonalTasks("sanshu");
assertEquals(1, listTasks.size());
List<WiserMessage> listWiserMessages=wiser.getMessages();
assertEquals(2, listWiserMessages.size());
processInstance=executionService.findProcessInstanceById(piId);
assertEquals(true, processInstance.isActive("Request_New_Move"));
if(processInstance.isActive("Request_New_Move")){
taskService.completeTask(listTasks.get(0).getId());
}
assertEquals(true,processInstance.isActive("Request_New_Move"));
for(WiserMessage wisMessage:listWiserMessages){
Message message=wisMessage.getMimeMessage();
Address[] from=message.getFrom();
assertEquals(1, from.length);
assertEquals("shusanzhan@163.com",from[0].toString());
Address[] to=message.getRecipients(RecipientType.TO);
assertEquals(1, to.length);
assertEquals("shusanzhan2010@163.com",to[0].toString());
Address[] cc=message.getRecipients(RecipientType.CC);
assertEquals(1,cc.length);
assertEquals("shusanzhan24@163.com",cc[0].toString());
String subject=message.getSubject();
assertEquals("Request_New_Move", subject);
String text=message.getContent().toString();
System.out.println("=================>Text content :"+text);
}
processInstance=executionService.findProcessInstanceById(piId);
listTasks=taskService.findPersonalTasks(rqLoginName);
assertEquals(1, listTasks.size());
assertEquals(true, processInstance.isActive("Line_Manager_review"));
if(processInstance.isActive("Line_Manager_review")){
taskService.completeTask(listTasks.get(0).getId(),"approve");
}
assertEquals(true,processInstance.isActive("Line_Manager_review"));
System.out.println("=========>"+processInstance.getExecution("Send_approval_email"));
listWiserMessages=wiser.getMessages();
assertEquals(4, listWiserMessages.size());//4是整个流程中所发的信息
for(int i=2;i<listWiserMessages.size();i++){
Message message=listWiserMessages.get(i).getMimeMessage();
Address[] from=message.getFrom();
assertEquals(1, from.length);
assertEquals("shusanzhan@163.com", from[0].toString());
Address[] all=message.getAllRecipients();
assertEquals(2, all.length);
Address[] to=message.getRecipients(RecipientType.TO);
assertEquals(1, to.length);
assertEquals("somebody@office.com",to[0].toString());
Address[] cc=message.getRecipients(RecipientType.CC);
assertEquals("nobody@office.com", cc[0].toString());
String subject=message.getSubject();
assertEquals("Your move request is approved!", subject);
String text=message.getContentType();
System.out.println("content text:========>"+text);
}
assertEquals(true, processInstance.isActive("Line_Manager_review"));
//assertEquals(true, processInstance.isEnded());
// Execution execution=executionService.findExecutionById(listTasks.get(0).getId());
// if(execution.getProcessInstance().isActive("Request_New_Move")){
// taskService.completeTask(listTasks.get(0).getId());
// }
}
}
是不是测试类里根本就没发邮件?而在实际项目里必须要通过邮件服务器才能发送消息?