Class SyslogServer<T extends reactor.netty.DisposableChannel>

java.lang.Object
org.openehealth.ipf.commons.audit.server.SyslogServer<T>
Type Parameters:
T -
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
TlsSyslogServer, UdpSyslogServer

public abstract class SyslogServer<T extends reactor.netty.DisposableChannel> extends Object implements Closeable
Abstract base class for TLS and UDP syslog servers. Received Syslog frames must be handled by a consumer; the frames are parsed into a Map with keys as specified in SyslogConstants. If parsing fails (e.g. due to incomplete syslog frames, the map is populated with the raw content and the exception that has been thrown by the parser.

Messages that cannot be parsed at all, and exceptions thrown by the consumer, are passed to the error consumer; they do not affect the handling of other messages. Messages are handled concurrently, so the consumer must be thread-safe and must not rely on the order in which messages were sent.

One example for a consumer is SyslogEventCollector that is practical for tests. With this respect, note that SyslogServer is Closeable, so it can easily be started and stopped by using a try-with-resources statement.

Since:
4.0
Author:
Christian Ohr
  • Field Details

    • channel

      protected T extends reactor.netty.DisposableChannel channel
    • consumer

      protected final Consumer<? super Map<String,Object>> consumer
    • errorConsumer

      protected final Consumer<Throwable> errorConsumer
    • timeoutSeconds

      protected int timeoutSeconds
  • Constructor Details

    • SyslogServer

      public SyslogServer(Consumer<? super Map<String,Object>> consumer, Consumer<Throwable> errorConsumer)
      Parameters:
      consumer - consumer for handled syslog frames
      errorConsumer - consumer for errors
  • Method Details

    • start

      public SyslogServer<T> start(String host, int port)
      Starts the server and returns when started within 10 seconds. Requests are handled on the receiver thread.
      Parameters:
      host - exposed host
      port - port
      Returns:
      this instance
    • address

      public SocketAddress address()
    • doStart

      protected abstract SyslogServer<T> doStart(String host, int port)
    • stop

      public void stop()
      Stops the server if it was started before, and returns when stopped within 10 seconds.
    • handleMessage

      protected reactor.core.publisher.Mono<Object> handleMessage(Object message)
      Handles a message received from the channel: a parsed syslog message is passed to the consumer, anything else (e.g. a DecoderException for a message that could not be parsed) to the error consumer.
      Parameters:
      message - received message
      Returns:
      completion of the handling, never failing
      Since:
      6.0
    • handleMap

      protected reactor.core.publisher.Mono<Object> handleMap(Map<String,Object> map)
      Asynchronously consumes syslog records. This trick is described here. It generates a Mono from a potentially blocking call and subscribes using a scheduler. An exception thrown by the consumer is passed to the error consumer.
      Parameters:
      map - syslog map
      Returns:
      nothing
    • handleError

      protected reactor.core.publisher.Mono<Object> handleError(Throwable throwable)
      Parameters:
      throwable - error that occurred while receiving or handling a message
      Returns:
      completion of passing the error to the error consumer
      Since:
      6.0
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • setTimeoutSeconds

      public void setTimeoutSeconds(int timeoutSeconds)