1
votes

Is there any way to get all the listeners for an object in Hazelcast?

Here's my use case. I create a bunch of queues and attach one to many listeners to those queues. Over time, some of those listeners will be removed and I would like to delete the queue when there are no listeners left. I realize that I can maintain an external structure in my code that keeps track of all the queues and associated listeners but it would be nice if I could do this using Hazelcast itself. Ideally something like queue.getListeners() which would return back a list of listener ids.

I don't see any way to do anything like this in the Hazelcast documentation. Is there a better way to accomplish what I'm trying to do?

1

1 Answers

2
votes

You can access the listeners using the SPI. This is some example code when the listeners are triggered when something happens (e.g. a put).

 public void publishEvent(ItemEventType eventType, Data data) {
    EventService eventService = getNodeEngine().getEventService();
    Collection<EventRegistration> registrations = eventService.getRegistrations(getServiceName(), name);
    Address thisAddress = getNodeEngine().getThisAddress();
    for (EventRegistration registration : registrations) {
        QueueEventFilter filter = (QueueEventFilter) registration.getFilter();
        QueueEvent event = new QueueEvent(name, filter.isIncludeValue() ? data : null, eventType, thisAddress);
        eventService.publishEvent(getServiceName(), registration, event, name.hashCode());
    }
}

So what you can do is to create your own Operation and have it executed periodically on the member that owns the queue (queue isn't partitioned) and check the listener count. If no listeners and some kind of grace period has exceeded, you could destroy the queue.