I am new to Vert.x and I want to run multiple verticles through jar. I have two files, one is MyFirstVertice.java and it routes the path "/q1/" and return something. The second is MySecondVertice.java which routes the path "/q2/". The second vertice is deployed in the first vertice.
MyFirstVertice.java
public class MyFirstVerticle extends AbstractVerticle {
@Override
public void start(Future<Void> fut) throws Exception {
HttpServer server = vertx.createHttpServer();
Router router = Router.router(vertx);
router.route("/q1/*").handler(routingContext -> {
HttpServerRequest request = routingContext.request();
String Y = request.getParam("key");
String cipherText = request.getParam("message");
HttpServerResponse response = routingContext.response();
response.setChunked(true);
response.putHeader("content-type", "text/plain");
response.write(Y + "\n");
response.write(cipherText + "\n");
response.end();
vertx.deployVerticle(new MySecondVerticle(), stringAsyncResult -> {
System.out.println("Second verticle is deployed successfully.");
});
});
server.requestHandler(router::accept).listen(8080, httpServerAsyncResult -> {
if (httpServerAsyncResult.succeeded()) {
fut.complete();
} else {
fut.fail(httpServerAsyncResult.cause());
}
});
}
}
MySecondVetice.java
public class MySecondVerticle extends AbstractVerticle {
@Override
public void start(Future<Void> fut) throws Exception {
HttpServer server = vertx.createHttpServer();
Router router = Router.router(vertx);
router.route("/q2/*").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.setChunked(true);
response.putHeader("content-type", "text/plain");
response.end("q2");
});
server.requestHandler(router::accept).listen(8080, httpServerAsyncResult -> {
if (httpServerAsyncResult.succeeded()) {
fut.complete();
} else {
fut.fail(httpServerAsyncResult.cause());
}
});
}
}
My pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>io.vertx.core.Starter</Main-Class>
<Main-Verticle>tutorial.diluo.MyFirstVerticle</Main-Verticle>
</manifestEntries>
</transformer>
</transformers>
<artifactSet/>
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
I run it through java -jar xxx-fat.jar.
When I type localhost:8080/q1/xxx in the browser, it can return desired content. But when I am trying to visit localhost:8080/q2/xxx, it says "Resource Not Found". Can you tell me how to deploy two verticles that route different paths? I know that I can route different path in the same verticles, I just want to know how to deploy and run multiple vertices. Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire