17.3. Configuring CDI Integration

In order to use the jbpm-services-cdi API in your system, you need to provide some JavaBeans for the out of the box services to satisfy all dependencies, such as:
  • Entity manager and entity manager factory
  • User group callback for human tasks
  • Identity provider to pass authenticated user information to the services
Here is an example of a producer bean, that satisfy all the requirements of the jbpm-services-cdi API in a JEE environment like the JBoss Application Server:
public class EnvironmentProducer { 

    @PersistenceUnit(unitName = "org.jbpm.domain")

    private EntityManagerFactory emf;


    @Inject

    @Selectable

    private UserGroupInfoProducer userGroupInfoProducer;


    @Inject

    @Kjar

    private DeploymentService deploymentService;


    @Produces

    public EntityManagerFactory getEntityManagerFactory() {

        return this.emf;

    }


    @Produces

    public org.kie.api.task.UserGroupCallback produceSelectedUserGroupCalback() {

        return userGroupInfoProducer.produceCallback();

    }


    @Produces

    public UserInfo produceUserInfo() {

        return userGroupInfoProducer.produceUserInfo();

    }


    @Produces

    @Named("Logs")

    public TaskLifeCycleEventListener produceTaskAuditListener() {

        return new JPATaskLifeCycleEventListener(true);

    }


    @Produces

    public DeploymentService getDeploymentService() {

        return this.deploymentService;

    }


    @Produces

    public IdentityProvider produceIdentityProvider {

        return new IdentityProvider() {

             // implement IdentityProvider

        };

    }

}
Provide an alternative for user group callback in the configuration file called the beans.xml. For example, the org.jbpm.kie.services.cdi.producer.JAASUserGroupInfoProducer class allows JBoss Application Server to reuse security settings on application server regardless of what it actually is (such as LDAP and DB):
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd">

	<alternatives>

		<class>org.jbpm.kie.services.cdi.producer.JAASUserGroupInfoProducer</class>

	</alternatives>

</beans>
Optionally, you can use several other producers provided to deliver components like Process, Agenda, WorkingMemory event listeners, and WorkItemHandlers. To provide these components, you need to implement the following interfaces:
  • /**
    
     * Allows to provide custom implementations to deliver WorkItem name and WorkItemHandler instance pairs
    
     * for the runtime.
    
     * <br/>
    
     * It will be invoked by RegisterableItemsFactory implementation (especially InjectableRegisterableItemsFactory 
    
     * in CDI world) for every KieSession. Recommendation is to always produce new instances to avoid unexpected 
    
     * results. 
    
     *
    
     */
    
    public interface WorkItemHandlerProducer {
    
    
        /**
    
         * Returns map of (key = work item name, value work item handler instance) of work items 
    
         * to be registered on KieSession
    
         * <br/>
    
         * Parameters that might be given are as follows:
    
         * <ul>
    
         *  <li>ksession</li>
    
         *  <li>taskService</li>
    
         *  <li>runtimeManager</li>
    
         * </ul>
    
         * 
    
         * @param identifier - identifier of the owner - usually RuntimeManager that allows the producer to filter out
    
         * and provide valid instances for given owner
    
         * @param params - owner might provide some parameters, usually KieSession, TaskService, RuntimeManager instances
    
         * @return map of work item handler instances (recommendation is to always return new instances when this method is invoked)
    
         */
    
        Map<String, WorkItemHandler> getWorkItemHandlers(String identifier, Map<String, Object> params);
    
    }
  • /**
    
     * Allows do define custom producers for know EventListeners. Intention of this is that there might be several 
    
     * implementations that might provide different listener instance based on the context they are executed in. 
    
     * <br/>
    
     * It will be invoked by RegisterableItemsFactory implementation (especially InjectableRegisterableItemsFactory 
    
     * in CDI world) for every KieSession. Recommendation is to always produce new instances to avoid unexpected 
    
     * results.
    
     *
    
     * @param <T> type of the event listener - ProcessEventListener, AgendaEventListener, WorkingMemoryEventListener
    
     */
    
    public interface EventListenerProducer<T> {
    
    
        /**
    
         * Returns list of instances for given (T) type of listeners
    
         * <br/>
    
         * Parameters that might be given are as follows:
    
         * <ul>
    
         *  <li>ksession</li>
    
         *  <li>taskService</li>
    
         *  <li>runtimeManager</li>
    
         * </ul>
    
         * @param identifier - identifier of the owner - usually RuntimeManager that allows the producer to filter out
    
         * and provide valid instances for given owner
    
         * @param params - owner might provide some parameters, usually KieSession, TaskService, RuntimeManager instances
    
         * @return list of listener instances (recommendation is to always return new instances when this method is invoked)
    
         */
    
        List<T> getEventListeners(String identifier, Map<String, Object>  params);}
JavaBeans implementing the above mentioned interfaces are collected on runtime and consulted when building KieSession by RuntimeManager.