有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在hazelcast中从地图中取出第一把未锁定的钥匙?

如何在hazelcast中从地图中取出第一把未锁定的钥匙

有人告诉我,最简单的方法是随机选择,然后检查它是否被阻止(),但我甚至不能随机选择一个,我试图像这样解决问题

但是一段接受随机密钥的代码对我来说不起作用,它会抛出一个错误java.lang.ArrayStoreException: java.util.AbstractMap $ SimpleImmutableEntry

我很担心这件事

Random random = new Random();
String[] keys = (String[]) hazelcastMap.entrySet().toArray(new String[0]);
String key = keys[random.nextInt(keys.length)];

完整代码

private String getNoLockedKey(HazelcastInstance hazelcastInstance, String mapName) {
    IMap hazelcastMap = hazelcastInstance.getMap(mapName);
    if (!hazelcastMap.isEmpty()) { //checking if the map is empty
        Random random = new Random();
        String[] keys = (String[]) hazelcastMap.entrySet().toArray(new String[0]);
        String key = keys[random.nextInt(keys.length)];

        try {
            hazelcastMap.lock(key);
            //  if blocked successfully and there is no execution, then delete it from the map
            hazelcastMap.remove(key);
            
            return key;
        } catch (Exception e) {
            return getNoLockedKey(hazelcastInstance, mapName);
        }

    } else {
        updateCache(mapName); //if the map is empty, then we request new data from the database and update the cache

        return getNoLockedKey(hazelcastInstance, mapName);
    }

}

我也尝试过QueryCahe,但不知道如何将其应用到我的任务中,请帮助我


共 (0) 个答案