gpt4 book ai didi

java - 将不同的 Grizzly HttpHandler 绑定(bind)到不同的 NetworkListener

转载 作者:行者123 更新时间:2023-12-01 14:51:52 27 4
gpt4 key购买 nike

Grizzly documentation指出

all HttpHandlers added to the ServerConfiguration will be shared across all listeners

是否有另一种方法将不同的处理程序绑定(bind)到不同的端口?或者我必须多次实例化HttpServer

最佳答案

您可以将其绑定(bind)到PortRange,即多个端口

NetworkListener(字符串名称、字符串主机、PortRange portRange) documented

HttpServer httpServer = new HttpServer();

NetworkListener networkListener1 = new NetworkListener("sample-listener1", "localhost", 8888);
NetworkListener networkListener2 = new NetworkListener("sample-listener2", "localhost", 8889);
httpServer.addListener(networkListener1);
httpServer.addListener(networkListener2);

这样您就可以为不同的端口添加多个处理程序

或者(没有尝试过,但很可能有效)

HttpServer server = HttpServer.createSimpleServer();
server.getServerConfiguration().addHttpHandler(handler1);
server.getServerConfiguration().addHttpHandler(handler2);
server.start()

关于java - 将不同的 Grizzly HttpHandler 绑定(bind)到不同的 NetworkListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14770674/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com