Class Service

java.lang.Object
com.framed.core.Service
Direct Known Subclasses:
Dispatcher, MqttService, Parser, Protocol, Reactor, Writer

public abstract class Service extends Object
An abstract base class for application services that provides access to an EventBus for inter-component communication / services and a Logger for logging.

Subclasses can use the event bus to send or receive messages and the logger for diagnostic output. This class also provides a default stop() method that can be overridden to implement custom shutdown logic.

  • Field Details

    • ADDRESS_REGISTRY_SUFFIX

      public static final String ADDRESS_REGISTRY_SUFFIX
      Topic suffix for the producer→sink address-discovery protocol.

      A producing service announces each of its output channel names on the topic "<group>" + ADDRESS_REGISTRY_SUFFIX (see announceAddress(String, String)). Sinks such as dispatchers and writers subscribe to that same topic (using addressRegistry(String)) to dynamically discover and bind the channels a producer emits. The group is typically a device name or a logical producer id.

      See Also:
    • eventBus

      protected EventBus eventBus
      The event bus used for communication between components.
    • logger

      protected final Logger logger
      Logger instance for this service, initialized with the class name.
    • formatter

      protected static final DateTimeFormatter formatter
      Shared timestamp formatter (see Timer.formatter) available to all services for consistent serialization and parsing of timestamps.
  • Constructor Details

    • Service

      protected Service(EventBus eventBus)
      Creates a new service instance with the specified event bus.
      Parameters:
      eventBus - the event bus used for communication
  • Method Details

    • addressRegistry

      public static String addressRegistry(String group)
      Returns the address-discovery topic for the given producer group.
      Parameters:
      group - device name or logical producer id
      Returns:
      the topic sinks subscribe to in order to discover group's output channels
    • announceAddress

      protected void announceAddress(String group, String address)
      Announces that address is an output channel of the producer group, so that sinks subscribed via subscribeToAnnouncements(String, Consumer) can discover and bind it.

      This call is synchronous with respect to binding. Every subscriber registered through subscribeToAnnouncements(String, Consumer) has finished binding address by the time this method returns, so a producer may announce a channel and publish its first sample on the very next statement without losing it.

      Parameters:
      group - device name or logical producer id this service emits under
      address - the output channel name being announced
      See Also:
    • subscribeToAnnouncements

      protected void subscribeToAnnouncements(String group, Consumer<String> binder)
      Subscribes to group's address-discovery topic, so this service learns each output channel the producer announces and can bind a handler to it.

      The subscription is registered with DispatchMode.SEQUENTIAL, which is what makes the discovery handshake race-free: binder runs inline on the announcing thread, so announceAddress(String, String) cannot return before every sink has bound. Registering for announcements with eventBus.register(addressRegistry(group), ..) instead leaves the binding to run on some other thread, and under DispatchMode.PER_HANDLER — the mode a deployment runs — the producer's first samples are then published to an address nobody is listening on yet and are silently discarded. Sinks must use this method rather than subscribing to addressRegistry(String) directly.

      Threading: binder runs on whichever thread called announceAddress(String, String), and different producers announce on different threads, so it must be safe to call concurrently. It should also be cheap and must not block: it is executing inside the producer's publish path. Binding a handler is the only work it should do.

      Parameters:
      group - device name or logical producer id whose announcements to follow
      binder - invoked once per announcement with the announced address
    • stop

      public void stop()
      Stops the service.

      By default, this method logs a message indicating that no stop logic is implemented. Subclasses should override this method to provide custom shutdown behavior when necessary.