@Override public void launch(final MessageInput input) throws MisfireException { final LinkedHashMap<String, Callable<? extends ChannelHandler>> handlerList = getBaseChannelHandlers(input); final LinkedHashMap<String, Callable<? extends ChannelHandler>> finalHandlers = getFinalChannelHandlers(input); handlerList.putAll(finalHandlers); try { bootstrap = getBootstrap(); bootstrap.setPipelineFactory(getPipelineFactory(handlerList)); // sigh, bindable bootstraps do not share a common interface int receiveBufferSize; if (bootstrap instanceof ConnectionlessBootstrap) { acceptChannel = ((ConnectionlessBootstrap) bootstrap).bind(socketAddress); final DefaultDatagramChannelConfig channelConfig = (DefaultDatagramChannelConfig) acceptChannel.getConfig(); receiveBufferSize = channelConfig.getReceiveBufferSize(); } else if (bootstrap instanceof ServerBootstrap) { acceptChannel = ((ServerBootstrap) bootstrap).bind(socketAddress); final ServerSocketChannelConfig channelConfig = (ServerSocketChannelConfig) acceptChannel.getConfig(); receiveBufferSize = channelConfig.getReceiveBufferSize(); } else { log.error("Unknown Netty bootstrap class returned: {}. Cannot safely bind.", bootstrap); throw new IllegalStateException("Unknown netty bootstrap class returned: " + bootstrap + ". Cannot safely bind."); } if (receiveBufferSize != getRecvBufferSize()) { log.warn("receiveBufferSize (SO_RCVBUF) for input {} should be {} but is {}.", input, getRecvBufferSize(), receiveBufferSize); } } catch (Exception e) { throw new MisfireException(e); } }
@Test public void testNettyConfigBuilder() { NettyServerConfigBuilder configBuilder = new NettyServerConfigBuilder(); configBuilder.getServerSocketChannelConfig().setReceiveBufferSize(10000); configBuilder.getServerSocketChannelConfig().setBacklog(1000); configBuilder.getServerSocketChannelConfig().setReuseAddress(true); ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory()); bootstrap.setOptions(configBuilder.getBootstrapOptions()); bootstrap.setPipelineFactory(Channels.pipelineFactory(Channels.pipeline())); Channel serverChannel = bootstrap.bind(new InetSocketAddress(port)); Assert.assertEquals(((ServerSocketChannelConfig) serverChannel.getConfig()).getReceiveBufferSize(), 10000); Assert.assertEquals(((ServerSocketChannelConfig) serverChannel.getConfig()).getBacklog(), 1000); Assert.assertTrue(((ServerSocketChannelConfig) serverChannel.getConfig()).isReuseAddress()); } }