我找到了a similar question,但它是2年前的旧版本,在hz 3.x中不起作用,所以我的问题是如何在hz 3.x中实现?

像APP1打开锁1,APP2打开锁2一样,我怎么知道已经采取的所有锁?

最佳答案

找到了许多技巧,可以与任何服务一起使用

首先运行svr,然后运行cli并测试。

public class PlayHZ
{
    @Test
    public void test() throws ExecutionException, InterruptedException
    {
        HazelcastInstance hz = Hazelcast.newHazelcastInstance();
        IExecutorService executor = hz.getExecutorService("gather");
        Map<Member, Future<List<String>>> futures = executor.submitToAllMembers(new LockNameGather());
        Thread.sleep(1000);
        System.out.println("Found in member");
        for (Map.Entry<Member, Future<List<String>>> entry : futures.entrySet())
        {
            System.out.printf("%s -> %s\n", entry.getKey(), entry.getValue().get());
        }
    }

    @Test
    public void svr() throws InterruptedException
    {
        HazelcastInstance hz = Hazelcast.newHazelcastInstance();
        Thread.sleep(Integer.MAX_VALUE);
    }

    @Test
    public void cli() throws InterruptedException
    {
        HazelcastInstance hz = Hazelcast.newHazelcastInstance();
        for (int i = 0; i < 100; i++)
        {
            ILock lock = hz.getLock("LOCK" + i);
            lock.lock();
        }
        Thread.sleep(Integer.MAX_VALUE);
    }

    private static class LockNameGather implements Callable<List<String>>, HazelcastInstanceAware, Serializable
    {
        transient HazelcastInstance hz;
        transient NodeEngineImpl node;

        @Override
        public List<String> call() throws Exception
        {
            node = ((HazelcastInstanceImpl) hz).node.getNodeEngine();
            LockService svc = node.getService(LockService.SERVICE_NAME);

            return svc.getAllLocks()
                      .stream()
                      .map(LockResource::getKey)
                      .map(d -> node.getSerializationService().createObjectDataInput(d))
                      .map((r) -> {
                          try
                          {
                              return r.readUTF();
                          } catch (IOException e)
                          {
                              throw new RuntimeException(e);
                          }
                      }).collect(Collectors.toList());
        }

        @Override
        public void setHazelcastInstance(HazelcastInstance hazelcastInstance)
        {
            hz = hazelcastInstance;
        }
    }
}

10-06 12:54