Implementing Event Listeners in Activiti 7

cancel
Showing results for 
Search instead for 
Did you mean: 
hbobertz
Active Member

Implementing Event Listeners in Activiti 7

I am currently running the Activiti Cloud Example version 7.10.m6, but I am having trouble implementing task and process listeners as outlined in the Deep Dive Series . Here are my implementaitons of the listeners as described by the deep dive series.

@Service
public class ProcessEventListener implements ProcessRuntimeEventListener {
    private Logger logger = LoggerFactory.getLogger(ProcessEventListener.class);

    @Override
    public void onEvent(RuntimeEvent runtimeEvent){

        System.out.println("\n\n------------------An Event Happened--------------------\n");
        if (runtimeEvent instanceof ProcessStartedEvent)
            logger.info("\n\n\nDo something, process is started: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof ProcessCompletedEvent)
            logger.info("\n\n\nDo something, process is completed: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof ProcessCancelledEvent)
            logger.info("\n\n\nDo something, process is cancelled: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof ProcessSuspendedEvent)
            logger.info("\n\n\nDo something, process is suspended: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof ProcessResumedEvent)
            logger.info("\n\n\nDo something, process is resumed: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof ProcessCreatedEvent)
            logger.info("\n\n\nDo something, process is created: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof SequenceFlowEvent)
            logger.info("\n\n\nDo something, sequence flow is taken: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof VariableCreatedEvent)
            logger.info("\n\n\nDo something, variable was created: " + runtimeEvent.toString());
        else
            logger.info("Unknown event: " + runtimeEvent.toString());

    }
}
@Service
public class TaskEventListener implements TaskEventListener {
    private Logger logger = LoggerFactory.getLogger(DerfTaskEventListener.class);

    @Override
    public void onEvent(RuntimeEvent runtimeEvent) {

        System.out.println("\n\n------------------An Event Happened--------------------\n");
        if (runtimeEvent instanceof TaskActivatedEvent)
            logger.info("Do something, task is activated: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof TaskAssignedEvent) {
            TaskAssignedEvent taskEvent = (TaskAssignedEvent)runtimeEvent;
            Task task = taskEvent.getEntity();
            logger.info("Do something, task is assigned: " + task.toString());
        } else if (runtimeEvent instanceof TaskCancelledEvent)
            logger.info("Do something, task is cancelled: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof TaskCompletedEvent)
            logger.info("Do something, task is completed: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof TaskCreatedEvent)
            logger.info("Do something, task is created: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof TaskSuspendedEvent)
            logger.info("Do something, task is suspended: " + runtimeEvent.toString());
        else
            logger.info("Unknown event: " + runtimeEvent.toString());

    }
}

When I make the dockers, I get some logging on startup that indicates my listener class is being called as I get several logs similar to this:

example-runtime-bundle                  |
example-runtime-bundle                  | ------------------An Event Happened--------------------
example-runtime-bundle                  |
example-runtime-bundle                  | 2020-02-27 18:32:41.956  INFO [,,,] 1 --- [           main] o.a.c.runtime.DerfProcessEventListener   : Unknown event: RuntimeEventImpl [id=635d6bab-3230-4790-a964-cf0d266b3159, timestamp=1582828361955, 
processInstanceId=null, processDefinitionId=SubprocessParent:1:838b625e-597f-11ea-b270-0242ac15000c, processDefinitionKey=SubprocessParent, processDefinitionVersion=null, businessKey=null, parentProcessInstanceId=null,
entity=ProcessDefinition{id='SubprocessParent:1:838b625e-597f-11ea-b270-0242ac15000c', name='Subprocess Parent', key='SubprocessParent', description='null', formKey='null', version=1}]

However, after the containers are finished initializing and are all running. Using the default postman collection to start processes or claim/complete tasks doesn't seem to give me any logging from my listeners. I just recieve some general logging from nginx and notifications such as: 

activiti-cloud-notifications-graphql    | 2020-02-27 19:09:15.337  INFO [,d5415bee8feab613,a7e2bdbe6a2824ab,false] 1 --- [9iP_nWL6Vke1w-2] g.e.c.EngineEventsConsumerMessageHandler : Recieved source message with routingKey: engineEvents.rb.default-app
nginx                                   | 172.24.0.1 - - [27/Feb/2020:19:09:15 +0000] "POST /rb/v1/tasks/a0390c00-598f-11ea-920a-0242ac18000a/complete HTTP/1.1" 200 1164 "-" "PostmanRuntime/7.22.0"

Is there a different way to create event listeners? Or is there something I'm missing in my implementation of the eventListeners? 

2 Replies
hbobertz
Active Member

Re: Implementing Event Listeners in Activiti 7

Just wanted to comeback and update my post now that I found a solution to my problem in case anyone had a similar issue. For some reason the listeners will not fire after startup if I implement the interface without typing it to a certain event as shown both in the original post, and in the acitiviti 7 deep dive series. i.e.

@Service
public class ProcessEventListener implements ProcessRuntimeEventListener {

@Override public void onEvent(RuntimeEvent runtimeEvent){
...
}
}  

 However, when I implement the event listener and type it to a specific event i.e.

@Service
public class ProcessEventListener implements TaskRuntimeEventListener<TaskCreatedEvent> {

    @Override
    public void onEvent(TaskCreatedEvent event){
         ...
    }
}

 This will actually trigger whenever that event occurs within the runtime bundle services

samwang
Member II

Re: Implementing Event Listeners in Activiti 7

Thanks for providing your solution. I have the same issue.