<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="true" /> <!-- 添加的自定义解析器,用于在任务中绑定监听器 --> <property name="customDefaultBpmnParseHandlers"> <list><bean class="me.lb.activiti.parser.CustomUserTaskParseHandler" /></list> </property> ... </bean>
package me.lb.activiti.parser; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; import org.activiti.bpmn.model.ActivitiListener; import org.activiti.bpmn.model.BaseElement; import org.activiti.bpmn.model.FieldExtension; import org.activiti.bpmn.model.ImplementationType; import org.activiti.bpmn.model.UserTask; import org.activiti.engine.delegate.TaskListener; import org.activiti.engine.impl.bpmn.parser.BpmnParse; import org.activiti.engine.impl.bpmn.parser.handler.UserTaskParseHandler; import org.activiti.engine.impl.task.TaskDefinition; import org.activiti.engine.parse.BpmnParseHandler; public class CustomUserTaskParseHandler implements BpmnParseHandler { public Collection<Class<? extends BaseElement>> getHandledTypes() { // 该方法会被activiti的设置调用,并与内置的任务解析器比对,如果类型一致,将代替默认的任务解析器 Set<Class<? extends BaseElement>> types = new HashSet<Class<? extends BaseElement>>(); types.add(UserTask.class); return types; } public void parse(BpmnParse bpmnParse, BaseElement baseElement) { // 调用默认的任务解析器 new UserTaskParseHandler().parse(bpmnParse, baseElement); TaskDefinition taskDefinition = (TaskDefinition) bpmnParse .getCurrentActivity().getProperty( UserTaskParseHandler.PROPERTY_TASK_DEFINITION); // 自定义任务解析(添加监听器) ActivitiListener custom = new ActivitiListener(); // 绑定监听事件 custom.setEvent(TaskListener.EVENTNAME_CREATE); // 设置监听器的实现类 custom.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_CLASS); custom.setImplementation("me.lb.activiti.listener.CustomTaskListener"); // 设置需要注入的属性 List<FieldExtension> fes = new ArrayList<FieldExtension>(); FieldExtension field_test = new FieldExtension(); field_test.setFieldName("test"); field_test.setStringValue("test:1:100"); fes.add(field_test); custom.setFieldExtensions(fes); // 将监听器绑定到任务 taskDefinition.addTaskListener(TaskListener.EVENTNAME_CREATE, bpmnParse .getListenerFactory().createClassDelegateTaskListener(custom)); } }
package me.lb.activiti.parser; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; import org.activiti.bpmn.model.ActivitiListener; import org.activiti.bpmn.model.BaseElement; import org.activiti.bpmn.model.FieldExtension; import org.activiti.bpmn.model.ImplementationType; import org.activiti.bpmn.model.UserTask; import org.activiti.engine.delegate.TaskListener; import org.activiti.engine.impl.bpmn.parser.BpmnParse; import org.activiti.engine.impl.bpmn.parser.handler.UserTaskParseHandler; import org.activiti.engine.impl.task.TaskDefinition; import org.activiti.engine.parse.BpmnParseHandler; public class CustomUserTaskParseHandler implements BpmnParseHandler { public Collection<Class<? extends BaseElement>> getHandledTypes() { // 该方法会被activiti的设置调用,并与内置的任务解析器比对,如果类型一致,将代替默认的任务解析器 Set<Class<? extends BaseElement>> types = new HashSet<Class<? extends BaseElement>>(); types.add(UserTask.class); return types; } public void parse(BpmnParse bpmnParse, BaseElement baseElement) { // 调用默认的任务解析器 new UserTaskParseHandler().parse(bpmnParse, baseElement); TaskDefinition taskDefinition = (TaskDefinition) bpmnParse .getCurrentActivity().getProperty( UserTaskParseHandler.PROPERTY_TASK_DEFINITION); // 自定义任务解析(添加监听器) ActivitiListener custom = new ActivitiListener(); // 绑定监听事件 custom.setEvent(TaskListener.EVENTNAME_CREATE); // 设置监听器的实现类 custom.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_CLASS); custom.setImplementation("me.lb.activiti.listener.CustomTaskListener"); // 设置需要注入的属性 List<FieldExtension> fes = new ArrayList<FieldExtension>(); FieldExtension field_test = new FieldExtension(); field_test.setFieldName("test"); field_test.setStringValue("test:1:100"); fes.add(field_test); custom.setFieldExtensions(fes); // 将监听器绑定到任务 taskDefinition.addTaskListener(TaskListener.EVENTNAME_CREATE, bpmnParse .getListenerFactory().createClassDelegateTaskListener(custom)); } }
原文地址:http://blog.csdn.net/tylanbin/article/details/38755301