activiti工作流Activitiactiviti

activiti总体框架分析

2016-08-11  本文已影响1144人  西普士

Engine解析

activiti包.png

1. org.activiti.engine

  1. 定义了流程管理服务的接口:RepositoryServiceRuntimeServiceFormServiceTaskServiceHistoryServiceIdentityServiceManagementService

org.activiti.engine是activiti的核心功能,控制工作流的流转。几个核心的类如下图所示:

engine

1.1 ProcessEngine

ProcessEngine接口继承EngineServicesEngineServices包括很多工作流/BPM方法的服务,它们都是线程安全的。EngineServices提供的服务包括:

EngineServices 代码如下所示:


  public interface EngineServices {

    RepositoryService getRepositoryService();

    RuntimeService getRuntimeService();

    FormService getFormService();

    TaskService getTaskService();

    HistoryService getHistoryService();

    IdentityService getIdentityService();

    ManagementService getManagementService();

    ProcessEngineConfiguration getProcessEngineConfiguration();
  }

ProcessEngine 代码如下所示:


  public interface ProcessEngine extends EngineServices {

    /** the version of the activiti library */
    public static String VERSION = "5.17.0.2";

    /** The name as specified in 'process-engine-name' in
     * the activiti.cfg.xml configuration file.
     * The default name for a process engine is 'default */
    String getName();

    void close();
  }

1.2 ProcessEngineConfiguration

ProcessEngineConfiguration是配置管理类,它管理的对象包括ProcessEngine,XXservice,数据库session等。ProcessEngineConfiguration的配置,activiti默认会从activiti.cfg.xml中读取,也可以在Spring的配置文件中读取。ProcessEngineConfiguration的实现包括:

  public class StandaloneProcessEngineConfiguration extends ProcessEngineConfigurationImpl {
    @Override
    protected CommandInterceptor createTransactionInterceptor() {
      return null;
    }
  }
  public class StandaloneInMemProcessEngineConfiguration extends StandaloneProcessEngineConfiguration {
   public StandaloneInMemProcessEngineConfiguration() {
     this.databaseSchemaUpdate = DB_SCHEMA_UPDATE_CREATE_DROP;
     this.jdbcUrl = "jdbc:h2:mem:activiti";
   }
  }

1.3 ActivitiException

activiti的基础异常类是org.activiti.engine.ActivitiException,一个非检查异常。Activiti的异常都是通过org.activiti.engine.ActivitiException抛出,但存在以下特殊情况:

2. org.activiti.engine.impl

  1. 实现了流程管理服务RepositoryServiceImpl, RuntimeServiceImpl, FormServiceImpl, TaskServiceImpl, HistoryServiceImpl, IdentityServiceImpl, ManagementServiceImpl
命令接口包.png

2.1 org.activiti.engine.impl.ServiceImpl

org.activiti.engine.impl.ServiceImpl是流程管理服务的基类,它的派生类包括RepositoryServiceImpl, RuntimeServiceImpl, FormServiceImpl, TaskServiceImpl, HistoryServiceImpl, IdentityServiceImpl, ManagementServiceImpl,它定义了配置管理服务processEngineConfiguration、命令执行接口commandExecutor(activiti方法调用都通过命令模式)。源码如下所示:


  public class ServiceImpl {

    protected ProcessEngineConfigurationImpl processEngineConfiguration;

    public ServiceImpl() {
    }

    public ServiceImpl(ProcessEngineConfigurationImpl processEngineConfiguration) {
      this.processEngineConfiguration = processEngineConfiguration;
    }

    protected CommandExecutor commandExecutor;

    public CommandExecutor getCommandExecutor() {
      return commandExecutor;
    }

    public void setCommandExecutor(CommandExecutor commandExecutor) {
      this.commandExecutor = commandExecutor;
    }
  }

XXServiceImpl继承类org.activiti.engine.impl.ServiceImpl,并且实现对应的XXService接口。下面是RepositoryServiceImpl示例代码:


  public class RepositoryServiceImpl extends ServiceImpl implements RepositoryService {
  }

