Hi,
In case anyone has interest, here is a complete code example
of an async servlet making a non blocking call to a JS vertx verticle
along the EB, getting the response and passing the response back through
the servlet response. As far as I understanding it, the following template can be used to have non-blocking passing from servlet to vertx and then back through servlet.
-Adam
//------------ Servlet code ---------
@WebServlet(name = "Vertx", urlPatterns = {"/Vertx"}, asyncSupported = true)
public class Vertx extends HttpServlet {
private static io.vertx.core.Vertx vertx;
private static EventBus eb;
// careful here, make this part thread safe!!
public void init(ServletConfig config) throws ServletException {
super.init(config);
vertx = io.vertx.core.Vertx.vertx(new VertxOptions());
String pathToJS = "/home/adam/test/vertx/first/files/servletTest.js";
vertx.deployVerticle(pathToJS);
eb = vertx.eventBus();
System.out.println("finished init");
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try {
System.out.println("in servlet");
request.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
AsyncContext aCtx = request.startAsync(request, response);
eb.send("sayHi", "", ar -> {
if (ar.succeeded()) {
try {
String s = ar.result().body() + "";
PrintWriter writer = aCtx.getResponse().getWriter();
writer.println(s);
writer.flush();
writer.close();
aCtx.complete();
} catch (Exception e) {
e.printStackTrace();
}
}
});
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("sent message");
}
//------------ Vertx JS code with EB handler ---------
var eb = vertx.eventBus();
eb.consumer("sayHi", function (message) {
var s = message.body();
var out = "hi from vertx " + Date.now();
message.reply(JSON.stringify(out));
});
On Sunday, October 9, 2016 at 7:45:03 AM UTC-4, omid pourhadi wrote: