JBPM4基础篇07-监听事件EventListener

在JBPM中配置监听,可以拦截某个任务节点的开始或者结束。

  1. event.jpdl.xml在设计器中的示意图:


    再来看看Source代码:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <process name="event" xmlns="http://jbpm.org/4.3/jpdl">
    
    	<on event="start">
    		<event-listener class="org.wxp.ProcessEventListener" />
    	</on>
    	<on event="end">
    		<event-listener class="org.wxp.ProcessEventListener" />
    	</on>
    
    	<start g="178,107,48,48" name="start1">
    		<on event="start">
    			<event-listener class="org.wxp.ProcessEventListener" />
    		</on>
    		<on event="end">
    			<event-listener class="org.wxp.ProcessEventListener" />
    		</on>
    		<transition name="to state1" to="state1" g="-59,-17" />
    	</start>
    	<end g="187,302,48,48" name="end1">
    		<on event="start">
    			<event-listener class="org.wxp.ProcessEventListener" />
    		</on>
    		<on event="end">
    			<event-listener class="org.wxp.ProcessEventListener" />
    		</on>
    	</end>
    	<state name="state1" g="207,222,92,52">
    		<on event="start">
    			<event-listener class="org.wxp.ProcessEventListener" />
    		</on>
    		<on event="end">
    			<event-listener class="org.wxp.ProcessEventListener" />
    		</on>
    		<transition name="to end1" to="end1" g="-47,-17" />
    	</state>
    </process>

  2. ProcessEventListener
    这个类是我们定义的一个实现了EventListener接口的实现类;在其方法中,输出一些信息来测试该方法是否被执行过。
    package org.wxp;
    
    import org.jbpm.api.listener.EventListener;
    import org.jbpm.api.listener.EventListenerExecution;
    
    public class ProcessEventListener implements EventListener {
    
    	@Override
    	public void notify(EventListenerExecution eventListenerExecution)
    			throws Exception {
    		System.out.println(eventListenerExecution);
    	}
    
    }
    

  3. EventTest
    至此,需要测试一下是否流程的start和end事件被截获到
    package org.wxp;
    
    import junit.framework.TestCase;
    
    import org.jbpm.api.Configuration;
    import org.jbpm.api.ExecutionService;
    import org.jbpm.api.ProcessEngine;
    import org.jbpm.api.RepositoryService;
    
    /**
     * 设定了事件监听器Listener,测试start和end事件
     * 
     * 1.如果一个节点是从其直接开始,并没有上个节点的进入,那么这个节点的start事件就不执行
     * 2.state是等待过程,那么它的end事件就不会执行
     * @author Champion.Wong
     * 
     */
    public class EventTest extends TestCase {
    	ProcessEngine processEngine;
    
    	public EventTest() {
    		processEngine = Configuration.getProcessEngine();
    	}
    
    	public void test() {
    		RepositoryService repositoryService = processEngine
    				.getRepositoryService();
    		String deploymentId = repositoryService.createDeployment()
    				.addResourceFromClasspath("event.jpdl.xml").deploy();
    
    		ExecutionService executionService = processEngine.getExecutionService();
    		executionService.startProcessInstanceByKey("event");
    	}
    }
    


你可能感兴趣的:(JBPM4基础篇07-监听事件EventListener)