我想知道如何使用Vertx在Java上编写程序来同时运行任务(调用远程服务器)100次,等待所有100个远程调用完成或失败然后分析结果。 我有一些想法,但我不确定它是否有效。 现在我只是使用random.nextBoolean而不是远程服务器调用。
我已经读过我可以创建100个Verticle并通过事件总线监听响应,但所有这些我需要定期运行所以我会遇到一些问题,确定哪个响应是针对哪个请求。
我不知道如何正确构建它。
如果您有一些建议,欢迎您。 非常感谢任何帮助。
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
WorkerExecutor sharedWorkerExecutor = vertx.createSharedWorkerExecutor("netstate-pool");
List<Future> works = new ArrayList<>(100);
Random random = new Random();
IntStream.iterate(0, operand -> ++operand).limit(100)
.forEach(itaration -> {
works.add(Future.future());
sharedWorkerExecutor.executeBlocking(future -> {
future.complete(random.nextBoolean());
}, false, asyncResult -> {
works.get(itaration).complete(asyncResult.result());
});
});
CompositeFuture.join(works).setHandler(result -> {
if (result.succeeded()) {
System.out.println(works.stream().map(future -> (Boolean) future.result()).filter(res -> res.equals(true)).count());
System.out.println(works.stream().map(future -> !(Boolean) future.result()).filter(res -> res.equals(true)).count());
} else {
System.out.println(works.stream().map(future -> (Boolean) future.result()).filter(res -> res.equals(true)).count());
System.out.println("fail at least one");
}
});
答案 0 :(得分:1)
也许这个例子可以让你开始:
import java.util.ArrayList;
import java.util.List;
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestOptions;
import io.vertx.ext.unit.TestSuite;
import io.vertx.ext.unit.report.ReportOptions;
public class HttpConcurrencyTest {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
TestSuite suite = TestSuite.create("HttpConcurrencyTest");
Integer requestCount = 100;
Integer responseDelay = 200;
Integer timeAllowed = 5000;
String title = String.format("Sends %d HTTP requests with a response delay of %dms each within less than %dms.", requestCount,
responseDelay, timeAllowed);
suite.test(title, context -> {
Async async = context.async();
HttpServer server = vertx.createHttpServer();
Long time = System.currentTimeMillis();
server.requestHandler(request -> {
vertx.setTimer(responseDelay, event -> {
request.response().end("ok");
});
});
Integer port = 8181;
server.listen(port, ar -> {
context.assertTrue(ar.succeeded(), "Server not started");
HttpClient client = vertx.createHttpClient();
List<Future> futures = new ArrayList<>();
for (int count = 0; count < requestCount; count++) {
Future future = Future.future();
futures.add(future);
}
CompositeFuture.all(futures).setHandler(result -> {
context.assertTrue(result.succeeded());
server.close();
Long duration = System.currentTimeMillis() - time;
context.assertTrue(duration < timeAllowed, duration + " >= " + timeAllowed);
async.complete();
System.exit(0);
});
futures.stream().forEach(future -> {
vertx.runOnContext(action -> {
client.getNow(port, "localhost", "/", result -> {
result.bodyHandler(body -> {
future.complete();
});
});
});
});
});
});
suite.run(new TestOptions().addReporter(new ReportOptions().setTo("console")));
}
}
在CompositeFuture.all(futures)
内,您可以分析所有个别电话的结果。
也可以在此处找到代码:https://github.com/thokari/vertx-http-concurrency