Event register problem

Hello.

I am trying to write some logic to do the assignment of multi-instance tasks when new members are included in some groups, but I am finding a hard time trying to register to receive such events.

If I register the listener using the code below, I receive lots of events, all from FlowableEngineEventType

runtimeService.addEventListener(new MembershipCreatedEventListener());

But using that, I receive nothing from IDM, like FlowableIdmEventType.MEMBERSHIP_CREATE

What is the right way to register for the FlowableIdmEventType types?

I have also tried with this:

ProcessEngines.getDefaultProcessEngine().getProcessEngineConfiguration().getEventDispatcher().addEventListener(new MembershipCreatedEventListener());

But again, nothing changes.

My code is packaged into a jar file, and I am deploying it inside the flowable-task/WEB-INF/lib folder.

Thank you.

Good morning.

Just to register here, one more test that I have done, with no success.

I tried to register the listener with

IdmEngines.getDefaultIdmEngine().getIdmEngineConfiguration().getEventDispatcher().addEventListene
r(new MembershipCreatedEventListener());

Besides that, I have also tried to deploy the resulting jar both inside the flowable-task and inside flowable-idm, but again, I got nothing from the event dispatcher.

Hmm, the second post should work - do you have a way and set a breakpoint and see what happens in the MembershipEntityManagerImpl#createMembership method?

Worst case, you’d be able to listen to generic entity events and check for the membership entity. However, the logic above should work, see also the test here: https://github.com/flowable/flowable-engine/blob/master/modules/flowable-engine/src/test/java/org/flowable/engine/test/api/event/GroupEventsTest.java#L110

Hello
Thank you for your answer, and sorry for the late response.
I was debugging flowable’s source code, and it seems the code creating the event is reached, but for some reason, the notification is not received by the listener.
And today, doing some more tests, I found the following in the code:

Am I wrong when I think such code is ignoring the types defined by the FlowableIdmEventType class?

The mentioned code seems to be searching by the events by the string, and in the case of FlowableIdmEventType.MEMBERSHIP_CREATED, it is not there.

I am going to keep testing everything and will be back with more information if I find something useful.

And another test.

When the membership is created or removed, the request to dispatch the events are executed, but when the code reaches https://github.com/flowable/flowable-engine/blob/88d6bbf60b719a7b30a9eea153f78b2408e79d82/modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/event/FlowableEventSupport.java#L87

both variables eventListeners and typedListeners are empty


and because of that, no real event is dispatched.

Any idea on how to proceed with this?

Do you think my previous question makes sense?

Hey @douglascrp,

When are you doing IdmEngines.getDefaultIdmEngine().getIdmEngineConfiguration().getEventDispatcher().addEventListene r(new MembershipCreatedEventListener());?

If you are repacking your jar and adding it to the flowable-task I would suggest you to use Spring Boot auto configuration and register your events like that. Perhaps invoking IdmEngines.getDefaultIdmEngine() is too early and has not been created by Spring.

What you shared should work (the registering by getting the event dispatcher and adding the event).

Cheers,
Filip

Hello.

This is the class with all the tests I have done so far (all commented, but I’ve tried all of the options, one at a time)

package br.com.dgcloud.flowable;

import java.util.List;

import org.flowable.common.engine.impl.event.FlowableEventSupport;
import org.flowable.engine.HistoryService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngines;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.idm.api.event.FlowableIdmEventType;
import org.flowable.idm.engine.IdmEngines;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.flowable.spring.SpringProcessEngineConfiguration;

import br.com.dgcloud.events.MembershipCreatedEventListener;

@Configuration
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
@ConditionalOnBean(type = "org.flowable.engine.ProcessEngine")
public class CustomConfiguration {

   @Configuration
   @ComponentScan({ "br.com.dgcloud.flowable" })
   public static class ComponentScanConfiguration {
   	// This class is needed in order to conditionally perform the component scan
   	// (i.e. when the ProcessEngine bean is present)
   	// It is an optional class, in case you don't need component scanning then you
   	// don't need to do this
   }

   @Bean
   public EngineConfigurationConfigurer<SpringProcessEngineConfiguration> customProcessEngineConfigurationConfigurer() {
   	return engineConfiguration -> {
   		// You can use this to add extra configuration to the process engine
           //IdmEngines.getDefaultIdmEngine().getIdmEngineConfiguration().getEventDispatcher().addEventListener(new MembershipCreatedEventListener(), FlowableIdmEventType.MEMBERSHIP_CREATED);
           //IdmEngines.getDefaultIdmEngine().getIdmEngineConfiguration().getEventDispatcher().addEventListener(new MembershipCreatedEventListener());
   		
   		// getEventDispatcher() == null
   		//engineConfiguration.getEventDispatcher().addEventListener(new MembershipCreatedEventListener(), FlowableIdmEventType.MEMBERSHIP_CREATED);
   	};
   }

