/
ClusterStarter.java
193 lines (174 loc) · 5.12 KB
/
ClusterStarter.java
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package org.rcdukes.common;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.rcdukes.error.ErrorHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.vertx.core.AsyncResult;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.eventbus.EventBusOptions;
import io.vertx.rxjava.core.AbstractVerticle;
/**
* starter for VertxCluster
*
* @author wf
*
*/
public class ClusterStarter {
private static final Logger LOG = LoggerFactory.getLogger(ClusterStarter.class);
public static final int MAX_START_TIME = 20000;
private VertxOptions options;
private boolean prepared = false;
private boolean debug = false;
private Vertx vertx;
public Vertx getVertx() {
return vertx;
}
/**
* prepare the starter
*/
public void prepare() {
if (!prepared) {
Config.configureLogging();
try {
setHostname(InetAddress.getLocalHost().getCanonicalHostName());
} catch (UnknownHostException e) {
LOG.error(e.getMessage());
}
prepared = true;
}
}
private String hostname;
/**
* configure the cluster
* @param clusterHostname
* @param publicHost
*/
public void configureCluster(String clusterHostname,String publicHost) {
if (clusterHostname == null) {
clusterHostname=getHostname();
}
if (publicHost==null) {
publicHost=getHostname();
}
String msg=String.format("starting cluster on %s setting host to %s and clusterPublicHost to %s",getHostname(),clusterHostname,publicHost);
LOG.info(msg);
EventBusOptions eventBusOptions = getOptions().getEventBusOptions();
// https://github.com/eclipse-vertx/vert.x/issues/3229
// https://stackoverflow.com/a/49028531/1497139
// https://vertx.io/docs/apidocs/io/vertx/core/eventbus/EventBusOptions.html#setClusterPublicHost-java.lang.String-
eventBusOptions.setHost(clusterHostname);
eventBusOptions.setClusterPublicHost(publicHost);
}
/**
*
* @param resultHandler
*/
public void clusteredVertx(Handler<AsyncResult<Vertx>> resultHandler) {
prepare();
Vertx.clusteredVertx(getOptions(), resultHandler);
}
/**
* get the deployment options
*
* @param worker
* - true if worker should be set to true
* @return the deployment options
* @throws Exception
*/
public DeploymentOptions getDeployMentOptions(boolean worker)
throws Exception {
DeploymentOptions deploymentOptions = new DeploymentOptions();
deploymentOptions.setWorker(worker);
return deploymentOptions;
}
/**
* deploy the given array of verticle instances
*
* @param verticles
* @throws Exception
*/
public void deployVerticles(AbstractVerticle... verticles) throws Exception {
DeploymentOptions deploymentOptions = this.getDeployMentOptions(true);
// if there is no clustered vertx yet make sure we create one
if (vertx == null) {
this.clusteredVertx(resultHandler -> {
vertx = resultHandler.result();
if (vertx == null) {
ErrorHandler.getInstance().handle(new RuntimeException(
"vertx not available resultHandler result is null"));
}
// and then deploy to it
this.deployVerticles(deploymentOptions, verticles);
});
} else {
// there is already a clustered vertx available
// let's use it
this.deployVerticles(deploymentOptions, verticles);
}
}
/**
* deploy the given verticles with the given deployment Options
* @param deploymentOptions
* @param verticles
*/
private void deployVerticles(DeploymentOptions deploymentOptions,
AbstractVerticle[] verticles) {
for (AbstractVerticle verticle : verticles) {
try {
vertx.deployVerticle(verticle, deploymentOptions);
} catch (Throwable th) {
ErrorHandler.getInstance().handle(th);
}
}
}
/**
* get the VertexOptions
*
* @return new Vertex Options with the HazelcastConfig from Config
*/
public VertxOptions getOptions() {
if (options == null) {
options = new VertxOptions();
options.getEventBusOptions().setClustered(true);
options.setClusterManager(Config.createHazelcastConfig());
// FIXME check necessity of this workaround
// https://stackoverflow.com/a/30056974/1497139
// setting check interval to 1 h
if (debug) {
options.setBlockedThreadCheckInterval(1000 * 60 * 60);
} else {
// error after 5 secs ...
options.setBlockedThreadCheckInterval(1000 * 5);
}
}
return options;
}
/**
* undeploy the given DukesVerticle
* @param verticle
*/
public void undeployVerticle(DukesVerticle verticle) {
verticle.preStop();
vertx.undeploy(verticle.deploymentID(),async ->{
if (async.succeeded()) {
verticle.postStop();
}
});
}
/**
* @return the hostname
*/
public String getHostname() {
return hostname;
}
/**
* @param hostname the hostname to set
*/
public void setHostname(String hostname) {
this.hostname = hostname;
}
}