Skip to content
一、事件监听
  • 事件及监听器原理

    image-20200713123525799

  • 配置Listener

    • eventListeners:监听所有事件派发通知。
    • typedEventListeners:监听指定事件类型的通知。
    • activiti:eventListener:只监听特定流程定义的事件。
  • 相关API

    • ActivitiEvent:事件对象
    • ActivitiEventListener:监听器
    • ActivitiEventType:事件类型
二、具体实现
1. eventListeners实现
  • 测试入口

    java
    package com.laogoubi.config;
    
    
    import org.activiti.engine.runtime.ProcessInstance;
    import org.activiti.engine.task.Task;
    import org.activiti.engine.test.ActivitiRule;
    import org.activiti.engine.test.Deployment;
    import org.junit.Rule;
    import org.junit.Test;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    
    /**
     * @ClassName ConfigEventListenerTest
     * @Description EventListener
     * @Author eastern
     * @Date 2020/6/28 上午11:49
     * @Version 1.0
     **/
    public class ConfigEventListenerTest {
    	private static final Logger logger = LoggerFactory.getLogger(ConfigEventListenerTest.class);
    
    	@Rule
    	public ActivitiRule activitiRule = new ActivitiRule("activiti_eventListener.cfg.xml");
    
    	@Test
    	@Deployment(resources = {"com/laogoubi/my-process.bpmn20.xml"})
    	public void test() {
    		ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("my-process");
    		Task task = activitiRule.getTaskService().createTaskQuery().singleResult();
    		activitiRule.getTaskService().complete(task.getId());
    	}
    }
  • CustomEventListener

    java
    package com.laogoubi.event;
    
    import org.activiti.engine.delegate.event.ActivitiEvent;
    import org.activiti.engine.delegate.event.ActivitiEventListener;
    import org.activiti.engine.delegate.event.ActivitiEventType;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     * @ClassName CustomEventListener
     * @Description CustomEventListener
     * @Author eastern
     * @Date 2020/6/29 下午11:10
     * @Version 1.0
     **/
    public class CustomEventListener implements ActivitiEventListener {
    
    	private static final Logger logger = LoggerFactory.getLogger(CustomEventListener.class);
    
    	@Override
    	public void onEvent(ActivitiEvent event) {
    		ActivitiEventType eventType = event.getType();
    		if (ActivitiEventType.CUSTOM.equals(eventType)) {
    			logger.info("监听自定义事件 {} \t {}", eventType, event.getProcessInstanceId());
    		}
    	}
    
    	@Override
    	public boolean isFailOnException() {
    		return false;
    	}
    }
  • 配置文件:activiti_eventListener.cfg.xml

    xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
            <property name="eventListeners">
                <list>
                    <bean class="com.laogoubi.event.CustomEventListener"></bean>
                </list>
            </property>
        </bean>
    </beans>
2. typedEventListeners实现
  • 代码实现与eventListeners相同

  • 配置文件:activiti_eventListener.cfg.xml

    xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
            <property name="typedEventListeners">
                <map>
                    <entry key="PROCESS_STARTED">
                        <list>
                            <bean class="com.laogoubi.event.ProcessEventListener"></bean>
                        </list>
                    </entry>
                </map>
            </property>
        </bean>
    </beans>
3. 代码动态添加addEventListener和dispatchEvent
  • 代码实现:

    java
    package com.laogoubi.config;
    
    import com.laogoubi.event.CustomEventListener;
    import org.activiti.engine.delegate.event.ActivitiEventType;
    import org.activiti.engine.delegate.event.impl.ActivitiEventImpl;
    import org.activiti.engine.runtime.ProcessInstance;
    import org.activiti.engine.task.Task;
    import org.activiti.engine.test.ActivitiRule;
    import org.activiti.engine.test.Deployment;
    import org.junit.Rule;
    import org.junit.Test;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    
    /**
     * @ClassName ConfigEventListenerTest
     * @Description EventListener
     * @Author eastern
     * @Date 2020/6/28 上午11:49
     * @Version 1.0
     **/
    public class ConfigEventListenerTest {
    	private static final Logger logger = LoggerFactory.getLogger(ConfigEventListenerTest.class);
    
    	@Rule
    	public ActivitiRule activitiRule = new ActivitiRule("activiti_eventListener.cfg.xml");
    
    	@Test
    	@Deployment(resources = {"com/laogoubi/my-process.bpmn20.xml"})
    	public void test() {
    		ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("my-process");
    		Task task = activitiRule.getTaskService().createTaskQuery().singleResult();
    		activitiRule.getTaskService().complete(task.getId());
    
    		activitiRule.getRuntimeService().addEventListener(new CustomEventListener());
    		activitiRule.getRuntimeService().dispatchEvent(new ActivitiEventImpl(ActivitiEventType.CUSTOM));
    	}
    }
  • 流程配置文件:my-process.bpmn20.xml

    xml
    <?xml version="1.0" encoding="UTF-8"?>
    
    <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn"
    	xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"
    	xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema"
    	expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
    	<process id="my-process">
    		<startEvent id="start" />
    		<sequenceFlow id="flow1" sourceRef="start" targetRef="someTask" />
    		<userTask id="someTask" name="Activiti is awesome!" />
    		<sequenceFlow id="flow2" sourceRef="someTask" targetRef="end" />
    		<endEvent id="end" />
    	</process>
    </definitions>
  • activiti_eventListener.cfg.xml

    xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
        </bean>
    </beans>

Released under the MIT License.