protected void configureTransport( final NetworkListener networkListener, final Transport transportConfig, final FilterChainBuilder filterChainBuilder) { final String transportClassName = transportConfig.getClassname(); if (TCPNIOTransport.class.getName().equals(transportClassName)) { transport = configureTCPTransport(transportConfig); } else if (UDPNIOTransport.class.getName().equals(transportClassName)) { transport = configureUDPTransport(); } else { throw new GrizzlyConfigException("Unsupported transport type " + transportConfig.getName()); } String selectorName = transportConfig.getSelectionKeyHandler(); if (selectorName != null) { if (getSelectionKeyHandlerByName(selectorName, transportConfig) != null) { if (LOGGER.isLoggable(Level.INFO)) { LOGGER.warning( "Element, selection-key-handler, has been deprecated and is effectively ignored by the runtime."); } } } if (!Transport.BYTE_BUFFER_TYPE.equalsIgnoreCase(transportConfig.getByteBufferType())) { transport.setMemoryManager( new ByteBufferManager( true, AbstractMemoryManager.DEFAULT_MAX_BUFFER_SIZE, ByteBufferManager.DEFAULT_SMALL_BUFFER_SIZE)); } final int acceptorThreads = Integer.parseInt(transportConfig.getAcceptorThreads()); transport.setSelectorRunnersCount(acceptorThreads); final int readSize = Integer.parseInt(transportConfig.getSocketReadBufferSize()); if (readSize > 0) { transport.setReadBufferSize(readSize); } final int writeSize = Integer.parseInt(transportConfig.getSocketWriteBufferSize()); if (writeSize > 0) { transport.setWriteBufferSize(writeSize); } final ThreadPoolConfig kernelThreadPoolConfig = transport.getKernelThreadPoolConfig(); kernelThreadPoolConfig.setPoolName(networkListener.getName() + "-kernel"); if (acceptorThreads > 0) { kernelThreadPoolConfig.setCorePoolSize(acceptorThreads).setMaxPoolSize(acceptorThreads); } transport.setIOStrategy(loadIOStrategy(transportConfig.getIoStrategy())); transport.setNIOChannelDistributor( new RoundRobinConnectionDistributor( transport, Boolean.parseBoolean(transportConfig.getDedicatedAcceptorEnabled()))); filterChainBuilder.add(new TransportFilter()); }
protected ThreadPoolConfig configureThreadPoolConfig( final NetworkListener networkListener, final ThreadPool threadPool) { final int maxQueueSize = threadPool.getMaxQueueSize() == null ? Integer.MAX_VALUE : Integer.parseInt(threadPool.getMaxQueueSize()); final int minThreads = Integer.parseInt(threadPool.getMinThreadPoolSize()); final int maxThreads = Integer.parseInt(threadPool.getMaxThreadPoolSize()); final int timeout = Integer.parseInt(threadPool.getIdleThreadTimeoutSeconds()); final ThreadPoolConfig poolConfig = ThreadPoolConfig.defaultConfig(); poolConfig.setPoolName(networkListener.getName()); poolConfig.setCorePoolSize(minThreads); poolConfig.setMaxPoolSize(maxThreads); poolConfig.setQueueLimit(maxQueueSize); // we specify the classloader that loaded this class to ensure // we present the same initial classloader no matter what mode // GlassFish is being run in. // See http://java.net/jira/browse/GLASSFISH-19639 poolConfig.setInitialClassLoader(this.getClass().getClassLoader()); poolConfig.setKeepAliveTime(timeout < 0 ? Long.MAX_VALUE : timeout, TimeUnit.SECONDS); if (transactionTimeoutMillis > 0 && !Utils.isDebugVM()) { poolConfig.setTransactionTimeout( obtainDelayedExecutor(), transactionTimeoutMillis, TimeUnit.MILLISECONDS); } return poolConfig; }
@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(); } }
/** * Added for http://java.net/jira/browse/GRIZZLY-1435. * * @throws Exception */ @Test public void testThreadPoolCoreThreadInitialization() throws Exception { final ThreadPoolConfig config = ThreadPoolConfig.defaultConfig(); config.setCorePoolSize(5); config.setMaxPoolSize(5); Field workers = AbstractThreadPool.class.getDeclaredField("workers"); workers.setAccessible(true); final SyncThreadPool syncThreadPool = new SyncThreadPool(config); assertEquals( "Pool did not properly initialize threads based on core pool size configuration.", 5, ((Map) workers.get(syncThreadPool)).size()); config.setQueue(new ArrayBlockingQueue<Runnable>(5)); final FixedThreadPool fixedThreadPool = new FixedThreadPool(config); assertEquals( "Pool did not properly initialize threads based on core pool size configuration.", 5, ((Map) workers.get(fixedThreadPool)).size()); }
@Override public ThreadPoolConfig createDefaultWorkerPoolConfig(final Transport transport) { final ThreadPoolConfig config = ThreadPoolConfig.defaultConfig().copy(); final int coresCount = Runtime.getRuntime().availableProcessors(); config.setPoolName("Grizzly-worker"); config.setCorePoolSize(coresCount * 2); config.setMaxPoolSize(coresCount * 2); config.setMemoryManager(transport.getMemoryManager()); return config; }
protected <T extends NIOTransport> T configureDefaultThreadPoolConfigs(final T transport) { transport.setKernelThreadPoolConfig(ThreadPoolConfig.defaultConfig()); transport.setWorkerThreadPoolConfig(ThreadPoolConfig.defaultConfig()); return transport; }
private void configureListener(final NetworkListener listener) { FilterChain chain = listener.getFilterChain(); if (chain == null) { final FilterChainBuilder builder = FilterChainBuilder.newInstance(); builder.add(new TransportFilter()); if (listener.isSecure()) { SSLEngineConfigurator sslConfig = listener.getSslEngineConfig(); if (sslConfig == null) { sslConfig = new SSLEngineConfigurator(SSLContextConfigurator.DEFAULT_CONFIG, false, false, false); listener.setSSLEngineConfig(sslConfig); } final SSLBaseFilter filter = new SSLBaseFilter(sslConfig); builder.add(filter); } final int maxHeaderSize = listener.getMaxHttpHeaderSize() == -1 ? org.glassfish.grizzly.http.HttpServerFilter.DEFAULT_MAX_HTTP_PACKET_HEADER_SIZE : listener.getMaxHttpHeaderSize(); // Passing null value for the delayed executor, because IdleTimeoutFilter should // handle idle connections for us final org.glassfish.grizzly.http.HttpServerFilter httpServerCodecFilter = new org.glassfish.grizzly.http.HttpServerFilter( listener.isChunkingEnabled(), maxHeaderSize, null, listener.getKeepAliveConfig(), null, listener.getMaxRequestHeaders(), listener.getMaxResponseHeaders()); final Set<ContentEncoding> contentEncodings = configureCompressionEncodings(listener); for (ContentEncoding contentEncoding : contentEncodings) { httpServerCodecFilter.addContentEncoding(contentEncoding); } httpServerCodecFilter .getMonitoringConfig() .addProbes(serverConfig.getMonitoringConfig().getHttpConfig().getProbes()); builder.add(httpServerCodecFilter); builder.add( new IdleTimeoutFilter( delayedExecutor, listener.getKeepAliveConfig().getIdleTimeoutInSeconds(), TimeUnit.SECONDS)); final Transport transport = listener.getTransport(); final FileCache fileCache = listener.getFileCache(); fileCache.initialize(delayedExecutor); final FileCacheFilter fileCacheFilter = new FileCacheFilter(fileCache); fileCache .getMonitoringConfig() .addProbes(serverConfig.getMonitoringConfig().getFileCacheConfig().getProbes()); builder.add(fileCacheFilter); final ServerFilterConfiguration config = new ServerFilterConfiguration(serverConfig); if (listener.isSendFileExplicitlyConfigured()) { config.setSendFileEnabled(listener.isSendFileEnabled()); fileCache.setFileSendEnabled(listener.isSendFileEnabled()); } if (listener.getBackendConfig() != null) { config.setBackendConfiguration(listener.getBackendConfig()); } if (listener.getDefaultErrorPageGenerator() != null) { config.setDefaultErrorPageGenerator(listener.getDefaultErrorPageGenerator()); } config.setTraceEnabled(config.isTraceEnabled() || listener.isTraceEnabled()); config.setMaxFormPostSize(listener.getMaxFormPostSize()); config.setMaxBufferedPostSize(listener.getMaxBufferedPostSize()); final HttpServerFilter httpServerFilter = new HttpServerFilter(config, delayedExecutor); httpServerFilter.setHttpHandler(httpHandlerChain); httpServerFilter .getMonitoringConfig() .addProbes(serverConfig.getMonitoringConfig().getWebServerConfig().getProbes()); builder.add(httpServerFilter); chain = builder.build(); final AddOn[] addons = listener.getAddOnSet().getArray(); if (addons != null) { for (AddOn addon : addons) { addon.setup(listener, chain); } } listener.setFilterChain(chain); final int transactionTimeout = listener.getTransactionTimeout(); if (transactionTimeout >= 0) { ThreadPoolConfig threadPoolConfig = transport.getWorkerThreadPoolConfig(); if (threadPoolConfig != null) { threadPoolConfig.setTransactionTimeout( delayedExecutor, transactionTimeout, TimeUnit.SECONDS); } } } configureMonitoring(listener); }