0

I have a similar question as this post: Consume message only once from Topic per listeners running in cluster

When I tried using a queue to publish messages and added an item listener in two different JVMs, I am receiving the messages twice in both of them. I want to receive the message only once in a clustered/distributed environments.

Here's my code snippet:

Publishing of the message:

getQueue().add("some sample message");

I have the same listener configured in two different JVMs which goes like this:

public HazelcastQueueListener(){
    HazelcastInstance instance = HazelcastClient.newHazelcastClient(HazelClientConfig.getClientConfig());
    IQueue<String> queue1 = instance.getQueue("SAMPLEQUEUE");
    queue1.addItemListener(this, false);
}

public static void main(String args[]){
    HazelcastQueueListener listener = new HazelcastQueueListener();
}

@Override
public void itemAdded(ItemEvent<String> arg0) {
    // TODO Auto-generated method stub
    if(arg0!=null){
        System.out.println("Item coming out of queue 1" +arg0);
    }
    else{
        System.out.println("null");
    }

}
Community
  • 1
  • 1
user2957656
  • 11
  • 2
  • 5

1 Answers1

2

You have to poll the queue, like a standard java BlockingQueue in order to consume an item only once.

String item = queue1.take()

AFAIK, Hazelcast doesn't support asynchronous operation on queue. The ItemListener doesn't consume the item, it only notifies that an item is available.

Jérémie B
  • 10,611
  • 1
  • 26
  • 43