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. 这是一个实验特性 ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: "Activiti 实战 pdf" 是一本介绍Activiti流程引擎实战的电子书。该书主要介绍了Activiti的架构、流程设计、流程部署、活动和事件处理、任务管理、用户管理、历史记录、定时任务等内容。通过该书的学习,读者可以了解Activiti的基本原理,掌握Activiti的基本使用方法,在实际项目中使用Activiti开发高效、可靠的业务流程应用。 Activiti是一个开源的BPM流程引擎,它实现了BPMN 2.0规范,并提供了基于Java和Spring的API和服务。Activiti可以方便地与各种应用和平台集成,例如Spring、Spring Boot、Camunda、Drools、MuleSoft等。Activiti被广泛应用于企业级业务流程管理、工作流、电子商务、人力资源、金融等领域。 该书的作者是Tijs Rademakers、Ronald van Luttikhuizen、Jos Dirksen等人,他们是Activiti架构师和专家。该书除了提供详尽的文档和指导外,还包含了一些实践案例和示例代码,可以帮助读者更好地理解和运用Activiti。无论是初学者还是有经验的开发者,都可以从该书中获得收益,它是一本不可多得的Activiti学习资源。 ### 回答2: Activiti 实战 PDF 是一本关于 Activiti 工作流引擎的实践指南书籍,该书适合对工作流引擎有一定了解并希望使用 Activiti 实现工作流的开发人员和架构师阅读。 该书主要涵盖了 Activiti 的基本概念和使用方法,包括工作流引擎的基础知识、流程定义和流程实例的创建、流程图的绘制、任务分配和处理、流程监控和报表等。 这本书通过丰富的实例代码和图示,展示了 Activiti 在实际项目中的应用场景,并详细介绍了如何配置和使用 Activiti,如如何在 Java 代码中使用 Activiti API,如何使用 Spring 集成 Activiti 等。 此外,该书还包含了一些高级的话题,如如何自定义 Activiti 服务、如何在 Activiti 中使用规则引擎和消息中间件等,这些内容对于需要深入了解 Activiti 的开发人员来说具有很大的价值。 总之,Activiti 实战 PDF 对于掌握 Activiti 工作流引擎的开发人员和架构师来说是一本非常实用的实践指南,它不仅能够帮助读者掌握 Activiti 的基本概念和使用方法,还能够帮助他们实现更加复杂的工作流场景。 ### 回答3: Activiti 实战 PDF 是一本关于 Activiti 工作流引擎的实战指南,主要面向开发者和系统管理员,并以实际案例为基础,介绍了 Activiti 的核心概念、基本流程的构建以及高级特性等内容。 本书由熟悉 Activiti 的领域专家撰写,详细介绍了 Activiti 的工作原理、配置和使用方式。其中涵盖了多种流程设计方法,包括界面设计、BPMN 规范和 XML 定义等,并结合实际案例演示了如何使用 Activiti 构建自定义流程应用。 此外,本书还阐述了 Activiti 中的用户、任务、表单、数据和事件等概念,以及如何使用 Activiti Explorer 管理和监控流程的运行与执行情况。最后,本书还介绍了使用 Activiti 进行流程自动化部署、管理与维护的技巧和方法。 总的来说,Activiti 实战 PDF 为初学者和高级用户提供了深入学习 Activiti 工作流引擎的最佳实践方法,帮助用户将其应用于实际开发项目中,提高工作效率和流程管理的可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值