5 Replies Latest reply on Feb 15, 2010 6:41 AM by swiderski.maciej

    Jbpm 4.3 Event Listeners

      Hi,

       

      I have written my cusom event listener and defined at process definition level.

       

      <on event="start">
                  <event-listener class="x.WorkflowStatusEvent">
                      <field name="Id">
                          <object expr="#{id}" />
                      </field>
                      <field name="status">
                          <string value="IN_PROGRESS" />
                      </field>
                      <field name="tcn">
                          <object expr="#{param1}" />
                      </field>
                  </event-listener>           
              </on>

       


          <on event="end">
              <event-listener class="x.WorkflowStatusEvent">
                  <field name="Id">
                      <object expr="#{id}" />
                  </field>
                  <field name="status">
                      <string value="COMPLETED" />
                  </field>
                  <field name="tcn">
                      <object expr="#{param1}" />
                  </field>
              </event-listener>
          </on>

       

      I am passing paramters to these event listeners #{id} and  #{param1}. During the first execution of this process the parameters are passed fine but during the second execution of the process and different parameter values for #{id} and  #{param1} it does not pass the new parameters. instead it takes the parameters of the first execution cycle.

       

      Any Help would be highly appreciated.

       

      Thanks.


        • 1. Re: Jbpm 4.3 Event Listeners
          sebastian.s
          Please post a full unit-test for this.
          • 2. Re: Jbpm 4.3 Event Listeners

            I dont have the unit test case but the basic code is below:

             

            Sample Workflow:

             

            <?xml version="1.0" encoding="UTF-8"?>

             

            <process name="TEST" xmlns="http://jbpm.org/4.3/jpdl">

             

                <start g="0,0,80,40">
                    <transition to="theEnd" />
                </start>   
               
                <end g="135,72,80,40" name="theEnd" />

             

                <on event="start">
                        <event-listener class="WorkflowStatusEvent">
                            <field name="transactionId" cache="disabled">
                                <object expr="#{id}" />
                            </field>
                            <field name="status" >
                                <string value="IN_PROGRESS" />
                            </field>
                            <field name="tcn" cache="disabled">
                                <object expr="#{param1}" />
                            </field>
                        </event-listener>           
                    </on>

             


                <on event="end">
                    <event-listener class="WorkflowStatusEvent">
                        <field name="transactionId" cache="disabled">
                            <object expr="#{id}" />
                        </field>
                        <field name="status">
                            <string value="COMPLETED" />
                        </field>
                        <field name="tcn" cache="disabled">
                            <object expr="#{param1}" />
                        </field>
                    </event-listener>
                </on>
            </process>

             

             

             

            Event Listener Code:

             

            public class WorkflowStatusEvent implements EventListener {

             

              Logger log = Logger.getLogger(WorkflowStatusEvent.class);
             
              //value gets injected from process definition
              String transactionId;
             
              String status;
             
              String tcn;
             
              public void notify(EventListenerExecution execution) {   
                  log.info( transactionId+" -- " + status);
                  log.info(tcn);
                     
              }
             
               
            }

             

             

             

            Code starting the business process:

             

            for (i = 0 ; i < 2 ; i++) {

             

                Map<String, Object> parameters = new HashMap<String, Object>();
                parameters.put("param1", Integer.toString(i));
                parameters.put("param2", Integer.toString(i));
                parameters.put("id", Integer.toString(i));
                       
                executionService.startProcessInstanceByKey("TEST",parameters);           
            }

             

            The SOPL in the Event listener displays the value of first cycle event if the value of i = 2.

            • 3. Re: Jbpm 4.3 Event Listeners
              swiderski.maciej

              Hi,

               

              I think it can have something to do with caching mechanism. I have encountered similar issues when working with java activities.

               

              It works fine for the first time since new instance of a class is created and members of that class are properly set. Afterwards this class is cached.

               

              The idea behind is that such items (java activity) should be stateless and should not rely on class members. In case of java activity solution was to use arguments that will be passed to method. If it comes to event listeners you have access to process variables inside notify method (using execution object).

               

              Could be good to get a confirmation of this from more experienced jBPM user. Any way I hope it will be useful.

               

              Cheers,

              Maciej

              • 4. Re: Jbpm 4.3 Event Listeners

                Thanks for the reply. It worked with execution.getVariable("var") inside the notify method.

                Is there any way i can disable the caching of the objects.

                • 5. Re: Jbpm 4.3 Event Listeners
                  swiderski.maciej

                  According to Dev guide you can use attribute

                   

                  cache="disabled"
                  

                  to prevent user code be cached.

                   

                  Please check out following chapters in user and dev guide for details:

                  User guide - http://http://docs.jboss.com/jbpm/v4/userguide/html_single/#usercodeclassloading

                   

                  Dev guide - http://docs.jboss.com/jbpm/v4/devguide/html_single/#d0e1360

                   

                  /Maciej