WebServer.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package hello;
  2. import java.io.IOException;
  3. import java.net.URI;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6. import org.glassfish.grizzly.filterchain.FilterChainBuilder;
  7. import org.glassfish.grizzly.http.server.AddOn;
  8. import org.glassfish.grizzly.http.server.FileCacheFilter;
  9. import org.glassfish.grizzly.http.server.HttpServer;
  10. import org.glassfish.grizzly.http.server.NetworkListener;
  11. import org.glassfish.grizzly.http.server.util.HttpPipelineOptAddOn;
  12. import org.glassfish.grizzly.memory.PooledMemoryManager;
  13. import org.glassfish.grizzly.nio.transport.TCPNIOTransport;
  14. import org.glassfish.grizzly.utils.IdleTimeoutFilter;
  15. import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
  16. import org.glassfish.jersey.server.ResourceConfig;
  17. public class WebServer {
  18. private static final URI BASE_URI = URI.create("http://0.0.0.0:8080/");
  19. public static void main(String[] args) {
  20. try {
  21. final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI,
  22. createApp(), false);
  23. Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
  24. @Override
  25. public void run() {
  26. server.shutdownNow();
  27. }
  28. }));
  29. // Some modifications
  30. NetworkListener defaultListener = server.getListener("grizzly");
  31. defaultListener.getKeepAlive().setIdleTimeoutInSeconds(-1);
  32. defaultListener.getKeepAlive().setMaxRequestsCount(-1);
  33. defaultListener.getFileCache().setEnabled(false);
  34. defaultListener.registerAddOn(new SimplifyAddOn());
  35. defaultListener.registerAddOn(new HttpPipelineOptAddOn());
  36. final TCPNIOTransport transport = defaultListener.getTransport();
  37. transport.setWorkerThreadPoolConfig(null); // force to not
  38. // initialize worker
  39. // thread pool
  40. transport.setSelectorRunnersCount(Runtime.getRuntime().availableProcessors() * 2);
  41. transport.setMemoryManager(new PooledMemoryManager());
  42. server.start();
  43. System.out.println(String
  44. .format("TFBApplication started.%nStop the application using CTRL+C"));
  45. Thread.currentThread().join();
  46. } catch (IOException | InterruptedException ex) {
  47. Logger.getLogger(WebServer.class.getName()).log(Level.SEVERE, null, ex);
  48. }
  49. }
  50. public static ResourceConfig createApp() {
  51. return new TFBApplication();
  52. }
  53. private static class SimplifyAddOn implements AddOn {
  54. @Override
  55. public void setup(final NetworkListener networkListener, final FilterChainBuilder builder) {
  56. final int fcIdx = builder.indexOfType(FileCacheFilter.class);
  57. if (fcIdx != -1) {
  58. builder.remove(fcIdx);
  59. }
  60. final int itIdx = builder.indexOfType(IdleTimeoutFilter.class);
  61. if (itIdx != -1) {
  62. builder.remove(itIdx);
  63. }
  64. }
  65. }
  66. }