XXService的初始化在ProcessEngineConfigurationImpl中

   protected RepositoryService repositoryService = new RepositoryServiceImpl();  
   protected RuntimeService runtimeService = new RuntimeServiceImpl();
   protected HistoryService historyService = new HistoryServiceImpl(this);
   protected IdentityService identityService = new IdentityServiceImpl();
   protected TaskService taskService = new TaskServiceImpl(this);
   protected FormService formService = new FormServiceImpl();
   protected ManagementService managementService = new ManagementServiceImpl();

XXServicecommandExecutor初始化在ProcessEngineConfigurationImpl的initService中

 protected void initService(Object service) {
   if (service instanceof ServiceImpl) {
     ((ServiceImpl)service).setCommandExecutor(commandExecutor);
   }
 }

2.2 org.activiti.engine.impl.interceptor

org.activiti.engine.impl.interceptor定义了拦截器和命令。activiti里面所有的指令都是通过命令模式执行,在命令执行之前,可以切入多个拦截器。

拦截器
 public interface Command <T> {
   T execute(CommandContext commandContext);
 }


 public interface CommandExecutor {

   /**
    * @return the default {@link CommandConfig}, used if none is provided.
    */
   CommandConfig getDefaultConfig();

   /**
    * Execute a command with the specified {@link CommandConfig}.
    */
   <T> T execute(CommandConfig config, Command<T> command);

   /**
    * Execute a command with the default {@link CommandConfig}.
    */
   <T> T execute(Command<T> command);

 }

 public interface CommandInterceptor {

   <T> T execute(CommandConfig config, Command<T> command);

   CommandInterceptor getNext();

   void setNext(CommandInterceptor next);

 }

RuntimeServiceImpl为例, RuntimeServiceImpl继承类ServiceImplServiceImpl包含CommandExecutor属性

ProcessEngineConfigurationImpl中有个init方法,里面有对于executor和intecerptor的初始化


 //  初始化各种服务
 protected void initServices() {
     initService(repositoryService);
     initService(runtimeService);
     initService(historyService);
     initService(identityService);
     initService(taskService);
     initService(formService);
     initService(managementService);
 }

 //  初始化服务方法  
 protected void initService(Object service) {
   if (service instanceof ServiceImpl) {
     ((ServiceImpl)service).setCommandExecutor(commandExecutor);
   }
 }

 //  初始化拦截器
 protected void initCommandInterceptors() {
     if (commandInterceptors==null) {
       commandInterceptors = new ArrayList<CommandInterceptor>();
       if (customPreCommandInterceptors!=null) {
         commandInterceptors.addAll(customPreCommandInterceptors);
       }
       commandInterceptors.addAll(getDefaultCommandInterceptors());
       if (customPostCommandInterceptors!=null) {
         commandInterceptors.addAll(customPostCommandInterceptors);
       }
       commandInterceptors.add(commandInvoker);
     }
 }

 //  将拦截器初始化成链式结构
 protected void initCommandExecutor() {
   if (commandExecutor==null) {
     CommandInterceptor first = initInterceptorChain(commandInterceptors);
     commandExecutor = new CommandExecutorImpl(getDefaultCommandConfig(), first);
   }
 }

2.3 org.activiti.engine.impl.delegate

org.activiti.engine.impl.delegate实现了监听器和事件处理,activiti 允许客户端代码介入流程的执行,为此提供了这个基础组件。

activiti5.16 用户手册的介绍,监听器事件处理

2.3.1 监听器

监听器可以捕获的事件包括:

  1. 流程实例的启动和结束
执行监听器

DelegateInterceptor是事件拦截器接口,DelegateInvocation是事件调用接口,XXXInvocationDelegateInvocation的实现类,XXXInvocation里面包含监听接口XXXListener

