-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncServletBridge.java
139 lines (121 loc) · 5.28 KB
/
AsyncServletBridge.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package org.vertxservlet;
import io.vertx.core.eventbus.EventBus;
import java.io.PrintWriter;
import javax.servlet.http.*;
import javax.servlet.AsyncContext;
import java.util.*;
import java.util.concurrent.*;
import io.vertx.core.*;
import java.util.function.*;
public class AsyncServletBridge {
private Vertx vertx;
private HttpServletRequest request;
private HttpServletResponse response;
public AsyncServletBridge(Vertx vertx, HttpServletRequest request, HttpServletResponse response) {
this.vertx = vertx;
this.request = request;
this.response = response;
}
/**
* *
* Send a route and message to the EventBus of this.vertx. the reply sent
* back from the EventBus is written back to the http connection of
* HttpServletReponse. This method starts the Async Context on the servlet
* and automatically completes the Async Context when the reply is written
* back to client.
*
* @param route the route for the EventBus
* @param message the message sent along the route
*/
public void asyncSend(String route, String message) {
AsyncServletBridge.send(this.vertx, request, response, route, message, (aWriter, body) -> {
aWriter.writeAndComplete(body);
});
}
/**
* *
* Send a route and message to the EventBus of this.vertx. the reply sent
* back from the EventBus and passed to the handler to be formated and sent
* through the servlet's httpConnection This method starts the Async Context
* on the servlet and completes the asyncContext when the handler's
* asyncWriter calls writeAndComplete() or complete().
*
* <p>
* example<p>
* <
* pre>
* asyncServletBridge.asyncSend("myRoute", "messageContent", (asyncWriter,
* reply) -> { String finalOutput = reply + ", modified before sending";
* asyncWriter.writeAndComplete(finalOutput); });
* </pre>
*
*
* @param route the route for the EventBus
* @param message the message sent along the route
* @param handler passed the AsyncWriter to write back along the servlet's
* http connection, is called on a Vert.x execute blocking thread
*/
public void asyncSend(String route, String message, BiConsumer<AsyncWriter, String> handler) {
AsyncServletBridge.send(this.vertx, request, response, route, message, handler);
}
/**
* The servlet parameters "route" and "message" are used for the route and
* message. The reply sent back from the EventBus and passed to the handler
* to be formated and sent through the servlet's httpConnection This method
* starts the Async Context on the servlet and completes the asyncContext
* when the handler's asyncWriter calls writeAndComplete() or complete().
*
* <p>
* example<p>
* <
* pre>
* asyncServletBridge.asyncSend("myRoute", "messageContent", (asyncWriter,
* reply) -> { String finalOutput = reply + ", modified before sending";
* asyncWriter.writeAndComplete(finalOutput); });
* </pre>
*
*
* @param route the route for the EventBus
* @param message the message sent along the route
* @param handler passed the AsyncWriter to write back along the servlet's
* http connection, is called on a Vert.x execute blocking thread
*/
public static void asyncPassByParams(Vertx vertx, HttpServletRequest request, HttpServletResponse response) {
String route = request.getParameter("route");
String message = request.getParameter("message");
send(vertx, request, response, route, message, (aWriter, body) -> {
aWriter.writeAndComplete(body);
});
}
static void send(Vertx vertx, HttpServletRequest request, HttpServletResponse response, String route, String message, BiConsumer<AsyncWriter, String> consumer) {
AsyncContext aContext = request.startAsync(request, response);
EventBus eb = vertx.eventBus();
aContext.start(() -> {
if (route != null) {
eb.request(route, message, ar -> {
String body = "";
AsyncContext context = aContext;
if (ar.succeeded()) {
body = ar.result().body().toString();
} else {
body = "AsyncServletBridge.send error on EventBus reply: " + ar.toString();
}
try {
final String outputString = body;
vertx.executeBlocking(promise -> {
AsyncWriter aWriter = new AsyncWriter(request, response);
consumer.accept(aWriter, outputString);
}, false, res -> {
});
} catch (Exception e) {
e.printStackTrace();
}
});
} else {
String outputString = "AsyncServletBridge.send null route";
AsyncWriter aWriter = new AsyncWriter(request, response);
consumer.accept(aWriter, outputString);
}
});
}
}