/** * Return a {@link Statistics} instance that can be used to gather * statistics. By default, the {@link Statistics} object <strong>is not</strong> * gathering statistics. Invoking {@link Statistics#startGatheringStatistics} * will do it. */ public Statistics getStatistics() { supportHttp(); if (statistics == null) { statistics = new Statistics(((SelectorThread) grizzlyListener)); } return statistics; }
/** * Stop grizzly server. * @throws java.lang.Exception */ public void stop() throws Exception { // stop the grizzly thread selector instance //threadSelector.stopEndpoint(); ws.stop(); }
/** * Create a WebServer that listen on port * @param port The port opened * @param webResourcesPath the path to the web resource (ex: /var/www) */ public GrizzlyWebServer(int port, String webResourcesPath){ createSelectorThread(port, 5); this.webResourcesPath = webResourcesPath; }
GrizzlyWebServer ws = new GrizzlyWebServer("/var/www"); try { ServletAdapter sa = new ServletAdapter(); sa.setRootFolder("/Path/To/Exploded/War/File"); sa.setServlet(new MyServlet()); ws.addGrizzlyAdapter(sa); ws.start(); } catch (IOException ex) { // Something when wrong. }
/** * Return an already configured {@link GrizzlyWebServer} that can serves * static resources. * @param path the directory to serve static resource from. * @return a ready to use {@link GrizzlyWebServer} that listen on port 8080 */ public final static GrizzlyWebServer newConfiguredInstance(String path){ GrizzlyWebServer ws = new GrizzlyWebServer(8080); ws.addGrizzlyAdapter(new GrizzlyAdapter(path){ { setHandleStaticResources(true); } @Override public void service(GrizzlyRequest request, GrizzlyResponse response) { try { response.setStatus(404); response.flushBuffer(); } catch (IOException ex) { Logger.getLogger(GrizzlyWebServer.class.getName()).log( Level.SEVERE, null, ex); } } }); return ws; }
/** * Create a WebServer that listen for secure tls/https requests * @param host The host to listen on. * @param port The port to listen on. * @param maxThreads The maximum number of Thread created * @param webResourcesPath the path to the web resource (ex: /var/www) * @param secure <tt>true</tt> if https needs to be used. */ protected GrizzlyWebServer(String host, int port, int maxThreads, String webResourcesPath, boolean secure) throws UnknownHostException { this.host = host; this.port = port; createSelectorThread(host, port, secure); setMaxThreads(maxThreads); this.webResourcesPath = webResourcesPath; }
/** * Return a {@link Statistics} instance that can be used to gather * statistics. By default, the {@link Statistics} object <strong>is not</strong> * gathering statistics. Invoking {@link Statistics#startGatheringStatistics} * will do it. */ public Statistics getStatistics(){ if (statistics == null){ statistics = new Statistics(st); } return statistics; }
/** * Return the underlying {@link SelectorThread}. Only advanced users * should manipulate that class. * @return {@link SelectorThread} */ public SelectorThread getSelectorThread(){ supportHttp(); return (SelectorThread)grizzlyListener; }
/** * Undeploy previously deployed deployable. * * @param gws Grizzly to undeploy from. * @param deploymentId Deployment identification. */ public final void undeploy(GrizzlyWebServer gws, DeploymentID deploymentId) { final Set<GrizzlyAdapter> adapters = deployed.get(deploymentId); for (GrizzlyAdapter adapter : adapters) { gws.removeGrizzlyAdapter(adapter); // TODO removal can go wrong } }
/** * Stop the GrizzlyWebServer. */ public void stop(){ if (!isStarted) return; isStarted = false; if (grizzlyListener instanceof SelectorThread){ ((SelectorThread)grizzlyListener).stopEndpoint(); } // need to stop gathering (there are threads running for stats collection) if (statistics != null) { statistics.stopGatheringStatistics(); } }
/** * Create a WebServer which server files from {@link #webResourcesPath} * @param webResourcesPath the path to the web resource (ex: /var/www) */ public GrizzlyWebServer(String webResourcesPath){ createSelectorThread(defaultPort, 5); this.webResourcesPath = webResourcesPath; }
/** * Create a WebServer that listen for secure tls/https requests * @param port The port to listen on * @param maxThreads The maximum number of Thread created * @param webResourcesPath the path to the web resource (ex: /var/www) * @param secure <tt>true</tt> if https needs to be used. * * @deprecated use {@link #setMaxThreads(int)} to set maximum number of * threads in a thread pool */ public GrizzlyWebServer(int port, int maxThreads, String webResourcesPath, boolean secure) { this.host = DEFAULT_HOST; this.port = port; createSelectorThread(port, secure); setMaxThreads(maxThreads); this.webResourcesPath = webResourcesPath; }
@Override public void stop() { if (this.gws != null) { this.gws.stop(); } Logger.info(this, "stopped on port #%s", this.port); this.port = 0; }
/** * Set the maximum number of threads in a thread pool. * @param maxThreads the maximum number of threads in a thread pool. */ public void setMaxThreads(int maxThreads) { supportHttp(); ((SelectorThread)grizzlyListener).setMaxThreads(maxThreads); }
/** * Create a default GrizzlyWebServer */ public GrizzlyWebServer(){ createSelectorThread(defaultPort,5); }
/** * Set the initial number of threads in a thread pool. * @param coreThreads the initial number of threads in a thread pool. */ public void setCoreThreads(int coreThreads) { supportHttp(); ((SelectorThread)grizzlyListener).setCoreThreads(coreThreads); }
/** * Create a WebServer that listen on port * @param port The port opened */ public GrizzlyWebServer(int port){ createSelectorThread(port,5); }
/** * Create a WebServer that listen on port * @param port The port opened */ public GrizzlyWebServer(int port, int maxThreads){ createSelectorThread(port, maxThreads); }
/** * Create a WebServer that listen on port * @param port The port opened * @param maxThreads The maximum number of Thread created * @param webResourcesPath the path to the web resource (ex: /var/www) */ public GrizzlyWebServer(int port, int maxThreads, String webResourcesPath){ createSelectorThread(port, maxThreads); this.webResourcesPath = webResourcesPath; }
/** * Create an underlying {@link SelectorThread} * @param port The port to listen on. * @param secure <tt>true</tt> if https needs to be used. */ private void createSelectorThread(int port, boolean secure) { try { createSelectorThread(DEFAULT_HOST, port, secure); } catch (UnknownHostException e) { throw new IllegalStateException("Unexpected exception", e); } }