Search code examples
hazelcast

Get all PN Counters in a hazelcast cluster


I would like to use PN Counters on Hazelcast to maintain Inventory numbers for fast lookup , but at the same time i would also like to clear them all from time to time and reload them as a "true up". Hazelcast allows PN Counter access by assigning a name but there is no way for a process to clear all the PN Counters in a cluster as there doesn't seem to be a client API to get a list of all PN Counters in the cluster.

How can i clear all PN counters in a cluster and start afresh ?


Solution

  • You can make use of the getDistributedObjects API, filter the PNCounters and destroy them.

    HazelcastInstance client = HazelcastClient.newHazelcastClient();
    client.getDistributedObjects()
            .stream()
            .filter(distributedObject -> distributedObject instanceof PNCounter)
            .forEach(DistributedObject::destroy);
    

    As the documentation of the linked API suggests, the list of distributed objects are returned on a best-effort basis, but it should work just fine most of the time.