Duplicate message

130 views
Skip to first unread message

007r...@gmail.com

unread,
Sep 26, 2015, 11:22:19 PM9/26/15
to vert.x
From a newbie looking for your expertise,

I am starting to experiment with Vert.x and use the examples code. I got 1 main verticle (HTTP front-end) which calls two worker verticles that are doing some blocking work. The verticles communicate via EventBus. Everything work, but I am getting duplicate request/reply. Could you please help?

public class Main extends AbstractVerticle {
public static void main(String[] args) {
VertxOptions vOptions = new VertxOptions();
vOptions.setWorkerPoolSize(10);
Vertx vertx = Vertx.vertx(vOptions);
DeploymentOptions options = new DeploymentOptions().setWorker(true);
vertx.deployVerticle(new MainVertical(), options);
options = new DeploymentOptions().setWorker(false);
vertx.deployVerticle(new WorkerVertical(), options);
    }
}

public class MainVertical extends AbstractVerticle {
@Override
public void start(Future<Void> fut) {
HttpServer server = vertx.createHttpServer();
Router router = Router.router(vertx);

router.route().blockingHandler(
routingContext -> {
StringBuffer ret = new StringBuffer();
HttpServerResponse response = routingContext.response();
String requestToWorker = "hi worker";
System.out.println("[Main] Sending request '" + requestToWorker + "' in " + Thread.currentThread().getName());

vertx.eventBus().send("worker.verticle", requestToWorker, replyFromWorker -> {
System.out.println("[Main] Receiving reply ' " + replyFromWorker.result().body()+ "' in "+ Thread.currentThread().getName());
});
System.out.println(ret.toString());
response.putHeader("content-type", "text/plain");
response.end(ret.toString());
}, false);
server.requestHandler(router::accept).listen(8080);
System.out.println(Thread.currentThread().getName()+ " HTTP server started");
}
}

public class WorkerVertical extends AbstractVerticle {

@Override
public void start(Future<Void> fut) {
vertx.eventBus().consumer("worker.verticle",message -> {
System.out.println("[Worker] Consuming data in " + Thread.currentThread().getName());
String body = (String) message.body();
message.reply(body.toUpperCase());
});
}
}

As you can see in the code, I am sending 2 message to the Worker, but after running "curl localhost:8080", I see two sends and two receives::
[Main] Sending request 'hi worker' in vert.x-worker-thread-76

[Worker] Consuming data in vert.x-eventloop-thread-4
[Main] Receiving reply ' HI WORKER' in vert.x-worker-thread-58
Beginning
[Main] Sending request 'hi worker' in vert.x-worker-thread-7

[Worker] Consuming data in vert.x-eventloop-thread-4
[Main] Receiving reply ' HI WORKER' in vert.x-worker-thread-7


What am I doing incorrect?

Thank you in advance.

Tim Fox

unread,
Sep 28, 2015, 1:34:52 PM9/28/15
to ve...@googlegroups.com
You could try logging out the path to see what is being received at the server...

Also.. any reason why you are using a blocking handler? I can't see anything blocking in that code.
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at http://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/ce7ad65d-28d7-40ba-948f-f854343956b0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

007r...@gmail.com

unread,
Sep 28, 2015, 5:24:51 PM9/28/15
to vert.x
In terms of blocking, i will have a blocking operation in the Worker when move from this example to the actual code. 

I actually fixed the issue although it remains a bit of mystery. I incorrectly started the MainVerticle as as worker verticle and the WorkerVerticle as a non-worker. When I fixed this issue, the duplicate message disappeared

WAS:
DeploymentOptions options = new DeploymentOptions().setWorker(true);
vertx.deployVerticle(new MainVertical(), options);
options = new DeploymentOptions().setWorker(false);
vertx.deployVerticle(new WorkerVertical(), options);

NOW (working ok):
DeploymentOptions options = new DeploymentOptions().setWorker(false);
vertx.deployVerticle(new MainVertical(), options);
options = new DeploymentOptions().setWorker(true);
vertx.deployVerticle(new WorkerVertical(), options);

Tim Fox

unread,
Sep 28, 2015, 5:31:20 PM9/28/15
to ve...@googlegroups.com
Ok, feel free to open an issue and someone will investigate.
Reply all
Reply to author
Forward
0 new messages