Class MessageRouter

  • All Implemented Interfaces:
    java.lang.Runnable

    public class MessageRouter
    extends java.lang.Object
    implements java.lang.Runnable
    The MessageRouter class facilitates intra-process message routing. It provides application classes with convenient access to the message channels via the PrintWriter class. The MessageRouter class, by default, defines 2 standard message channels - for standard output and standard error messages. Applications can publish their standard output and standard error messages using the outWriter() and errWriter() methods correspondingly.
    The MessageRouter class distributes the messages to a number of message channel listeners, added by applications. Standard message channel listeners should implement the MessageRouter.StdChannelListener interface. The MessageRouter class collects all published messages. When a new message channel listener is added, it receives all collected messages from the message history.

    The MessageRouter code runs in a separate thread that should be started and terminated by applications. Applications should use the start() and terminate() methods to start and terminate the MessageRouter thread correspondingly.
    For terminology see the Enterprise Integration Patterns book.

    • Field Detail

      • _errStream

        private java.io.StringWriter _errStream
      • _errBuffer

        private java.lang.StringBuffer _errBuffer
      • _errWriter

        private java.io.PrintWriter _errWriter
      • _errOffset

        private int _errOffset
      • _outStream

        private java.io.StringWriter _outStream
      • _outBuffer

        private java.lang.StringBuffer _outBuffer
      • _outWriter

        private java.io.PrintWriter _outWriter
      • _outOffset

        private int _outOffset
      • _terminated

        private boolean _terminated
      • _thread

        private java.lang.Thread _thread
      • _stdHistory

        private java.util.List<java.lang.String> _stdHistory
    • Constructor Detail

      • MessageRouter

        public MessageRouter()
        Default constructor for the MessageRouter class. This constructor allocates all resources, but does not start the main service thread. Applications should start the MessageRouter thread using the start() method.
    • Method Detail

      • addChannelListener

        public void addChannelListener​(MessageRouter.StdChannelListener listener)
        Adds a given object, implementing the StdChannelListener interface, to the list of standard message channel listeners. Sends to the new listener all previously collected messages for this channel.
        Parameters:
        listener - The given new standard message channel listener.
      • countStdChannelListeners

        public int countStdChannelListeners()
        Returns:
        Current number of standard channel listeners.
      • isRunning

        public boolean isRunning()
        Returns:
        true, if the router thread is running, false otherwise.
      • removeChannelListener

        public void removeChannelListener​(MessageRouter.StdChannelListener listener)
        Removes a given StdChannelListener object from the list of standard channel listeners.
        Parameters:
        listener - The StdChannelListener object to be removed from the list.
      • run

        public void run()
        Implements the main service method that runs in a separate thread.
        Specified by:
        run in interface java.lang.Runnable
      • errWriter

        public java.io.PrintWriter errWriter()
        Returns:
        The standard error message channel writer.
      • outWriter

        public java.io.PrintWriter outWriter()
        Returns:
        The standard output message channel writer.
      • start

        public void start()
        Starts the main service thread.
      • terminate

        public void terminate()
        Terminates the main service thread.