Class CrossProtocolEventBus

java.lang.Object
org.mockserver.mock.CrossProtocolEventBus

public class CrossProtocolEventBus extends Object
In-process event bus that bridges protocol events to scenario state transitions. Listeners (registered via register(org.mockserver.model.CrossProtocolScenario)) associate a CrossProtocolTrigger + optional match pattern with a scenario name and target state. When an event is fire(org.mockserver.model.CrossProtocolTrigger, java.lang.String)d, every matching listener triggers a ScenarioManager.setState(java.lang.String, java.lang.String) call.

Thread-safe: uses ConcurrentHashMap and CopyOnWriteArrayList.

Fleet-awareness (G11 follow-up): when a clustered StateBackend is wired via setStateBackend(StateBackend), registrations (trigger-to-scenario mappings) are replicated via the backend's crudEntities("cross-protocol-bus") store: register/unregister/reset write-through to the backend, and a separate InvalidationListener rebuilds the node-local bus from the backend on remote writes. The fire(org.mockserver.model.CrossProtocolTrigger, java.lang.String) path remains purely node-local for zero-overhead event dispatching. When no backend is set or the backend is not clustered, behaviour is identical to the pre-clustering node-local-only bus.

  • Constructor Details

    • CrossProtocolEventBus

      public CrossProtocolEventBus()
      Creates a fresh, non-singleton instance. Public for testing (e.g. per-test isolation in the clustered 2-node integration test).

      Singleton-coupling note: the InvalidationListener registered by HttpState targets getInstance() (the static singleton). A non-singleton bus (as used by tests) must register its own invalidation listener on the backend to receive reconciliation callbacks.

  • Method Details

    • getInstance

      public static CrossProtocolEventBus getInstance()
    • setScenarioManager

      public void setScenarioManager(ScenarioManager manager)
    • setStateBackend

      public void setStateBackend(StateBackend backend)
      Wires the clustered state backend for fleet-wide registration replication. When the backend isClustered(), registrations are replicated via the backend's CRUD entity store, and an InvalidationListener is registered to rebuild the node-local bus on remote writes. When the backend is not clustered, this method is a no-op -- the bus stays purely node-local.
    • register

      public void register(CrossProtocolScenario scenario)
    • unregister

      public void unregister(CrossProtocolScenario scenario)
    • fire

      public void fire(CrossProtocolTrigger trigger, String identifier)
      Fire an event. For each matching registered scenario, calls ScenarioManager.setState(String, String).

      This path is purely node-local -- no backend round-trip. In a clustered deployment, each node fires from its own replicated copy of the registration set.

      Parameters:
      trigger - the event type
      identifier - the relevant identifier (e.g. DNS query name, gRPC service name, HTTP path, WebSocket URL)
    • reset

      public void reset()
    • reconcileFromBackend

      public void reconcileFromBackend()
      Rebuilds the node-local listener map from the backend store. Called by the InvalidationListener when a remote write is detected. Thread-safe but weakly-consistent: the removeIf + put loop is NOT atomic, so a concurrent fire(org.mockserver.model.CrossProtocolTrigger, java.lang.String) call may miss a registration for one cycle. This matches the existing ConcurrentHashMap weakly-consistent iteration semantics and is acceptable for event-bus registration convergence.