7 Replies Latest reply on Feb 1, 2013 11:54 PM by enriquezrene

    Process Instance Variable within WorkItemHandler

    aminmc

      Hi

       

      I have a custom work item handler and I would like to load the value of a process instance variable while executing the work item handler.  Basically I have a service method which does the following:

       

      public String getValue(Long processId, String variableName) {

       

        session.getProcessInstance(..);

         instance.getVariable(variableName);

       

        //do something with the value...but not modify

       

      }

       

       

      When i call this service method within the execute method of the work item handler I get a nullpointerexception and the stacktrace indicates that the processinstancebytearray value of ProcessInstanceInfo is null. I guess this is because the process has not reached a safe point to persist.  Is there another way to get the value of a variable defined in the process within the execution of the work item handler?

       

      This is the final part of a POC I am currently working on demonstrating the use of JBPM 5 so any help on this would be extremely grateful.

       

      Cheers

        • 1. Process Instance Variable within WorkItemHandler
          salaboy21

          Hi Amin,

          Why are you doing that? the main idea of using work items is that you can copy information from the process scope to the work item scope using parameter mappings. If you define what pieces of information you will need inside the work item handler you can do the mappings and the engine will copy those values for you. You don't need to have the session to retrieve the process variables.

           

          Take a look at the examples here: https://github.com/Salaboy/Drools_jBPM5-Training-Examples

          inside the jBPM5 directory for more information.

          Look at the processes definition to see how to move information from the process scope to the workitem scope.

          Greetings.

          • 2. Process Instance Variable within WorkItemHandler
            aminmc

            Hi Mauricio,

             

            Thanks for your reply.  I'll give more info behind what I am trying to do.  I have an object model which looks something like this:

             

            Class A

               String id

              Set<ClassB> data --> ClassB String name, String value

             

             

            In my flow we create Class A and a collection of ClassB but in the process variable we store the reference as opposed to the serialised version of ClassA (there is already persistence behind ClassA).

             

            I now have a domain specific work item handler which is registered in eclipse and has a set of properties which a user can define.  These properties can be statically defined or they can use an expression based approach for example

             

            Name: ClassA(variableName defined in flow).elementName + ':'  + ClassA(variableName defined in flow).reference

             

            The idea is then to load ClassA id via variableName and then get the value of elementName and reference which are stored in ClassA object model. I have a small ANTLR program that parses the string and then looks up the data.  The ANTLR parser uses session.getProcessInstance(..) to load the relevant data and I was hoping to load the process and get the value out, although it probably is possible to pass the work item parameter mapping to my parser.     

             

            Hope this made sense!

             

            Cheers

            • 3. Re: Process Instance Variable within WorkItemHandler
              francesco.pietrobelli

              I think that you can pass the StatefulKnowledgeSession to a constructor of your custom WorkItemHandler and then in executeWorkItem(WorkItem i,WorkItemManager m) method calls:

              ksession.getProcessInstance(i.getProcessInstanceId());

               

              Hope this is helpful.

              • 4. Re: Process Instance Variable within WorkItemHandler
                salaboy21

                Hi Amin,

                Your procedure looks so complicated to me. You should use the Variable Persisntece Strategies to handle such situations. Are your expessions propietary? Do you need to do that kind of resolutions in runtime? or you can solve the same things with the current methods?

                If you create a variable persistence strategy for handling your classes that are being persisted externally,  you shouldn't use the process instance ID to restore the information. You should use your own/external persisters (the one that handles the Class A and B) to restore the information about those classes using the ID that you pass to the work item handler.

                Greetings.

                • 5. Re: Process Instance Variable within WorkItemHandler
                  aminmc

                  Wow...completely forgot about this post.  I have handled this differently and so longer an issue.

                   

                  Cheers

                  • 6. Re: Process Instance Variable within WorkItemHandler
                    newbird

                    Can you share ur approach????????????

                    • 7. Re: Process Instance Variable within WorkItemHandler
                      enriquezrene