1. 执行监听器的使用场景
1.1 人员动态分配
- 节点审批人员需要在流程运行过程中动态分配
- 当前任务节点完成的时候,指定需要指定下一个节点的处理人(比如,一个请假流程,a员工请假,需要指定下一步需要处理请假流程的领导。)。
1.2 任务节点调取业务
任务节点完成的时候,需要一些复杂业务,(比如当前节点完成的时候,需要调用我们的jms消息系统发送消息)。
任务流转到当前的节点的时候,需要监控当前任务节点的一些信息或者其他的业务信息。
当前的任务节点分配处理人的时候,需要触发自定义的一些业务。
1.3 流程上处理业务
流程开始结束的时候,需要处理业务信息。
1.4 连线上处理业务
经过任务节点的出线,也就是连线的时候,需要触发自定义的业务。
1.5 等等...
2. 执行监听器的监听事件
源码中如下:
/** Callback interface to be notified of execution events like starting a process instance,
* ending an activity instance or taking a transition.
*
* @author Tom Baeyens
* @author Joram Barrez
*/
public interface ExecutionListener extends Serializable {
String EVENTNAME_START = "start";//作用在流程实例的开始事件
String EVENTNAME_END = "end";//作用在流程实例的结束事件
String EVENTNAME_TAKE = "take";//作用在流程实例的连线上
void notify(DelegateExecution execution) throws Exception;
}
3.流程监听器实现方式——类class
3.1 实现ExecutionListener方式
3.1.1 xml文件
流程定义文件:
<process id="process_id_or_key" name="企业信息完善审批流程模型" isExecutable="true">
定义在流程上的监听器
<extensionElements>
<activiti:executionListener event="start" class="org.jeecg.modules.activiti.ext.listener.SiteReportExecutionListener"></activiti:executionListener>
</extensionElements>
<startEvent id="sid-2C00B4D7-2746-4AFB-904E-A2B17C071EC7"></startEvent>
<userTask id="sid-4BDFBF55-11C2-465E-A90E-8D926ABD2D90" name="办事处审批"></userTask>
<userTask id="sid-D3C34D06-9E4D-4EA3-BF9C-20CF9BFB0F71" name="县区审批">
定义在任务节点上的监听
<extensionElements>
<activiti:executionListener event="start" class="org.jeecg.modules.activiti.ext.listener.SiteReportExecutionListener"></activiti:executionListener>
</extensionElements>
</userTask>
<userTask id="sid-BCAA6507-465C-4B7D-9319-3E92909BA897" name="市审批"></userTask>
<sequenceFlow id="sid-B010604D-1D3F-4296-8FD4-136F18A08A61" sourceRef="sid-D3C34D06-9E4D-4EA3-BF9C-20CF9BFB0F71" targetRef="sid-BCAA6507-465C-4B7D-9319-3E92909BA897"></sequenceFlow>
<endEvent id="sid-70082386-CA2C-4B0C-BC20-40642ACBA994"></endEvent>
<sequenceFlow id="sid-F7AD426E-DE89-42AB-8EB5-0E9873DEEA71" sourceRef="sid-BCAA6507-465C-4B7D-9319-3E92909BA897" targetRef="sid-70082386-CA2C-4B0C-BC20-40642ACBA994"></sequenceFlow>
<sequenceFlow id="sid-9F804DC4-CBC3-455F-B11F-D82C405E93F3" sourceRef="sid-2C00B4D7-2746-4AFB-904E-A2B17C071EC7" targetRef="sid-4BDFBF55-11C2-465E-A90E-8D926ABD2D90"></sequenceFlow>
定义在流转连线上的监听器
<sequenceFlow id="sid-9B00C62D-3355-4FEE-95B8-04C7C157D8BB" sourceRef="sid-4BDFBF55-11C2-465E-A90E-8D926ABD2D90" targetRef="sid-D3C34D06-9E4D-4EA3-BF9C-20CF9BFB0F71">
<extensionElements>
<activiti:executionListener event="start" class="org.jeecg.modules.activiti.ext.listener.SiteReportExecutionListener"></activiti:executionListener>
</extensionElements>
</sequenceFlow>
</process>
3.1.2 监听器代码
/**
* 执行监听器可以在流程执行中发生特定的事件时,执行外部Java代码或计算表达式。可以被捕获的事件有
*
* >> 流程实例的start(启动)和end(结束)。
* >> take(进行)转移(transition)。(这里是指顺序流线 上监听事件只有take)
* >> 活动的start和end。
* >> 网关的start和end。
* >> 中间事件的start和end。
* >> 启动事件的end,和结束事件的start。
*
*/
public class SiteReportExecutionListener implements ExecutionListener {
private static final long serialVersionUID = 8282921521567009453L;
@Override
public void notify(DelegateExecution execution) throws Exception {
//【1】监听的时间
String eventName = execution.getEventName();//监听的事件名称
if(EVENTNAME_START.equals(eventName)){//start
}else if(EVENTNAME_END.equals(eventName)){//end
}else if(EVENTNAME_TAKE.equals(eventName)){//序流线 上监听事件只有take
}
//【2】流程定义 以及 当前活动节点
String currentExecutionId = execution.getId();//正在执行的流程对象的执行id
String currentActivityId = execution.getCurrentActivityId();//正在执行的流程对象的活动节点id 获取当前的.Activityid
String currentActivityName = execution.getCurrentActivityName();//正在执行的流程对象的活动节点名称
String processInstanceId = execution.getProcessInstanceId();//当前流程实例id
String processDefinitionId = execution.getProcessDefinitionId();//流程定义id
String parentExecutionId = execution.getParentId();//获取父id,并发的时候有用
String tenantId = execution.getTenantId();//获取TenantId 当有多个TenantId 有用
String businessKey = execution.getBusinessKey();//业务id已经废弃
String processBusinessKey = execution.getProcessBusinessKey();//业务id
/**
* 这个非常有用吧。当拿到EngineServices 对象所有的xxxService都可以拿到
*/
EngineServices engineServices = execution.getEngineServices();
TaskService taskService = engineServices.getTaskService();
RuntimeService runtimeService = engineServices.getRuntimeService();
//【3】流程变量的获取与设置,删除 增晒改查
execution.getVariable("");
execution.setVariable("","");
}
3.2 委托类DelegateExecution介绍
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.engine.delegate;
import org.activiti.engine.EngineServices;
/**
* Execution used in {@link JavaDelegate}s and {@link ExecutionListener}s.
*
* @author Tom Baeyens
*/
public interface DelegateExecution extends VariableScope {
/** Unique id of this path of execution that can be used as a handle to provide external signals back into the engine after wait states. */
String getId();
/** Reference to the overall process instance */
String getProcessInstanceId();
/** The {@link ExecutionListener#EVENTNAME_START event name} in case this execution is passed in for an {@link ExecutionListener} */
String getEventName();
/** The business key for this execution. Only returns a value if the delegate execution
* is a process instance.
*
* @deprecated use {@link #getProcessBusinessKey()} to get the business key for the process
* associated with this execution, regardless whether or not this execution is a
* process-instance.
*/
String getBusinessKey();
/**
* The business key for the process instance this execution is associated with.
*/
String getProcessBusinessKey();
/**
* The process definition key for the process instance this execution is associated with.
*/
String getProcessDefinitionId();
/**
* Gets the id of the parent of this execution. If null, the execution represents a process-instance.
*/
String getParentId();
/**
* Gets the id of the calling execution. If not null, the execution is part of a subprocess.
*/
String getSuperExecutionId();
/**
* Gets the id of the current activity.
*/
String getCurrentActivityId();
/**
* Gets the name of the current activity.
*/
String getCurrentActivityName();
/**
* Returns the tenant id, if any is set before on the process definition or process instance.
*/
String getTenantId();
/**
* All Activiti services can be accessed through this interface.
*/
EngineServices getEngineServices();
}
4. 流程监听器实现方式——表达式
同5.2 activiti任务监听器TaskListener_青苔猿猿的博客-CSDN博客
5. 流程监听器实现方式——委托表达式
同5.2 activiti任务监听器TaskListener_青苔猿猿的博客-CSDN博客
6. 实现JavaDelegate方式
实现了接口JavaDelegate
6.1 实现代码
package org.jeecg.modules.activiti.ext.listener;
import org.activiti.engine.EngineServices;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.JavaDelegate;
/**
* 委托类的实现方式
*
*/
public class SiteReportJavaDelegate implements JavaDelegate {
/**
* 自定义方法
*/
public void test1(){
System.out.println("coming_2");
}
@Override
public void execute(DelegateExecution execution) throws Exception {
//【1】监听的时间
String eventName = execution.getEventName();//监听的事件名称
if(ExecutionListener.EVENTNAME_START.equals(eventName)){//start
}else if(ExecutionListener.EVENTNAME_END.equals(eventName)){//end
}else if(ExecutionListener.EVENTNAME_TAKE.equals(eventName)){//序流线 上监听事件只有take
}
//【2】流程定义 以及 当前活动节点
String currentExecutionId = execution.getId();//正在执行的流程对象的执行id
String currentActivityId = execution.getCurrentActivityId();//正在执行的流程对象的活动节点id 获取当前的.Activityid
String currentActivityName = execution.getCurrentActivityName();//正在执行的流程对象的活动节点名称
String processInstanceId = execution.getProcessInstanceId();//当前流程实例id
String processDefinitionId = execution.getProcessDefinitionId();//流程定义id
String parentExecutionId = execution.getParentId();//获取父id,并发的时候有用
String tenantId = execution.getTenantId();//获取TenantId 当有多个TenantId 有用
String businessKey = execution.getBusinessKey();//业务id已经废弃
String processBusinessKey = execution.getProcessBusinessKey();//业务id
/**
* 这个非常有用吧。当拿到EngineServices 对象所有的xxxService都可以拿到
*/
EngineServices engineServices = execution.getEngineServices();
TaskService taskService = engineServices.getTaskService();
RuntimeService runtimeService = engineServices.getRuntimeService();
//【3】流程变量的获取与设置,删除 增晒改查
execution.getVariable("");
execution.setVariable("","");
}
}
6.2 xml定义三种实现方式
6.2.1 xml定义在类上
<extensionElements>
<activiti:executionListener event="start" class="org.jeecg.modules.activiti.ext.listener.SiteReportJavaDelegate"></activiti:executionListener>
</extensionElements>
6.2.2 xml定义在表达式上
表达式处:${siteReportJavaDelegate.test1()}会执行test1方法
6.2.2 xml定义在委托表达式上
委托表达式:${siteReportJavaDelegate} 直接执行execute方法。