1 Reply Latest reply on Nov 12, 2011 10:47 AM by amin-mc

    StatefulKnowledgeSession in custom work item handlers

    amin-mc

      Hello

       

      My application provides the ability to register custom work item handlers.   Users register an instance of the custom handlers and when i create a session i get these handlers and register them against the statefulknowledgesession.  The problem is that some of these handlers might need access to the StatefulKnowledgesession however I can't find the best way to inject the session into these handlers.  For example I decided to use StatefulKnowledgeSessionAware interface where i set the session if the handler implements the handler.  The problem with this is that there is a single instance of the handler and i run the risk that the handler might have a session injected when it is currently already in use by another session.

       

      What would be great is to have some kind of handler with a method signature

       

      public void executeWorkItemWithSession(WorkItem workItem, WorkItemManager workItemManager, StatefulKnowledgeSession)

       

      that way it doesn't matter that there is only one instance of the handler everytime the handler is invoked the session is injected into the method.  Is this possible to achieve? Can i do some extension to support this?

       

      Any help would be appreciated.

        • 1. Re: StatefulKnowledgeSession in custom work item handlers
          amin-mc

          Just looking at the source code...could i extend the JPAWorkItemManager to achive this?  For example:

           

          in JPAWorkItemManager

           

          public void internalExecuteWorkItem(WorkItem workItem) {

                  Environment env = this.kruntime.getEnvironment();

           

                  WorkItemInfo workItemInfo = new WorkItemInfo(workItem, env);

                 

                  PersistenceContext context = ((PersistenceContextManager) env.get( EnvironmentName.PERSISTENCE_CONTEXT_MANAGER )).getCommandScopedPersistenceContext();

                  context.persist( workItemInfo );

           

                  ((WorkItemImpl) workItem).setId(workItemInfo.getId());

                  workItemInfo.update();

                 

                  if (this.workItems == null) {

                      this.workItems = new HashMap<Long, WorkItemInfo>();

                  }

                  workItems.put(workItem.getId(), workItemInfo);

                 

                  WorkItemHandler handler = (WorkItemHandler) this.workItemHandlers.get(workItem.getName());

                  if (handler != null) {

           

                            if (handler instanceof StatefulKnowledgeSessionAware) {

                                            //invoke a different execute and inject the session into this?

                                      handler.executeWorkItemWithSession(workItem, this, kruntime);

                            } else {

                                      handler.executeWorkItem(workItem, this);

                            }

                  } else {

                      throwWorkItemNotFoundException( workItem );

                  }

              }

           

           

          Would this work?  If so would i register a custom JPAWorkItemManager other than creating my own version of this...