Reputation: 85
Right now we are creating map using the default option .getMap(). We do not use the getConfig().addMapConfig option as we want to avoid checking if map empty etc., to avoid calling this code everytime we need access to a map (client code abstracted by interface getMap()).
Now the question is at some time if we want to destroy certain maps we want to iterate through all the map names and destroy only the ones matching a certain criteria. Since we do not add to MapConfig we are not able to use getConfig().getMapConfigs().keyset() to get the map names.
Now we are trying to use .getDistributedObjects() to iterate through. Only worry is is the distributed object as a whole loaded in memory (i hope not..) or just the ID and names loaded into the DistributedObject reference (like proxy). I could not make out from the code.Is it a good idea to do this way or is there a better way to get the map object references. Please help
Upvotes: 4
Views: 12210
Reputation: 111
hzconn = hzconnection()
for obj in hzconn.client.get_distributed_objects():
if isinstance(obj, hazelcast.proxy.map.Map):
if not "__" in obj.name:
mapObj = hzconn.client.get_map(obj.name)
mapSize = mapObj.size().result()
if mapSize == 0:
print("{}: {} DESTROYED".format(obj.name, mapSize))
mapObj.destroy()
hzconn.client.shutdown()
Upvotes: 0
Reputation: 673
As noctarius stated (and if I understood correctly) it should be safe to iterate through the return of getDistributedObjects()
and then get the relevant data we want to process. Maybe something like this would do the job:
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.core.DistributedObject;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import java.util.Collection;
public class HazelcastMapsTest {
public static void main(String[] args) {
HazelcastInstance hazelcastInstance = HazelcastClient.newHazelcastClient();
Collection<DistributedObject> distributedObjects = hazelcastInstance.getDistributedObjects();
for (DistributedObject object : distributedObjects) {
if (object instanceof IMap) {
IMap map = hazelcastInstance.getMap(object.getName());
System.out.println("Mapname=" + map.getName());
map.entrySet().forEach(System.out::println);
}
}
hazelcastInstance.shutdown();
}
}
Upvotes: 6
Reputation: 6094
HazelcastInstance::getDistributedObjects just recalls all locally known proxy objects, however these are all objects known to the cluster. On clients those information are requested using a server node inside the cluster itself. Therefore it is safe to iterate those proxy objects and test their types.
Upvotes: 5