<p>Currently, I have two microservices. I want to send message to KubeMQ Queue from first microservice and want it to be received by Second microservice. I am able to send message to a KubeMQ Queue using below code:</p>
<pre><code>Queue queue = new Queue(&quot;QueueName&quot;, &quot;ClientID&quot;, &quot;localhost:50000&quot;);
  SendMessageResult resSend = queue.SendQueueMessage(new Message()
          .setBody(Converter.ToByteArray(&quot;some-simple_queue-queue-message&quot;))
          .setMetadata(&quot;someMeta&quot;));
  if (resSend.getIsError()) {
      System.out.printf(&quot;Message enqueue error, error: %s&quot;, resSend.getError());
  }
</code></pre>
<p>I need Listener in the second microservice in order to receive the message from Queue.
Below is code provided by KubeMQ to receive the message:</p>
<pre><code> Queue queue = new Queue(&quot;QueueName&quot;, &quot;ClientID&quot;, &quot;localhost:50000&quot;);
  ReceiveMessagesResponse resRec = queue.ReceiveQueueMessages(10, 1);
  if (resRec.getIsError()) {
      System.out.printf(&quot;Message dequeue error, error: %s&quot;, resRec.getError());
      return;
  }
  System.out.printf(&quot;Received Messages %s:&quot;, resRec.getMessagesReceived());
  for (Message msg : resRec.getMessages()) {
      System.out.printf(&quot;MessageID: %s, Body:%s&quot;, msg.getMessageID(), Converter.FromByteArray(msg.getBody()));
  }
</code></pre>
<p>How to configure it in the second microservice to receive message instantly as they are added into the queue?</p>
<p>Please help.</p>

<blockquote>
<p>I need a Listener in the second microservice in order to receive the message from Queue.</p>
</blockquote>
<p>Why polling, when you can be notified through the <a href="https://docs.kubemq.io/learn/message-patterns/pubsub" rel="nofollow noreferrer">KubeMQ Pub/Sub pattern</a>?</p>
<p><a href="https://i.stack.imgur.com/0Y2hd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0Y2hd.png" alt="https://3720647888-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M2b9dwAGbMPWty0fPGr%2F-M2cg-nL53EVg5rPnOZB%2F-M2chJWo9-utUu1Q8v2C%2Fpubsub.png?alt=media&amp;token=e003b332-df67-4ffb-be81-6b9218cefc4e" /></a></p>
<p>In the context of message queues, &quot;polling&quot; refers to a process where your application continually checks the queue to see if a new message has arrived. This can be inefficient, as it requires your application to make many requests when there may not be any new messages to process.</p>
<p>On the other hand, a &quot;listener&quot; (also known as a subscriber or a callback) is a function that is automatically called when a new message arrives. This is more efficient because your application does not need to continually check the queue; instead, it can wait and react when a message arrives.</p>
<hr />
<p>The Publish-Subscribe pattern (or pub/sub) is a messaging pattern supported by KubeMQ, and it differs slightly from the queue-based pattern you are currently using.<br />
In the pub/sub pattern, senders of messages (publishers) do not program the messages to be sent directly to specific receivers (subscribers). Instead, the programmer “publishes” messages (events), without any knowledge of any subscribers there may be. Similarly, subscribers express interest in one or more events and only receive messages that are of interest, without any knowledge of any publishers.</p>
<p>In this pattern, KubeMQ provides two types of event handling, <code>Events</code> and <code>Events Store</code>.</p>
<ul>
<li><p>The <code>Events</code> type is an asynchronous real-time Pub/Sub pattern, meaning that messages are sent and received in real-time but only if the receiver is currently connected to KubeMQ. There is no message persistence available in this pattern.</p>
</li>
<li><p>The <code>Events Store</code> type, however, is an asynchronous Pub/Sub pattern with persistence. This means that messages are stored and can be replayed by any receiver, even if they were not connected at the time the message was sent.<br />
The system also supports replaying all events from the first stored event, replaying only the last event, or only sending new events.</p>
</li>
</ul>
<p>However, it is important to note that the uniqueness of a client ID is essential when using Events Store.<br />
At any given time, only one receiver can connect with a unique Client ID.<br />
If two receivers try to connect to KubeMQ with the same Client ID, one of them will be rejected. Messages can only be replayed once per Client ID and Subscription type. If a receiver disconnects and reconnects with any subscription type, only new events will be delivered for this specific receiver with that Client ID. To replay messages, a receiver needs to connect with a different Client ID.</p>
<p>Given these features, if you switch your architecture to a pub/sub pattern using the Events Store type, your second microservice could instantly receive messages as they are added into the channel, and even replay old messages if needed. You would need to ensure each microservice has a unique Client ID and manages its subscriptions appropriately.</p>
<p>However, the pub/sub pattern may require changes in the architecture and coding of your microservices, so you would need to evaluate whether this change is suitable for your use case. It is also important to note that the pub/sub pattern, especially with message persistence, may have different performance characteristics and resource requirements compared to the queue pattern.</p>
<hr />
<p>Here is a high-level overview of the classes that are present and their usage:</p>
<ol>
<li><p><code>Channel.java</code>: This class appears to represent a channel for sending events in a publish-subscribe model.</p>
</li>
<li><p><code>ChannelParameters.java</code>: This class defines the parameters for creating a Channel instance.</p>
</li>
<li><p><code>Event.java</code>: This class represents an event that can be sent via a Channel.</p>
</li>
<li><p><code>EventReceive.java</code>: This class is used to process received events.</p>
</li>
<li><p><code>Result.java</code>: This class contains the result of a sent event.</p>
</li>
<li><p><code>Subscriber.java</code>: This class allows you to subscribe to a channel and handle incoming events.</p>
</li>
</ol>
<p>So here is an example of how you might use the existing classes to publish and subscribe to messages.</p>
<pre class="lang-java prettyprint-override"><code>import io.kubemq.sdk.Channel;
import io.kubemq.sdk.ChannelParameters;
import io.kubemq.sdk.Result;
import io.kubemq.sdk.event.Event;
import io.kubemq.sdk.event.Subscriber;

