Interface MessageBus

All Known Implementing Classes:
LinkedQueueMessageBus

public interface MessageBus
High-performance message bus for job lifecycle events. Implementations must be thread-safe and non-blocking for publishers.
Since:
0.1 (2025-08-10)
Author:
Andrey Santrosyan
  • Method Details

    • publish

      void publish(JobEvent message)
      Publishes a message to all registered listeners. This method must never block the calling thread.
      Parameters:
      message - the message to publish
    • subscribe

      <T extends JobEvent> MessageBus.Subscription subscribe(Class<? extends Job> jobType, Class<T> messageType, JobObserver<T> observer)
      Subscribes to messages of a specific type from a specific job type.
      Type Parameters:
      T - the message type
      Parameters:
      jobType - the job class to filter by (null or Job.class for all job types)
      messageType - the message class to filter by
      observer - the observer to invoke for matching messages
      Returns:
      a subscription handle that can be used to unsubscribe
    • subscribe

      <T extends JobEvent> MessageBus.Subscription subscribe(Class<? extends Job> jobType, Class<T> messageType, String workflowId, JobObserver<T> observer)
      Subscribes to messages of a specific type from a specific job type and workflow.
      Type Parameters:
      T - the message type
      Parameters:
      jobType - the job class to filter by (null or Job.class for all job types)
      messageType - the message class to filter by
      workflowId - the workflow ID to filter by (null for all workflows)
      observer - the observer to invoke for matching messages
      Returns:
      a subscription handle that can be used to unsubscribe
    • unsubscribe

      void unsubscribe(MessageBus.Subscription subscription)
      Removes a subscription.
      Parameters:
      subscription - the subscription to remove
    • start

      void start()
      Starts the message bus. Must be called before publishing or consuming messages.
    • stop

      void stop()
      Stops the message bus gracefully. Waits for in-flight messages to be processed.
    • cleanupStaleSubscriptions

      int cleanupStaleSubscriptions()
      Removes stale subscriptions by calling isStale() on all observers. This method iterates through all active subscriptions and unsubscribes those whose observers report being stale.

      This is typically called periodically by a cleanup task, but can also be invoked manually for immediate cleanup.

      Returns:
      the number of stale subscriptions that were removed