Class MessageCenter

java.lang.Object
xal.tools.messaging.MessageCenter
All Implemented Interfaces:
Serializable

public class MessageCenter extends Object implements Serializable
MessageCenter provides an interface to the messaging system with lots of convenience methods for easy access to messaging features.
Author:
tap
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Generic class for all MessageCenter related exceptions
    class 
    Exception when an attempt is made to register a null source
    class 
    Exception when an attempt is made to register a null source
    class 
    Exception when an attempt is made to register a target for a protocol not implemented by its class.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    forward events from a new thread
    static final int
    forward fresh events (drop old unprocessed pending events) on a common thread per protocol
    static final int
    forward events on the invoking thread
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates new MessageCenter
    MessageCenter(int threadPoolSize)
    Create a new MessageCenter with given thread pool size
    Create a new MessageCenter with given name
    MessageCenter(String newName, int newThreadPoolSize)
    Create a new MessageCenter with given name and thread pool size
  • Method Summary

    Modifier and Type
    Method
    Description
    default message center instance
    <T> T
    getProxy(Object source, Class<T> protocol)
    get the proxy for the specified source and protocol
    get the name of the MessageCenter instance
    Create a new MessageCenter
    newCenter(int newThreadPoolSize)
    Create a new MessageCenter
    newCenter(String newName)
    Create a new MessageCenter
    newCenter(String newName, int newThreadPoolSize)
    Create a new MessageCenter
    <T> T
    registerSource(Object source, Class<T> protocol)
    register the specified source to be associated with the specified event protocol defaults to synchronous messaging
    <T> T
    registerSource(Object source, Class<T> protocol, boolean isSynchronous)
    Deprecated.
    Use the version of this method that takes the synchronous type instead
    <T> T
    registerSource(Object source, Class<T> protocol, int synchronousType)
    Register the specified source to be associated with the specified event protocol and processed as the synchronous type
    <T> void
    registerTarget(T target, Class<T> protocol)
    register target for messages from any source which posts to the interface
    <T> void
    registerTarget(T target, Object source, Class<T> protocol)
    register target for messages from the source and for the specified interface
    <T> void
    removeSource(Object source, Class<T> protocol)
    Remove source registration which means the proxy for the source/protocol pair is no longer able to broadcast messages.
    <T> void
    removeTarget(T target, Class<T> protocol)
    Removes the target from listening for the specified protocol.
    <T> void
    removeTarget(T target, Object source, Class<T> protocol)
    remove target from listening to specified source with specified protocol
    <T> void
    removeTarget(T target, Collection<? extends Object> sources, Class<T> protocol)
    Unregister the target from listening to the specified protocol from the specified collection of sources.
    <T> void
    removeTargetFromAllSources(T target, Class<T> protocol)
    Remove the target from every source that broadcasts the specified protocol
    Override toString() to return a description of the message center.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • SYNCHRONOUS

      public static final int SYNCHRONOUS
      forward events on the invoking thread
      See Also:
    • ASYNCHRONOUS

      public static final int ASYNCHRONOUS
      forward events from a new thread
      See Also:
    • FRESH

      public static final int FRESH
      forward fresh events (drop old unprocessed pending events) on a common thread per protocol
      See Also:
  • Constructor Details

    • MessageCenter

      public MessageCenter()
      Creates new MessageCenter
    • MessageCenter

      public MessageCenter(String newName)
      Create a new MessageCenter with given name
      Parameters:
      newName - The name of this Message Center.
    • MessageCenter

      public MessageCenter(int threadPoolSize)
      Create a new MessageCenter with given thread pool size
      Parameters:
      threadPoolSize - The thread pool size of this message center
    • MessageCenter

      public MessageCenter(String newName, int newThreadPoolSize)
      Create a new MessageCenter with given name and thread pool size
      Parameters:
      newName - The name of this message center
      newThreadPoolSize - The thread pool size of this message center
  • Method Details

    • defaultCenter

      public static MessageCenter defaultCenter()
      default message center instance
      Returns:
      the default message center
    • newCenter

      public static MessageCenter newCenter()
      Create a new MessageCenter
      Returns:
      a new message center
    • newCenter

      public static MessageCenter newCenter(String newName)
      Create a new MessageCenter
      Parameters:
      newName - the name to assign this message center
      Returns:
      a new message center
    • newCenter

      public static MessageCenter newCenter(int newThreadPoolSize)
      Create a new MessageCenter
      Parameters:
      newThreadPoolSize - The thread pool size of this message center
      Returns:
      a new message center
    • newCenter

      public static MessageCenter newCenter(String newName, int newThreadPoolSize)
      Create a new MessageCenter
      Parameters:
      newName - The name of this message center
      newThreadPoolSize - The thread pool size of this message center
      Returns:
      a new message center
    • registerTarget

      public <T> void registerTarget(T target, Object source, Class<T> protocol)
      register target for messages from the source and for the specified interface
      Parameters:
      target - The target to receive messages
      source - The source from which we wish to receive messages
      protocol - The protocol identifying the message type to receive
    • registerTarget

      public <T> void registerTarget(T target, Class<T> protocol)
      register target for messages from any source which posts to the interface
      Parameters:
      target - The target to receive messages
      protocol - The protocol identifying the message type to receive
    • removeTarget

      public <T> void removeTarget(T target, Object source, Class<T> protocol)
      remove target from listening to specified source with specified protocol
      Parameters:
      target - The target receiving messages
      source - The source from which we are receiving messages
      protocol - The protocol identifying the message type being received
    • removeTargetFromAllSources

      public <T> void removeTargetFromAllSources(T target, Class<T> protocol)
      Remove the target from every source that broadcasts the specified protocol
      Parameters:
      target - The target receiving messages
      protocol - The protocol identifying the message type being received
    • removeTarget

      public <T> void removeTarget(T target, Collection<? extends Object> sources, Class<T> protocol)
      Unregister the target from listening to the specified protocol from the specified collection of sources.
      Parameters:
      target - The target receiving messages
      sources - The sources from which we are receiving messages
      protocol - The protocol identifying the message type being received
    • removeTarget

      public <T> void removeTarget(T target, Class<T> protocol)
      Removes the target from listening for the specified protocol. It only removes the target from listening to the protocol from anonymous sources. For example, it unregisters a call of registerTarget(target, protocol). This method does NOT remove the target from listening to directly registered sources.
      Parameters:
      target - The target receiving messages
      protocol - The protocol identifying the message type being received
    • registerSource

      public <T> T registerSource(Object source, Class<T> protocol)
      register the specified source to be associated with the specified event protocol defaults to synchronous messaging
      Parameters:
      source - The source of the message
      protocol - The type and interface of the messages the source will send
      Returns:
      The proxy (implementing protocol) to call to broadcast messages
    • registerSource

      @Deprecated public <T> T registerSource(Object source, Class<T> protocol, boolean isSynchronous)
      Deprecated.
      Use the version of this method that takes the synchronous type instead
      Register the specified source to be associated with the specified event protocol and using synchronous (true) or asynchronous messaging (false)
      Parameters:
      source - The source of the message
      protocol - The type and interface of the messages the source will send
      isSynchronous - true to enable synchronous messaging and false for asynchronous messaging
      Returns:
      The proxy (implementing protocol) to call to broadcast messages
    • registerSource

      public <T> T registerSource(Object source, Class<T> protocol, int synchronousType)
      Register the specified source to be associated with the specified event protocol and processed as the synchronous type
      Parameters:
      source - The source of the message
      protocol - The type and interface of the messages the source will send
      synchronousType - one of SYNCHRONOUS, ASYNCHRONOUS or FRESH to indicate how events are processed
      Returns:
      The proxy (implementing protocol) to call to broadcast messages
    • removeSource

      public <T> void removeSource(Object source, Class<T> protocol)
      Remove source registration which means the proxy for the source/protocol pair is no longer able to broadcast messages. The handler for this source/protocol pair will be removed.
      Parameters:
      source - The source of messages
      protocol - The interface/type of messages
    • getProxy

      public <T> T getProxy(Object source, Class<T> protocol)
      get the proxy for the specified source and protocol
      Parameters:
      source - The source on behalf of which messages will be broadcast
      protocol - The interface/type of message implemented by the proxy
      Returns:
      The proxy (implementing protocol) to call on behalf of the source to broadcast messages
    • name

      public String name()
      get the name of the MessageCenter instance
      Returns:
      the name of the message center
    • toString

      public String toString()
      Override toString() to return a description of the message center. At this point it simply returns the name of the message center.
      Overrides:
      toString in class Object
      Returns:
      the description of the MessageCenter instance