Class PostgresClusterEventBus
- All Implemented Interfaces:
ClusterEventBus,org.springframework.beans.factory.DisposableBean,org.springframework.beans.factory.InitializingBean
ClusterEventBus backed by Postgres LISTEN/NOTIFY — the default cross-node
transport, needing no infrastructure beyond the database the app already uses.
Publish is a short pooled SELECT pg_notify(channel, json). Receive runs on one daemon
thread holding a dedicated connection that LISTENs on the channel and blocks in
PGConnection.getNotifications(int); on a dropped connection it reconnects with capped
exponential backoff. Events the node published itself are filtered out by
ClusterEvent.originNodeId() (Postgres echoes a NOTIFY back to the publisher's own
LISTEN, and the publisher already fanned the change out to its local SSE clients).
This is a best-effort live-UI signal: a notice missed during a reconnect is acceptable (browsers re-fetch on their next interaction). The held listener connection costs one slot from the datasource pool for the bus's lifetime.
-
Constructor Summary
ConstructorsConstructorDescriptionPostgresClusterEventBus(DataSource dataSource, com.fasterxml.jackson.databind.ObjectMapper mapper, OnecClusterProperties props) -
Method Summary
Modifier and TypeMethodDescriptionvoidvoiddestroy()nodeId()The id this node stamps on published events and filters incoming echoes against.voidpublish(ClusterEvent event) Publish a change to peer nodes.voidsubscribe(Consumer<ClusterEvent> sink) Register a sink invoked for events that originated on other nodes.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.onec.cluster.ClusterEventBus
isDistributed
-
Constructor Details
-
PostgresClusterEventBus
public PostgresClusterEventBus(DataSource dataSource, com.fasterxml.jackson.databind.ObjectMapper mapper, OnecClusterProperties props)
-
-
Method Details
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean
-
publish
Description copied from interface:ClusterEventBusPublish a change to peer nodes. Fan-out to the publishing node's own SSE clients has already happened synchronously on the local event path, so an implementation that echoes the event back to its own subscribers must let them filter it byClusterEvent.originNodeId().The implementation stamps the event's
ClusterEvent.originNodeId()with its own node id before sending (seeClusterEvent.withOrigin(java.lang.String)); callers may leave that fieldnull(asClusterEvent.entityChanged(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)does).- Specified by:
publishin interfaceClusterEventBus
-
subscribe
Description copied from interface:ClusterEventBusRegister a sink invoked for events that originated on other nodes. A bus must not invoke the sink for the local node's own events (seeClusterEvent.originNodeId()). May be called more than once to attach multiple sinks.- Specified by:
subscribein interfaceClusterEventBus
-
nodeId
The id this node stamps on published events and filters incoming echoes against. -
destroy
public void destroy()- Specified by:
destroyin interfaceorg.springframework.beans.factory.DisposableBean
-