How to create a hazelcast instance

I am using hazelcast using client and server setup (not peer to peer). Should I create a client and hazelcast instance using the singleton pattern (I'm using the lazy idiom which probably needs a different look anyway). Should I use singleton or just call HazelcastInstance hz = Hazelcast. newHazelcastInstance (); to get an instance when I need it?

Hazelcast accessory used in server implementation:

public class HCastAccessor {

    private static final Logger Logger = LoggerFactory.getLogger(HCastAccessor.class.getName());

//  private volatile static HCastAccessor instance = null;  
    private HCastAccessor() {
    }

    //lazy loaded holder idiom
    private static class LazyHolder {
        private static final HCastAccessor INSTANCE = new HCastAccessor();
    }

    public static HCastAccessor getAccessorInstance() {
        return LazyHolder.INSTANCE;
    }

    public HazelcastInstance getHCastInstance() {
        HazelcastInstance hcast = null;
        try {
            final Config config = new FileSystemXmlConfig("resources/hazelcast.xml");  
            hcast = Hazelcast.newHazelcastInstance(config);  
        }
        catch (FileNotFoundException fe) {
            Logger.error("File not found for Hazelcast:{}" + fe.getMessage());
        }
        catch (Exception e) {
            Logger.error("Exception while starting Hazelcast server:{}" + e.getMessage());
        }
        return hcast;
    }
}

      

link for getting forest stone:

    final HCastAccessor hcastAccessor = HCastAccessor.getAccessorInstance();
    HazelcastInstance hcast = hcastAccessor.getHCastInstance(); // handle shutdown as well

      

I am accessing multiple IMaps after this, but in the same class.

log statements:

2014-09-05 05:15:29,473 INFO c.a.w.StartHcastServer [main] Starting Hazelcast Server on 2014/09/05 05:15:29
2014-09-05 05:15:30,105 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Interfaces is disabled, trying to pick one address from TCP-IP config addresses: [127.0.0.1]
2014-09-05 05:15:30,113 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Picked Address[127.0.0.1]:5701, using socket ServerSocket[addr=/0.0.0.0,localport=5701], bind any local is true
2014-09-05 05:15:30,336 INFO c.h.system [main] [127.0.0.1]:5701 [dev] [3.2.4] Hazelcast 3.2.4 (20140721) starting at Address[127.0.0.1]:5701
2014-09-05 05:15:30,336 INFO c.h.system [main] [127.0.0.1]:5701 [dev] [3.2.4] Copyright (C) 2008-2014 Hazelcast.com
2014-09-05 05:15:30,336 INFO c.h.i.Node [main] [127.0.0.1]:5701 [dev] [3.2.4] Creating TcpIpJoiner
2014-09-05 05:15:30,343 INFO c.h.c.LifecycleService [main] [127.0.0.1]:5701 [dev] [3.2.4] Address[127.0.0.1]:5701 is STARTING
2014-09-05 05:15:30,446 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5701 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:15:30,449 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5701 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5702
2014-09-05 05:15:30,454 INFO c.h.n.SocketConnector [hz._hzInstance_1_dev.cached.thread-2] [127.0.0.1]:5701 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:15:30,456 INFO c.h.n.SocketConnector [hz._hzInstance_1_dev.cached.thread-2] [127.0.0.1]:5701 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:15:30,456 INFO c.h.n.SocketConnector [hz._hzInstance_1_dev.cached.thread-3] [127.0.0.1]:5701 [dev] [3.2.4] Connecting to /127.0.0.1:5702, timeout: 0, bind-any: true
2014-09-05 05:15:30,464 INFO c.h.n.SocketConnector [hz._hzInstance_1_dev.cached.thread-3] [127.0.0.1]:5701 [dev] [3.2.4] Could not connect to: /127.0.0.1:5702. Reason: SocketException[Connection re
fused to address /127.0.0.1:5702]
2014-09-05 05:15:31,450 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5701 [dev] [3.2.4]


Members [1] {
        Member [127.0.0.1]:5701 this
}

2014-09-05 05:15:31,482 INFO c.h.c.LifecycleService [main] [127.0.0.1]:5701 [dev] [3.2.4] Address[127.0.0.1]:5701 is STARTED
2014-09-05 05:15:31,488 INFO c.a.c.n.SendServerEmail [main] hazelcast server email notification for server 192.168.110.154 WildMetrix Hazelcast server launched at 192.168.110.154
2014-09-05 05:15:32,421 INFO c.h.p.InternalPartitionService [hz._hzInstance_1_dev.cached.thread-1] [127.0.0.1]:5701 [dev] [3.2.4] Initializing cluster partition table first arrangement...
2014-09-05 05:15:32,615 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Interfaces is disabled, trying to pick one address from TCP-IP config addresses: [127.0.0.1]
2014-09-05 05:15:32,616 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Picked Address[127.0.0.1]:5702, using socket ServerSocket[addr=/0.0.0.0,localport=5702], bind any local is true
2014-09-05 05:15:32,690 INFO c.h.system [main] [127.0.0.1]:5702 [dev] [3.2.4] Hazelcast 3.2.4 (20140721) starting at Address[127.0.0.1]:5702
2014-09-05 05:15:32,690 INFO c.h.system [main] [127.0.0.1]:5702 [dev] [3.2.4] Copyright (C) 2008-2014 Hazelcast.com
2014-09-05 05:15:32,691 INFO c.h.i.Node [main] [127.0.0.1]:5702 [dev] [3.2.4] Creating TcpIpJoiner
2014-09-05 05:15:32,692 INFO c.h.c.LifecycleService [main] [127.0.0.1]:5702 [dev] [3.2.4] Address[127.0.0.1]:5702 is STARTING
2014-09-05 05:15:32,701 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:15:32,701 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5701
2014-09-05 05:15:32,702 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:15:32,702 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:15:32,702 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5701, timeout: 0, bind-any: true
2014-09-05 05:15:32,708 INFO c.h.n.SocketAcceptor [hz._hzInstance_1_dev.IO.thread-Acceptor] [127.0.0.1]:5701 [dev] [3.2.4] Accepting socket connection from /127.0.0.1:38086
2014-09-05 05:15:32,722 INFO c.h.n.TcpIpConnectionManager [hz._hzInstance_1_dev.IO.thread-Acceptor] [127.0.0.1]:5701 [dev] [3.2.4] 5701 accepted socket connection from /127.0.0.1:38086
2014-09-05 05:15:32,722 INFO c.h.n.TcpIpConnectionManager [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] 38086 accepted socket connection from /127.0.0.1:5701
2014-09-05 05:16:09,722 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Couldn't join to the master : Address[127.0.0.1]:5701
2014-09-05 05:16:09,722 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Failed to connect, node joined= false, allConnected= false to all other members after 0 seconds.
2014-09-05 05:16:09,723 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Rebooting after 10 seconds.
2014-09-05 05:16:19,724 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:16:19,724 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5701
2014-09-05 05:16:19,724 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:16:19,724 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:16:45,732 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Failed to connect, node joined= false, allConnected= false to all other members after 0 seconds.
2014-09-05 05:16:45,732 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Rebooting after 10 seconds.
2014-09-05 05:16:55,733 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:16:55,733 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5701
2014-09-05 05:16:55,733 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-3] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:16:55,734 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-3] [127.0.0.1]:5702 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:17:21,740 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Failed to connect, node joined= false, allConnected= false to all other members after 0 seconds.
2014-09-05 05:17:21,740 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Rebooting after 10 seconds.
2014-09-05 05:17:31,741 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:17:31,741 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5701
2014-09-05 05:17:31,742 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:17:31,742 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:17:31,743 WARN c.h.n.ConnectionMonitor [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Removing connection to endpoint Address[127.0.0.1]:5703 Cause => java.net.
SocketException {Connection refused to address /127.0.0.1:5703}, Error-Count: 5
2014-09-05 05:17:57,749 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Failed to connect, node joined= false, allConnected= false to all other members after 0 seconds.
2014-09-05 05:17:57,749 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Rebooting after 10 seconds.
2014-09-05 05:18:07,750 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5703
2014-09-05 05:18:07,750 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5701
2014-09-05 05:18:07,751 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Connecting to /127.0.0.1:5703, timeout: 0, bind-any: true
2014-09-05 05:18:07,751 INFO c.h.n.SocketConnector [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Could not connect to: /127.0.0.1:5703. Reason: SocketException[Connection re
fused to address /127.0.0.1:5703]
2014-09-05 05:18:07,751 WARN c.h.n.ConnectionMonitor [hz._hzInstance_2_dev.cached.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] Removing connection to endpoint Address[127.0.0.1]:5703 Cause => java.net.
SocketException {Connection refused to address /127.0.0.1:5703}, Error-Count: 6
2014-09-05 05:18:33,760 WARN c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4] Join try count exceed limit, setting this node as master!
2014-09-05 05:18:33,760 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5702 [dev] [3.2.4]


Members [1] {
        Member [127.0.0.1]:5702 this
}

2014-09-05 05:18:33,760 WARN c.h.i.Node [main] [127.0.0.1]:5702 [dev] [3.2.4] Config seed port is 5701 and cluster size is 1. Some of the ports seem occupied!
2014-09-05 05:18:33,761 INFO c.h.c.LifecycleService [main] [127.0.0.1]:5702 [dev] [3.2.4] Address[127.0.0.1]:5702 is STARTED
2014-09-05 05:18:33,777 INFO c.h.c.ClusterService [hz._hzInstance_1_dev.global-operation.thread-4] [127.0.0.1]:5701 [dev] [3.2.4]

Members [2] {
        Member [127.0.0.1]:5701 this
        Member [127.0.0.1]:5702
}

2014-09-05 05:18:33,784 WARN c.h.p.InternalPartitionService [hz._hzInstance_2_dev.global-operation.thread-2] [127.0.0.1]:5702 [dev] [3.2.4] This is the master node and received a PartitionRuntimeSt
ate from Address[127.0.0.1]:5701. Ignoring incoming state!
2014-09-05 05:18:33,826 WARN c.h.p.InternalPartitionService [hz._hzInstance_2_dev.global-operation.thread-1] [127.0.0.1]:5702 [dev] [3.2.4] This is the master node and received a PartitionRuntimeSt
ate from Address[127.0.0.1]:5701. Ignoring incoming state!
2014-09-05 05:18:33,827 INFO c.h.p.InternalPartitionService [hz._hzInstance_1_dev.migration] [127.0.0.1]:5701 [dev] [3.2.4] Re-partitioning cluster data... Migration queue size: 135
2014-09-05 05:18:34,132 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Interfaces is disabled, trying to pick one address from TCP-IP config addresses: [127.0.0.1]
2014-09-05 05:18:34,133 INFO c.h.i.DefaultAddressPicker [main] null [dev] [3.2.4] Picked Address[127.0.0.1]:5703, using socket ServerSocket[addr=/0.0.0.0,localport=5703], bind any local is true
2014-09-05 05:18:34,207 INFO c.h.system [main] [127.0.0.1]:5703 [dev] [3.2.4] Hazelcast 3.2.4 (20140721) starting at Address[127.0.0.1]:5703
2014-09-05 05:18:34,207 INFO c.h.system [main] [127.0.0.1]:5703 [dev] [3.2.4] Copyright (C) 2008-2014 Hazelcast.com
2014-09-05 05:18:34,207 INFO c.h.i.Node [main] [127.0.0.1]:5703 [dev] [3.2.4] Creating TcpIpJoiner
2014-09-05 05:18:34,208 INFO c.h.c.LifecycleService [main] [127.0.0.1]:5703 [dev] [3.2.4] Address[127.0.0.1]:5703 is STARTING
2014-09-05 05:18:34,228 INFO c.h.c.TcpIpJoiner [main] [127.0.0.1]:5703 [dev] [3.2.4] Connecting to possible member: Address[127.0.0.1]:5702

      

+3


source to share


1 answer


What you need to understand is that every call

HazelcastInstance hz = Hazelcast.newHazelcastInstance ();

Will create exactly this - a new carousel node in your JVM (yes, you can have multiple cariboutika nodes inside one JVM) that will need to join the cluster, etc. If you don't have a use case for this (for example, this is very useful during unit tests where you can set up your in-memory cluster on a single node. I guess another use case would be if you need to change the configuration, from which, as I know, still cannot be changed at runtime), you shouldn't call it too often and just cache it (like in that single of yours).



@Edit: you create multiple instances HazelcastInstance

. You will only have one instance LazyHolder.INSTANCE

, but you do not store hcast

in HCastAccessor

, so every time you call HCastAccessor.getAccessorInstance().getHCastInstance()

it will create a new object. You only need to put hcast

inside HCastAccessor

and create a new hazelcast instance if hcast

it is null, otherwise you will return it.

Take care of the need to either sync the whole method getHCastInstance()

, or do a double reset with sync (check for null, sync only the construct part inside which sync the null check again before doing the actual creation) or it won't be thread safe. Something like:

if(hcas == null) {
  synchronize(this) {
    if(hcas == null) {
      // create the instance here
    }
  }
}

      

+5


source







All Articles