2

I have two tomcat servers in my local machine and "n" wars at each server.

I want to have just One Instance of Hazelcast by server, but i just get 1 instance by WAR, also using a Singleton Wrapper because this singletton is "attached" to WAR Classloader....

Any easy way to do this ?

  1. One Hazelcast instance by Tomcat Server (want to use the same approach in Oracle Weblogic), and a shared hazelcast instance along all war into the same server. Just one node by JVM

  2. Those Hazelcast instances joining to one and just one cluster.

One thing i don't get sense it is that if i create a HazelCast Instance in one WAR, and try to get in another one , this is null..being the same process (JVM ...) .

Something like this:

@Slf4j
public class MyCustomHazelCastManager {

    public static  void debugInstances() {
        Set<HazelcastInstance>  debugginIsntances  = Hazelcast.getAllHazelcastInstances();
        log.debug("Current Instances:" + debugginIsntances.size());;
        for (HazelcastInstance i : debugginIsntances ) {
            log.debug(i.getName());
        }
    }

    public static HazelcastInstance getInstance() {     
        log.debug("MyCustomHazelCastManager.getInstance() for {}" , ManagementFactory.getRuntimeMXBean().getName());        
        HazelcastInstance instance =
                Hazelcast.getHazelcastInstanceByName(ManagementFactory.getRuntimeMXBean().getName());
        if (instance == null) {
            Config config = new Config();       
            log.debug("RuntimeProcess ID :" +ManagementFactory.getRuntimeMXBean().getName());
            config.setInstanceName(ManagementFactory.getRuntimeMXBean().getName());
            instance = Hazelcast.newHazelcastInstance(config);  
            log.debug("Create Hazelcast instance with name:"+  ManagementFactory.getRuntimeMXBean().getName() );
        } else {
            System.out.println("\n\n Warning ! there is one instance with name :{}"+ManagementFactory.getRuntimeMXBean().getName());

        }
        return instance;
    }

}

Why doesn't work Hazelcast.getHazelcastInstanceByName() ??????

UPDATED:

If HazelCast library is put on Tomcat Classpath, (lib directory for example) works fine.

So , I have deduced that for get a Hazelcast instance is used the active process and the classloader used by Hazelcast objects. If in a different WARs deployed on Tomcat (on the same virtual machine ), having this wars different Hazelcast libraries ( by WAR ) Hazelcast will attempt to create new instances, even if it is the same process

Community
  • 1
  • 1
Azimuts
  • 1,212
  • 4
  • 16
  • 35

2 Answers2

2

you need to use the hazelcast client to connect to the hazelcast server. You need to run the hazelcast server nodes as runnable jar files (on different server nodes) - and they must be configured to host on their network interfaces and to connect to all the other server hosts running hazelcast nodes in order to form a hazelcast cluster. The hazelcast client can connect to this cluster (you can specify that in the hazelcast client xml file or do that via the Hazelcast API). You can use an enum to create the singleton which returns the hazelcast client instance - get a reference to this hazelcast client via the enum. You can use this client on as many java processes as you need.

ali haider
  • 19,175
  • 17
  • 80
  • 149
  • I'm trying to attach JVMPID (http://stackoverflow.com/questions/35842/how-can-a-java-program-get-its-own-process-id ) to HazelCastInstance, by setting this PID as unique identifier for HazelCastInstance Config, so different wars belonging to same server, should share the same Hazelcast Instance...but when i try to get a instance by name, no one is returned...(Hazelcast.getHazelcastInstanceByName) – Azimuts Aug 06 '16 at 15:20
  • why would you even want to do that? Simply get a client connection instance per process. That is the way to do it. – ali haider Aug 06 '16 at 21:28
  • That is what i want to do : one connection instance per process. In this case the running process is the tomcat running instance, but no way to get the "hazelcast instance per process". I cant use a singleton pattern cause there will be so many instances as WAR classloaders (and i don't want to use outside classes at tomcat lib), so i thought to get the Hazelcast instance using Hazelcast.getHazelcastInstanceByName() but no one is returned..... – Azimuts Aug 07 '16 at 09:20
1

As said into the updated entry, just to have Hazelcast libraries on Tomcat, Weblogic , any server, startup-classpath libraries... :

I have deduced that for get a Hazelcast instance is used the active process and the classloader used by Hazelcast objects. If in a different WARs deployed on Tomcat (on the same virtual machine ), having this wars different Hazelcast libraries ( by WAR ) Hazelcast will attempt to create new instances, even if it is the same process

Azimuts
  • 1,212
  • 4
  • 16
  • 35