Class ServerBus
- All Implemented Interfaces:
AutoCloseable
Publish from anywhere in the application; every connected page whose bridge was granted the topic receives it
(and every page that connects later receives the retained value). Topics not allowed by the BusPolicy
are refused loudly (E-56).
The methods under "transport SPI" are public for BusEventsMixin and BusWebSockets; application code
does not call them.
Example:
- Since:
- 10.0.0
-
Method Summary
Modifier and TypeMethodDescriptionvoidRemoves a retained value and sendsclearto every connected session granted the topic.voidclose()Closes every connection with1001(bus:closed), forgets every session and stops the timer.voidconnect(BusSession session, FrameSink sink) Transport SPI: attaches a connection to a session, then sends the resync.static ServerBusCreates a bus and starts its timer (one daemon thread: heartbeat pings and session expiry).voiddisconnect(BusSession session, FrameSink sink) Transport SPI: the connection behind a sink has closed.voidenableWebSocket(String contextRelativePathPrefix) Transport SPI: records that the WebSocket transport is registered, sotransport:"websocket"sessions may open and the session POST response carriespaths.websocket.openSession(jakarta.servlet.http.HttpServletRequest req, JsonMap body) Transport SPI: opens a session from a session POST body.policy()booleanPublishes a value to every connected session granted the topic, and stores it if the topic is retained.intpublishTo(Predicate<BusSession> audience, String topic, Object payload) Publishes a value to the connected sessions that match an audience.voidreceive(BusSession session, String frameText) Transport SPI: one upstream frame from a WebSocket connection.retainedJson(String topic) The stored JSON of a retained topic.Transport SPI: finds a live session by its capability id, for the principal that opened it.sessions()The live sessions (connected, or disconnected within their grace period).
-
Method Details
-
create
Creates a bus and starts its timer (one daemon thread: heartbeat pings and session expiry).- Parameters:
policy- The policy. Must not benull .- Returns:
- A new bus.
close()it when the application stops.
-
publish
Publishes a value to every connected session granted the topic, and stores it if the topic is retained.For a retained topic, a value whose JSON equals the stored JSON is not republished (distinct-until-changed).
- Parameters:
topic- A concrete topic the policy allows downstream (not a:*pattern).payload- The payload, serialized withJson.of(Object). Must not benull ; useclear(String)to remove a retained value.- Returns:
false when the topic is retained and the value is unchanged; otherwisetrue .- Throws:
IllegalArgumentException- E-56 when the policy denies the topic or the frame is larger thanBusPolicy.maxFrameBytes().
-
clear
Removes a retained value and sendsclearto every connected session granted the topic.Clearing a topic that holds no value does nothing.
- Parameters:
topic- A retained topic.- Throws:
IllegalArgumentException- E-56 when the policy denies the topic or does not retain it.
-
publishTo
Publishes a value to the connected sessions that match an audience. Never retained, even on a retained topic.- Parameters:
audience- Selects sessions. Runs under the bus lock: keep it cheap and side-effect free.topic- A concrete topic the policy allows downstream.payload- The payload. Must not benull .- Returns:
- The number of sessions that accepted the frame.
- Throws:
IllegalArgumentException- E-56, as forpublish(String, Object).
-
retainedJson
The stored JSON of a retained topic.- Parameters:
topic- The topic.- Returns:
- The JSON, or empty when nothing is stored.
-
sessions
The live sessions (connected, or disconnected within their grace period).- Returns:
- An immutable snapshot.
-
policy
- Returns:
- The policy this bus enforces.
-
close
Closes every connection with1001(bus:closed), forgets every session and stops the timer. Retained values are kept. Idempotent.- Specified by:
closein interfaceAutoCloseable
-
openSession
public BusSession openSession(jakarta.servlet.http.HttpServletRequest req, JsonMap body) throws BusRefusal Transport SPI: opens a session from a session POST body.Body:
{"v":1, "bridge":"ops", "transport":"sse"|"websocket", "downstream":[...], "upstream":[...]}.vdefaults to1andtransportto"sse";bridgeis required.- Parameters:
req- The POST request: the authorizer's argument and the source of the principal and session attributes.body- The parsed body.null is a 400.- Returns:
- The new session.
- Throws:
BusRefusal- 400bus:bad-request, 403bus:topic-denied, 429bus:too-many-sessions, 501bus:transport-unavailable, or 503bus:closed.
-
session
Transport SPI: finds a live session by its capability id, for the principal that opened it.- Parameters:
id- The session id from the request path. Can benull .principal- The request principal. Can benull (anonymous).- Returns:
- The session, or empty when the id is unknown or expired, or the principal differs. The three cases are deliberately indistinguishable.
-
connect
Transport SPI: attaches a connection to a session, then sends the resync.Any live sink is replaced and closed with
4409(bus:replaced). The resync isresync-begin, one retainedpubfor every stored value the session's grant covers, in topic order, thenresync-end. When the bus is closed or the session has expired, the sink is closed at once (1001/4401).- Parameters:
session- A session of this bus.sink- The connection's outbound side.
-
disconnect
Transport SPI: the connection behind a sink has closed. Starts the session's grace period.A sink that is no longer the session's live sink (it was replaced, or detached as a slow consumer) is ignored.
- Parameters:
session- A session of this bus.sink- The sink passed toconnect(BusSession, FrameSink).
-
receive
Transport SPI: one upstream frame from a WebSocket connection.Refusals are answered with an
errorframe and the frame is dropped; the connection stays open.- Parameters:
session- A session of this bus.frameText- The frame text.
-
enableWebSocket
Transport SPI: records that the WebSocket transport is registered, sotransport:"websocket"sessions may open and the session POST response carriespaths.websocket.- Parameters:
contextRelativePathPrefix- The endpoint path without its/{sessionId}segment, for example/juneau-bus/ws. Must start with/and not end with/.
-