executing workflows in a synchronous manner

62 views
Skip to first unread message

Julian Purse

unread,
Dec 11, 2023, 4:51:15 AM12/11/23
to nflow-users
Hi, 

a bit of a random question, in a scenario of needing to have the result of a workflow sent back. ie 
REST request comes in -> starts workflow
workflow executes
result sent on REST response

i have achieved this by doing a Thread.sleep on the main request thread then query for the workflow by id every 500ms 
instances = workflowInstances.listWorkflowInstances(query);

if (instance.state.equals(MyWorkFlow.State.done.name())) {
return instance.getStateVariable("RESPONSE",Response.class)

is there a builtin hook/callback that can be put on a workflow or should i create a service layer with callable futures that the workflow puts the response on.

i suppose my question being, "is there a best practice way of doing this"

Regards,
Julian




Edvard Fonsell

unread,
Dec 17, 2023, 10:17:46 AM12/17/23
to nflow-users
Hi,

At the moment there is no built-in hook in nFlow. There are couple of ways to do this, each with some pros and cons:

1) Your way.
2) Have the sleep on client side: one request to start the workflow, then poll for the response in a loop with sleep until the response is available.
3) Implement the callback as last step in your workflow.

You may also want to handle all states where instance.state.isFinal() is true instead of just the "done" state to cover possible error scenarios also.

br, Edvard

Julian Purse

unread,
Sep 6, 2024, 1:48:38 AM9/6/24
to nflow-users
incase anyone ends up reading this, there is a listener service you can hook into to achieve this, ie implement this then from here you can complete Futures or perform other actions, (*note* this is a singleton so dont do heavy processing in it) 



@Service
public class TestListener implements WorkflowExecutorListener {

@Override
public void beforeProcessing(ListenerContext listenerContext) {

System.out.println("before processing:"+listenerContext.instance.id);

WorkflowExecutorListener.super.beforeProcessing(listenerContext);
}

@Override
public void afterProcessing(ListenerContext listenerContext) {

System.out.println("after proessing:"+listenerContext.instance.id);
WorkflowExecutorListener.super.afterProcessing(listenerContext);
}

@Override
public void afterFailure(ListenerContext listenerContext, Throwable throwable) {
System.out.println("afterFailure:"+listenerContext.instance.id);
WorkflowExecutorListener.super.afterFailure(listenerContext, throwable);
}

@Override
public boolean handlePotentiallyStuck(ListenerContext listenerContext, Duration processingTime) {
System.out.println("handlePotentiallyStuck:"+listenerContext.instance.id);
return WorkflowExecutorListener.super.handlePotentiallyStuck(listenerContext, processingTime);
}
}
Reply all
Reply to author
Forward
0 new messages