问题
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