Using the JBStrap MessageBus

If you subscribe to a channel, you will need to implement the method that processes messages on the channel. You can do this by implementing one of the MessageBusListener or SyncMessageBusListener interfaces. In the case of implementing the MessageBusListener interface, we create an asynchronous listener, and in the case of SyncMessageBusListener , we create a synchronous listener. Both cases work in the same way but differ only in the processing time of the message. If the listener is asynchronous, it can process the message parallel to other listener methods. With this method, you can achieve faster operation by keeping each listener on its own thread, without having to wait for each other. If you implement a synchronous listener, the listeners will be executed one after the other on the same thread. That is, the second listener receives the message only after the first listener method has completed processing the message.

If you implement a listener, you must implement a method that receives the name of the channel on which the message was received and the object containing the message in the parameter. Even if you are listening to multiple channels at the same time, you only need to implement one listener, which must be passed to when you subscribe.

An example of a listener implementation:

In this example, if the listener receives a message through the sampleBus1 channel, it will display it on the interface. Messages from other channels are ignored.

Once you've implemented the listener, all you have to do is subscribe to one or more channels on which you want to listen to messages. If you know which channel level you want to subscribe to, you can do so in one of the following ways:

Subscribing to a global-level message bus:
Subscribing to a client-level message bus:
Subscribing to a component-level message bus:

More often than not, we want to take advantage of the hierarchy between individual channels. In this case, you do not need to know where a specific channel is located, as the listener will subscribe to a channel closer to the component. Details are provided in the Message bus / Message bus hierarchy section. So let's take a look at an example where we create a button. We want this button to subscribe to a message bus called sampleBus1 . The message bus will receive a logical value as a message. If this logical value is true, the button is enabled, if it has a false value, the button is disabled.

Implementing a button, a listener and subscription:

Opening the sampleBus1 bus is not included in the example code because the bus must be opened on another component where we want to place our own button. In this case, the bus can be placed on a parent component, pages or UI, possibly on the client, or it can even be a global channel. Wherever the bus is, it will subscribe to the channel. If you have multiple buses, the bus closest to our button will be subscribed to. So, for example, if the bus named sampleBus1 exists on the page, in the client and even as a global channel, then the page channel will be subscribed to. If you place the button on a page that has no open channel, it will subscribe to the channel open in the client.

Sending messages

You can send any object as a message, as long as it is serializable, and implements the serializable interface. Every type in Java (such as String, Long, etc.) is serializable, so you can send them as messages. The Record object is also serializable, so you can send a Record as a message through a MessageBus . You can also create your own POJO class, that you can use as a message, as long as it implements the Serializable interface.

Sending a custom made POJO class as a message, through a MessageBus:

You can also send messages in multiple ways. If you know the level of the MessageBus where you want to send the message, you can do it like this:

Sending a message through a Global level MessageBus:
Sending a message through a Client level MessageBus:
Sending a message through a Component level MessageBus:

You may also want to make use of the hierarchy between certain buses. In this case, you don’t have to know where the channel is, since the message is sent to the channel that is the closest to the component. This is further explained in the Message Bus hierarchy section. So for example, you create a page, where you open a MessageBus : sampleBus1 . You place a button (which was created in the previous chapter ) which is listening to the opened channel.

Creating a page, which opens a channel, and the component that was placed on it, is listening to the channel. The sending is done by another button, which will send a logic value to the bus, as a message:

If you display the page in the above example, you will see two buttons. One will have the “Send” text on it, the other will have “Target”. If you click on the Send button, it will send a message through the sampleBus1 MessageBus . The message will be a false logic value. The other button will be listening to this channel, which will switch over to be inactive when it receives the message.

As you can see, the MessageBus ’ level was not specified, so the message will be sent to the external component, through the channel which is closest to it in the hierarchy. In this case, first, the external button is examined first. The framework checks if there is a channel, but since it doesn’t find one, it will proceed up the hierarchy. The external button’s parent is the page, which does have a channel so that one will be used.

Unsubscribing from a message bus

You may also need to manually unsubscribe components from MessageBus es. This is done automatically in most cases, but you may need to do this, without removing, replacing or disabling a component, and want to just unsubscribe it. You can unsubscribe components like this:

Unsubscribing from a global bus:
Unsubscribing from a client bus:
Unsubscribing from a component bus: