简体   繁体   中英

Hazelcast nodes not get the first published message

I have two nodes. Both of them are subscribed for a topic.

When one of the nodes publishes a message, the other one not get the message at first time. If the node publishes a message second time, then the other node gets the message.

If i call hazelcastInstance.getTopic(TopicX) at the application initialization phase, message listeners work as desired.

I think it is about lazy-init attribute.

Is there more reliable way not to face this problem? Reliable-topic could be solution?? If so, is there any sample code to implement reliable topic with spring?

@vourla, I'd suggest using ReliableTopic since it's backed by RingBuffer & as long as backing ringbuffer is not full, listeners can read the first message properly.

Also, please see the related doc section: https://docs.hazelcast.org/docs/3.11.1/manual/html-single/index.html#configuring-reliable-topic

Instead of adding listener programatically, add it via configuration. Also, for Topic, since events are fire and forget, if you add the listener after the event fired from another node, you wont get it, whether you define it programatically or via config, but with ReliableTopic, both should work.

You can check the Spring releated doc section & related code samples as well: https://github.com/hazelcast/hazelcast-code-samples/tree/master/hazelcast-integration/spring-configuration

@gokhan-oner, thank you for the answer.

Actually I tried to implement reliable-topic at first. But I couldn't find sample implementation in spring. Syntax is a little different in spring. Now, the implementation is done like this:

 <hz:hazelcast id="instance">
    <hz:ringbuffer name="topicX" capacity="1000" time-to-live-seconds="5"/>
    <hz:ringbuffer name="topicY" capacity="1000" time-to-live-seconds="5"/>
    <hz:reliable-topic name="topicX" topic-overload-policy="BLOCK"/>
    <hz:reliable-topic name="topicY" topic-overload-policy="BLOCK"/>
</hz:hazelcast>

But, declaratively implementing topic listeners not worked. I addded listeners programmatically when context is initializing.

What is not working for me:

<hz:reliable-topic name="topicZ" topic-overload-policy="BLOCK">
    <hz:message-listeners>
        <hz:message-listener class-name="tr.com.test.HazelcastTopicListener"/>
    </hz:message-listeners>
</hz:reliable-topic>

What is working:

HazelcastTopicListener hazelcastTopicListener = new HazelcastTopicListener();
HazelcastInstance hazelcastInstance = SpringIntegration.getBean(HazelcastInstance.class);
ITopic<Message> testTopic= hazelcastInstance.getTopic("topicZ");
testTopic.addMessageListener(hazelcastTopicListener );

@vourla, please check the hazelcast-spring-XX.xsd file. Attribute name is class-or-bean-name , not class-name . Can you try like below:

<hz:reliable-topic name="topicZ" topic-overload-policy="BLOCK">
    <hz:message-listeners>
        <hz:message-listener class-or-bean-name="tr.com.test.HazelcastTopicListener"/>
    </hz:message-listeners>
</hz:reliable-topic>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM