user800014
user800014

Reputation:

Hazelcast: Can I configure a Map per JVM?

Is there some option in Hazelcast to make the map cache values per node, and not replicate his state? I thought that this was the difference between getMap() and getDistributedMap() but it seems that both is replicated between nodes.

I have an application in a Weblogic cluster, my configuration is:

<?xml version="1.0" encoding="UTF-8"?>
<hazelcast>
    <properties>
        <property name="hazelcast.logging.type">slf4j</property>
    </properties>
    <network>
        <port auto-increment="true">5107</port>
        <join>
            <multicast enabled="false" />
            <tcp-ip enabled="true">
                <members>127.0.0.1:5701, 127.0.0.2:5702</members>
            </tcp-ip>
        </join>
    </network>
    <map name="default">
        <time-to-live-seconds>1800</time-to-live-seconds>
        <backup-count>0</backup-count>
        <eviction-policy>LRU</eviction-policy>
    </map>
</hazelcast>

In the logs I can see that both nodes are up

Members [2] {
        Member [127.0.0.1]:5107
        Member [127.0.0.1]:5108 this
}

When I call my method in the first node I can see that the cache is added

.Default (self-tuning)'] [] DEBUG b.c.l.c.c.i.e.c.h.AbstractHazelcastCacheInterceptor - Hazelcast instance HazelcastInstance{name='my-instance', node=Address[127.0.0.1]:5107}
2015-03-31 21:09:40.040 [[ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'] [] DEBUG b.c.l.c.c.i.e.c.h.AbstractHazelcastCacheInterceptor - Cache map IMap{name='MY_APP.Cache.cacheName'}
2015-03-31 21:09:40.040 [[ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'] [] DEBUG b.c.l.c.c.i.e.c.h.AbstractHazelcastCacheInterceptor - adding object in the cache with key 2342425

But in the second node I only see the cache name log, so it's using the already added cache...

.Default (self-tuning)'] [] DEBUG b.c.l.c.c.i.e.c.h.AbstractHazelcastCacheInterceptor - Hazelcast instance HazelcastInstance{name='my-instance', node=Address[127.0.0.1]:5108}
2015-03-31 21:09:40.040 [[ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'] [] DEBUG b.c.l.c.c.i.e.c.h.AbstractHazelcastCacheInterceptor - Cache map IMap{name='MY_APP.Cache.cacheName'}

Am I missing some configuration? Is it possible to achieve with Hazelcast?

Upvotes: 0

Views: 1021

Answers (1)

Mateusz Dymczyk
Mateusz Dymczyk

Reputation: 15141

As I mentioned in the comment a use case where:

  • put(key, value) adds the tuple only locally
  • evict(key) evicts it from all the maps

Isn't really that transparent and I doubt Hazelcast (or any popular in-memory grid) supports it out of the box but what you can do is implement a very simple pub-sub with Hazelcast. On each hazelcast node you start a topic listener:

ITopic<String> eviction = hazelcast.getTopic("evict");
eviction.addMessageListener(new EvictionListener());

Where EvictionListener would be something like:

public class EvictionListener implements MessageListener<String> {

   @Override
   public void onMessage(Message<String> message) {
      // Gets the local (per JVM) cache and evicts an entry from it
      Cache.getCache().evict(message.getMessageObject());
   }
}

And then you can implement your eviction method as:

public void evict(String key) {
    hazelcast.getTopic("evict").publish(key);
}

Something like this. Then you need to create a cache (as I mentioned a simple Java Map or a Guava cache) per JVM and use the evict method for eviction.

Upvotes: 2

Related Questions