3 Replies Latest reply on Jul 22, 2009 3:45 AM by Ronald van Kuijk

    EventListener does not get executed?

    Derrick Lin Newbie

      I am using jBPM 4.0, jdk1.5.0_19, and try to run a simple process contains a event listener inside a state node.

      When I run the test case, it seems that the process executes that state node (GetFromStock) and move to the next, but the attached Event listener does not run (i.e. the variable does not get updated).

      process file

      <?xml version="1.0" encoding="UTF-8"?>
      
      <process name="order" xmlns="http://jbpm.org/4.0/jpdl">
       <swimlane assignee="alex" name="buyer"/>
       <swimlane assignee="peter" name="deliverDepartment"/>
       <start g="236,49,48,48" name="start">
       <transition g="-88,-22" name=" Start to PlaceOrder" to="PlaceOrder"/>
       </start>
       <task g="218,155,92,52" name="PlaceOrder" swimlane="buyer">
       <transition g="-121,-22" name="Order to Check" to="CheckAvailability"/>
       </task>
       <decision g="245,273,48,48" name="CheckAvailability">
       <handler class="org.jbpm.order.VerifyOrder" />
       <transition g="447,297:-110,-22" name="Check to Stock" to="GetFromStock"/>
       <transition g="-63,-22" name="Check to Deliver" to="Deliver"/>
       </decision>
       <state g="394,377,110,52" name="GetFromStock">
       <on event="FillStock">
       <event-listener class="org.jbpm.order.GetFromStockListener"/>
       </on>
       <transition g="-121,-22" name="Stock to Check" to="CheckAvailability"/>
       </state>
       <task g="83,382,92,52" name="Deliver" swimlane="deliverDepartment">
       <transition g="-45,-22" name="to end" to="end"/>
       </task>
       <end g="106,538,48,48" name="end"/>
      </process>


      Event Listener
      /**
       *
       */
      package org.jbpm.order;
      
      import org.jbpm.api.listener.EventListener;
      import org.jbpm.api.listener.EventListenerExecution;
      
      public class GetFromStockListener implements EventListener {
      
       public void notify(EventListenerExecution execution) throws Exception {
      
       Integer newStock = 300;
      
       execution.setVariable("goodsInStock", newStock);
      
       System.out.println("New stock: " + newStock);
       }
      }
      


      Test case
      public void testOrder() throws Exception {
      
       HashMap<String, Object> vars = new HashMap<String, Object>();
       vars.put("goodsOrdered", new Integer(100));
       vars.put("goodsInStock", new Integer(50));
      
       ProcessInstance pi = executionService
       .startProcessInstanceByKey("order", vars);
       String pid = pi.getId();
      
       assertTrue(pi.isActive("PlaceOrder"));
      
       List tasks = taskService.findPersonalTasks("alex");
       Task task = (Task) tasks.get(0);
       taskService.completeTask(task.getId());
      
       pi = executionService.findProcessInstanceById(pid);
       assertTrue(pi.isActive("GetFromStock"));
      
       String exeId = pi.findActiveExecutionIn("GetFromStock").getId();
       assertNotNull(exeId);
       //
       executionService.signalExecutionById(exeId, "Stock to Check");
       assertEquals(300, executionService.getVariable(exeId, "goodsInStock"));
       }


      Thanks in advanced

        • 1. Re: EventListener does not get executed?
          Ronald van Kuijk Master

          hmmmm... semantical issue... the event="FillStock" is the type of event that has to 'watched' not the name of this 'on' element.

          So if you want this event to fire, fire an event somewhere with the 'FillStock' as a name. If you want to fire this event on e.g. node-enter, use the correct event name.

          In old jbpm 3 what you do would have been written as something like:


          <action=".....">

          • 2. Re: EventListener does not get executed?
            Derrick Lin Newbie

             

            "kukeltje" wrote:

            If you want to fire this event on e.g. node-enter, use the correct event name.


            I read the user guide again, what you meant is by default, the event attribute of on element can only be either "start" or "end", right?

            So if you want this event to fire, fire an event somewhere with the 'FillStock' as a name.


            Is there a way that I can fire a specific event with a type rather than start and end?

            I have no previous experience on jBPM 3. :)

            Thanks

            • 3. Re: EventListener does not get executed?
              Ronald van Kuijk Master

              I have not much experience yet with jBPM 4 so I'm not sure which events can happen when.

              Also I have not a real clue (yet) on how to fire specific events, which was possible in 3.

              The sourcecode (specifically testcases) might be your best shot (it often was en still is for me)