-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathClusterBroadcast.java
More file actions
78 lines (70 loc) · 2.65 KB
/
ClusterBroadcast.java
File metadata and controls
78 lines (70 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package fun.lib.actor.example;
import fun.lib.actor.core.DFActor;
import fun.lib.actor.core.DFActorManager;
import fun.lib.actor.core.DFClusterManager;
import fun.lib.actor.helper.DFActorLogLevel;
import fun.lib.actor.po.ActorProp;
import fun.lib.actor.po.DFActorClusterConfig;
import fun.lib.actor.po.DFActorManagerConfig;
/**
* 集群结点广播示例
* 模拟3个"game"类型结点和一个"chat"类型结点组网
* "game-1"结点每隔2秒交替进行"game"类型和全局类型广播
* @author lostsky
*
*/
public final class ClusterBroadcast {
private static int NODE_IDX = 0; // 0~3
private static final String[] ARR_NODE_NAME = {"game-1", "game-2", "game-3", "chat-1"};
private static final String[] ARR_NODE_TYPE = {"game", "game", "game", "chat"};
public static void main(String[] args) {
String nodeName = ARR_NODE_NAME[NODE_IDX]; //当前启动结点名字
String nodeType = ARR_NODE_TYPE[NODE_IDX]; //当前启动结点类型
DFActorClusterConfig cfgCluster =
DFActorClusterConfig.newCfg(nodeName) //结点名字,一个结点在集群内名字必须唯一(可不设置,默认会自动生成唯一名字)
.setNodeType(nodeType);
DFActorManagerConfig cfgStart = new DFActorManagerConfig()
.setClusterConfig(cfgCluster); //设置集群配置,开启集群功能
//设置业务入口actor,集群初始化和发现完毕后将启动该actor
ActorProp prop = ActorProp.newProp()
.classz(EntryActor.class)
.name("EntryActor");
DFActorManager.get().start(cfgStart, prop);
}
private static class EntryActor extends DFActor{
public EntryActor(Integer id, String name, Boolean isBlockActor) {
super(id, name, isBlockActor);
// TODO Auto-generated constructor stub
}
@Override
public void onStart(Object param) {
log.info("onStart");
if(NODE_IDX == 0){ //
timer.timeout(2000, 0);
}
}
private int reqCount = 0;
@Override
public void onTimeout(int requestId) {
if(NODE_IDX == 0){
if(++reqCount%2 == 0){ //向"game"类型结点广播
int nodeNum = sys.getNodeNumByType("game");
if(nodeNum > 0){
sys.toNodeByType("game", "EntryActor", NODE_IDX, "reqByType from "+ARR_NODE_NAME[NODE_IDX]);
}
}else{ //向全部结点广播
int nodeNum = sys.getAllNodeNum();
if(nodeNum > 0){
sys.toAllNode("EntryActor", NODE_IDX, "reqForAll from "+ARR_NODE_NAME[NODE_IDX]);
}
}
timer.timeout(2000, 0);
}
}
@Override
public int onClusterMessage(String srcType, String srcNode, String srcActor, int cmd, Object payload) {
log.info(ARR_NODE_NAME[NODE_IDX]+"::onClusterMessage, from="+srcNode+", cmd="+cmd+", payload="+payload);
return 0;
}
}
}