@Override protected void doOpen() throws Throwable { FilterChainBuilder filterChainBuilder = FilterChainBuilder.stateless(); filterChainBuilder.add(new TransportFilter()); filterChainBuilder.add(new GrizzlyCodecAdapter(getCodec(), getUrl(), this)); filterChainBuilder.add(new GrizzlyHandler(getUrl(), this)); TCPNIOTransportBuilder builder = TCPNIOTransportBuilder.newInstance(); ThreadPoolConfig config = builder.getWorkerThreadPoolConfig(); config.setPoolName(SERVER_THREAD_POOL_NAME).setQueueLimit(-1); String threadpool = getUrl().getParameter(Constants.THREADPOOL_KEY, Constants.DEFAULT_THREADPOOL); if (Constants.DEFAULT_THREADPOOL.equals(threadpool)) { int threads = getUrl().getPositiveParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS); config.setCorePoolSize(threads).setMaxPoolSize(threads) .setKeepAliveTime(0L, TimeUnit.SECONDS); } else if ("cached".equals(threadpool)) { int threads = getUrl().getPositiveParameter(Constants.THREADS_KEY, Integer.MAX_VALUE); config.setCorePoolSize(0).setMaxPoolSize(threads) .setKeepAliveTime(60L, TimeUnit.SECONDS); } else { throw new IllegalArgumentException("Unsupported threadpool type " + threadpool); } builder.setKeepAlive(true).setReuseAddress(false) .setIOStrategy(SameThreadIOStrategy.getInstance()); transport = builder.build(); transport.setProcessor(filterChainBuilder.build()); transport.bind(getBindAddress()); transport.start(); }
@Override protected void doOpen() throws Throwable { FilterChainBuilder filterChainBuilder = FilterChainBuilder.stateless(); filterChainBuilder.add(new TransportFilter()); filterChainBuilder.add(new GrizzlyCodecAdapter(getCodec(), getUrl(), this)); filterChainBuilder.add(new GrizzlyHandler(getUrl(), this)); TCPNIOTransportBuilder builder = TCPNIOTransportBuilder.newInstance(); ThreadPoolConfig config = builder.getWorkerThreadPoolConfig(); config.setPoolName(SERVER_THREAD_POOL_NAME).setQueueLimit(-1); String threadpool = getUrl().getParameter(Constants.THREADPOOL_KEY, Constants.DEFAULT_THREADPOOL); if (Constants.DEFAULT_THREADPOOL.equals(threadpool)) { int threads = getUrl().getPositiveParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS); config.setCorePoolSize(threads).setMaxPoolSize(threads) .setKeepAliveTime(0L, TimeUnit.SECONDS); } else if ("cached".equals(threadpool)) { int threads = getUrl().getPositiveParameter(Constants.THREADS_KEY, Integer.MAX_VALUE); config.setCorePoolSize(0).setMaxPoolSize(threads) .setKeepAliveTime(60L, TimeUnit.SECONDS); } else { throw new IllegalArgumentException("Unsupported threadpool type " + threadpool); } builder.setKeepAlive(true).setReuseAddress(false) .setIOStrategy(SameThreadIOStrategy.getInstance()); transport = builder.build(); transport.setProcessor(filterChainBuilder.build()); transport.bind(getBindAddress()); transport.start(); }
@Override protected void doOpen() throws Throwable { FilterChainBuilder filterChainBuilder = FilterChainBuilder.stateless(); filterChainBuilder.add(new TransportFilter()); filterChainBuilder.add(new GrizzlyCodecAdapter(getCodec(), getUrl(), this)); filterChainBuilder.add(new GrizzlyHandler(getUrl(), this)); TCPNIOTransportBuilder builder = TCPNIOTransportBuilder.newInstance(); ThreadPoolConfig config = builder.getWorkerThreadPoolConfig(); config.setPoolName(CLIENT_THREAD_POOL_NAME) .setQueueLimit(-1) .setCorePoolSize(0) .setMaxPoolSize(Integer.MAX_VALUE) .setKeepAliveTime(60L, TimeUnit.SECONDS); builder.setTcpNoDelay(true).setKeepAlive(true) .setConnectionTimeout(getConnectTimeout()) .setIOStrategy(SameThreadIOStrategy.getInstance()); transport = builder.build(); transport.setProcessor(filterChainBuilder.build()); transport.start(); }
@Override protected void doOpen() throws Throwable { FilterChainBuilder filterChainBuilder = FilterChainBuilder.stateless(); filterChainBuilder.add(new TransportFilter()); filterChainBuilder.add(new GrizzlyCodecAdapter(getCodec(), getUrl(), this)); filterChainBuilder.add(new GrizzlyHandler(getUrl(), this)); TCPNIOTransportBuilder builder = TCPNIOTransportBuilder.newInstance(); ThreadPoolConfig config = builder.getWorkerThreadPoolConfig(); config.setPoolName(CLIENT_THREAD_POOL_NAME) .setQueueLimit(-1) .setCorePoolSize(0) .setMaxPoolSize(Integer.MAX_VALUE) .setKeepAliveTime(60L, TimeUnit.SECONDS); builder.setTcpNoDelay(true).setKeepAlive(true) .setConnectionTimeout(getConnectTimeout()) .setIOStrategy(SameThreadIOStrategy.getInstance()); transport = builder.build(); transport.setProcessor(filterChainBuilder.build()); transport.start(); }
public GrizzlyAsyncHttpProvider(final AsyncHttpClientConfig clientConfig) { this.clientConfig = clientConfig; this.providerConfig = clientConfig.getAsyncHttpProviderConfig() instanceof GrizzlyAsyncHttpProviderConfig ? (GrizzlyAsyncHttpProviderConfig) clientConfig.getAsyncHttpProviderConfig() : new GrizzlyAsyncHttpProviderConfig(); final TCPNIOTransportBuilder builder = TCPNIOTransportBuilder.newInstance(); clientTransport = builder.build(); initializeTransport(clientConfig); connectionManager = new ConnectionManager(this, clientTransport, providerConfig); try { clientTransport.start(); } catch (IOException ioe) { throw new RuntimeException(ioe); } }
private static TCPNIOTransport getOrCreateSharedTransport( ThreadPoolConfig workerThreadPoolConfig, ThreadPoolConfig selectorThreadPoolConfig) throws IOException { synchronized (TRANSPORT_LOCK) { if (transport == null) { Logger.getLogger(GrizzlyClientSocket.class.getName()).log(Level.FINE, "Starting shared container."); transport = createTransport(workerThreadPoolConfig, selectorThreadPoolConfig, true); transport.start(); } } return transport; }
private static TCPNIOTransport getOrCreateSharedTransport( ThreadPoolConfig workerThreadPoolConfig, ThreadPoolConfig selectorThreadPoolConfig) throws IOException { synchronized (TRANSPORT_LOCK) { if (transport == null) { Logger.getLogger(GrizzlyClientSocket.class.getName()).log(Level.FINE, "Starting shared container."); transport = createTransport(workerThreadPoolConfig, selectorThreadPoolConfig, true); transport.start(); } } return transport; }
/** * Starts the transport and the {@code idleTimeoutExecutorService} if not started. This is because * they should be started lazily when the first server is registered (otherwise there will be Grizzly * threads even if there is no listener-config in the app). */ private void startTransportIfNotStarted() throws IOException { if (!transportStarted) { transportStarted = true; transport.start(); idleTimeoutDelayedExecutor.start(); } }
/** * Starts the transport if not started. This is because it should be started lazily when the first server is registered * (otherwise there will be Grizzly threads even if there is no HTTP usage in any app). */ private void startTransportIfNotStarted() throws ServerCreationException { withContextClassLoader(this.getClass().getClassLoader(), () -> { if (!transportStarted) { transportStarted = true; transport.start(); } return null; }, ServerCreationException.class, e -> { throw new ServerCreationException("Transport failed at startup.", e); }); }
public GrizzlyAsyncHttpProvider(final AsyncHttpClientConfig clientConfig) { this.clientConfig = clientConfig; this.providerConfig = clientConfig.getAsyncHttpProviderConfig() instanceof GrizzlyAsyncHttpProviderConfig ? (GrizzlyAsyncHttpProviderConfig) clientConfig.getAsyncHttpProviderConfig() : new GrizzlyAsyncHttpProviderConfig(); final TCPNIOTransportBuilder builder = TCPNIOTransportBuilder.newInstance(); clientTransport = builder.build(); initializeTransport(clientConfig); connectionManager = new ConnectionManager(this, clientTransport, providerConfig); try { clientTransport.start(); } catch (IOException ioe) { throw new RuntimeException(ioe); } }
public GrizzlyAsyncHttpProvider(final AsyncHttpClientConfig clientConfig) { this.clientConfig = clientConfig; this.providerConfig = clientConfig.getAsyncHttpProviderConfig() instanceof GrizzlyAsyncHttpProviderConfig ? (GrizzlyAsyncHttpProviderConfig) clientConfig.getAsyncHttpProviderConfig() : new GrizzlyAsyncHttpProviderConfig(); final TCPNIOTransportBuilder builder = TCPNIOTransportBuilder.newInstance(); clientTransport = builder.build(); initializeTransport(clientConfig); connectionManager = new ConnectionManager(this, clientTransport, providerConfig); try { clientTransport.start(); } catch (IOException ioe) { throw new RuntimeException(ioe); } }
public GrizzlyAsyncHttpProvider(final AsyncHttpClientConfig clientConfig) { this.clientConfig = clientConfig; this.providerConfig = clientConfig.getAsyncHttpProviderConfig() instanceof GrizzlyAsyncHttpProviderConfig ? (GrizzlyAsyncHttpProviderConfig) clientConfig.getAsyncHttpProviderConfig() : new GrizzlyAsyncHttpProviderConfig(); final TCPNIOTransportBuilder builder = TCPNIOTransportBuilder.newInstance(); clientTransport = builder.build(); initializeTransport(clientConfig); connectionManager = new ConnectionManager(this, clientTransport, providerConfig); try { clientTransport.start(); } catch (IOException ioe) { throw new RuntimeException(ioe); } }
public GrizzlyAsyncHttpProvider(final AsyncHttpClientConfig clientConfig) { this.clientConfig = clientConfig; this.providerConfig = clientConfig.getAsyncHttpProviderConfig() instanceof GrizzlyAsyncHttpProviderConfig ? (GrizzlyAsyncHttpProviderConfig) clientConfig.getAsyncHttpProviderConfig() : new GrizzlyAsyncHttpProviderConfig(); final TCPNIOTransportBuilder builder = TCPNIOTransportBuilder.newInstance(); clientTransport = builder.build(); initializeTransport(clientConfig); connectionManager = new ConnectionManager(this, clientTransport, providerConfig); try { clientTransport.start(); } catch (IOException ioe) { throw new RuntimeException(ioe); } }
public void setupServer() { servertransport = TCPNIOTransportBuilder.newInstance().build(); // no use for default memorymanager servertransport.configureStandalone(true); try { final TCPNIOServerConnection serverConnection = servertransport.bind(PORT); servertransport.start(); // Start echo server thread startEchoServerThread(servertransport, serverConnection); } catch (Exception ex) { LOGGER.log(Level.SEVERE, "Server start error", ex); } }
public void setupClient() { clienttransport = TCPNIOTransportBuilder.newInstance().build(); try { clienttransport.start(); clienttransport.configureBlocking(false); clienttransport.configureStandalone(true); Future<Connection> future = clienttransport.connect("localhost", PORT); clientconnection = future.get(10, TimeUnit.SECONDS); assertTrue(clientconnection != null); clientconnection.configureStandalone(true); clientWriter = ((StandaloneProcessor) clientconnection.getProcessor()). getStreamWriter(clientconnection); } catch (Exception ex) { LOGGER.log(Level.SEVERE, "Client start error", ex); } }
@Test public void testCustomThreadPoolSameThreadStrategy() throws Exception { final int poolSize = Math.max(Runtime.getRuntime().availableProcessors()/2, 1); final ThreadPoolConfig poolCfg = ThreadPoolConfig.defaultConfig(); poolCfg.setCorePoolSize(poolSize).setMaxPoolSize(poolSize); final TCPNIOTransport tcpTransport = TCPNIOTransportBuilder.newInstance() .setReuseAddress(true) .setIOStrategy(SameThreadIOStrategy.getInstance()) .setSelectorThreadPoolConfig(poolCfg) .setWorkerThreadPoolConfig(null) .build(); try { tcpTransport.start(); } finally { tcpTransport.shutdownNow(); } }
private void initializeClientTransport() throws IOException { final TCPNIOTransport tcpTransport = TCPNIOTransportBuilder.newInstance().build(); tcpTransport.start(); clientTransport = tcpTransport; final FilterChain clientFilterChain = FilterChainBuilder.stateless() .add(new TransportFilter()) .add(new StringFilter(Charsets.UTF8_CHARSET)) .add(new ClientFilter(this)) .build(); connectorHandler = TCPNIOConnectorHandler.builder(tcpTransport) .processor(clientFilterChain) .build(); }
@Before public void init() throws IOException { final FilterChain filterChain = FilterChainBuilder.stateless() .add(new TransportFilter()) .add(new BaseFilter() { @Override public NextAction handleAccept(FilterChainContext ctx) throws IOException { serverSideConnections.add(ctx.getConnection()); return ctx.getStopAction(); } @Override public NextAction handleClose(FilterChainContext ctx) throws IOException { serverSideConnections.remove(ctx.getConnection()); return ctx.getStopAction(); } }).build(); transport = TCPNIOTransportBuilder.newInstance().build(); transport.setProcessor(filterChain); transport.bind(PORT); transport.start(); }
@Override protected void doOpen() throws Throwable { FilterChainBuilder filterChainBuilder = FilterChainBuilder.stateless(); filterChainBuilder.add(new TransportFilter()); filterChainBuilder.add(new GrizzlyCodecAdapter(getCodec(), getUrl(), this)); filterChainBuilder.add(new GrizzlyHandler(getUrl(), this)); TCPNIOTransportBuilder builder = TCPNIOTransportBuilder.newInstance(); ThreadPoolConfig config = builder.getWorkerThreadPoolConfig(); config.setPoolName(CLIENT_THREAD_POOL_NAME) .setQueueLimit(-1) .setCorePoolSize(0) .setMaxPoolSize(Integer.MAX_VALUE) .setKeepAliveTime(60L, TimeUnit.SECONDS); builder.setTcpNoDelay(true).setKeepAlive(true) .setConnectionTimeout(getConnectTimeout()) .setIOStrategy(SameThreadIOStrategy.getInstance()); transport = builder.build(); transport.setProcessor(filterChainBuilder.build()); transport.start(); }
@Override protected void doOpen() throws Throwable { FilterChainBuilder filterChainBuilder = FilterChainBuilder.stateless(); filterChainBuilder.add(new TransportFilter()); filterChainBuilder.add(new GrizzlyCodecAdapter(getCodec(), getUrl(), this)); filterChainBuilder.add(new GrizzlyHandler(getUrl(), this)); TCPNIOTransportBuilder builder = TCPNIOTransportBuilder.newInstance(); ThreadPoolConfig config = builder.getWorkerThreadPoolConfig(); config.setPoolName(CLIENT_THREAD_POOL_NAME) .setQueueLimit(-1) .setCorePoolSize(0) .setMaxPoolSize(Integer.MAX_VALUE) .setKeepAliveTime(60L, TimeUnit.SECONDS); builder.setTcpNoDelay(true).setKeepAlive(true) .setConnectionTimeout(getConnectTimeout()) .setIOStrategy(SameThreadIOStrategy.getInstance()); transport = builder.build(); transport.setProcessor(filterChainBuilder.build()); transport.start(); }