Class Dispatcher

java.lang.Object
com.framed.core.Service
com.framed.io.dispatch.Dispatcher
Direct Known Subclasses:
FfillDispatcher, InfluxDispatcher, JsonlDispatcher

public abstract class Dispatcher extends Service
Drop-in replacement: - Never blocks EventBus handler on IO. - Retries on transient IO errors in a dedicated worker thread. - Adds bounded queue to avoid unbounded memory growth (configurable via system properties).
  • Constructor Details

    • Dispatcher

      protected Dispatcher(EventBus eventBus, org.json.JSONArray devices)
      Creates a dispatcher bound to the given event bus and set of devices.

      Starts a dedicated single-thread push worker and, for each device, subscribes to that device's address-discovery topic so that channels announced by the device are dynamically registered and forwarded to push(DataPoint).

      Parameters:
      eventBus - the event bus used to receive announced addresses and incoming messages
      devices - the device identifiers whose announced channels this dispatcher binds to
  • Method Details

    • onHandlerError

      protected void onHandlerError(String deviceID, String address, Object rawMsg, Exception e)
      Called when handler fails before enqueueing (e.g., parse error). Override to write dead-letter files, metrics, etc.
      Parameters:
      deviceID - the identifier of the device whose message failed to be handled
      address - the channel address the failing message arrived on
      rawMsg - the raw, unparsed message payload
      e - the exception raised while handling the message
    • onDrop

      protected void onDrop(DataPoint<?> dp, Throwable cause)
      Called when datapoint cannot be queued or pushed. Override for dead-letter storage / metrics.
      Parameters:
      dp - the datapoint that could not be queued or pushed
      cause - the reason the datapoint was dropped
    • shutdown

      public void shutdown(Duration drainTimeout)
      Optional: call this when stopping your service to stop worker thread. (Drop-in: doesn't require changes elsewhere, but recommended to call.)
      Parameters:
      drainTimeout - maximum time to wait for queued pushes to drain before forcing shutdown; if null, termination is not awaited
    • stop

      public void stop()
      Description copied from class: Service
      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.

      Overrides:
      stop in class Service
    • push

      public abstract void push(DataPoint<?> dataPoint) throws IOException
      Pushes a single datapoint to the underlying sink.

      Invoked on the dispatcher's worker thread. Implementations may throw IOException to signal a transient failure, which triggers retry with exponential backoff.

      Parameters:
      dataPoint - the datapoint to push
      Throws:
      IOException - if the push fails due to a transient IO error and should be retried
    • pushBatch

      public abstract void pushBatch(List<DataPoint<?>> batch)
      Pushes a batch of datapoints to the underlying sink.
      Parameters:
      batch - the datapoints to push