1 Reply Latest reply on Dec 12, 2013 3:22 AM by swiderski.maciej

    Error in Custom workitem Handler

    mpankaj123

      I am trying to get process variables in a custom work item handler. My process has a human task immediately after it starts. That triggers execution of the custom work item handler. I try to retrieve process variables in the handler but the getProcessInstance() throws error because jBPM has not written byteArray yet into the processInstanceInfo table.

       

      Can any one help me with writing this correctly. I am using jBPM 6. Is there a better way?

       

      Regards,

       

      P.

       

      Here is the code

       

       

      public class TaskWorkItemHandler implements WorkItemHandler {

         public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {

        KieSession ksession = ProcessRun.getSession();

        long procId = workItem.getProcessInstanceId();

        ProcessInstance p = ksession.getProcessInstance(procId);  // return exception

         WorkflowProcessInstance w = (WorkflowProcessInstance)p;

         System.out.println(w.getVariable("recordId"));

         System.out.println(workItem.getName());

         }

      }

       

      public class ProcessRun

      {

        public void run()

        {

        try

        {

         KieSession ksession = getSession();

        JPAWorkingMemoryDbLogger logger = new JPAWorkingMemoryDbLogger(ksession);

        HashMap<String,Object> h = new HashMap<String,Object>();

        h.put("recordId","11");

        ProcessInstance pi = ksession.startProcess( "com.sample.evaluation",h );

        }

        catch (Exception e)

        {

        e.printStackTrace();

        }

       

        }

       

        public static KieSession getSession()

        {

        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

        kbuilder.add(ResourceFactory.newFileResource("c:\\temp\\HR.bpmn"), ResourceType.BPMN2);

         KnowledgeBase kbase = kbuilder.newKnowledgeBase();

        EntityManagerFactory emf =   Persistence.createEntityManagerFactory( "org.jbpm.persistence.jpa" );

         Environment env = KnowledgeBaseFactory.newEnvironment();

        env.set( EnvironmentName.ENTITY_MANAGER_FACTORY, emf );

        env.set( EnvironmentName.TRANSACTION_MANAGER,TransactionManagerServices.getTransactionManager() );

         KieSession ksession =

           JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env );

        ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new TaskWorkItemHandler());

        return ksession;

        }

      }

        • 1. Re: Error in Custom workitem Handler
          swiderski.maciej

          you should pass all variables to the node (user task) using data input and data input associations and then you'll get all these variables as parameters of the work item in your handler. You should not access process instance from within handler directly to get variables.

           

          HTH