将不同的Grizzly HttpHandler绑定到不同的NetworkListener

时间:2013-02-08 10:38:47

标签: java grizzly

Grizzly documentation表示

  

添加到ServerConfiguration的所有HttpHandler都将被共享   所有听众

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

1 个答案:

答案 0 :(得分:1)

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

NetworkListener(String name, String host, 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()