如何在 hazelcast 中显示所有结构名称 3.x

How to show all structure names in hazelcast 3.x

我找到了 a similar question,但这是 2 年前的事了,对于以前的版本,在 hz 3.x.So 中不起作用,我的问题是如何在 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;
        }
    }
}