use of org.jboss.netty.bootstrap.ServerBootstrap in project neo4j by neo4j.
the class PortRangeSocketBinderTest method shouldReThrowExceptionIfCannotBindToAnyOfThePortsInTheRange.
@Test
public void shouldReThrowExceptionIfCannotBindToAnyOfThePortsInTheRange() {
// given
HostnamePort localhost = new HostnamePort("localhost", 9000, 9002);
ServerBootstrap bootstrap = mock(ServerBootstrap.class);
when(bootstrap.bind(new InetSocketAddress("localhost", 9000))).thenThrow(new ChannelException("Failed to bind to: 9000"));
when(bootstrap.bind(new InetSocketAddress("localhost", 9001))).thenThrow(new ChannelException("Failed to bind to: 9001"));
when(bootstrap.bind(new InetSocketAddress("localhost", 9002))).thenThrow(new ChannelException("Failed to bind to: 9002"));
try {
// when
new PortRangeSocketBinder(bootstrap).bindToFirstAvailablePortInRange(localhost);
fail("should have thrown ChannelException");
} catch (ChannelException ex) {
// expected
assertEquals(2, suppressedExceptions(ex));
}
}
use of org.jboss.netty.bootstrap.ServerBootstrap in project neo4j by neo4j.
the class PortRangeSocketBinderTest method shouldReturnChannelAndSocketIfPortRangeIsInverted.
@Test
public void shouldReturnChannelAndSocketIfPortRangeIsInverted() {
// given
HostnamePort localhost = new HostnamePort("localhost", 9001, 9000);
ServerBootstrap bootstrap = mock(ServerBootstrap.class);
Channel channel = mock(Channel.class);
when(bootstrap.bind(new InetSocketAddress("localhost", 9001))).thenReturn(channel);
// when
Connection connection = new PortRangeSocketBinder(bootstrap).bindToFirstAvailablePortInRange(localhost);
//then
assertEquals(channel, connection.getChannel());
assertEquals(new InetSocketAddress(localhost.getHost(), 9001), connection.getSocketAddress());
}
use of org.jboss.netty.bootstrap.ServerBootstrap in project hadoop by apache.
the class SimpleTcpServer method run.
public void run() {
// Configure the Server.
ChannelFactory factory;
if (workerCount == 0) {
// Use default workers: 2 * the number of available processors
factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
} else {
factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), workerCount);
}
server = new ServerBootstrap(factory);
server.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(RpcUtil.constructRpcFrameDecoder(), RpcUtil.STAGE_RPC_MESSAGE_PARSER, rpcProgram, RpcUtil.STAGE_RPC_TCP_RESPONSE);
}
});
server.setOption("child.tcpNoDelay", true);
server.setOption("child.keepAlive", true);
server.setOption("child.reuseAddress", true);
server.setOption("reuseAddress", true);
// Listen to TCP port
ch = server.bind(new InetSocketAddress(port));
InetSocketAddress socketAddr = (InetSocketAddress) ch.getLocalAddress();
boundPort = socketAddr.getPort();
LOG.info("Started listening to TCP requests at port " + boundPort + " for " + rpcProgram + " with workerCount " + workerCount);
}
use of org.jboss.netty.bootstrap.ServerBootstrap in project pinpoint by naver.
the class PinpointServerAcceptor method createBootStrap.
private ServerBootstrap createBootStrap(int bossCount, int workerCount) {
// profiler, collector
ExecutorService boss = Executors.newCachedThreadPool(new PinpointThreadFactory("Pinpoint-Server-Boss"));
NioServerBossPool nioServerBossPool = new NioServerBossPool(boss, bossCount, ThreadNameDeterminer.CURRENT);
ExecutorService worker = Executors.newCachedThreadPool(new PinpointThreadFactory("Pinpoint-Server-Worker"));
NioWorkerPool nioWorkerPool = new NioWorkerPool(worker, workerCount, ThreadNameDeterminer.CURRENT);
NioServerSocketChannelFactory nioClientSocketChannelFactory = new NioServerSocketChannelFactory(nioServerBossPool, nioWorkerPool);
return new ServerBootstrap(nioClientSocketChannelFactory);
}
use of org.jboss.netty.bootstrap.ServerBootstrap in project motan by weibocom.
the class NettyServer method initServerBootstrap.
private synchronized void initServerBootstrap() {
boolean shareChannel = url.getBooleanParameter(URLParamType.shareChannel.getName(), URLParamType.shareChannel.getBooleanValue());
final int maxContentLength = url.getIntParameter(URLParamType.maxContentLength.getName(), URLParamType.maxContentLength.getIntValue());
int maxServerConnection = url.getIntParameter(URLParamType.maxServerConnection.getName(), URLParamType.maxServerConnection.getIntValue());
int workerQueueSize = url.getIntParameter(URLParamType.workerQueueSize.getName(), URLParamType.workerQueueSize.getIntValue());
int minWorkerThread = 0, maxWorkerThread = 0;
if (shareChannel) {
minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MIN_WORKDER);
maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_SHARECHANNEL_MAX_WORKDER);
} else {
minWorkerThread = url.getIntParameter(URLParamType.minWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MIN_WORKDER);
maxWorkerThread = url.getIntParameter(URLParamType.maxWorkerThread.getName(), MotanConstants.NETTY_NOT_SHARECHANNEL_MAX_WORKDER);
}
standardThreadExecutor = (standardThreadExecutor != null && !standardThreadExecutor.isShutdown()) ? standardThreadExecutor : new StandardThreadExecutor(minWorkerThread, maxWorkerThread, workerQueueSize, new DefaultThreadFactory("NettyServer-" + url.getServerPortStr(), true));
standardThreadExecutor.prestartAllCoreThreads();
// 连接数的管理,进行最大连接数的限制
channelManage = new NettyServerChannelManage(maxServerConnection);
bootstrap = new ServerBootstrap(channelFactory);
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
final NettyChannelHandler handler = new NettyChannelHandler(NettyServer.this, messageHandler, standardThreadExecutor);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
// FrameDecoder非线程安全,每个连接一个 Pipeline
public ChannelPipeline getPipeline() {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("channel_manage", channelManage);
pipeline.addLast("decoder", new NettyDecoder(codec, NettyServer.this, maxContentLength));
pipeline.addLast("encoder", new NettyEncoder(codec, NettyServer.this));
pipeline.addLast("handler", handler);
return pipeline;
}
});
}
Aggregations