-
Notifications
You must be signed in to change notification settings - Fork 5
/
WorkerVerticle.java
54 lines (45 loc) · 1.74 KB
/
WorkerVerticle.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
package org.genericsystem.ir.app.gui.utils;
import org.genericsystem.common.GSVertx;
import org.genericsystem.common.Root;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Future;
/**
* This class extends {@link AbstractVerticle} in order to provide a worker verticle. One must override the default {@link #start()} or {@link #start(io.vertx.core.Future)} methods.
*
* @author Pierrik Lassalas
*/
@SuppressWarnings("unused")
public class WorkerVerticle extends AbstractVerticle {
private final Root root;
private static final DeploymentOptions OPTIONS_NORMAL = new DeploymentOptions();
private static final DeploymentOptions OPTIONS_WORKER = new DeploymentOptions().setWorker(true).setMaxWorkerExecuteTime(Long.MAX_VALUE);
public WorkerVerticle() {
this(null);
}
public WorkerVerticle(Root root) {
this.root = root;
}
@Override
public void start(Future<Void> startFuture) throws Exception {
System.out.println("Worker thread: " + Thread.currentThread().getName());
start();
startFuture.complete();
}
@Override
public void start() throws Exception {
throw new IllegalStateException("You need to implement the start() method of the worker verticle.");
}
/**
* Deploy this verticle as a worker.
*
* @param errorMessage - the error message that will be displayed if the deployment fails
* @throws IllegalStateException when the verticle can not be deployed
*/
public void deployAsWorkerVerticle(String errorMessage) throws IllegalStateException {
GSVertx.vertx().getVertx().deployVerticle(this, OPTIONS_WORKER, res -> {
if (res.failed())
throw new IllegalStateException(errorMessage != null ? errorMessage : "Deployment of worker verticle failed.", res.cause());
});
}
}