---
title: "JBPM 4.3 Email与Notification例子"
author: "shusanzhan"
date: "2010-02-27T17:29:59Z"
tags: ["JBPM", "JBoss", "Office", "XML", "XSL"]
categories: ["企业架构"]
slug: "jbpm-4-3-email-notification"
draft: false
lastmod: "2010-03-29T22:43:11Z"
id: 604321
url: "https://shusanzhan.iteye.com/blog/604321"
---

第一步：jpdl.xml文件

 

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;process key="moves" name="moves" xmlns="[http://jbpm.org/4/jpdl](http://jbpm.org/4/jpdl)"&gt;  
 &lt;start g="372,12,49,73" name="start1"&gt;  
  &lt;transition to="Request\_New\_Move"/&gt;  
 &lt;/start&gt;  
 &lt;task assignee="sanshu" form="request\_move.xsl" g="312,72,169,52" name="Request\_New\_Move"&gt;  
  &lt;notification&gt;  
   &lt;to addresses="${toEmail}" /&gt;  
   &lt;cc addresses="${lmEmail}" /&gt;  
   &lt;subject&gt;${task.name}&lt;/subject&gt;  
   &lt;text&gt;  
     &lt;![CDATA[Hi ${task.assignee},  
     Task "${task.name}" has been assigned to you.  
     Sent by JBoss jBPM  
     ]]&gt;  
   &lt;/text&gt;  
  &lt;/notification&gt;  
  &lt;transition g="-43,-5" name="submit" to="Line\_Manager\_review"/&gt;  
 &lt;/task&gt;  
 &lt;task assignee="${reqLoginName}" g="312,288,169,52" name="Line\_Manager\_review"&gt;  
  &lt;transition g="-57,-4" name="approve" to="Send\_approval\_email"/&gt;  
  &lt;transition g="18,-5" name="reject" to="Send\_rejection\_email"/&gt;  
 &lt;/task&gt;  
 &lt;mail g="240,408,133,40" name="Send\_approval\_email"&gt;  
  &lt;to addresses="[somebody@office.com"/](mailto:somebody@office.com%22/)&gt;  
  &lt;cc addresses="[nobody@office.com](mailto:nobody@office.com)" /&gt;  
  &lt;subject&gt;Your move request is approved!&lt;/subject&gt;  
  &lt;text&gt;Employee name ${employeeName} and request details  
   ${requestDetails}&lt;/text&gt;  
  &lt;transition to="end1"/&gt;  
 &lt;/mail&gt;  
 &lt;mail g="432,408,133,37" name="Send\_rejection\_email"&gt;  
  &lt;to addresses="[somebody@office.com"/](mailto:somebody@office.com%22/)&gt;  
  &lt;subject&gt;Your move request is rejected!&lt;/subject&gt;  
  &lt;text&gt;Employee name ${employeeName} and request details ${requestDetails}&lt;/text&gt;  
  &lt;transition to="end1"/&gt;  
 &lt;/mail&gt;  
 &lt;end g="384,480,48,48" name="end1"/&gt;  
&lt;/process&gt;

 

 

 

第二步：

jbpm.mial.properties配置文件,配置邮件属性

