以下文章记录与笔记中,当时做项目用到了,可能是转载了部分的,如果转载部分,请私信
1、运行如下代码:
public static void main(String[] args) throws Exception {
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
IExecutorService executor = hz.getExecutorService("executor");
for (int k = 1; k <= 10; k++) {
Thread.sleep(1000);
System.out.println("Producing echo task: " + k);
executor.execute(new EchoTask("" + k));
}
System.out.println("MasterMember finished!");
executor.execute(new EchoTask("foo"));
System.in.read();
executor.shutdown();
}
——————依耐代码:
public class EchoTask implements Runnable, Serializable {
private final String msg;
public EchoTask(String msg) {
this.msg = msg;
}
public void run() {
while(true){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
System.out.println("Echo:" + msg);
}
}
}
——————
2、在第一个节点可以运行,正常打印信息:但是新建如下节点并运行:
public class Member {
public static void main(String[] args) {
Hazelcast.newHazelcastInstance();
}
}
没有任何反应,显示是已经和前面的阶段连成集群了的。
3、原因:
猜测:没必要再每个节点上都运行,只需要数据是一样的就可以了。第一个运行的节点,是因为他是运行者,自然output的标的指向它自己的console了。
——————奇怪的现象:再重新打开一个生产任务的Hazelcast的话,那个不输出的,突然能输出了(但不输出:3和8,10,还有foo),这是为什么呢?。——输出见附件:——看完附件,原因已经知道,验证猜想。
4、那这个有什么作用呢?
代码分离!是的,多线程任务处理。这样的话:
1、Hazelcast只管自己的事情,具体的业务操作、计算交给专门的类来运行。
2、分布式多线程处理,数据是同步的,因此,可以根据其它节点算出的数据,动态规划自己节点的计算任务,或指导其它节点的计算任务。
————————————————————
一月 06, 2016 4:34:00 下午 com.hazelcast.nio.tcp.SocketAcceptor
信息: [106.37.92.101]:5702 [dev] [3.5.4] Accepting socket connection from /106.37.92.101:1852
一月 06, 2016 4:34:00 下午 com.hazelcast.nio.tcp.TcpIpConnectionManager
信息: [106.37.92.101]:5702 [dev] [3.5.4] Established socket connection between /106.37.92.101:5702
一月 06, 2016 4:34:06 下午 com.hazelcast.cluster.ClusterService
信息: [106.37.92.101]:5702 [dev] [3.5.4]
Members [2] {
Member [106.37.92.101]:5702 this
Member [106.37.92.101]:5701
}
一月 06, 2016 4:34:06 下午 com.hazelcast.partition.InternalPartitionService
信息: [106.37.92.101]:5702 [dev] [3.5.4] Re-partitioning cluster data... Migration queue size: 135
一月 06, 2016 4:34:08 下午 com.hazelcast.partition.InternalPartitionService
信息: [106.37.92.101]:5702 [dev] [3.5.4] All migration tasks have been completed, queues are empty.
Echo:1
Echo:2
Echo:4
Echo:5
Echo:1
Echo:6
Echo:2
Echo:7
Echo:4
Echo:9
Echo:5
Echo:1
Echo:6
Echo:2
Echo:7
——————————————
一月 06, 2016 4:33:58 下午 com.hazelcast.config.XmlConfigLocator
信息: Loading 'hazelcast-default.xml' from classpath.
一月 06, 2016 4:33:59 下午 com.hazelcast.instance.DefaultAddressPicker
信息: [LOCAL] [dev] [3.5.4] Prefer IPv4 stack is true.
一月 06, 2016 4:33:59 下午 com.hazelcast.instance.DefaultAddressPicker
信息: [LOCAL] [dev] [3.5.4] Picked Address[106.37.92.101]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
一月 06, 2016 4:33:59 下午 com.hazelcast.spi.OperationService
信息: [106.37.92.101]:5701 [dev] [3.5.4] Backpressure is disabled
一月 06, 2016 4:33:59 下午 com.hazelcast.spi.impl.operationexecutor.classic.ClassicOperationExecutor
信息: [106.37.92.101]:5701 [dev] [3.5.4] Starting with 2 generic operation threads and 4 partition operation threads.
一月 06, 2016 4:34:00 下午 com.hazelcast.system
信息: [106.37.92.101]:5701 [dev] [3.5.4] Hazelcast 3.5.4 (20151125 - 56676b2) starting at Address[106.37.92.101]:5701
一月 06, 2016 4:34:00 下午 com.hazelcast.system
信息: [106.37.92.101]:5701 [dev] [3.5.4] Copyright (c) 2008-2015, Hazelcast, Inc. All Rights Reserved.
一月 06, 2016 4:34:00 下午 com.hazelcast.instance.Node
信息: [106.37.92.101]:5701 [dev] [3.5.4] Creating MulticastJoiner
一月 06, 2016 4:34:00 下午 com.hazelcast.core.LifecycleService
信息: [106.37.92.101]:5701 [dev] [3.5.4] Address[106.37.92.101]:5701 is STARTING
一月 06, 2016 4:34:00 下午 com.hazelcast.cluster.impl.MulticastJoiner
信息: [106.37.92.101]:5701 [dev] [3.5.4] Trying to join to discovered node: Address[106.37.92.101]:5702
一月 06, 2016 4:34:00 下午 com.hazelcast.nio.tcp.SocketConnector
信息: [106.37.92.101]:5701 [dev] [3.5.4] Connecting to /106.37.92.101:5702, timeout: 0, bind-any: true
一月 06, 2016 4:34:00 下午 com.hazelcast.nio.tcp.TcpIpConnectionManager
信息: [106.37.92.101]:5701 [dev] [3.5.4] Established socket connection between /106.37.92.101:1852
一月 06, 2016 4:34:06 下午 com.hazelcast.cluster.ClusterService
信息: [106.37.92.101]:5701 [dev] [3.5.4]
Members [2] {
Member [106.37.92.101]:5702
Member [106.37.92.101]:5701 this
}
一月 06, 2016 4:34:08 下午 com.hazelcast.core.LifecycleService
信息: [106.37.92.101]:5701 [dev] [3.5.4] Address[106.37.92.101]:5701 is STARTED
Producing echo task: 1
Producing echo task: 2
Producing echo task: 3
Producing echo task: 4
Producing echo task: 5
Producing echo task: 6
Producing echo task: 7
Echo:3
Producing echo task: 8
Producing echo task: 9
Producing echo task: 10
MasterMember finished!
Echo:3
Echo:8
Echo:10
Echo:foo
Echo:3
Echo:8
Echo:10
Echo:foo
Echo:3
Echo:8
Echo:10
Echo:foo
Echo:3
Echo:8
Echo:10
Echo:foo
Echo:3
Echo:8
Echo:10
Echo:foo
Echo:3
Echo:8
Echo:10
Echo:foo
————————上面是断开之前,下面是断开之后(看完,就知道原因了)
Echo:3
Echo:8
Echo:10
Echo:foo
Echo:3
Echo:8
Echo:10
Echo:foo
一月 06, 2016 4:35:55 下午 com.hazelcast.nio.tcp.TcpIpConnection
信息: [106.37.92.101]:5701 [dev] [3.5.4] Connection [Address[106.37.92.101]:5702] lost. Reason: java.io.IOException[远程主机强迫关闭了一个现有的连接。]
一月 06, 2016 4:35:55 下午 com.hazelcast.nio.tcp.ReadHandler
警告: [106.37.92.101]:5701 [dev] [3.5.4] hz._hzInstance_1_dev.IO.thread-in-0 Closing socket to endpoint Address[106.37.92.101]:5702, Cause:java.io.IOException: 远程主机强迫关闭了一个现有的连接。
一月 06, 2016 4:35:55 下午 com.hazelcast.nio.tcp.SocketConnector
信息: [106.37.92.101]:5701 [dev] [3.5.4] Connecting to /106.37.92.101:5702, timeout: 0, bind-any: true
一月 06, 2016 4:35:56 下午 com.hazelcast.nio.tcp.SocketConnector
信息: [106.37.92.101]:5701 [dev] [3.5.4] Could not connect to: /106.37.92.101:5702. Reason: SocketException[Connection refused: connect to address /106.37.92.101:5702]
Echo:3
Echo:8
一月 06, 2016 4:35:57 下午 com.hazelcast.nio.tcp.SocketConnector
信息: [106.37.92.101]:5701 [dev] [3.5.4] Connecting to /106.37.92.101:5702, timeout: 0, bind-any: true
一月 06, 2016 4:35:58 下午 com.hazelcast.nio.tcp.SocketConnector
信息: [106.37.92.101]:5701 [dev] [3.5.4] Could not connect to: /106.37.92.101:5702. Reason: SocketException[Connection refused: connect to address /106.37.92.101:5702]
Echo:10
Echo:foo
一月 06, 2016 4:35:59 下午 com.hazelcast.cluster.ClusterService
警告: [106.37.92.101]:5701 [dev] [3.5.4] This node does not have a connection to Member [106.37.92.101]:5702
一月 06, 2016 4:35:59 下午 com.hazelcast.nio.tcp.SocketConnector
信息: [106.37.92.101]:5701 [dev] [3.5.4] Connecting to /106.37.92.101:5702, timeout: 0, bind-any: true
一月 06, 2016 4:36:00 下午 com.hazelcast.nio.tcp.SocketConnector
信息: [106.37.92.101]:5701 [dev] [3.5.4] Could not connect to: /106.37.92.101:5702. Reason: SocketException[Connection refused: connect to address /106.37.92.101:5702]
一月 06, 2016 4:36:00 下午 com.hazelcast.nio.tcp.TcpIpConnectionMonitor
警告: [106.37.92.101]:5701 [dev] [3.5.4] Removing connection to endpoint Address[106.37.92.101]:5702 Cause => java.net.SocketException {Connection refused: connect to address /106.37.92.101:5702}, Error-Count: 5
一月 06, 2016 4:36:00 下午 com.hazelcast.cluster.ClusterService
信息: [106.37.92.101]:5701 [dev] [3.5.4] Master Address[106.37.92.101]:5702 left the cluster. Assigning new master Member [106.37.92.101]:5701 this
一月 06, 2016 4:36:00 下午 com.hazelcast.cluster.ClusterService
信息: [106.37.92.101]:5701 [dev] [3.5.4] Removing Member [106.37.92.101]:5702
一月 06, 2016 4:36:00 下午 com.hazelcast.cluster.ClusterService
信息: [106.37.92.101]:5701 [dev] [3.5.4]
Members [1] {
Member [106.37.92.101]:5701 this
}
Echo:3
Echo:8
Echo:10
Echo:foo
一月 06, 2016 4:36:05 下午 com.hazelcast.partition.InternalPartitionService
信息: [106.37.92.101]:5701 [dev] [3.5.4] Partition balance is ok, no need to re-partition cluster data...
Echo:3
Echo:8
Echo:9
Echo:5
Echo:4
Echo:1
Echo:6
Echo:7
Echo:2
Echo:10
Echo:foo