Class SseGqmBridge<BeanType>
- Type Parameters:
BeanType- the type of the bean that the query manager handles
- All Implemented Interfaces:
GenericQueryManagerListener<BeanType>
GenericQueryManager to a server-sent
events (SSE) SseBroadcaster, so that every bean that is inserted,
updated or deleted through the query manager is pushed to all the
connected SSE clients.
The bridge is registered like any other query manager listener:
manager.addListener(new SseGqmBridge<>(broadcaster));
By default, the name of the operation (inserted,
updated or deleted) becomes the SSE event name, and the
string representation of the bean becomes the SSE event data, or the
string representation of the object ID in case of deletions. The
conversion of each operation can be customized individually, which also
makes it possible to render template fragments with setBean(), or
to filter operations out by returning null:
manager.addListener(new SseGqmBridge<Product>(broadcaster)
.onInserted(product -> {
var t = TemplateFactory.HTML.get("catalog");
t.setBean(product);
return new ServerSentEvent().name("product").templateBlock(t, "product_row");
})
.onDeleted(objectId -> null));
Restoring beans doesn't broadcast events, and neither do the installation and the removal of the database structure. Note that this bridge only observes the changes that are made through the query manager that it is registered with, since modifications that reach the database in other ways aren't broadcast.
The converters run on the thread that performs the database
operation, after that operation has completed. Since the database change
has already happened at that point, exceptions from the conversion or
from the broadcast don't propagate to the database operation: they are
handed to the onError listener, or logged to the
rife.engine logger when no listener was provided. You should
configure the converters before registering the bridge as a listener.
-
Constructor Summary
ConstructorsConstructorDescriptionSseGqmBridge(SseBroadcaster broadcaster) Creates a new bridge with the default conversions. -
Method Summary
Modifier and TypeMethodDescriptionvoiddeleted(int objectId) Executed when a bean was successfully deleted.voidExecuted when a bean was successfully inserted.voidExecuted when the database structure has been successfully installed.static <BeanType> SseGqmBridge<BeanType>json(SseBroadcaster broadcaster) Creates a new bridge whose conversions transmit JSON event data, which is suitable for clients that parse the events withJSON.parse(event.data).onDeleted(IntFunction<ServerSentEvent> converter) Customizes the conversion of deletions.onError(SseErrorListener listener) Provides a listener for conversion and broadcast failures.onInserted(Function<BeanType, ServerSentEvent> converter) Customizes the conversion of inserted beans.onUpdated(Function<BeanType, ServerSentEvent> converter) Customizes the conversion of updated beans.voidremoved()Executed when the database structure has been successfully removed.voidExecuted when a bean was successfully restored.voidExecuted when a bean was successfully updated.
-
Constructor Details
-
SseGqmBridge
Creates a new bridge with the default conversions.- Parameters:
broadcaster- the broadcaster the converted events will be sent to- Since:
- 1.10
- See Also:
-
-
Method Details
-
json
Creates a new bridge whose conversions transmit JSON event data, which is suitable for clients that parse the events withJSON.parse(event.data).Inserted and updated beans are converted to a JSON object of their properties with
ServerSentEvent.json(java.lang.Object), while deletions transmit a JSON object with the object ID as itsidmember. The conversions can still be customized individually afterwards.- Type Parameters:
BeanType- the type of the bean that the query manager handles- Parameters:
broadcaster- the broadcaster the converted events will be sent to- Returns:
- the new bridge instance
- Since:
- 1.10
- See Also:
-
onInserted
Customizes the conversion of inserted beans.The converter returns the server-sent event that will be broadcast, or
nullwhen nothing should be broadcast for insertions.- Parameters:
converter- the converter that will be used for inserted beans- Returns:
- this bridge instance
- Since:
- 1.10
- See Also:
-
onUpdated
Customizes the conversion of updated beans.The converter returns the server-sent event that will be broadcast, or
nullwhen nothing should be broadcast for updates.- Parameters:
converter- the converter that will be used for updated beans- Returns:
- this bridge instance
- Since:
- 1.10
- See Also:
-
onDeleted
Customizes the conversion of deletions.The converter receives the object ID of the deleted bean and returns the server-sent event that will be broadcast, or
nullwhen nothing should be broadcast for deletions.- Parameters:
converter- the converter that will be used for deletions- Returns:
- this bridge instance
- Since:
- 1.10
- See Also:
-
onError
Provides a listener for conversion and broadcast failures.The bridge is notified after the database operation has completed, so that a failure can't affect the result of a change that has already happened. Failures are logged to the
rife.enginelogger by default, and providing a listener will replace that behavior.A listener that fails itself can't affect the operation either, since its failure is logged together with the failure that it was given.
- Parameters:
listener- the listener that will receive conversion and broadcast failures- Returns:
- this bridge instance
- Since:
- 1.10
- See Also:
-
installed
public void installed()Description copied from interface:GenericQueryManagerListenerExecuted when the database structure has been successfully installed.- Specified by:
installedin interfaceGenericQueryManagerListener<BeanType>
-
removed
public void removed()Description copied from interface:GenericQueryManagerListenerExecuted when the database structure has been successfully removed.- Specified by:
removedin interfaceGenericQueryManagerListener<BeanType>
-
inserted
Description copied from interface:GenericQueryManagerListenerExecuted when a bean was successfully inserted.- Specified by:
insertedin interfaceGenericQueryManagerListener<BeanType>- Parameters:
bean- the bean that was inserted
-
updated
Description copied from interface:GenericQueryManagerListenerExecuted when a bean was successfully updated.- Specified by:
updatedin interfaceGenericQueryManagerListener<BeanType>- Parameters:
bean- the bean that was updated
-
restored
Description copied from interface:GenericQueryManagerListenerExecuted when a bean was successfully restored.- Specified by:
restoredin interfaceGenericQueryManagerListener<BeanType>- Parameters:
bean- the bean that was restored
-
deleted
public void deleted(int objectId) Description copied from interface:GenericQueryManagerListenerExecuted when a bean was successfully deleted.- Specified by:
deletedin interfaceGenericQueryManagerListener<BeanType>- Parameters:
objectId- the identifier of the bean that was deleted
-