mail.smtp.host  localhost  
mail.smtp.port 2525  
mail.from  [shusanzhan@163.com](mailto: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](mailto:#mail.from=myemail@gmail.com)  
#mail.user=myemail  
##mail.smtp.quitwait=false  
#mail.debug=true

 

 

 

第三步：

jbpm.mail.template.example.xml配置邮件模板等

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;jbpm-configuration&gt;

  &lt;process-engine-context&gt;

    &lt;mail-template name="rectify-template"&gt;  
      &lt;to addresses="${addresses}" /&gt;  
      &lt;cc users="shusanzhan2010" groups="home" /&gt;  
      &lt;bcc groups="friend" /&gt;  
      &lt;subject&gt;rectify ${newspaper}&lt;/subject&gt;  
      &lt;text&gt;${newspaper} ${date} ${details}&lt;/text&gt;  
    &lt;/mail-template&gt;  
     
    &lt;business-calendar&gt;   
      &lt;monday    hours="9:00-12:00 and 12:30-17:00"/&gt;   
      &lt;tuesday   hours="9:00-12:00 and 12:30-17:00"/&gt;   
      &lt;wednesday hours="9:00-12:00 and 12:30-17:00"/&gt;   
      &lt;thursday  hours="9:00-12:00 and 12:30-17:00"/&gt;   
      &lt;friday    hours="9:00-12:00 and 12:30-17:00"/&gt;   
      &lt;holiday period="01/07/2008 - 31/08/2008"/&gt;   
    &lt;/business-calendar&gt;  
     
    &lt;mail-template name='task-notification'&gt;   
      &lt;to users="${task.assignee}"/&gt;   
      &lt;subject&gt;${task.name}&lt;/subject&gt;   
      &lt;text&gt;&lt;![CDATA[Hi ${task.assignee},   
  Task "${task.name}" has been assigned to you.   
  ${task.description}   
   
  Sent by JBoss jBPM   
  ]]&gt;&lt;/text&gt;   
    &lt;/mail-template&gt;  
     
    &lt;mail-template name='task-reminder'&gt;   
      &lt;to users="${task.assignee}"/&gt;   
      &lt;subject&gt;${task.name}&lt;/subject&gt;   
      &lt;text&gt;&lt;![CDATA[Hey ${task.assignee},   
  Do not forget about task "${task.name}".   
  ${task.description}   
   
  Sent by JBoss jBPM   
  ]]&gt;&lt;/text&gt;  
   &lt;/mail-template&gt;  
    
   &lt;transaction-context&gt;   
    &lt;repository-session /&gt;   
    &lt;db-session /&gt;   
   
    &lt;message-session /&gt;   
    &lt;timer-session /&gt;   
    &lt;history-session /&gt;   
    &lt;mail-session&gt;   
      &lt;mail-server&gt;   
        &lt;session-properties resource="jbpm.mail.properties" /&gt;   
      &lt;/mail-server&gt;   
    &lt;/mail-session&gt;   
  &lt;/transaction-context&gt; 

  &lt;/process-engine-context&gt;

&lt;/jbpm-configuration&gt;

其它配置文件不用修改

 

第四 测试程序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&lt;String,Object&gt; variables=new HashMap&lt;String, Object&gt;();  
  String lmEmail="[shusanzhan24@163.com](mailto:shusanzhan24@163.com)";  
  String rqLoginName="shusanzhan";  
  String employeeName="zhangsan";  
  String requestDetails="Must to do id";  
  String toEail="[shusanzhan2010@163.com](mailto: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&lt;Task&gt; listTasks=taskService.findPersonalTasks("sanshu");  
  assertEquals(1, listTasks.size());  
    
  List&lt;WiserMessage&gt; 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](mailto:shusanzhan@163.com%22,from%5B0%5D.toString)());  
     
   Address\[] to=message.getRecipients(RecipientType.TO);  
   assertEquals(1, to.length);  
   assertEquals("[shusanzhan2010@163.com",to\[0\].toString](mailto:shusanzhan2010@163.com%22,to%5B0%5D.toString)());  
     
   Address\[] cc=message.getRecipients(RecipientType.CC);  
   assertEquals(1,cc.length);  
   assertEquals("[shusanzhan24@163.com",cc\[0\].toString](mailto:shusanzhan24@163.com%22,cc%5B0%5D.toString)());  
     
   String subject=message.getSubject();  
   assertEquals("Request\_New\_Move", subject);  
     
   String text=message.getContent().toString();  
   System.out.println("=================&gt;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("=========&gt;"+processInstance.getExecution("Send\_approval\_email"));  
     listWiserMessages=wiser.getMessages();  
     assertEquals(4, listWiserMessages.size());//4是整个流程中所发的信息  
     for(int i=2;i&lt;listWiserMessages.size();i++){  
      Message message=listWiserMessages.get(i).getMimeMessage();  
      Address\[] from=message.getFrom();  
      assertEquals(1, from.length);  
      assertEquals("[shusanzhan@163.com](mailto: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](mailto:somebody@office.com%22,to%5B0%5D.toString)());  
        
      Address\[] cc=message.getRecipients(RecipientType.CC);  
      assertEquals("[nobody@office.com](mailto: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:========&gt;"+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());  
//  }  
    
 }  
}