如何创建hazelcast实例

时间:2014-09-05 00:38:49

标签: java hazelcast

我使用客户端和服务器设置(不是点对点)使用hazelcast。我应该使用单例模式创建hazelcast客户端和实例的实例(我使用的是懒惰的习惯用法,在任何情况下都可能需要另外看)。我应该使用单身人士还是只打电话 HazelcastInstance hz = Hazelcast。 newHazelcastInstance(); 可以在需要时获取实例吗?

服务器实施中使用的Hazelcast访问器:

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;
    }
}
检索到hazelcast的

参考:

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

我之后访问了多个IMaps,但是在同一个类中。

日志声明:

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

1 个答案:

答案 0 :(得分:4)

您需要了解的是每次调用

HazelcastInstance hz = Hazelcast.newHazelcastInstance ();

将完全创建 - JVM中的一个新的hazelcast节点(是的,你可以在一个JVM中有多个hazelcast节点),这必须加入集群等。除非你有一个用例(例如这个在单元测试期间非常有用,您可以在单个节点上设置内存中的集群!我猜另一个用例是,如果您需要更改配置,我知道它仍然无法在运行时更改)您不应该不经常调用它并简单地缓存它(例如在你的单身中)。

@Edit:您正在创建HazelcastInstance的多个实例。您确实只有LazyHolder.INSTANCE的一个实例,但您不会在hcast中存储HCastAccessor,因此每次调用HCastAccessor.getAccessorInstance().getHCastInstance()时,它都会创建一个新对象。您需要将hcast置于HCastAccessor内并仅在hcast为空时创建新的hazelcast实例,否则返回它。

请注意,您需要同步整个getHCastInstance()方法或使用同步进行双重nullcheck检查(检查null,仅同步构造部分,同步内部再次检查null,然后再进行实际创建)或者它不是线程安全的。类似的东西:

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