Class SseGqmBridge<BeanType>

java.lang.Object
rife.database.querymanagers.generic.SseGqmBridge<BeanType>
Type Parameters:
BeanType - the type of the bean that the query manager handles
All Implemented Interfaces:
GenericQueryManagerListener<BeanType>

public class SseGqmBridge<BeanType> extends Object implements GenericQueryManagerListener<BeanType>
Bridges the data changes of a 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.

Since:
1.10
See Also: