Correlate Message directly from message event

Hi,

why is it not possible in camunda to correlate a message directly from intermediate throwing message event without writing java code but defining a message to throw on bpmn event.

The semantic could be to correlate to receivers in same process instance. So if a receiver exits in same process instance this would directly work without writing java code.

Best regards,

Markus

Hi @Markus,

I think that message correlation is an engine specific functionality and there are too many ways to implement it.

Cheers,
Askar

Ok this could be a reason.
I found a simple solution for us and would like to share the code. Its a simple ExecutionListener activated with camunda reactor. It checks if intermediateThrowEvent ha a message and correlates it to all receivers in process instance with same business Key.

@Log4j
@CamundaSelector(type = "intermediateThrowEvent", event = ExecutionListener.EVENTNAME_START)
public class CorrelateMessageExecutionListener implements ExecutionListener {

@Override
public void notify(DelegateExecution execution) throws Exception {
	IntermediateThrowEvent event =(IntermediateThrowEvent)execution.getBpmnModelElementInstance();
	
	Optional<MessageEventDefinition> messageEventDefinitionOptional = event.getEventDefinitions().stream()
		.filter((ed) -> ed instanceof MessageEventDefinition)
		.map((ed) -> ((MessageEventDefinition)ed))
		.findFirst();
	
	messageEventDefinitionOptional.ifPresent((messageEventDefinition) -> {
		if(messageEventDefinition.getMessage()!=null){
			
			if(execution.getProcessBusinessKey()!=null){
				RuntimeService runtimeService = execution.getProcessEngineServices().getRuntimeService();
				String messageName = messageEventDefinition.getMessage().getName();

				runtimeService.createMessageCorrelation(messageName).processInstanceBusinessKey(execution.getProcessBusinessKey()).correlateAll();
			}else{
				log.info(String.format("No message is thrown from execution=%s because no businessKey is defined in process instance",execution.getId()));
			}
		}
	});		
}

}

1 Like

Hey Markus,

maybe you can use the Signal Event instead, which will also do a broadcast.
See the Signal documentation for more information.

Best regards,
Chris

Hi,

no that is no good solution for me because I dont want a process instance overall broadcast event but a event which is only delegated to receivers in same process instance.

The code posted below works fine for me.

Thank you and best regards,

Markus