activiti学习

Exposing configuration beans in expressions and scripts

在表达式和脚本中公开配置bean

By default, all beans that you specify in the activiti.cfg.xml configuration or in your own Spring configuration file are available to expressions and in the scripts. If you want to limit the visibility of beans in your configuration file, you can configure a property called beans in your process engine configuration. The beans property in ProcessEngineConfiguration is a map. When you specify that property, only beans specified in that map will be visible to expressions and scripts. The exposed beans will be exposed with the names as you specify in that map.

默认情况下,您在activiti.cfg.xml配置或您自己的Spring配置文件中指定的所有bean对表达式和脚本都是可用的。如果希望限制配置文件中bean的可见性,可以在流程引擎配置中配置名为beans的属性。ProcessEngineConfiguration中的beans属性是一个映射。当您指定该属性时,只有在该映射中指定的bean才对表达式和脚本可见。公开的bean将使用您在该映射中指定的名称公开。

Deployment cache configuration (部署缓存配置)

All process definition are cached (after they’re parsed) to avoid hitting the database every time a process definition is needed and because process definition data doesn’t change. By default, there is no limit on this cache. To limit the process definition cache, add following property

所有流程定义都会缓存(在解析之后),以避免每次需要流程定义时都碰到数据库,因为流程定义数据不会更改。默认情况下,这个缓存没有限制。要限制流程定义缓存,请在表达式和脚本中添加以下propertyexpose配置bean

<property name="processDefinitionCacheLimit" value="10" />

Setting this property will swap the default hashmap cache with a LRU cache that has the provided hard limit. Of course, the best value of this property depends on the total amount of process definitions stored and the number of process definitions actually used at runtime by all the runtime process instances.

You can also inject your own cache implementation. This must be a bean that implements the org.activiti.engine.impl.persistence.deploy.DeploymentCache interface:

<property name="processDefinitionCache">

There is a similar property called knowledgeBaseCacheLimit and knowledgeBaseCache for configuring the rules cache. This is only needed when you use the rules task in your processes.

默认使用的是hashMap缓存的方式,缓存个数的大小取决于硬件的配置和流程定义总数及实际使用的流程数量。
我们也可以注入自己的缓存实现,必须要实现接口org.activiti.engine.impl.persistence.deploy.DeploymentCache
有一个类似的属性叫做knowledgeBaseCacheLimit和knowledgeBaseCache,用于配置规则缓存。只有在流程中使用规则任务时才需要这样做。

Logging

所有的日志都是通过SLF4J路由的,并允许选择我们选择的日志实现。
默认情况下,在activiti-engine依赖项中不存在SFL4J-binding jar,为了使用您选择的日志框架,应该将其添加到您的项目中。如果没有添加任何实现jar, SLF4J将使用一个NOP-logger,根本不记录任何日志,只是发出一个不记录任何日志的警告。
activiti-ui和activiti-rest webapps被配置为使用Log4j-binding。当运行所有activiti-*模块的测试时,也使用Log4j。

Event handlers(事件处理程序)

The event mechanism in the Activiti engine allows you to get notified when various events occur within the engine. Take a look at all supported event types for an overview of the events available.

tiviti引擎中的事件机制允许您在引擎中发生各种事件时得到通知。查看所有受支持的事件类型,以获得可用事件的概述。

All events dispatched are a subtype of org.activiti.engine.delegate.event.ActivitiEvent. The event exposes (if available) the type, executionId, processInstanceId and processDefinitionId

所有事件实现都是org.activiti.engine.delegate.event.ActivitiEvent的子类型。该事件公开(如果可用)类型executionId、processInstanceId和processDefinitionId

一个事件监听的例子

public class MyEventListener implements ActivitiEventListener {

  @Override
  public void onEvent(ActivitiEvent event) {
    switch (event.getType()) {

      case JOB_EXECUTION_SUCCESS:
        System.out.println("A job well done!");
        break;

      case JOB_EXECUTION_FAILURE:
        System.out.println("A job has failed...");
        break;

      default:
        System.out.println("Event received: " + event.getType());
    }
  }

  @Override
  public boolean isFailOnException() {
    // The logic in the onEvent method of this listener is not critical, exceptions
    // can be ignored if logging fails...
    return false;
  }
}

There are a few base implementations provided by Activiti to facilitate common use cases of event-listeners. These can be used as base-class or as an example listener implementation:
org.activiti.engine.delegate.event.BaseEntityEventListener: An event-listener base-class that can be used to listen for entity-related events for a specific type of entity or for all entities. It hides away the type-checking and offers 4 methods that should be overridden: onCreate(…), onUpdate(…) and onDelete(…) when an entity is created, updated or deleted. For all other entity-related events, the onEntityEvent(…) is called.

Activiti提供了一些基本实现来促进事件监听器的通用用例。这些可以用作基类或示例侦听器实现:
org.activiti.engine.delegate.event.BaseEntityEventListenerr:一个事件监听器基类,可用于监听特定类型实体或所有实体的实体相关事件。它隐藏了类型检查,并提供了4个应该被覆盖的方法:onCreate(…)、onUpdate(…)和onDelete(…)当一个实体被创建、更新或删除时。对于所有其他与实体相关的事件,将调用onEntityEvent(…)。

配置事件监听如下:

<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
    ...
    <property name="eventListeners">
      <list>
         <bean class="org.activiti.engine.example.MyEventListener" />
      </list>
    </property>
</bean>

也可以配置一个执行类型的事件监听:

<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
    ...
    <property name="typedEventListeners">
      <map>
        <entry key="JOB_EXECUTION_SUCCESS,JOB_EXECUTION_FAILURE" >
          <list>
            <bean class="org.activiti.engine.example.MyJobEventListener" />
          </list>
        </entry>
      </map>
    </property>
</bean>

也可以通过代码运行时添加监听,API接口如下:

/**
 * Adds an event-listener which will be notified of ALL events by the dispatcher.
 * @param listenerToAdd the listener to add
 */
void addEventListener(ActivitiEventListener listenerToAdd);

/**
 * Adds an event-listener which will only be notified when an event occurs, which type is in the given types.
 * @param listenerToAdd the listener to add
 * @param types types of events the listener should be notified for
 */
void addEventListener(ActivitiEventListener listenerToAdd, ActivitiEventType... types);

/**
 * Removes the given listener from this dispatcher. The listener will no longer be notified,
 * regardless of the type(s) it was registered for in the first place.
 * @param listenerToRemove listener to remove
 */
 void removeEventListener(ActivitiEventListener listenerToRemove);

还可以在流程定义中添加监听,xml配置如下:

<process id="testEventListeners">
  <extensionElements>
    <activiti:eventListener class="org.activiti.engine.test.MyEventListener" entityType="task" />
    <activiti:eventListener delegateExpression="${testEventListener}" events="ENTITY_CREATED" entityType="task" />
  </extensionElements>

  ...

</process>

entityType的值可以为:attachment, comment, execution, identity-link, job, process-instance, process-definition, task。

处理被调度事件的另一种方法是抛出BPMN事件1。请记住,只有使用特定类型的Activiti事件类型抛出BPMN-events才有意义。例如,在删除流程实例时抛出BPMN事件将导致错误。下面的代码片段展示了如何在流程实例中抛出一个信号,向外部流程(全局)抛出一个信号,在流程实例中抛出一个消息事件,在流程实例中抛出一个错误事件。属性throwEvent与一个附加属性一起使用,而不是使用类或delegateExpression,具体到要抛出的事件类型。

<process id="testEventListeners">
  <extensionElements>
    <activiti:eventListener throwEvent="signal" signalName="My signal" events="TASK_ASSIGNED" />
  </extensionElements>
</process>


<process id="testEventListeners">
  <extensionElements>
    <activiti:eventListener throwEvent="globalSignal" signalName="My signal" events="TASK_ASSIGNED" />
  </extensionElements>
</process>


<process id="testEventListeners">
  <extensionElements>
    <activiti:eventListener throwEvent="message" messageName="My message" events="TASK_ASSIGNED" />
  </extensionElements>
</process>

<process id="testEventListeners">
  <extensionElements>
    <activiti:eventListener throwEvent="error" errorCode="123" events="TASK_ASSIGNED" />
  </extensionElements>
</process>

Notes on listeners on a process-definition (关于进程定义监听器的说明)

1、 Event-listeners can only be declared on the process element, as a child-element of the extensionElements. Listeners cannot be defined on individual activities in the process.

2、Expressions used in the delegateExpression do not have access to the execution-context, as other expressions (e.g. in gateways) have. They can only reference beans defined in the beans property of the process engine configuration or when using spring (and the beans property is absent) to any spring-bean that implements the listener interface.

3、When using the class attribute of a listener, there will only be a single instance of that class created. Make sure the listener implementations do not rely on member-fields or ensure safe usage from multiple threads/contexts.

4、 When an illegal event-type is used in the events attribute or illegal throwEvent value is used, an exception will be thrown when the process-definition is deployed (effectively failing the deployment). When an illegal value for class or delegateExecution is supplied (either a nonexistent class, a nonexistent bean reference or a delegate not implementing listener interface), an exception will be thrown when the process is started (or when the first valid event for that process-definition is dispatched to the listener). Make sure the referenced classes are on the classpath and that the expressions resolve to a valid instance.

1、事件监听器只能作为extensionelement的子元素在process元素上声明。不能在流程中的单个活动上定义监听器。

2、与其他表达式(例如在网关中)不同,在delegateExpression中使用的表达式不能访问执行上下文。它们只能将在流程引擎配置的beans属性中定义的bean,或者在使用spring(且beans属性不存在)时任何spring bean,引用到监听器实现中。

3、在使用侦听器的class属性时,将只创建该类的一个实例。确保侦听器实现不依赖于成员字段或确保从多个线程/上下文安全使用。

4、当在events属性中使用非法事件类型或使用非法throwEvent值时,将在部署流程定义时抛出异常(实际上部署失败)。当一个类或非法值delegateExecution提供(一个不存在的类,不存在bean引用或委托不是实现侦听器接口),会抛出一个异常,当这个过程开始(或当第一个有效的事件流程定义分派到侦听器)。确保引用的类位于类路径上,并且表达式解析为有效的实例。


  1. 这是一个实验特性 ↩︎

发布了16 篇原创文章 · 获赞 1 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/a0604030212/article/details/105158759