Callback mechanism for REST service call


#1

We have REST web api which takes longer time to return data. So when we call web api from Edoras process model, it should provide some callback mechanism to receive data into Edoras.


#2

Hi Sachin,

I think what you want is an asynchronous confirmation, I would call the other service from a ServiceTask and then just after this ServiceTask use a BPMN Message catching intermediate event. Like in the following image:

That way the process will wait until the other system provides the answer. You can then ‘ping’ the waiting process by using the following snippet:

`public class ProcessMessageService {

private static final Logger LOG = LoggerFactory.getLogger(ProcessMessageService.class);

@Autowired
private ProcessEngineInfo processEngineInfo;

@Autowired
private ProcessService processService;

public ProcessId startProcessByMessage(String messageName, Map<String, Object> variables) {
    LOG.debug("Received BPMN Start Message with name '{}'. Variables: {}", messageName, convertToJSONString(variables));

    ProcessEngine processEngine = this.processEngineInfo.getNativeProcessEngine(ProcessEngine.class);
    ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByMessage(messageName, variables);

    ProcessId processId = resolveIdFromProcessInstance(processInstance);
    LOG.debug("Started process with id '{}' via MessageStart", processId.getValue());
    return processId;
}

public void sendMessageToProcess(final ProcessId processId, final String messageName) {
    this.sendMessageToProcess(processId, messageName, Collections.emptyMap());
}

public void sendMessageToProcess(final ProcessId processId, final String messageName, final Map<String, Object> variables) {
    LOG.debug("Received BPMN Message for process '{}' with name '{}' received. Variables: {}", processId.getValue(), messageName, convertToJSONString(variables));

    ExecutionQuery query = getRuntimeService().createExecutionQuery().messageEventSubscriptionName(messageName);

    com.edorasware.gear.core.process.Process process = ProcessMessageService.this.processService.findProcessById(processId);
    if (process == null) {
        LOG.debug("Target process '{}' of message '{}' not found. Message ignored.", processId.getValue(), messageName);
        return;
    }
    query = query.processInstanceId(process.getExternalId().getValue());

    List<Execution> messageEventSubscriptions = query.list();
    if (messageEventSubscriptions.isEmpty()) {
        LOG.debug("No executions in process '{}' of message '{}' found. Will try again.", processId.getValue(), messageName);
        try {
            Thread.sleep(1000);
            messageEventSubscriptions = query.list();
        } catch (InterruptedException e) {
            LOG.error("Sleeping thread interrupted", e);
        }
    }
    if (messageEventSubscriptions.isEmpty()) {
        LOG.debug("No executions in process '{}' of message '{}' found. Message ignored.", processId.getValue(), messageName);
    } else {
        Execution execution = messageEventSubscriptions.get(0);
        LOG.debug("Sending message '{}' to execution '{}'.", messageName, execution.getId());
        getRuntimeService().messageEventReceived(messageName, execution.getId(), variables);
    }
}

public boolean isProcessWaitingForMessage(ProcessId processId, String messageName) {
    com.edorasware.gear.core.process.Process process = this.processService.getProcessById(processId);

    ExecutionQuery query = getRuntimeService().createExecutionQuery().messageEventSubscriptionName(messageName);
    query = query.processInstanceId(process.getExternalId().getValue());
    return !query.list().isEmpty();
}

private ProcessId resolveIdFromProcessInstance(ProcessInstance processInstance) {
    ProcessId externalProcessId = ProcessId.get(processInstance.getProcessInstanceId());
    com.edorasware.gear.core.process.Process process = this.processService.findEntityByExternalId(externalProcessId);
    return process.getId();
}

private RuntimeService getRuntimeService() {
    return this.processEngineInfo.getNativeProcessEngine(ProcessEngine.class).getRuntimeService();
}

}`


#3

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.