Send message for all active process


Hey. How to send a message to all active process instances?
If I send to the instances that are on the message, then everything is ok.
If there are instances in the active selection that have already passed the message, then the error:
Cannot correlate message ‘WaitAcceptance’: No process definition or execution matches the parameters.

I do this:
@Component(“selectApplicationDelegate”)
class SelectApplicationDelegate : JavaDelegate {
override fun execute(execution: DelegateExecution) {
var rs = execution.processEngineServices.runtimeService
var processInstancesList = rs.createProcessInstanceQuery().active().processDefinitionKey(“ApplicationApprove”).list()
var applications = ArrayList()
processInstancesList.forEach() {
var amountFromProcess = rs.createVariableInstanceQuery().activityInstanceIdIn(it.id).variableName(“Amount”).singleResult().value as Long
var idFromProcess = rs.createVariableInstanceQuery().activityInstanceIdIn(it.id).variableName(“ID”).singleResult().value as Long
var descriptionFromProcess = rs.createVariableInstanceQuery().activityInstanceIdIn(it.id).variableName(“Description”).singleResult().value as String
var processId = it.id

        var application = application().apply {
            id = idFromProcess
            desctiption = descriptionFromProcess
            amount = amountFromProcess
            processInstanceId = processId
        }
        applications.add(application)
    }
    execution.setVariable("applications", applications)
}

}

Send message:
@Component(“sendAllDelegate”)
class SendAllDelegate : JavaDelegate {
override fun execute(execution: DelegateExecution) {
var rs = execution.processEngineServices.runtimeService
var applications = execution.getVariable(“applications”) as List
applications.forEach(){
rs.createMessageCorrelation(“WaitAcceptance”)
.processInstanceId(it.processInstanceId)
.setVariable(“acceptanceResult”, it.acceptanceResult)
.setVariable(“Comment”, it.comment)
.correlate()
}
}
}

Hi @Evgeny,

the docs state that “A message has a name and a payload. Unlike a signal, a message event is always directed at a single recipient.” If there is no recipient the correlation fails.
Maybe what you need is a signal event. Unlike messages a signal is “broadcasted” to all active instances.

Cheers,
Miklas

https://docs.camunda.org/manual/latest/reference/bpmn20/events/signal-events/
https://docs.camunda.org/manual/latest/reference/bpmn20/events/message-events/

1 Like

thanks for your reply.@ Miklas
The fact is that I need to send my parameters to each process, and they will either output or create a task, so the signal does not fit.

In my version, everything is ok, only I need to find all the instances that are waiting for the message.
It was verified empirically that if there are 2 processes waiting for one message, then my script works, because in it I send a message to each specific process separately.

Search Example:
var processInstancesList = rs.createProcessInstanceQuery().active().processDefinitionKey(“ApplicationApprove”).list()

How can I fix it to find processes waiting for my message?

@Evgeny if you’re trying to find all the instances waiting for something, you could take a look at creating an EventSubscriptionQuery from the RuntimeService.

If what you’re looking for instead (since you mentioned the instances that have already passed the message event), you could consider an Event subprocess that allows you to react to certain events (in your case a message) during a complete scope of the process, or the entire process. That way you could handle a certain message at any time the process is running.

1 Like

@tiesebarrell
all perfectly! thank you.

Code:

var subscription = rs.createEventSubscriptionQuery() .processInstanceId(it.processInstanceId).eventType(“message”).eventName(“WaitAcceptance”).singleResult()
if (subscription != null) {
var myVariables = mutableMapOf<String, Any>()
myVariables.put(“acceptanceResult”, it.acceptanceResult)
myVariables.put(“Comment”, it.comment as String)

            rs.messageEventReceived("WaitAcceptance", subscription.executionId, myVariables)

        }

Glad you found what you need :slight_smile:

1 Like