Reputation: 3
I am trying to use hazelcast for caching. We have two applications running on two jboss 7.1 instances in the same physical machines. Both the applications are running in different ports.
Below is my code for starting a server node.
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
HazelcastInstance hazelcastInstance = CacheHelper.getCacheInstance();
Map<Long, String> cache = hazelcastInstance.getMap("data");
Long inputKey = Long.parseLong(request.getParameter("inputKey"));
String inputMsg = request.getParameter("inputMsg");
StringBuffer mainStr = null;
if(null != inputMsg && !(inputMsg.trim().equalsIgnoreCase(""))){
if(null!=inputKey && inputKey != 0 && cache.containsKey(inputKey)) {
mainStr = new StringBuffer(cache.get(inputKey));
mainStr.append(inputMsg);
} else {
mainStr = new StringBuffer(inputMsg);
}
cache.put(inputKey,mainStr.toString());
}
}
Every time I am getting a new cache.
Is my understanding correct we just need to create two server nodes. It will automatically discover the other nodes. It doesn't require any kind of XML configuration.
Thanks, Biswa
Upvotes: 0
Views: 705
Reputation: 3257
Biswa,
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
This line creates a new Hazelcast instance every time your doGet
method called. You need to create Hazelcast instance only once, then use it to access distributed objects. I believe CacheHelper.getCacheInstance()
method does that.
For the second question, yes, by default Hazelcast nodes discover other members via multicast, so no config needed.
Please make sure that you have only one Hazelcast.newHazelcastInstance()
call in each app & you use that instance to access dist.objects.
Upvotes: 1