17.4. RuntimeManager as CDI Bean

You can inject RuntimeManager as CDI bean into any other CDI bean within your application. RuntimeManager comes with the following predefined strategies and each of them have CDI qualifiers:
  • @Singleton
  • @PerRequest
  • @PerProcessInstance

Note

Though you can directly inject RuntimeManager as CDI bean, it is recommended to utilize JBoss BPM Suite services when frameworks like CDI, ejb or Spring are used. JBoss BPM Suite services provide significant amount of features that encapsulate best practices when using RuntimeManager.
Here is an example of a producer method implementation that provides RuntimeEnvironment:
public class EnvironmentProducer { 

    //add same producers as for services


    @Produces

    @Singleton

    @PerRequest

    @PerProcessInstance

    public RuntimeEnvironment produceEnvironment(EntityManagerFactory emf) {


        RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get()

                .newDefaultBuilder()

                .entityManagerFactory(emf)

                .userGroupCallback(getUserGroupCallback())

                .registerableItemsFactory(InjectableRegisterableItemsFactory.getFactory(beanManager, null))

                .addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2)

                .addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTask.bpmn2"), ResourceType.BPMN2)

                .get();

        return environment;

    }

}
In the example above, a single producer method is capable of providing RuntimeEnvironment for all strategies of RuntimeManager by specifying all qualifiers on the method level. Once a complete producer is available, you can inject RuntimeManager into the application's CDI bean as shown below:
public class ProcessEngine {

    @Inject

    @Singleton

    private RuntimeManager singletonManager;


    public void startProcess() {
       

        RuntimeEngine runtime = singletonManager.getRuntimeEngine(EmptyContext.get());

        KieSession ksession = runtime.getKieSession();

         ProcessInstance processInstance = ksession.startProcess("UserTask");

        singletonManager.disposeRuntimeEngine(runtime);     

    }

}

Note

Although there is an option available for having a single RuntimeManager in the application, but it is recommended to make use of DeploymentService whenever you need to have many RuntimeManagers active within your application.
As an alternative to DeploymentService, the application can inject RuntimeManagerFactory and then create RuntimeManager instance manually. In such cases, EnvironmentProducer remains the same as the DeploymentService. Here is an example of a simple ProcessEngine bean:
public class ProcessEngine {


    @Inject

    private RuntimeManagerFactory managerFactory;

    

    @Inject

    private EntityManagerFactory emf;

    

    @Inject

    private BeanManager beanManager;


    public void startProcess() {

        RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get()

                .newDefaultBuilder()

                .entityManagerFactory(emf)

                .addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2)

                .addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTask.bpmn2"), ResourceType.BPMN2)

                .registerableItemsFactory(InjectableRegisterableItemsFactory.getFactory(beanManager, null))

                .get();

        

        RuntimeManager manager = managerFactory.newSingletonRuntimeManager(environment);

        RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());

        KieSession ksession = runtime.getKieSession();

        

        ProcessInstance processInstance = ksession.startProcess("UserTask");

        

        manager.disposeRuntimeEngine(runtime);

        manager.close();     

    }

}