Java 多线程Vert.x每秒处理数千个连接
我正在构建一个类似messenger的服务器,我对vert.x做了如下更改Java 多线程Vert.x每秒处理数千个连接,java,vert.x,Java,Vert.x,我正在构建一个类似messenger的服务器,我对vert.x做了如下更改 vertx.deployVerticle(MainDeployment.class.getCanonicalName(),res -> { if (res.succeeded()) { Logger.Log("Deployed"); } else { Logger.Log(res.cause().ge
vertx.deployVerticle(MainDeployment.class.getCanonicalName(),res -> {
if (res.succeeded()) {
Logger.Log("Deployed");
} else {
Logger.Log(res.cause().getMessage());
}
});
server.requestHandler(router::accept).listen(port);
public void createContext(String path,MyHttpHandler handler){
//handlersMap.put(path.split("\\/")[1], handler);
if(!path.endsWith("/")){
path += "/";
}
path+="*";
map.put(path, handler);
}
这是我的主要部署类
public class MainDeployment extends AbstractVerticle{
@Override
public void start() throws Exception {
//GUI.display("Sub Verticle Has Deployed");
// Different ways of deploying verticles
// Deploy a verticle and don't wait for it to start
for(Entry<String, MyHttpHandler> entry : MyVertxServer.map.entrySet()){
MyVertxServer.router.route(entry.getKey()).handler(new Handler<RoutingContext>() {
@Override
public void handle(RoutingContext ctx) {
String[] handlerID = ctx.request().uri().split(ctx.currentRoute().getPath());
String suffix = handlerID.length > 1 ? handlerID[1] : null;
entry.getValue().Handle(ctx, new VertxUtils(), suffix);
}
});
}
}
}
因为每个verticle都有自己的线程,并且都是单线程的,所以如果我的verticle上载文件,它不能同时处理2次上载,或者我误解了NIO的概念?我想你应该深入阅读Vert.x文档,以便更熟悉支持API和设计 我极力建议的官方文件如下: 即使Vertx实例维护多个事件循环,任何特定的处理程序也永远不会并发执行,并且在大多数情况下,除了辅助垂直轴之外,将始终使用完全相同的事件循环调用 您尝试实施的文件上载过程被视为阻塞操作,因此应执行: 或者通过使用阻塞API VertXecuteBlock 或者在可以通过VertxcreateSharedWorkerExecutor调用获得的工作池中
这两种方式都会导致事件循环线程不会因阻塞操作而停止。我正在说明事件循环线程,但实际上我们讨论的是所有的事件循环线程,因为它们可能不止一个。提供的源代码与多线程问题有何关系?据我所知,没有什么,但它们都提到了基本用法。有太多的例子,但没有一个能提供这样的信息,所以vertx对于大型文件服务器是没有用的?的确如此,但你应该仔细设计你的应用程序,这样它就可以根据需要进行扩展。甚至一个文件上传过程也被认为是阻塞的,有许多工作人员落后,加上集群配置可以满足您的要求。非常感谢。因此,我将与executor一起处理持久工作,并使用Pump和sendFile发送、接收文件
Vert.x guarantees that a particular verticle instance is never executed by more than one thread concurrently. This gives you a huge advantage as a developer, since you can program all your code as single threaded