public class KubeMQExample {

    public static void main(String[] args) {
        try {
            // Initialize ChannelParameters
            ChannelParameters params = new ChannelParameters();
            params.setChannel(&quot;your_channel&quot;);
            params.setClient(&quot;your_client_id&quot;);

            // Initialize a new Channel
            Channel channel = new Channel(params);

            // Create a new Event
            Event event = new Event();
            event.setBody(&quot;Your message here&quot;.getBytes());

            // Send the Event
            Result sendResult = channel.SendEvent(event);
            System.out.println(&quot;Event sent, Result: &quot; + sendResult.getIsError());

            // Initialize a new Subscriber
            Subscriber subscriber = new Subscriber(&quot;localhost:5000&quot;);

            // Subscribe to the Channel
            subscriber.SubscribeToEvents(params, (eventReceive) -&gt; {
                System.out.println(&quot;Received Event: &quot; + new String(eventReceive.getBody()));
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
</code></pre>
<p>Do note that this code is based on the existing SDK and may not reflect the functionality of the original code. You will need to replace &quot;<code>your_channel</code>&quot; and &quot;<code>your_client_id</code>&quot; with your actual channel name and client ID. The event body can also be replaced with the actual message you want to send.</p>
<p>The <code>Subscriber</code> class is used here to listen for and process incoming events. The <code>SubscribeToEvents</code> method takes a <code>ChannelParameters</code> object and a lambda function that processes received events.</p>
<p>Do also note that the <code>Queue</code> and <code>EventsStore</code> classes seem to have been removed from the SDK. The SDK now seems to primarily use the publish-subscribe model, which differs from queue-based communication in that messages are not stored if no consumer is available to consume them.<br />
Events Store was a hybrid model that allowed for persistence in the pub/sub model, storing events that could be replayed by receivers connecting at a later time.</p>
<p>For your original functionality of reading queue messages and peeking at messages in a queue, unfortunately, it does not seem like the current state of the Kubemq Java SDK on the provided GitHub repository supports these actions.</p>
