Binding different Grizzly HttpHandlers to different NetworkListeners

蹲街弑〆低调 提交于 2019-12-11 14:24:54

问题


The Grizzly documentation states that

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

Is there another way of binding different handlers to different ports? Or will I have to multiply instantiate HttpServer?


回答1:


You can bind it to a PortRange i.e multiple ports

NetworkListener(String name, String host, PortRange portRange) documented

or

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);

This way you can add multiple handlers for different ports

or (haven't tried but most probably work)

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


来源:https://stackoverflow.com/questions/14770674/binding-different-grizzly-httphandlers-to-different-networklisteners

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!