Class Service
- Direct Known Subclasses:
Dispatcher,MqttService,Parser,Protocol,Reactor,Writer
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final StringTopic suffix for the producer→sink address-discovery protocol.protected EventBusThe event bus used for communication between components.protected static final DateTimeFormatterShared timestamp formatter (seeTimer.formatter) available to all services for consistent serialization and parsing of timestamps.protected final LoggerLogger instance for this service, initialized with the class name. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringaddressRegistry(String group) Returns the address-discovery topic for the given producergroup.protected voidannounceAddress(String group, String address) Announces thataddressis an output channel of the producergroup, so that sinks subscribed viasubscribeToAnnouncements(String, Consumer)can discover and bind it.voidstop()Stops the service.protected voidsubscribeToAnnouncements(String group, Consumer<String> binder) Subscribes togroup's address-discovery topic, so this service learns each output channel the producer announces and can bind a handler to it.
-
Field Details
-
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(seeannounceAddress(String, String)). Sinks such as dispatchers and writers subscribe to that same topic (usingaddressRegistry(String)) to dynamically discover and bind the channels a producer emits. Thegroupis typically a device name or a logical producer id.- See Also:
-
eventBus
The event bus used for communication between components. -
logger
Logger instance for this service, initialized with the class name. -
formatter
Shared timestamp formatter (seeTimer.formatter) available to all services for consistent serialization and parsing of timestamps.
-
-
Constructor Details
-
Service
Creates a new service instance with the specified event bus.- Parameters:
eventBus- the event bus used for communication
-
-
Method Details
-
addressRegistry
Returns the address-discovery topic for the given producergroup.- Parameters:
group- device name or logical producer id- Returns:
- the topic sinks subscribe to in order to discover
group's output channels
-
announceAddress
Announces thataddressis an output channel of the producergroup, so that sinks subscribed viasubscribeToAnnouncements(String, Consumer)can discover and bind it.This call is synchronous with respect to binding. Every subscriber registered through
subscribeToAnnouncements(String, Consumer)has finished bindingaddressby 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 underaddress- the output channel name being announced- See Also:
-
subscribeToAnnouncements
Subscribes togroup'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:binderruns inline on the announcing thread, soannounceAddress(String, String)cannot return before every sink has bound. Registering for announcements witheventBus.register(addressRegistry(group), ..)instead leaves the binding to run on some other thread, and underDispatchMode.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 toaddressRegistry(String)directly.Threading:
binderruns on whichever thread calledannounceAddress(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 followbinder- 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.
-
