Java Code Examples for org.glassfish.grizzly.http.server.HttpServer#shutdownNow()

The following examples show how to use org.glassfish.grizzly.http.server.HttpServer#shutdownNow() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GrizzlyHttpServerITest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws IOException {
  final HttpServer server = new HttpServer();
  final NetworkListener listener = new NetworkListener("grizzly", DEFAULT_NETWORK_HOST, 18906);
  server.addListener(listener);
  server.start();

  server.getServerConfiguration().addHttpHandler(new HttpHandler() {
    @Override
    public void service(final Request request, final Response response) {
      TestUtil.checkActiveSpan();
      response.setStatus(200);
    }
  });

  int responseCode = -1;
  try {
    final URL url = new URL("http://localhost:18906/");
    final HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.setRequestMethod("GET");
    responseCode = connection.getResponseCode();
    connection.disconnect();
  }
  finally {
    server.shutdownNow();

    if (200 != responseCode)
      throw new AssertionError("ERROR: response: " + responseCode);

    TestUtil.checkSpan(true, new ComponentSpanCount("java-grizzly-http-server", 1), new ComponentSpanCount("http-url-connection", 1));
  }
}
 
Example 2
Source File: RpcServerManager.java    From nuls-v2 with MIT License 5 votes vote down vote up
public void startServer(String ip, int port) {
    URI serverURI = UriBuilder.fromUri("http://" + ip).port(port).build();
    // Create test web application context.
    WebappContext webappContext = new WebappContext("NULS-V2-SDK-PROVIDER-SERVER", "/");

    ServletRegistration servletRegistration = webappContext.addServlet("jersey-servlet", ServletContainer.class);
    servletRegistration.setInitParameter("javax.ws.rs.Application", "io.nuls.provider.api.config.NulsResourceConfig");
    servletRegistration.addMapping("/*");

    httpServer = new HttpServer();
    NetworkListener listener = new NetworkListener("grizzly2", ip, port);
    TCPNIOTransport transport = listener.getTransport();
    ThreadPoolConfig workerPool = ThreadPoolConfig.defaultConfig()
            .setCorePoolSize(4)
            .setMaxPoolSize(4)
            .setQueueLimit(1000)
            .setThreadFactory((new ThreadFactoryBuilder()).setNameFormat("grizzly-http-server-%d").build());
    transport.configureBlocking(false);
    transport.setSelectorRunnersCount(2);
    transport.setWorkerThreadPoolConfig(workerPool);
    transport.setIOStrategy(WorkerThreadIOStrategy.getInstance());
    transport.setTcpNoDelay(true);
    listener.setSecure(false);
    httpServer.addListener(listener);

    ServerConfiguration config = httpServer.getServerConfiguration();
    config.setDefaultQueryEncoding(Charsets.UTF8_CHARSET);

    webappContext.deploy(httpServer);

    try {
        ClassLoader loader = this.getClass().getClassLoader();
        httpServer.start();
        Log.info("http restFul server is started!url is " + serverURI.toString());
    } catch (IOException e) {
        Log.error(e);
        httpServer.shutdownNow();
    }
}
 
Example 3
Source File: App.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, new JerseyApplication());

    System.out.println("Press any key to close");
    System.in.read();
    server.shutdownNow();
}
 
Example 4
Source File: SrvTakeTest.java    From takes with MIT License 5 votes vote down vote up
@Test
public void executeATakesAsAServlet() throws Exception {
    final String name = "webapp";
    final HttpServer server = HttpServer.createSimpleServer("./", 18080);
    final WebappContext context = new WebappContext(name);
    final ServletRegistration servlet = context.addServlet(
        "takes",
        SrvTake.class
    );
    servlet.setInitParameter("take", TkApp.class.getName());
    servlet.addMapping("/test");
    context.deploy(server);
    server.start();
    new JdkRequest("http://localhost:18080/test")
        .fetch()
        .as(RestResponse.class)
        .assertStatus(HttpURLConnection.HTTP_OK)
        .assertBody(
            new StringContains(
                new FormattedText(
                    SrvTakeTest.MSG,
                    name
                ).asString()
            )
        );
    server.shutdownNow();
}