1 Reply Latest reply on Jul 17, 2014 8:51 AM by Kris Verlaenen

    Register WorkItemHandler in persistence mode

    akoskm Newbie

      How is that in persistence mode, whenever I'm interacting with the task service, the engine is unable to find my handler implementations.

      Even if I do registerWorkItemHandler before starting the process the engine reports that

       

      org.drools.core.WorkItemHandlerNotFoundException: Could not find work item handler for LogHandler
      
      
      
      
      
      
      
      
      

      If I'm not interacting with the task service (starting a process without human task) everything works fine.

      I have a dedicated manager to set up the RuntimeEnvironment:

       

          public AppRuntime createSession(Long execId, List<Resource> resources, HashMap<String, Object> globals) throws ServiceException {
              KieBase kbase = createKieBase(execId, resources, null);
      
              PartyDAO dao = ComponentManager.getDBI().onDemand(PartyDAO.class);
              List<String> usernames = dao.findAllUserNames();
      
              // set up user roles
              // temporary insert every username into the knowledge base
              Properties properties = new Properties();
              for (String username : usernames) {
                  properties.setProperty(username, "default_group");
              }
      
              UserGroupCallback userGroupCallback = new JBossUserGroupCallbackImpl(properties);
              RuntimeEnvironment environment = RuntimeEnvironmentBuilder.getDefault()
                                                                        .persistence(true)
                                                                        .entityManagerFactory(emf)
                                                                        .userGroupCallback(userGroupCallback)
                                                                        .knowledgeBase(kbase)
                                                                        .get();
      
              RuntimeManager runtimeManager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment, "exec-" + execId);
              RuntimeEngine runtime = runtimeManager.getRuntimeEngine(ProcessInstanceIdContext.get());
      
              KieSession session = runtime.getKieSession();
              modifySession(session, null, globals);
      
              AppRuntime appRuntime = new AppRuntime(runtimeManager, runtime);
      
              return appRuntime;
          }
      
      
      
      
      
      
      
      
      

      then in the caller I'm using the AppRuntime returned by the above method, retrieving the ksession from runtimeEngine and registering my handlers:

       

      ksession.getWorkItemManager().registerWorkItemHandler(at, (WorkItemHandler) handlerObj);
      
      
      
      
      
      
      
      
      

      Why this works if I'm not interacting with the taskService, can somebody explain me this?

       

      Now comes the interesting part, if I'm registering my handlers before doing runtime.getKieSession():

       

          public AppRuntime createSession(Long execId, List<Resource> resources, HashMap<String, Object> globals) throws ServiceException {
              KieBase kbase = createKieBase(execId, resources, null);
         
              ....
      
              RuntimeManager runtimeManager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment, "exec-" + execId);
              RuntimeEngine runtime = runtimeManager.getRuntimeEngine(ProcessInstanceIdContext.get());
         
              // register handlers here: runtime.getKieSession.getWorkItemManager.registerWorkItem(....)
      
              KieSession session = runtime.getKieSession();
              modifySession(session, null, globals);
         
              AppRuntime appRuntime = new AppRuntime(runtimeManager, runtime);
         
              return appRuntime;
          }
      
      
      
      
      
      

      everything works fine. The problem is that I have to pass the reference of the created KieSession to every handler I create, so they have to be created after the session is available.


      Also, if you can provide any references, blog posts, articles, anything about jBPM and persistence mode, I would really appreciate it!

      I'm modifying an existing application which worked fine for years with jBPM 5.4, and the QA version even with jBPM 6.

      Seems that persistence mode isn't just an on/off switch and in some cases it requires you to redesign your existing backend code, which worked fine without persistence.


      EDIT


      The above solution sometimes does work. I've just tried the following: repeatedly called the taskService.complete(...) methods and after the fifth call the engine found the handler implementation and executed it successfully.