How to check whether any ItemListener is added to Hazelcast IQueue or not

From instance A, I have created one q1(IQueue) also added ItemListener to it. Now from Instace B I need to check, is there any ItemListener added to the same queue. If not then needs to added.

So programatically how can we check whether ItemListener added to IQueue or not ?

Updated with usecase

In our Ecommerce site we are usign this for flashsale

When admin configure an item for flashsale : we will create queue(IQueue) for that item also bind one ItemListener to it

item Queue role:- When user buy n quantity of that item, we will add n number of flag to that queue.

ItemListener role :- on itemAdded we will compare available stock and size of queue, make item out of stock if queue size is reached to available stock.

goal :- There should be a single ItemListener for particuler item acrross the application nodes

Answers


It sounds to me that you want to guarantee only one listener to be added to a queue. Why not use the oldest member (the privileged cluster member) to register the listener? Do you need any kind of simple load balancing?import

import com.hazelcast.core.*;
import java.util.UUID;

public class OneListenerPerQueue {

    public static void main(String[] args) {
        // Create Hazelcast instance
        HazelcastInstance hz = Hazelcast.newHazelcastInstance();

        // Add a basic distributed object listener
        hz.addDistributedObjectListener(new QueueListener(hz));

        // Create 100 unique queues
        for (int i = 0; i < 100; i++) {
            String uniqueQueue = UUID.randomUUID().toString();
            hz.getQueue(uniqueQueue);
        }
    }

    private static class QueueListener
            implements DistributedObjectListener {

        private final PartitionService partitionService;

        private QueueListener(HazelcastInstance hz) {
            this.partitionService = hz.getPartitionService();
        }

        public void distributedObjectCreated(DistributedObjectEvent distributedObjectEvent) {
            // DistirbutedObject from the event
            DistributedObject distObj = distributedObjectEvent.getDistributedObject();

            // If queue ask PartitionService if the name is assigned to the local member
            if (distObj instanceof IQueue) {
                Partition partition = partitionService.getPartition(distObj.getName());
                if (partition.getOwner().localMember()) {
                    // If local than add our ItemListener
                    ((IQueue) distObj).addItemListener(new MyItemListener(), true);
                }
            }
        }

        public void distributedObjectDestroyed(DistributedObjectEvent distributedObjectEvent) {
        }
    }

    private static class MyItemListener implements ItemListener<String> {

        public void itemAdded(ItemEvent<String> itemEvent) {
            System.out.println(itemEvent);
        }

        public void itemRemoved(ItemEvent<String> itemEvent) {
            System.out.println(itemEvent);
        }
    }
}

Need Your Help

instanceof Double/Object won't work :(

java instanceof

I've got a problem with java's instanceof. Here's a gap of code that causes me trouble:

WPF - What to do when a Dependency Property is another Control in the XAML

wpf xaml controls dependency-properties behavior

I'm hoping this will be my last question today. I'm in a hurry and google is not helping much (that or I'm searching the wrong places).