forked from vert-x3/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVertxVerticleHost.java
52 lines (43 loc) · 1.35 KB
/
VertxVerticleHost.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
package io.vertx.example.osgi;
import io.vertx.core.Verticle;
import io.vertx.core.Vertx;
import org.apache.felix.ipojo.annotations.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A component consuming all verticles exposed in the service registry and deploying them.
*/
@Component(immediate = true)
@Instantiate
public class VertxVerticleHost {
private final static Logger LOGGER = Logger.getLogger("VertxVerticleHost");
@Requires
Vertx vertx;
ConcurrentHashMap<Verticle, String> deploymentIds = new ConcurrentHashMap<>();
@Invalidate
public void stop() {
// We should unregister all deployed verticles.
}
@Bind(aggregate = true)
public void bindVerticle(Verticle verticle) {
LOGGER.info("Deploying verticle " + verticle);
TcclSwitch.executeWithTCCLSwitch(() -> {
vertx.deployVerticle(verticle, ar -> {
if (ar.succeeded()) {
deploymentIds.put(verticle, ar.result());
} else {
LOGGER.log(Level.SEVERE, "Cannot deploy " + verticle, ar.cause());
}
});
});
}
@Unbind(aggregate = true)
public void unbindVerticle(Verticle verticle) {
String id = deploymentIds.get(verticle);
LOGGER.info("Undeploying verticle " + verticle + " (" + id + ")");
if (id != null) {
vertx.undeploy(id);
}
}
}