Question

I am trying to set up a hazelcast topic for pub/sub between two tomcat instances. To start with, I have just a servlet which initializes Hazelcast and creates the topic when tomcat starts. This servlet will also publish messages to the topic when invoked through HTTP GET. Here is my init() method in the servlet:

Config cfg = new Config();

System.out.println("hzapp: Creating hazelcast instance...");
hzInstance = Hazelcast.newHazelcastInstance(cfg);
System.out.println("hzapp: Created hazelcast instance.");

topic = hzInstance.getTopic("my-topic");

System.out.println("hzapp: Adding listener to the topic...");
topic.addMessageListener(new HzMessageListener());
System.out.println("hzapp: listener added to the topic.");

The doGet(req, res) method of the servlet just publishes a message to the topic like this:

String message = request.getParameter("message");
System.out.println("hzapp: publishing message: " + message);
topic.publish(message);
System.out.println("hzapp: published message: " + message);

The HzMessageListener does nothing but prints the received message:

public void onMessage(Message<String> message) {
    System.out.println("hzapp: Message Received: " + message.getMessageObject());
}

Now, the issue I am facing is that, when I start tomcat, it gives me the below error:

INFO: Starting Servlet Engine: Apache Tomcat/6.0.36
hzapp: Creating hazelcast instance...
Jan 23, 2014 10:43:26 AM com.hazelcast.instance.DefaultAddressPicker
INFO: Prefer IPv4 stack is true.
Jan 23, 2014 10:43:26 AM com.hazelcast.instance.DefaultAddressPicker
INFO: Picked Address[192.168.1.10]:5702, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0%0,localport=5702], bind any local is true
Jan 23, 2014 10:43:26 AM com.hazelcast.system
INFO: [192.168.1.10]:5702 [dev] Hazelcast Community Edition 3.1.4 (20140111) starting at Address[192.168.1.10]:5702
Jan 23, 2014 10:43:26 AM com.hazelcast.system
INFO: [192.168.1.10]:5702 [dev] Copyright (C) 2008-2013 Hazelcast.com
Jan 23, 2014 10:43:26 AM com.hazelcast.instance.Node
INFO: [192.168.1.10]:5702 [dev] Creating MulticastJoiner
Jan 23, 2014 10:43:26 AM com.hazelcast.core.LifecycleService
INFO: [192.168.1.10]:5702 [dev] Address[192.168.1.10]:5702 is STARTING
Jan 23, 2014 10:43:26 AM com.hazelcast.nio.SocketConnector
INFO: [192.168.1.10]:5702 [dev] Connecting to /192.168.1.10:5701, timeout: 0, bind-any: true
Jan 23, 2014 10:43:26 AM com.hazelcast.nio.TcpIpConnectionManager
INFO: [192.168.1.10]:5702 [dev] 55635 accepted socket connection from /192.168.1.10:5701
Jan 23, 2014 10:43:50 AM com.hazelcast.instance.Node
WARNING: [192.168.1.10]:5702 [dev] Trying to rejoin: 
======================================================
Couldn't connect to discovered master! tryCount: 50
address: Address[192.168.1.10]:5702
masterAddress: Address[192.168.1.10]:5701
multicast: true
connection: null
======================================================

Any idea what's going wrong here? I am using Hazelcast version 3.1.4 and I have hazelcast-all-3.1.4.jar in WEB-INF/lib.

Irrespective of what I have tried, it never goes beyond Hazelcast.newHazelcastInstance(cfg);

Was it helpful?

Solution

Could you try it with tcp ip discovery instead of multicast to make sure that nothing else than multicast is the problem:

Config config = new Config();
JoinConfig join = config.getNetworkConfig().getJoin();
join.getMulticastConfig().setEnabled(false);
join.getTcpIpConfig().setEnabled(true);

Then we have narrowed down the scope of the problem. It could be that your network doesn't support multicast btw.. That could be the cause.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top