Search in sources :

Example 66 with ServerBootstrap

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));
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HostnamePort(org.neo4j.helpers.HostnamePort) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelException(org.jboss.netty.channel.ChannelException) Test(org.junit.Test)

Example 67 with ServerBootstrap

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());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HostnamePort(org.neo4j.helpers.HostnamePort) Channel(org.jboss.netty.channel.Channel) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) Test(org.junit.Test)

Example 68 with ServerBootstrap

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);
}
Also used : NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) InetSocketAddress(java.net.InetSocketAddress) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) ChannelFactory(org.jboss.netty.channel.ChannelFactory) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline)

Example 69 with ServerBootstrap

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);
}
Also used : NioServerBossPool(org.jboss.netty.channel.socket.nio.NioServerBossPool) NioWorkerPool(org.jboss.netty.channel.socket.nio.NioWorkerPool) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) ExecutorService(java.util.concurrent.ExecutorService) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) PinpointThreadFactory(com.navercorp.pinpoint.common.util.PinpointThreadFactory)

Example 70 with ServerBootstrap

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;
        }
    });
}
Also used : DefaultThreadFactory(com.weibo.api.motan.core.DefaultThreadFactory) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline)

Aggregations

ServerBootstrap (org.jboss.netty.bootstrap.ServerBootstrap)94 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)58 TrackerServer (org.traccar.TrackerServer)42 InetSocketAddress (java.net.InetSocketAddress)38 NioServerSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory)34 ConnectionlessBootstrap (org.jboss.netty.bootstrap.ConnectionlessBootstrap)25 StringEncoder (org.jboss.netty.handler.codec.string.StringEncoder)16 Channel (org.jboss.netty.channel.Channel)13 ChannelPipelineFactory (org.jboss.netty.channel.ChannelPipelineFactory)12 StringDecoder (org.jboss.netty.handler.codec.string.StringDecoder)12 LengthFieldBasedFrameDecoder (org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder)11 ChannelFactory (org.jboss.netty.channel.ChannelFactory)8 ExecutorService (java.util.concurrent.ExecutorService)5 DefaultChannelGroup (org.jboss.netty.channel.group.DefaultChannelGroup)5 Test (org.junit.Test)5 HostnamePort (org.neo4j.helpers.HostnamePort)5 CharacterDelimiterFrameDecoder (org.traccar.CharacterDelimiterFrameDecoder)5 IOException (java.io.IOException)4 ChannelException (org.jboss.netty.channel.ChannelException)4 NioClientSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory)4