activiti learning

Exposing configuration beans in expressions and scripts

Configuring bean public in expressions and scripts

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.

By default, your configuration or your own Spring configuration file specified in the bean activiti.cfg.xml all expressions and scripts are available. If you want to limit the bean configuration file visibility, you can configure a property called beans in the process engine configuration. ProcessEngineConfiguration beans property is a map. When you specify this property, bean and fishes expression specified in the script is only visible in the map. The bean will open you specify the mapping name in public.

Deployment cache configuration (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

All process definition will have encountered cache database (after parsing), in order to avoid the need for each process definition, process definition because the data does not change. By default, this cache is unlimited. To limit the caching process definition, add the following configuration bean propertyexpose in expressions and scripts

<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 the default cache mode, the size of the cache depends on the configuration and the number of processes and the number of the total number of custom hardware actually used processes.
We can also inject their own cache implementation, we must implement the interface org.activiti.engine.impl.persistence.deploy.DeploymentCache
have a similar property called knowledgeBaseCacheLimit and knowledgeBaseCache, used to configure the cache rule. Only when using a rule task in the process to do so.

Logging

All logs are routed through SLF4J, and allows you to select the log we have chosen to achieve.
By default, there is no SFL4J-binding jar in activiti-engine dependencies in order to use the log frame you choose, you should add it to your project. If you do not add any implementation jar, SLF4J will use a NOP-logger, did not record any log, just a warning not record any log.
activiti-ui and activiti-rest webapps is configured to use Log4j-binding. When all the tests activiti- * modules run, also use Log4j.

Event handlers (event handler)

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.

Event mechanism tiviti engine allows you to be notified when various events occur in the engine. View all types of events supported, in order to obtain an overview of the available events.

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

All events are realized subtypes of org.activiti.engine.delegate.event.ActivitiEvent. This event is open (if available) Type executionId, processInstanceId and processDefinitionId

An event listener example

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 provides some basic common use cases to achieve the promotion of the event listener. These examples may be used as a base or listeners realize:
org.activiti.engine.delegate.event.BaseEntityEventListenerr: a base class event listener can listen to a particular type of entity or entities all entities related events. It hides the type checking, and provides four methods should be covered: onCreate (...), onUpdate ( ...) and onDelete (...) when an entity is created, updated or deleted. For all other events related to the entity will be called onEntityEvent (...).

Configure event listener as follows:

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

You can also be configured to perform a type of event listeners:

<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>

The code may be added by running monitor, API interfaces as follows:

/**
 * 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);

You may be added to the process definition monitor, xml configuration is as follows:

<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 values ​​may be: attachment, comment, execution, identity-link, job, process-instance, process-definition, task.

Another way to deal with scheduled events are thrown BPMN event 1 . Remember, only use a specific type of event types thrown Activiti BPMN-events makes sense. For example, when deleting a process instance throws BPMN event will result in an error. The following code fragment shows how to throw in a signal process instance, throws a signal to the external flow (global), throws an event message process instance, an error event is thrown in the process instance. Properties throwEvent use with an additional property together, rather than using the class or delegateExpression, specific to the type of event to be thrown.

<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 (defined on the process described listener)

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, the event listener only as a child element extensionelement statement on the process elements. You can not be defined in a single event listener process.

2, and other expressions (e.g., in a gateway) is different expressions used in the execution context delegateExpression not access. They can only be defined in the flow properties of beans in the bean engine configuration, or in the use of any spring bean Spring (beans and the attribute is absent), a reference to the listener implementations.

3, when the listener class attribute, creates an instance of the class only. Ensure that the implementation does not depend on the listener or ensure that members of the field from multiple threads / context safe to use.

4, when using illegal event type events in the property or use illegal throwEvent value, will throw an exception (actually deploy failure) in the deployment process definition. When a class or illegal value delegateExecution provide (a category that does not exist, there is no reference or bean commission not implement listener interface) will throw an exception when this process begins (or when the first valid event process definition assigned to the listener). Ensure that the referenced class on the classpath, and expression resolves to a valid instance.


  1. This is an experimental characteristic ↩︎

Published 16 original articles · won praise 1 · views 10000 +

Guess you like

Origin blog.csdn.net/a0604030212/article/details/105158759