activiti global process listens to ActivitiEventListener, which realizes listening to different types of events. It is very convenient without configuring task listening in acitivit

Keywords: Java Spring xml

If we want to configure monitoring for tasks, it's the normal way

One by one configuration, it's more troublesome.

Now use ActivitiEventListener to listen to global events, judge different event types, and then execute different business logic.

1. Define event handler interface

Using interfaces to define uniform conventions

public interface EventHandler {
    void handle(ActivitiEvent event);
}

2. Implement different events

Task end event

 */
public class TaskCompleteListener implements EventHandler {

    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public void handle(ActivitiEvent event) {
        ActivitiEntityEventImpl eventImpl = (ActivitiEntityEventImpl) event;
        TaskEntity taskEntity = (TaskEntity) eventImpl.getEntity();
   

    }
}

Task creation event

public class TaskCreateListener implements EventHandler {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void handle(ActivitiEvent event) {
        ActivitiEntityEventImpl eventImpl = (ActivitiEntityEventImpl) event;
        TaskEntity taskEntity = (TaskEntity) eventImpl.getEntity();
        
    }

}

There are many other events, such as task ﹣ assigned, process ﹣ started, process ﹣ completed

3. Implement ActivitiEventListener

public class GlobalEventListener implements ActivitiEventListener {
    protected Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * Processors for various events
     */
    private Map<ActivitiEventType, EventHandler> handlers = new HashMap<ActivitiEventType, EventHandler>();



    @Override
    public void onEvent(ActivitiEvent event) {
        EventHandler eventHandler = handlers.get(event.getType());
        if(eventHandler!=null){
            eventHandler.handle(event);
        }
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

    public Map<ActivitiEventType, EventHandler> getHandlers() {
        return handlers;
    }

    public void setHandlers(Map<ActivitiEventType, EventHandler> handlers) {
        this.handlers = handlers;
    }
}

4. Register event listening

 <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
        <property name="dataSource" ref="dataSource"/>
        <property name="transactionManager" ref="transactionManager"/>
        <property name="databaseSchemaUpdate" value="true"/>
        <property name="jobExecutorActivate" value="false"/>
        <property name="history" value="full"/>
        <property name="processDefinitionCacheLimit" value="10"/>

        <!-- Generate font for flowchart -->
        <property name="activityFontName" value="Song style"/>
        <property name="labelFontName" value="Song style"/>
        <property name="annotationFontName" value="Song style"/>
        <!-- Automatic deployment -->
        <property name="deploymentResources">
            <list>
                <value>classpath*:*</value>
            </list>
        </property>
        <!--  <property name="idGenerator" ref="idGenerator"/>-->
        <property name="eventListeners">
            <list>
                  <ref bean="globalEventListener"/>
            </list>
        </property>
        <property name="customFormTypes">
            <bean class="org.activiti.engine.impl.form.DateFormType">
                <constructor-arg value="yyyy-MM-dd HH:mm:ss"/>
            </bean>
        </property>
    </bean>

    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
        <property name="processEngineConfiguration" ref="processEngineConfiguration"/>
    </bean>

    <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>
    <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>
    <bean id="formService" factory-bean="processEngine" factory-method="getFormService"/>
    <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService"/>
    <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>
    <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>
    <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService"/>

    <!--Process global event handler-->
    <bean id="globalEventListener" class="GlobalEventListener">
         <property name="handlers">
             <map>
                 <entry key="TASK_CREATED">
                     <bean class="TaskCreateListener"/>
                 </entry>
                   <entry key="TASK_COMPLETED">
                     <bean class="TaskCompleteListener"/>
                   </entry>
             </map>
         </property>
     </bean>

Advantage

  • The code is clear, the business logic is decoupled, and the responsibility is single
  • It is convenient to omit configuration in bpmn.xml.

Posted by rd2010 on Sat, 08 Feb 2020 06:07:21 -0800