org.glassfish.grizzly.nio.transport.TCPNIOTransport Java Examples

The following examples show how to use org.glassfish.grizzly.nio.transport.TCPNIOTransport. 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: JsonRpcServer.java    From nuls-v2 with MIT License 5 votes vote down vote up
public void startServer(String ip, int port) {
    initRpcMethodHandlers();
    this.httpServer = new HttpServer();
    NetworkListener listener = new NetworkListener("NULS-RPC", ip, port);
    TCPNIOTransport transport = listener.getTransport();
    ThreadPoolConfig workerPool = ThreadPoolConfig.defaultConfig()
            .setCorePoolSize(8)
            .setMaxPoolSize(8)
            .setQueueLimit(2000)
            .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.addHttpHandler(new JsonRpcHandler());
    config.setDefaultQueryEncoding(Charsets.UTF8_CHARSET);

    try {
        httpServer.start();
    } catch (IOException e) {
        LoggerUtil.commonLog.error(e);
        httpServer.shutdownNow();
    }
}
 
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: RestServer.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
private void configureWorkerThreadPool(final NetworkListener networkListener, final int workerThreads) {
    final TCPNIOTransport transport = networkListener.getTransport();

    if (transport.getWorkerThreadPoolConfig() == null) {
        transport.setWorkerThreadPoolConfig(ThreadPoolConfig.defaultConfig());
    }
    transport.getWorkerThreadPoolConfig().setMaxPoolSize(workerThreads).setCorePoolSize(workerThreads);
}
 
Example #4
Source File: WebServer.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) {
	try {
		final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI,
				createApp(), false);
		Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
			@Override
			public void run() {
				server.shutdownNow();
			}
		}));

		// Some modifications
		NetworkListener defaultListener = server.getListener("grizzly");
		defaultListener.getKeepAlive().setIdleTimeoutInSeconds(-1);
		defaultListener.getKeepAlive().setMaxRequestsCount(-1);
		defaultListener.getFileCache().setEnabled(false);
		defaultListener.registerAddOn(new SimplifyAddOn());
		defaultListener.registerAddOn(new HttpPipelineOptAddOn());

		final TCPNIOTransport transport = defaultListener.getTransport();
		transport.setWorkerThreadPoolConfig(null); // force to not
													// initialize worker
													// thread pool
		transport.setSelectorRunnersCount(Runtime.getRuntime().availableProcessors() * 2);
		transport.setMemoryManager(new PooledMemoryManager());

		server.start();

		System.out.println(String
				.format("TFBApplication started.%nStop the application using CTRL+C"));

		Thread.currentThread().join();
	} catch (IOException | InterruptedException ex) {
		Logger.getLogger(WebServer.class.getName()).log(Level.SEVERE, null, ex);
	}
}
 
Example #5
Source File: RpcServerManager.java    From nuls-v2 with MIT License 4 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-RPC-SERVER", "/api");

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

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

        addSwagerUi(loader);
        addClientUi(loader);

        httpServer.start();
        log.info("http restFul server is started!url is " + serverURI.toString());
    } catch (IOException e) {
        log.error("",e);
        httpServer.shutdownNow();
    }
}
 
Example #6
Source File: RpcServerManager.java    From nuls with MIT License 4 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-RPC-SERVER", "/api");

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

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

        addSwagerUi(loader);
        addClientUi(loader);

        httpServer.start();
        Log.info("http restFul server is started!url is " + serverURI.toString());
    } catch (IOException e) {
        Log.error(e);
        httpServer.shutdownNow();
    }
}
 
Example #7
Source File: GrizzlyEchoServer.java    From Chronicle-Network with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {
    // Create a FilterChain using FilterChainBuilder
    @NotNull FilterChainBuilder filterChainBuilder = FilterChainBuilder.stateless();

    // Add TransportFilter, which is responsible
    // for reading and writing data to the connection
    filterChainBuilder.add(new TransportFilter());

    // EchoFilter is responsible for echoing received messages
    filterChainBuilder.add(new BaseFilter() {
        @Override
        public NextAction handleRead(@NotNull FilterChainContext ctx) {
            // Peer address is used for non-connected UDP Connection :)
            final Object peerAddress = ctx.getAddress();
            final Object message = ctx.getMessage();

            ctx.write(peerAddress, message, null);

            return ctx.getStopAction();
        }
    });

    // Create TCP transport
    final TCPNIOTransport transport = newInstance().build();

    transport.setProcessor(filterChainBuilder.build());
    try {
        // binding transport to start listen on certain host and port
        transport.bind(PORT);

        // start the transport
        transport.start();

        countDownLatch.await(50, TimeUnit.SECONDS);
    } finally {
        LOG.info("Stopping transport...");
        // stop the transport
        transport.shutdownNow();

        LOG.info("Stopped transport...");
    }
}
 
Example #8
Source File: Server.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void main(String[] args) throws Exception {
	final int port = args.length > 0 ? Integer.parseInt(args[0]) : 8080;

	final HttpServer httpServer = new HttpServer();

	final NetworkListener networkListener = new NetworkListener("http-listener", "0.0.0.0",
			port);
	final TCPNIOTransport transport = networkListener.getTransport();

	// force to not initialize worker thread pool
	transport.setWorkerThreadPoolConfig(null);
	transport.setSelectorRunnersCount(Runtime.getRuntime().availableProcessors() * 2);

	// set PooledMemoryManager
	transport.setMemoryManager(new PooledMemoryManager());

	// always keep-alive
	networkListener.getKeepAlive().setIdleTimeoutInSeconds(-1);
	networkListener.getKeepAlive().setMaxRequestsCount(-1);

	// disable transaction timeout
	networkListener.setTransactionTimeout(-1);

	// remove the features we don't need
	networkListener.registerAddOn(new SimplifyAddOn());
	// add HTTP pipeline optimization
	networkListener.registerAddOn(new HttpPipelineOptAddOn());

	// disable file-cache
	networkListener.getFileCache().setEnabled(false);

	httpServer.addListener(networkListener);

	httpServer.getServerConfiguration().addHttpHandler(new RootHttpHandler(), "/");

	try {
		httpServer.start();
		// This can't be done before the call to start(). Also note the
		// positions
		httpServer.getListener("http-listener").getFilterChain().add(3, new HeadersFilter());
		synchronized (Server.class) {
			Server.class.wait();
		}
	} finally {
		httpServer.shutdown();
	}
}