1. 程式人生 > >學習JBPM 工作流引擎監聽事件(四)

學習JBPM 工作流引擎監聽事件(四)

 

監聽器的原理圖

 新增監聽器的步驟與方法

 

 

建立一個新的流程  holiday.xml

在holiday.xml 新增上監聽的程式碼

<?xml version="1.0" encoding="UTF-8"?>

<process name="holiday" xmlns="http://jbpm.org/4.4/jpdl">
   <!-- 第一次 -->
   <on event="start">
   	<event-listener class="cn.itcast.jbpm.h_event.MyEventListener"></event-listener>
   </on>
   <on event="end">
   	<event-listener class="cn.itcast.jbpm.h_event.MyEventListener"></event-listener>
   </on>
   
   <start name="start1" g="275,34,48,48">
   	   <!-- 第二次 -->
   	   <on event="end">
	   	<event-listener class="cn.itcast.jbpm.h_event.MyEventListener"></event-listener>
	   </on>
      <transition name="to 辦理" to="辦理" g="-45,-22"/>
   </start>
   
   <end name="end1" g="275,248,48,48">
   	  <on event="start">
	   	<event-listener class="cn.itcast.jbpm.h_event.MyEventListener"></event-listener>
	   </on>
   </end>
   <task name="辦理" g="253,134,92,52">
   	   <!-- 第三次 -->
       <on event="start">
	   	<event-listener class="cn.itcast.jbpm.h_event.MyEventListener"></event-listener>
	   </on>
	   <on event="end">
	   	<event-listener class="cn.itcast.jbpm.h_event.MyEventListener"></event-listener>
	   </on>
      <transition name="to end1" to="end1" g="-50,-22"/>
   </task>
   
</process>

 

 

建立事件監聽類 MyEventListener

 

package cn.itcast.jbpm.h_event;

import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;

public class MyEventListener implements EventListener {

	@Override
	//EventListenerExecution  繼承  OpenExecution  獲得流程中引數
	public void notify(EventListenerExecution eventListenerExecution) throws Exception {
		System.out.println("監聽器執行了..........");
		System.err.println("id :" + eventListenerExecution.getId());
		System.err.println("key :" + eventListenerExecution.getKey());
		System.err.println("name :" + eventListenerExecution.getName());
		System.err.println("activity :" + eventListenerExecution.getActivity());
		
	}

}

 

 

監聽測試類 EventTest  先部署流程,在釋出流程就可以在控制檯中看到監聽事件,所執行的次數與獲取到的流程例項物件

package cn.itcast.jbpm.h_event;

import org.jbpm.api.ExecutionService;
import org.jbpm.api.NewDeployment;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.RepositoryService;
import org.junit.Test;

public class EventTest {

	 @Test
	 //部署流程
	 public void testDeploy(){
		//獲取引擎
		 ProcessEngine processEngine = org.jbpm.api.Configuration.getProcessEngine();
		 //獲得例項管理service
		 RepositoryService repositoryService = processEngine.getRepositoryService();
		//建立一個部署物件
		NewDeployment createDeployment = repositoryService.createDeployment();
		createDeployment.addResourceFromClasspath("holiday/holiday.jpdl.xml");
		createDeployment.addResourceFromClasspath("holiday/holiday.png");
		// 釋出
		createDeployment.deploy();
		 
	 }
	 
	 @Test
	 //測試啟動流程
	 public void testStartProccess(){
		 ProcessEngine processEngine = org.jbpm.api.Configuration.getProcessEngine();
		 //啟動流程 獲得ExecutionService
		 ExecutionService executionService = processEngine.getExecutionService();
		 //啟動 (通過看錶才能知道id,使用較少)
//		 executionService.startProcessInstanceById("1");
		 
		 //設計流程時,流程有name -----預設生成流程key
		 executionService.startProcessInstanceByKey("holiday");
		 
	 }
	 
}

 

監聽成功  釋出一次總共監聽了 三次