我在vertx java 中创建了一个 java 后端服务。我使用启用了连接池的httpClient ( io.vertx.core.http.HttpClient)来连接到外部服务。我不包括50 的吞吐量。对于对我的服务的每个请求,我都需要连接到外部服务。我的服务的平均响应时间为 4 秒,外部服务的平均响应时间约为 3 秒。
现在我的问题是
maxPoolSize和maxWaitQueueSize取值?maxPoolSize和maxWaitQueueSize价值?maxPoolSize和的最大值是maxWaitQueueSize多少?setPipelining选项HttpClient吗?我试图单元测试从vertx一个HTTP调用WebClientuing VertxUnitRunner和vertx的RXified版本.
问题是我的单元测试总是因超时异常而失败.是否有不同的单元测试WebClienthttp调用方式?以下是我的代码:
import io.vertx.core.AsyncResult;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import io.vertx.rxjava.core.Vertx;
import io.vertx.rxjava.core.buffer.Buffer;
import io.vertx.rxjava.core.http.HttpServer;
import io.vertx.rxjava.ext.web.client.HttpResponse;
import io.vertx.rxjava.ext.web.client.WebClient;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import rx.Single;
@RunWith(VertxUnitRunner.class)
public class MyVertxTest {
private Vertx vertx;
private WebClient client;
@Before
public void setUp() throws Exception {
vertx = Vertx.vertx();
}
@Test
public void testGetContactDetails(TestContext context) {
System.out.println("start");
long start = System.currentTimeMillis();
HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(TEST_SERVER_PORT));
server.requestStream().handler(req -> {
req.response().setChunked(true).write("foo bar").end();
}); …Run Code Online (Sandbox Code Playgroud) 在 Vert.x Web 客户端手册中,有一个将传入的 JSON 响应解码为 POJO 的示例:
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.as(BodyCodec.json(User.class))
.send(ar -> {
// Process the response
})
Run Code Online (Sandbox Code Playgroud)
有没有办法将传入的 JSON 数组解码为对象集合?
我正在VertX中创建cookie,并希望在用户注销后再次删除它们.
AccountController.handleLogin(vertx, router.post("/login"))
Run Code Online (Sandbox Code Playgroud)
...
fun handleLogin(vertx: Vertx, route: Route) {
route.handler { rtx ->
rtx.request().bodyHandler { btx ->
vertx.executeBlocking<Login>({
it.complete(AccountController.login(Json.decodeValue(String(btx.bytes), Login::class.java)))
}, {
if (it.succeeded()) {
// set some cookies
rtx.addCookie(Cookie.cookie("atom-session", it.result().session).setHttpOnly(true).setSecure(secure))
Run Code Online (Sandbox Code Playgroud)
现在可以在Chrome中看到该Cookie:
当我想再次删除该cookie时:
AccountController.handleLogout(vertx, router.post("/logout"))
Run Code Online (Sandbox Code Playgroud)
...
fun handleLogout(vertx: Vertx, route: Route) {
route.handler { rtx ->
rtx.request().bodyHandler { btx ->
vertx.executeBlocking<Logout>({
val logout = Json.decodeValue(String(btx.bytes), Logout::class.java)
it.complete(AccountController.logout(logout))
}, {
if (it.succeeded()) {
log.info("Cookies Will No Be Removed ...")
rtx.removeCookie("atom-session")
log.info("DONE!")
Run Code Online (Sandbox Code Playgroud)
我可以看到正在打印的消息说将删除cookie,但是当我刷新Chrome中的资源时,登录时设置的所有cookie仍然存在.包含atom-session
我这样做错了还是VertX中的一个错误?
我们如何处理 vertx HttpClientRequest 中的重定向(302 响应代码)。是否可以让 vertx 本身处理重定向,或者我们必须显式处理。如果需要明确执行,请解释如何执行。
我是 vertx 和 RxJava 的新手。我正在尝试实现一个简单的测试程序。但是,我无法理解这个程序的动态。为什么有些请求需要 10 秒以上才能响应?
以下是我的示例测试应用程序
public class Test {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
WebClient webClient = WebClient.create(vertx);
Observable < Object > google = hitURL("www.google.com", webClient);
Observable < Object > yahoo = hitURL("www.yahoo.com", webClient);
for (int i = 0; i < 100; i++) {
google.repeat(100).subscribe(timeTaken -> {
if ((Long) timeTaken > 10000) {
System.out.println(timeTaken);
}
}, error -> {
System.out.println(error.getMessage());
});
yahoo.repeat(100).subscribe(timeTaken -> {
if ((Long) timeTaken > 10000) {
System.out.println(timeTaken);
} …Run Code Online (Sandbox Code Playgroud) 如何从Java Vertex连接Cex.IO Websocket API?
问题是Vert.x不能像Node.JS那样为我提供仅与WsURI连接的方法。我必须指定端口和主机并获取HTTP 400 Bad Request异常。
使用Node.js,您可以:
var WebSocketClient = require('websocket').client;
var client = new WebSocketClient();
client.connect("wss://ws.cex.io/ws/");
Run Code Online (Sandbox Code Playgroud)
使用Vert.x,您必须做
int host = 443; // That's defaults
String host = "cex.io"; // Am I right by specifying this host?
HttpClient client = Vertx.vertx().createHttpClient();
client.websocket(port, host, "wss://ws.cex.io/ws/", ws -> { ...});
Run Code Online (Sandbox Code Playgroud)