Class MessageBus

java.lang.Object
com.jbstrap.core.messagebus.MessageBus
Direct Known Subclasses:
Client, Component, UI

public class MessageBus extends Object

MessageBus implementation for the JBStrap framework

MessageBuses are the communication channels, that allows the components and clients to communicate with each other. On the MessageBus, any serializable class can be sent as a message. If a message is sent through a MessageBus, every class/component that is subscribed to that MessageBus, will receive the message.

The MessageBuses are sorted by their levels, depending on how their messages are sent. There are four levels: Global, Client, UI and Component level

Messages sent on the global level MessageBus are sent throughout the entire application, meaning that any class (be it a server side class, component, etc.) can listen to it. Messages like this aren't limited by the client, if a component is listening to the global MessageBus, it will receive the message, even if it was sent from another client. (As an example, this could be used to create a chat solution in an application.)

Messages sent on the client level MessageBus will only be sent received by listeners, that are connected to the same client (or extend from something that is connected to the client). Meaning that the component can only listen to a client level MessageBus, if it is from the same client.

Messages sent on the UI level MessageBus will only be sent and received by components, that are on the same UI (or extend from something that is connected to the UI). Only components can listen to a UI level MessageBus.

Messages sent on the component level MessageBus will only be sent and received within components. This means that for a component to receive the message, it must be assigned to that component (or extend from something that is connected to the component) .

The created MessageBuses must have a unique name on it's level. The MessageBus will be deleted, if the class containing the MessageBus is deleted, then the MessageBus will be deleted as well. This means that a Global level MessageBus (which is created through the JBStrap class will keep running until the application is shut down. A client level bus can be created on the client instance, and it will run as long as the client is running as well. This logic applies to every level.

Subscribing to the buses is done from the bottom towards the top (following hierarchy) by specifying the bus name. That is, if a component wants to subscribe to a bus, it must specify the bus name. If that bus is in the component, it will subscribe to it. If it is not found, it will try to look for it among the parent classes. If it's found there, it will subscribe, if not, it will continue looking for, along the hierarchy, to the top-level. If no component has the bus, then it will look for it on the UI, then the client. If still not found, then it will look among the global buses. If not found there, the subscribing will fail.

When a component is removed from a different component, or page, the component will automatically unsubscribe from the buses it subscribed to, with the exception of the global buses. If the component is placed on another component, it will automatically subscribe to the correct buses. This means that if a component is placed on another parent, it will change its buses too.

Since:
4.0
Author:
JBStrap
  • Constructor Details

    • MessageBus

      public MessageBus()
  • Method Details

    • openMessageBus

      public void openMessageBus(String busName)
      Opens a MessageBus
      Parameters:
      busName - The name of the MessageBus
      Throws:
      NullPointerException - Occurs if the name is null
      IllegalArgumentException - Occurs if the bus already exists in the system
    • messageBusOpened

      public boolean messageBusOpened(String busName)
      Determines if the specified message bus exists, and is opened.
      Parameters:
      busName - The name of the MessageBus
      Returns:
      true if yes, otherwise false
    • closeMessageBus

      public void closeMessageBus(String busName)
      Closes the specified MessageBus
      Parameters:
      busName - The name of the MessageBus
      Throws:
      NullPointerException - Occurs if the name is null
      IllegalArgumentException - Occurs if the message bus doesn't exist, or was closed previously.
    • subscribeMessageBus

      public void subscribeMessageBus(String busName, MessageBusListener listener)
      Subscribes to the specified message bus. If the message bus does not exist on the client level, subscribes to the global level message bus that has the same name.
      Parameters:
      busName - The name of the MessageBus
      listener - The message listener implementation
      Throws:
      NullPointerException - If the name or listener implementation is null
    • unsubscribeMessageBus

      public void unsubscribeMessageBus(String busName, MessageBusListener listener)
      Unsubscribes from the specified MessageBus
      Parameters:
      busName - The name of the MessageBus
      listener - The message listener implementation
      Throws:
      NullPointerException - If the name or listener implementation is null
    • sendMessageToMessageBus

      public void sendMessageToMessageBus(String busName, Serializable message)

      Sends a message on the specified message bus

      The process sends the message on the MessageBus that is the first in the hierarchy, that has the specified name. Meaning that if the specified bus is on the component, then the message will be sent through that bus. If the specified bus is not on the component, then it will look for it first on the parent component, then the UI, then on the client, and lastly among the global level buses. The message will be sent on the first bus, that is encountered in the hierarchy, that has the specified name.

      If the bus is not found anywhere, the message won't be sent.

      Parameters:
      busName - The name of the MessageBus
      message - The message to be sent. Can be any serializable class.
      Throws:
      NullPointerException - If the name is null
    • unsubscribeAllListeners

      protected void unsubscribeAllListeners()
    • renewSubscription

      protected void renewSubscription()