   @Bean
   public CommandLineRunner init(final RepositoryService repositoryService, final RuntimeService runtimeService,
   		final TaskService taskService) {
   	return new CommandLineRunner() {
   		@Override
   		public void run(String... strings) throws Exception {
   			//runtimeService.addEventListener(new MembershipCreatedEventListener());
   			//runtimeService.addEventListener(new MembershipCreatedEventListener(), FlowableIdmEventType.MEMBERSHIP_CREATED);

               //ProcessEngines.getDefaultProcessEngine().getProcessEngineConfiguration().getEventDispatcher().addEventListener(new MembershipCreatedEventListener());
               //IdmEngines.getDefaultIdmEngine().getIdmEngineConfiguration().getEventDispatcher().addEventListener(new MembershipCreatedEventListener());
               //IdmEngines.getDefaultIdmEngine().getIdmEngineConfiguration().getEventDispatcher().addEventListener(new MembershipCreatedEventListener(), FlowableIdmEventType.MEMBERSHIP_CREATED);
               
               //processEngineConfiguration.getEventDispatcher().addEventListener(membershipListener);

               //runtimeService.addEventListener(new MembershipCreatedEventListener(), FlowableEngineEventType.TASK_ASSIGNED);
               //runtimeService.addEventListener(new MembershipCreatedEventListener(), FlowableEngineEventType.TASK_COMPLETED);
   		}
   	};
   }
}

Nothing works… I receive notifications for everything, except for the IDM ones.

Is it too much to ask somebody to try to create a sample project with these events working?

Thank you all.

The latest attempt for the week.

package br.com.dgcloud.flowable;

import org.flowable.engine.ProcessEngines;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.idm.engine.IdmEngines;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

import br.com.dgcloud.flowable.events.GroupFlowableEventListener;

@Configuration
// Makes sure that this configuration will be processed last by Spring Boot
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
//The configuration will only be used when the ProcessEngine bean is present
//@ConditionalOnBean(type = {"org.flowable.engine.ProcessEngine", "org.flowable.idm.engine.IdmEngine"})
@ConditionalOnBean(type = {"org.flowable.idm.engine.IdmEngine"})
public class CustomConfiguration {

	@Bean
	public CommandLineRunner init(final RepositoryService repositoryService, final RuntimeService runtimeService,
			final TaskService taskService) {
		return new CommandLineRunner() {
			@Override
			public void run(String... strings) throws Exception {

				if (ProcessEngines.getDefaultProcessEngine().getProcessEngineConfiguration().getEventDispatcher() != null) {
					
					ProcessEngines.getDefaultProcessEngine().getProcessEngineConfiguration().getEventDispatcher().addEventListener(new GroupFlowableEventListener());
					
				} else {
					System.out.println("DG: Event Dispatcher Process nulo");
				}
				
				if (IdmEngines.getDefaultIdmEngine().getIdmEngineConfiguration().getEventDispatcher() != null) {
					
					IdmEngines.getDefaultIdmEngine().getIdmEngineConfiguration().getEventDispatcher().addEventListener(new GroupFlowableEventListener());
					
				} else {
					System.out.println("DG: Event Dispatcher IDM nulo");
				}
				
			}
		};
	}
}

With this code, the event listener class receives all kind of events, but nothing from IDM.
I can see events for entities, tasks, flows, everything, except for the IDM related ones.

I have no idea what else to try.

Good morning all.

Rereading the docs, I found this:

Listeners are only notified for events dispatched from the engine they are registered with. So if you have different engines - running against the same database - only events that originated in the engine the listener is registered to are dispatched to that listener. The events that occur in other engines are not dispatched to the listeners, regardless of whether they are running in the same JVM or not.

So maybe the problem is because I am deploying my custom code inside flowable-task, and the event is fired by flowable-idm.
As I’m using the 6.5 version, I have the multi-apps setup, so maybe if I use the new version 6.6, with the single app, the problem can go away.

I think I 'm going to test it.

Yes, using the single app way with Flowable 6.6 did the trick.

Now the event is received by the event listener.

Once I finish the solution, I will post something here in order to let it as a reference for the future.

Thank you all for the help