1 Reply Latest reply on Sep 2, 2015 11:11 AM by Carlo Carradori

    WorkItemHandlers as Spring beans in JBPM 6.2 via RuntimeManager

    Carlo Carradori Newbie

      Hi everyone,

       

      i followed Maciej's article to integrate JBPM 6 with Spring framework and everything is working good.

      In fact, in my Tomee webapp i'm able to start process and signal events, the transactions are configured well so the AuditLogService is correctly interacting with my PostgreSQL data store.

      I access to JBPM layer via RuntimeManager injected in my Spring controller, but now i need to develop some business logic in custom WorkItemHandlers and need to declare them as spring beans in my application-context.xml. So i'm NOT using Jbpm Services.

      i read this post and this, but i can't figure out,

      I created a class that implements RegisterableItemsFactory called SpringRegisterableItemsFactory

       

      public class SpringRegisterableItemsFactory implements RegisterableItemsFactory {
      
      
        Map<String, WorkItemHandler> workItemHandlers;
      
        @Override
        public Map<String, WorkItemHandler> getWorkItemHandlers(
        RuntimeEngine runtime) {
        // TODO Auto-generated method stub
        return workItemHandlers;
        }
      
      
        @Override
        public List<ProcessEventListener> getProcessEventListeners(
        RuntimeEngine runtime) {
        // TODO Auto-generated method stub
        return null;
        }
      
      
        @Override
        public List<AgendaEventListener> getAgendaEventListeners(
        RuntimeEngine runtime) {
        // TODO Auto-generated method stub
        return null;
        }
      
      
        @Override
        public List<RuleRuntimeEventListener> getRuleRuntimeEventListeners(
        RuntimeEngine runtime) {
        // TODO Auto-generated method stub
        return null;
        }
      
      
        @Override
        public Map<String, Object> getGlobals(RuntimeEngine runtime) {
        // TODO Auto-generated method stub
        return null;
        }
      
      
        @Override
        public List<TaskLifeCycleEventListener> getTaskListeners() {
        // TODO Auto-generated method stub
        return null;
        }
      
      
        public void setWorkItemHandlers(Map<String, WorkItemHandler> workItemHandlers) {
        this.workItemHandlers = workItemHandlers;
        }
      
      }
      
      

       

      in this class i inject my custom WorkItemHandler in this way

       

      <bean id="verificaDatiEnelWorkItemHandler" class="com.sample.workitemhandlers.VerificaDatiEnelWorkItemHandler">
      </bean>
      
      
      <bean id="registerableItemsFactory" class="com.sample.SpringRegisterableItemsFactory">
        <property name="workItemHandlers">
        <map>
        <entry key="VerificaDatiEnelWorkItemHandler" value-ref="verificaDatiEnelWorkItemHandler" />
        </map>
        </property>
      
      </bean>
      
      

       

      now i need to register my SpringRegisterableItemsFactory in such way that is configured in runtimeManager. I'm doing it in this way

       

      <bean id="runtimeEnvironment"
        class="org.kie.spring.factorybeans.RuntimeEnvironmentFactoryBean">
        <property name="type" value="DEFAULT" />
        <property name="entityManagerFactory" ref="jbpmEMF" />
        <property name="transactionManager" ref="jbpmTxManager" />
        <property name="registerableItemsFactory" ref="registerableItemsFactory" /> <--------- here 
        <property name="assets">
        <map>
        <entry key-ref="process">
        <util:constant static-field="org.kie.api.io.ResourceType.BPMN2" />
        </entry>
        </map>
        </property>
      </bean>
      
      
      <bean id="runtimeManager" class="org.kie.spring.factorybeans.RuntimeManagerFactoryBean"
        destroy-method="close">
        <property name="identifier" value="spring-rm" />
        <property name="runtimeEnvironment" ref="runtimeEnvironment" />
      </bean>
      
      

       

      It seem to be very simple and linear but i get this ClassCastException:

       

      java.lang.ClassCastException: com.sample.SpringRegisterableItemsFactory cannot be cast to org.kie.internal.runtime.manager.InternalRegisterableItemsFactory

       

      This only work for JBPM 6.1?

      Anyone give me a hint?

       

       

      Thanks

      Carlo

        • 1. Re: WorkItemHandlers as Spring beans in JBPM 6.2 via RuntimeManager
          Carlo Carradori Newbie

          I found a workaround that is not so "frameworkly" good but it works.

          I registered my custom WorkItemHandler from the Controller and inside the executeWorkItem() method i imported the Spring Context in this way:

           

           

          ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

          MyService myService = (MyService)context.getBean("myService");

           

           

          i know that this solution isn't the best one, but i can't find a way to figure out via registerableItemsFactory.

          If anyone has a hint, it would be appreciated.


          Bye

          Carlo