总订阅了hazelcast主题

时间:2015-01-12 14:41:58

标签: java hazelcast

我想问一下,是否有可能通过Hazelcast获得在全集范围内订阅主题的总数以及如何?

e.g。

ITopic topic = hazelcastInstance.getTopic("foo");

long totalSubscribed = topic.getSubscribed()

谢谢

2 个答案:

答案 0 :(得分:0)

至少有两种方法可用于查找主题订阅者的数量。

  1. 使用公共API并获取群集Member的列表。
  2. 使用Hazelcast SPI并提供可以访问内部Hazelcast的ManagedService
  3. 这两种方法都运行良好,但后者涉及更多编码和SPI的良好知识,而这种方法并不常用。

    方法1:找到群集Member s 要查找(当前)群集的所有成员,您可以使用以下代码:

    final Set<Member> members = hazelcastInstance.getCluster().getMembers();
    

    以上内容会返回SetMember个对象,这些对象已经过详尽描述here

    在群集中启动Member时,您可以设置自定义属性。这使您可以为自己开始的Member添加自己的逻辑。

    以下测试用例说明了如何使用它:

    public class TopicCounterTest {
        // Setup a hazelcast instance, bind the topic name as a custom property
        public HazelcastInstance setupHazelcastInstance(final String topicName) {
            // Set the custom property (in this case a boolean bound to the topic name)
            final MemberAttributeConfig topicMemberConfig = new MemberAttributeConfig();
            topicMemberConfig.setBooleanAttribute(topicName, true);
    
            final Config config = new Config();
            config.setMemberAttributeConfig(topicMemberConfig);
            config.setProperty("hazelcast.initial.min.cluster.size", "1");
    
            // Create a HazelcastInstance
            return Hazelcast.newHazelcastInstance(config);
        }
    
        // Use a Java 8 stream to count all "Member"s that have the 
        // custom property set
        public long numberOfTopicListeners(
                final HazelcastInstance hazelcastInstance, 
                final String topicName) {
    
            return hazelcastInstance.getCluster().getMembers().stream()
                    .map(member -> {
                        final Map<String, Object> attributes = member.getAttributes();
                        return Optional.ofNullable(
                                member.getBooleanAttribute(topicName))
                                .orElse(false);
                    })
                    .count();
        }
    
        // Test case, start two instances and verify that they both are listeners on the topic
        @Test
        public void testTopicCounter() {
            final HazelcastInstance h1 = setupHazelcastInstance("testTopic");
            final HazelcastInstance h2 = setupHazelcastInstance("testTopic");
    
            Assert.assertEquals(2l, numberOfTopicListeners(h1, "testTopic"));
            Assert.assertEquals(2l, numberOfTopicListeners(h2, "testTopic"));
        }
    }
    

    方法2:使用Hazelcast SPI并提供自定义ManagedService

    另一种方法要求您使用Hazelcast SPI(服务提供程序接口)。它需要比第一种方法更多的代码,除此之外,您必须提供ManagedService,注册该服务,然后通过回调获取NodeEngineNodeEngine有一个EventService句柄,您可以使用它来调用方法getRegistrations

    SPI方法的一个问题是它需要SPI更多代码,您需要找到一种在托管服务和应用程序之间共享数据的方法。如何使用SPI在Hazelcast website

    中有所描述

    <强>建议

    我的建议是使用第一种方法。利用公共API:s,只需注册一个可用于建立逻辑的自定义属性。简单,快速,安全。

答案 1 :(得分:0)

您可以访问EventService(SPI的一部分)并调用以下方法:

Collection<EventRegistration> getRegistrations(String serviceName, String topic);

这可以让您了解AFAIK对特定主题的听众数量的洞察力。