下面的流程定义文件包含2个监听器,event表示时间类型,class表示处理事件的java类。

 <extensionElements>
   <activiti:taskListener event="create" class="com.alfrescoblog.MyTest.imple.CreateTaskDelegate"></activiti:taskListener>
   <activiti:taskListener event="complete" class="com.alfrescoblog.MyTest.imple.MyJavaDelegate"></activiti:taskListener>
 </extensionElements>

CreateTaskDelegate是客户端实现的监听类,TaskListener``是activiti的监听接口。

package com.alfrescoblog.MyTest.imple;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

public class CreateTaskDelegate implements TaskListener {

  public void notify(DelegateTask delegateTask) {
    // TODO Auto-generated method stub
    System.out.println("创建任务啦!!"); 
  }

}

TaskListener代码如下所示:


 public interface TaskListener extends Serializable {

   String EVENTNAME_CREATE = "create";
   String EVENTNAME_ASSIGNMENT = "assignment";
   String EVENTNAME_COMPLETE = "complete";
   String EVENTNAME_DELETE = "delete";

   /**
    * Not an actual event, used as a marker-value for {@link TaskListener}s that should be called for all events,
    * including {@link #EVENTNAME_CREATE}, {@link #EVENTNAME_ASSIGNMENT} and {@link #EVENTNAME_COMPLETE} and {@link #EVENTNAME_DELETE}.
    */
   String EVENTNAME_ALL_EVENTS = "all";

   void notify(DelegateTask delegateTask);
 }

BPMN流程文件部署的时候会注入各种listener。比如TaskListener在org.activiti.engine.impl.task.TaskDefinition的addTaskListener方法中被注入,代码如下:


 public void addTaskListener(String eventName, TaskListener taskListener) {
   if(TaskListener.EVENTNAME_ALL_EVENTS.equals(eventName)) {
     // In order to prevent having to merge the "all" tasklisteners with the ones for a specific eventName,
     // every time "getTaskListener()" is called, we add the listener explicitally to the individual lists
     this.addTaskListener(TaskListener.EVENTNAME_CREATE, taskListener);
     this.addTaskListener(TaskListener.EVENTNAME_ASSIGNMENT, taskListener);
     this.addTaskListener(TaskListener.EVENTNAME_COMPLETE, taskListener);
     this.addTaskListener(TaskListener.EVENTNAME_DELETE, taskListener);

   } else {
     List<TaskListener> taskEventListeners = taskListeners.get(eventName);
     if (taskEventListeners == null) {
       taskEventListeners = new ArrayList<TaskListener>();
       taskListeners.put(eventName, taskEventListeners);
     }
     taskEventListeners.add(taskListener);
   }
 }

以TaskListener为例,调用链:UserTaskActivityBehavior.execute()task.fireEvent(TaskListener.EVENTNAME_CREATE);DelegateInterceptor.handleInvocation()DefaultDelegateInterceptor.handleInvocation()DelegateInvocation.proceed()TaskListenerInvocation.invoke()TaskListener.notify()

UserTaskActivityBehavior 是任务新增、修改、删除行为,UserTask节点解析(UserTaskParseHandler.executeParse)的时候设置到activiti中,触发的代码还没找到。

 protected void executeParse(BpmnParse bpmnParse, UserTask userTask) {
   ActivityImpl activity = createActivityOnCurrentScope(bpmnParse, userTask, BpmnXMLConstants.ELEMENT_TASK_USER);

   activity.setAsync(userTask.isAsynchronous());
   activity.setExclusive(!userTask.isNotExclusive());

   TaskDefinition taskDefinition = parseTaskDefinition(bpmnParse, userTask, userTask.getId(), (ProcessDefinitionEntity) bpmnParse.getCurrentScope().getProcessDefinition());
   activity.setProperty(PROPERTY_TASK_DEFINITION, taskDefinition);
   activity.setActivityBehavior(bpmnParse.getActivityBehaviorFactory().createUserTaskActivityBehavior(userTask, taskDefinition));
 }

参考资料

上一篇 下一篇

猜你喜欢

热点阅读