Class SseWorkflowBridge
- All Implemented Interfaces:
EventListener
Workflow to a server-sent events (SSE)
SseBroadcaster, so that every event that is triggered or informed
about in the workflow is pushed to all the connected SSE clients.
The bridge is registered like any other workflow listener:
workflow.addListener(new SseWorkflowBridge(broadcaster));
By default, a workflow event is converted with the string
representation of its type as the SSE event name, and with the string
representation of its data as the SSE event data. You can provide a
custom converter instead, which also makes it possible to render
templates as event data, or to filter events out by returning
null:
workflow.addListener(new SseWorkflowBridge(broadcaster, event -> {
if (event.getType() != MyTypes.PROGRESS) return null;
var t = TemplateFactory.HTML.get("progress_fragment");
t.setValueEncoded("step", event.getData());
return new ServerSentEvent().name("progress").template(t);
}));
The converter runs on the thread that triggers the workflow event, and the exceptions that it throws propagate to the caller of the trigger, while the paused work is still resumed.
- Since:
- 1.10
- See Also:
- API Note:
- The workflow engine is in a BETA STAGE and might still change.
-
Constructor Summary
ConstructorsConstructorDescriptionSseWorkflowBridge(SseBroadcaster broadcaster) Creates a new bridge that converts workflow events with the default conversion: the string representation of the event type becomes the SSE event name, and the string representation of the event data, when it's present, becomes the SSE event data.SseWorkflowBridge(SseBroadcaster broadcaster, Function<Event, ServerSentEvent> converter) Creates a new bridge with a custom event converter. -
Method Summary
Modifier and TypeMethodDescriptionvoideventTriggered(Event event) Called when an event was triggered.static SseWorkflowBridgejson(SseBroadcaster broadcaster) Creates a new bridge whose conversion transmits JSON event data, which is suitable for clients that parse the events withJSON.parse(event.data).
-
Constructor Details
-
SseWorkflowBridge
Creates a new bridge that converts workflow events with the default conversion: the string representation of the event type becomes the SSE event name, and the string representation of the event data, when it's present, becomes the SSE event data.- Parameters:
broadcaster- the broadcaster the converted events will be sent to- Since:
- 1.10
- See Also:
-
SseWorkflowBridge
Creates a new bridge with a custom event converter.The converter receives every workflow event and returns the server-sent event that will be broadcast, or
nullwhen nothing should be broadcast for that workflow event.- Parameters:
broadcaster- the broadcaster the converted events will be sent toconverter- the converter that will be used to convert workflow events into server-sent events- Since:
- 1.10
- See Also:
-
-
Method Details
-
json
Creates a new bridge whose conversion transmits JSON event data, which is suitable for clients that parse the events withJSON.parse(event.data).The string representation of the workflow event type becomes the SSE event name, just like it does with the default conversion, while the event data, when it's present, is converted with
ServerSentEvent.json(java.lang.Object).- Parameters:
broadcaster- the broadcaster the converted events will be sent to- Returns:
- the new bridge instance
- Since:
- 1.10
- See Also:
-
eventTriggered
Description copied from interface:EventListenerCalled when an event was triggered.- Specified by:
eventTriggeredin interfaceEventListener- Parameters:
event- the event that was triggered
-