2 Replies Latest reply on Nov 10, 2013 6:45 AM by bwallis42

    ProcessEventListener in container

    bwallis42

      In Querying and Sorting Tasks a solution was shown for how to add a task event listener using CDI.

       

      Can something similar be done for a ProcessEventListener?

       

      I can create and add a listener to the session when I start a process:

       

              KieSession ksession = engine.getKieSession();
              
              ksession.addEventListener(new MyProcessEventListener());
      

       

      but this listener is lost at the first process pause (a human task).

       

      I have a working DeploymentListener like so:

       

      @ApplicationScoped
      public class DeploymentListener {
      
          private static final Logger logger = LoggerFactory.getLogger(DeploymentListener.class);
        
          public void onDeployment(@Observes @Deploy DeploymentEvent event) {
              logger.info("Unit {} has been successfully deployed ", event.getDeploymentId(), event.getDeployedUnit());
          }
        
          public void onUndeployment(@Observes @Undeploy DeploymentEvent event) {
              logger.info("Unit {} has been successfully undeployed", event.getDeploymentId());
          }
      }
      

       

      and a working task listener (as described in the other post,) like so:

       

      @ApplicationScoped
      public class TaskEventListener implements TaskLifeCycleEventListener
      {
          private static final Logger logger = LoggerFactory.getLogger(TaskEventListener.class);
        
          @Inject
          private TaskContentService contentService;
      
          @Override
          public void afterTaskActivatedEvent(@Observes @AfterTaskActivatedEvent Task ti)
          {
              logger.info("AfterTaskActivated event, task = " + new TaskInfo(ti, contentService));
          }
      // ...
      }
      

       

      but if I try something similar for a ProcessEventListener it doesn't work, but I'm not sure what the correct annotations would be (I cannot find an example).

       

      @ApplicationScoped
      public class MyProcessEventListener implements ProcessEventListener
      {
          private static final Logger logger = LoggerFactory.getLogger(MyProcessEventListener.class);
      
          @Override
          public void beforeProcessStarted(@Observes @Any ProcessStartedEvent event)
          {
              logger.info("BeforeProcessStarted event, id = " + event.getProcessInstance().getId());
          }
      //...
      }
      
        • 1. Re: ProcessEventListener in container
          swiderski.maciej

          Brian,

           

          if you use RuntimeManager your custom listener should be added to the RegisterableItemsFactory used by the RuntimeManager. It can be set when building RuntimeEnvironment instance. Then whenever session is created or loaded the listener will be automatically registered on it.

          When CDI is used then you can provide ProcessEventListener producer that implements this interface. Make use that your class gets annotated with @Process and it will be automatically resolved on runtime (assuming you use InjectableRegisterableItemsFactory).

           

          HTH

          • 2. Re: ProcessEventListener in container
            bwallis42

            Bingo! Got it working now, thanks.

             

            I added and implemented the EventListenerProducer to my existing class of other producers (DeploymentService, UserGroupCallback and EntityManager) added the @Process annotation and it worked first time.

             

            regards, brian...