org.jboss.netty.channel.ChannelFactory Java Examples

The following examples show how to use org.jboss.netty.channel.ChannelFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UdpServer.java    From feeyo-hlsserver with Apache License 2.0 6 votes vote down vote up
public void startup(int port) {
	
	ChannelFactory channelFactory = new NioDatagramChannelFactory(Executors.newCachedThreadPool());
	
	bootstrap = new ConnectionlessBootstrap( channelFactory );
	bootstrap.setOption("reuseAddress", false);
	bootstrap.setOption("child.reuseAddress", false);		
	bootstrap.setOption("readBufferSize", 1024 * 1024 * 15); //15M
	bootstrap.setOption("writeBufferSize", 1024 * 20);		
	bootstrap.setOption("receiveBufferSizePredictor", new FixedReceiveBufferSizePredictor(1024 * 3));
	bootstrap.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(1024 * 3));
	bootstrap.setPipelineFactory( new ChannelPipelineFactory() {
		@Override
		public ChannelPipeline getPipeline() throws Exception {
			ChannelPipeline pipeline = Channels.pipeline();
			pipeline.addLast("handler", new UdpServerChannelHandler());
			return pipeline;
		}			
	});		
	datagramChannel = (DatagramChannel) bootstrap.bind( new InetSocketAddress( port ) );
}
 
Example #2
Source File: DefaultPinpointClientFactory.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
public void release() {
    if (this.closed.isClosed()) {
        return;
    }
    if (!this.closed.close()) {
        return;
    }

    if (!useExternalResource) {
        final ChannelFactory channelFactory = this.channelFactory;
        if (channelFactory != null) {
            channelFactory.releaseExternalResources();
        }
        Set<Timeout> stop = this.timer.stop();
        if (!stop.isEmpty()) {
            logger.info("stop Timeout:{}", stop.size());
        }

        // stop, cancel something?
    }
}
 
Example #3
Source File: SimpleTcpClient.java    From hadoop with Apache License 2.0 6 votes vote down vote up
public void run() {
  // Configure the client.
  ChannelFactory factory = new NioClientSocketChannelFactory(
      Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), 1, 1);
  ClientBootstrap bootstrap = new ClientBootstrap(factory);

  // Set up the pipeline factory.
  bootstrap.setPipelineFactory(setPipelineFactory());

  bootstrap.setOption("tcpNoDelay", true);
  bootstrap.setOption("keepAlive", true);

  // Start the connection attempt.
  ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));

  if (oneShot) {
    // Wait until the connection is closed or the connection attempt fails.
    future.getChannel().getCloseFuture().awaitUninterruptibly();

    // Shut down thread pools to exit.
    bootstrap.releaseExternalResources();
  }
}
 
Example #4
Source File: ThriftModuleLifeCycle.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Inject
public ThriftModuleLifeCycle(
        Provider<CommandDispatcher> commandDispatcherProvider,
        @DefaultClientFactory Provider<PinpointClientFactory> clientFactoryProvider,
        @AgentDataSender Provider<EnhancedDataSender<Object>> tcpDataSenderProvider,
        @SpanStatConnectTimer Provider<Timer> spanStatConnectTimerProvider,
        @SpanStatChannelFactory Provider<ChannelFactory> spanStatChannelFactoryProvider,
        @SpanClientFactory Provider<PinpointClientFactory> spanClientFactoryProvider,
        @StatClientFactory Provider<PinpointClientFactory> statClientFactoryProvider,
        @SpanDataSender Provider<DataSender> spanDataSenderProvider,
        @StatDataSender Provider<DataSender> statDataSenderProvider
        ) {
    this.commandDispatcherProvider = Assert.requireNonNull(commandDispatcherProvider, "commandDispatcherProvider");
    this.clientFactoryProvider = Assert.requireNonNull(clientFactoryProvider, "clientFactoryProvider");
    this.tcpDataSenderProvider = Assert.requireNonNull(tcpDataSenderProvider, "tcpDataSenderProvider");

    this.spanStatConnectTimerProvider = Assert.requireNonNull(spanStatConnectTimerProvider, "spanStatConnectTimerProvider");

    this.spanStatChannelFactoryProvider = Assert.requireNonNull(spanStatChannelFactoryProvider, "spanStatChannelFactoryProvider");

    this.spanClientFactoryProvider = Assert.requireNonNull(spanClientFactoryProvider, "spanClientFactoryProvider");
    this.statClientFactoryProvider = Assert.requireNonNull(statClientFactoryProvider, "statClientFactoryProvider");

    this.spanDataSenderProvider = Assert.requireNonNull(spanDataSenderProvider, "spanDataSenderProvider");
    this.statDataSenderProvider = Assert.requireNonNull(statDataSenderProvider, "statDataSenderProvider");
}
 
Example #5
Source File: SimpleTcpClient.java    From big-c with Apache License 2.0 6 votes vote down vote up
public void run() {
  // Configure the client.
  ChannelFactory factory = new NioClientSocketChannelFactory(
      Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), 1, 1);
  ClientBootstrap bootstrap = new ClientBootstrap(factory);

  // Set up the pipeline factory.
  bootstrap.setPipelineFactory(setPipelineFactory());

  bootstrap.setOption("tcpNoDelay", true);
  bootstrap.setOption("keepAlive", true);

  // Start the connection attempt.
  ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));

  if (oneShot) {
    // Wait until the connection is closed or the connection attempt fails.
    future.getChannel().getCloseFuture().awaitUninterruptibly();

    // Shut down thread pools to exit.
    bootstrap.releaseExternalResources();
  }
}
 
Example #6
Source File: SpanStatChannelFactoryProvider.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Override
public ChannelFactory get() {
    int workerCount = 0;

    if ("TCP".equalsIgnoreCase(thriftTransportConfig.getSpanDataSenderTransportType())) {
        workerCount++;
    }
    if ("TCP".equalsIgnoreCase(thriftTransportConfig.getStatDataSenderTransportType())) {
        workerCount++;
    }

    if (workerCount == 0) {
        return null;
    } else {
        Timer timer = connectTimerProvider.get();

        final ClientChannelFactory clientChannelFactory = new ClientChannelFactory();
        return clientChannelFactory.createChannelFactory(1, workerCount, timer);
    }
}
 
Example #7
Source File: SyslogTcpSource.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Override
public void start() {
  ChannelFactory factory = new NioServerSocketChannelFactory(
      Executors.newCachedThreadPool(), Executors.newCachedThreadPool());

  ServerBootstrap serverBootstrap = new ServerBootstrap(factory);
  serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    @Override
    public ChannelPipeline getPipeline() {
      syslogTcpHandler handler = new syslogTcpHandler();
      handler.setEventSize(eventSize);
      handler.setFormater(formaterProp);
      return Channels.pipeline(handler);
    }
  });

  logger.info("Syslog TCP Source starting...");

  if (host == null) {
    nettyChannel = serverBootstrap.bind(new InetSocketAddress(port));
  } else {
    nettyChannel = serverBootstrap.bind(new InetSocketAddress(host, port));
  }

  super.start();
}
 
Example #8
Source File: BgpControllerImplTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
  * Starts the BGP peer.
  *
  * @param connectToSocket the socket to connect to
  */
 private void connect(InetSocketAddress connectToSocket)
     throws InterruptedException {

     ChannelFactory channelFactory =
         new NioClientSocketChannelFactory(
                 Executors.newCachedThreadPool(),
                 Executors.newCachedThreadPool());
     ChannelPipelineFactory pipelineFactory = () -> {
         ChannelPipeline pipeline = Channels.pipeline();
         pipeline.addLast("BgpPeerFrameDecoderTest",
                 peerFrameDecoder);
         pipeline.addLast("BgpPeerChannelHandlerTest",
                 peerChannelHandler);
         return pipeline;
     };

     peerBootstrap = new ClientBootstrap(channelFactory);
     peerBootstrap.setOption("child.keepAlive", true);
     peerBootstrap.setOption("child.tcpNoDelay", true);
     peerBootstrap.setPipelineFactory(pipelineFactory);
     peerBootstrap.connect(connectToSocket);
}
 
Example #9
Source File: NettyServer.java    From anima with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void doOpen() throws Throwable {
    ExecutorService boss = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerBoss", false));
       ExecutorService worker = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerWorker", true));
       int ioThread = conf.getInt(Constants.IO_THREADS,Constants.DEFAULT_IO_THREADS);
       ChannelFactory channelFactory = new NioServerSocketChannelFactory(boss, worker, ioThread);
       bootstrap = new ServerBootstrap(channelFactory);
       
       final NettyHandler nettyHandler = new NettyHandler(getConf(), this);
       channels = nettyHandler.getChannels();
       bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
           public ChannelPipeline getPipeline() {
               NettyCodecAdapter adapter = new NettyCodecAdapter(conf,getCodec(), NettyServer.this);
               ChannelPipeline pipeline = Channels.pipeline();
               pipeline.addLast("decoder", adapter.getDecoder());
               pipeline.addLast("encoder", adapter.getEncoder());
               pipeline.addLast("handler", nettyHandler);
               return pipeline;
           }
       });
       // bind
       channel = bootstrap.bind(getBindAddress());
}
 
Example #10
Source File: BgpControllerImplTest.java    From onos with Apache License 2.0 6 votes vote down vote up
private Channel connectFrom(InetSocketAddress connectToSocket, SocketAddress localAddress)
     throws InterruptedException {

     ChannelFactory channelFactory =
         new NioClientSocketChannelFactory(
                 Executors.newCachedThreadPool(),
                 Executors.newCachedThreadPool());
     ChannelPipelineFactory pipelineFactory = () -> {
         ChannelPipeline pipeline = Channels.pipeline();
         pipeline.addLast("BgpPeerFrameDecoderTest",
                 peerFrameDecoder);
         pipeline.addLast("BgpPeerChannelHandlerTest",
                 peerChannelHandler);
         return pipeline;
     };

     peerBootstrap = new ClientBootstrap(channelFactory);
     peerBootstrap.setOption("child.keepAlive", true);
     peerBootstrap.setOption("child.tcpNoDelay", true);
     peerBootstrap.setPipelineFactory(pipelineFactory);
     Channel channel = peerBootstrap.connect(connectToSocket, localAddress).getChannel();
     return channel;
}
 
Example #11
Source File: ClientChannelFactory.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
public ChannelFactory createChannelFactory(int bossCount, int workerCount, Timer timer) {
    ExecutorService boss = newCachedThreadPool("Pinpoint-Client-Boss");
    BossPool bossPool = new NioClientBossPool(boss, bossCount, timer, ThreadNameDeterminer.CURRENT);

    ExecutorService worker = newCachedThreadPool("Pinpoint-Client-Worker");
    WorkerPool workerPool = new NioWorkerPool(worker, workerCount, ThreadNameDeterminer.CURRENT);
    return new NioClientSocketChannelFactory(bossPool, workerPool);
}
 
Example #12
Source File: DefaultPinpointClientFactory.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
public DefaultPinpointClientFactory(ChannelFactory channelFactory, Timer timer, ConnectionFactoryProvider connectionFactoryProvider) {
    this.channelFactory = Assert.requireNonNull(channelFactory, "channelFactory");
    this.timer = Assert.requireNonNull(timer, "timer");

    this.useExternalResource = true;
    this.socketOptionBuilder = new SocketOption.Builder();
    this.connectionFactoryProvider = Assert.requireNonNull(connectionFactoryProvider, "connectionFactoryProvider");
}
 
Example #13
Source File: NettyServer.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    ExecutorService boss = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerBoss", true));
    ExecutorService worker = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerWorker", true));
    ChannelFactory channelFactory = new NioServerSocketChannelFactory(boss, worker, getUrl().getPositiveParameter(Constants.IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS));
    bootstrap = new ServerBootstrap(channelFactory);

    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    channels = nettyHandler.getChannels();
    // https://issues.jboss.org/browse/NETTY-365
    // https://issues.jboss.org/browse/NETTY-379
    // final Timer timer = new HashedWheelTimer(new NamedThreadFactory("NettyIdleTimer", true));
    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        @Override
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyServer.this);
            ChannelPipeline pipeline = Channels.pipeline();
            /*int idleTimeout = getIdleTimeout();
            if (idleTimeout > 10000) {
                pipeline.addLast("timer", new IdleStateHandler(timer, idleTimeout / 1000, 0, 0));
            }*/
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
    // bind
    channel = bootstrap.bind(getBindAddress());
}
 
Example #14
Source File: FpmManager.java    From onos with Apache License 2.0 5 votes vote down vote up
private void startServer() {
    HashedWheelTimer timer = new HashedWheelTimer(
            groupedThreads("onos/fpm", "fpm-timer-%d", log));

    ChannelFactory channelFactory = new NioServerSocketChannelFactory(
            newCachedThreadPool(groupedThreads("onos/fpm", "sm-boss-%d", log)),
            newCachedThreadPool(groupedThreads("onos/fpm", "sm-worker-%d", log)));
    ChannelPipelineFactory pipelineFactory = () -> {
        // Allocate a new session per connection
        IdleStateHandler idleHandler =
                new IdleStateHandler(timer, IDLE_TIMEOUT_SECS, 0, 0);
        FpmSessionHandler fpmSessionHandler =
                new FpmSessionHandler(this, new InternalFpmListener());
        FpmFrameDecoder fpmFrameDecoder = new FpmFrameDecoder();

        // Setup the processing pipeline
        ChannelPipeline pipeline = Channels.pipeline();
        pipeline.addLast("FpmFrameDecoder", fpmFrameDecoder);
        pipeline.addLast("idle", idleHandler);
        pipeline.addLast("FpmSession", fpmSessionHandler);
        return pipeline;
    };

    InetSocketAddress listenAddress = new InetSocketAddress(FPM_PORT);

    serverBootstrap = new ServerBootstrap(channelFactory);
    serverBootstrap.setOption("child.reuseAddr", true);
    serverBootstrap.setOption("child.keepAlive", true);
    serverBootstrap.setOption("child.tcpNoDelay", true);
    serverBootstrap.setPipelineFactory(pipelineFactory);
    try {
        serverChannel = serverBootstrap.bind(listenAddress);
        allChannels.add(serverChannel);
    } catch (ChannelException e) {
        log.debug("Exception binding to FPM port {}: ",
                listenAddress.getPort(), e);
        stopServer();
    }
}
 
Example #15
Source File: BgpSessionManager.java    From onos with Apache License 2.0 5 votes vote down vote up
public void start() {
    log.debug("BGP Session Manager start.");
    isShutdown = false;

    ChannelFactory channelFactory = new NioServerSocketChannelFactory(
            newCachedThreadPool(groupedThreads("onos/bgp", "sm-boss-%d", log)),
            newCachedThreadPool(groupedThreads("onos/bgp", "sm-worker-%d", log)));
    ChannelPipelineFactory pipelineFactory = () -> {
        // Allocate a new session per connection
        BgpSession bgpSessionHandler =
                new BgpSession(BgpSessionManager.this);
        BgpFrameDecoder bgpFrameDecoder =
                new BgpFrameDecoder(bgpSessionHandler);

        // Setup the processing pipeline
        ChannelPipeline pipeline = Channels.pipeline();
        pipeline.addLast("BgpFrameDecoder", bgpFrameDecoder);
        pipeline.addLast("BgpSession", bgpSessionHandler);
        return pipeline;
    };
    InetSocketAddress listenAddress =
            new InetSocketAddress(bgpPort);

    serverBootstrap = new ServerBootstrap(channelFactory);
    // serverBootstrap.setOptions("reuseAddr", true);
    serverBootstrap.setOption("child.keepAlive", true);
    serverBootstrap.setOption("child.tcpNoDelay", true);
    serverBootstrap.setPipelineFactory(pipelineFactory);
    try {
        serverChannel = serverBootstrap.bind(listenAddress);
        allChannels.add(serverChannel);
    } catch (ChannelException e) {
        log.debug("Exception binding to BGP port {}: ",
                  listenAddress.getPort(), e);
    }
}
 
Example #16
Source File: NioSocketChannel.java    From android-netty with Apache License 2.0 5 votes vote down vote up
public NioSocketChannel(
        Channel parent, ChannelFactory factory,
        ChannelPipeline pipeline, ChannelSink sink,
        SocketChannel socket, NioWorker worker) {
    super(parent, factory, pipeline, sink, worker, socket);
    config = new DefaultNioSocketChannelConfig(socket.socket());
}
 
Example #17
Source File: NioClientSocketChannel.java    From android-netty with Apache License 2.0 5 votes vote down vote up
NioClientSocketChannel(
        ChannelFactory factory, ChannelPipeline pipeline,
        ChannelSink sink, NioWorker worker) {

    super(null, factory, pipeline, sink, newSocket(), worker);
    fireChannelOpen(this);
}
 
Example #18
Source File: AbstractNioChannel.java    From android-netty with Apache License 2.0 5 votes vote down vote up
protected AbstractNioChannel(
        Integer id, Channel parent, ChannelFactory factory, ChannelPipeline pipeline,
        ChannelSink sink, AbstractNioWorker worker, C ch) {
    super(id, parent, factory, pipeline, sink);
    this.worker = worker;
    channel = ch;
}
 
Example #19
Source File: AbstractNioChannel.java    From android-netty with Apache License 2.0 5 votes vote down vote up
protected AbstractNioChannel(
        Channel parent, ChannelFactory factory,
        ChannelPipeline pipeline, ChannelSink sink, AbstractNioWorker worker, C ch)  {
    super(parent, factory, pipeline, sink);
    this.worker = worker;
    channel = ch;
}
 
Example #20
Source File: Bootstrap.java    From android-netty with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the {@link ChannelFactory} that will be used to perform an I/O
 * operation.  This method can be called only once and can't be called at
 * all if the factory was specified in the constructor.
 *
 * @throws IllegalStateException
 *         if the factory is already set
 */
public void setFactory(ChannelFactory factory) {
    if (factory == null) {
        throw new NullPointerException("factory");
    }
    if (this.factory != null) {
        throw new IllegalStateException(
                "factory can't change once set.");
    }
    this.factory = factory;
}
 
Example #21
Source File: Bootstrap.java    From android-netty with Apache License 2.0 5 votes vote down vote up
/**
 * This method simply delegates the call to
 * {@link ChannelFactory#releaseExternalResources()}.
 */
public void releaseExternalResources() {
    ChannelFactory factory = this.factory;
    if (factory != null) {
        factory.releaseExternalResources();
    }
}
 
Example #22
Source File: Bootstrap.java    From android-netty with Apache License 2.0 5 votes vote down vote up
/**
 * This method simply delegates the call to
 * {@link ChannelFactory#shutdown()}.
 */
public void shutdown() {
    ChannelFactory factory = this.factory;
    if (factory != null) {
        factory.shutdown();
    }
}
 
Example #23
Source File: NettyClientAsync.java    From jstorm with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
NettyClientAsync(Map conf, ChannelFactory factory, String host, int port, ReconnectRunnable reconnector, final Set<Integer> sourceTasks, final Set<Integer> targetTasks) {
    super(conf, factory, host, port, reconnector);

    clientChannelFactory = factory;
    initFlowCtrl(conf, sourceTasks, targetTasks);
    
    flushCheckInterval = Utils.getInt(conf.get(Config.STORM_NETTY_FLUSH_CHECK_INTERVAL_MS), 5);
    flusher = new NettyClientFlush(flushCheckInterval);
    flusher.start();

    start();
}
 
Example #24
Source File: NettyServer.java    From aion-germany with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param channelFactory
 * @param listenAddress
 * @param port
 * @param channelPipelineFactory
 * @return Channel
 */
private Channel initChannel(ChannelFactory channelFactory, InetSocketAddress address, ChannelPipelineFactory channelPipelineFactory) {
    ServerBootstrap bootstrap = new ServerBootstrap(channelFactory);
    bootstrap.setPipelineFactory(channelPipelineFactory);
    bootstrap.setOption("child.bufferFactory", HeapChannelBufferFactory.getInstance(ByteOrder.LITTLE_ENDIAN));
    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setOption("child.keepAlive", true);
    bootstrap.setOption("child.reuseAddress", true);
    bootstrap.setOption("child.connectTimeoutMillis", 100);
    bootstrap.setOption("readWriteFair", true);

    return bootstrap.bind(address);
}
 
Example #25
Source File: ConnectionFactory.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
ConnectionFactory(Timer connectTimer, Closed closed, ChannelFactory channelFactory,
                         SocketOption socketOption, ClientOption clientOption, ClientHandlerFactory clientHandlerFactory, PipelineFactory pipelineFactory) {

    this.connectTimer = Assert.requireNonNull(connectTimer, "connectTimer");
    this.closed = Assert.requireNonNull(closed, "release");

    this.channelFactory = Assert.requireNonNull(channelFactory, "channelFactory");

    this.socketOption = Assert.requireNonNull(socketOption, "option");
    this.clientOption = Assert.requireNonNull(clientOption, "connectTimer");
    this.clientHandlerFactory = Assert.requireNonNull(clientHandlerFactory, "clientHandlerFactory");
    this.pipelineFactory = Assert.requireNonNull(pipelineFactory, "pipelineFactory");
}
 
Example #26
Source File: NettyServer.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    ExecutorService boss = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerBoss", true));
    ExecutorService worker = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerWorker", true));
    ChannelFactory channelFactory = new NioServerSocketChannelFactory(boss, worker, getUrl().getPositiveParameter(Constants.IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS));
    bootstrap = new ServerBootstrap(channelFactory);
    
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    channels = nettyHandler.getChannels();
    // https://issues.jboss.org/browse/NETTY-365
    // https://issues.jboss.org/browse/NETTY-379
    // final Timer timer = new HashedWheelTimer(new NamedThreadFactory("NettyIdleTimer", true));
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec() ,getUrl(), NettyServer.this);
            ChannelPipeline pipeline = Channels.pipeline();
            /*int idleTimeout = getIdleTimeout();
            if (idleTimeout > 10000) {
                pipeline.addLast("timer", new IdleStateHandler(timer, idleTimeout / 1000, 0, 0));
            }*/
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
    // bind
    channel = bootstrap.bind(getBindAddress());
}
 
Example #27
Source File: TcpMeta.java    From parallec with Apache License 2.0 5 votes vote down vote up
public TcpMeta(String command, int tcpPort, int tcpConnectTimeoutMillis,
        int tcpIdleTimeoutSec,
        ChannelFactory channelFactory) {
    super();
    this.command = command;
    this.tcpPort = tcpPort;
    this.tcpConnectTimeoutMillis = tcpConnectTimeoutMillis;
    this.tcpIdleTimeoutSec = tcpIdleTimeoutSec;
    this.channelFactory = channelFactory;
    
}
 
Example #28
Source File: NettyServer.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    ExecutorService boss = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerBoss", true));
    ExecutorService worker = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerWorker", true));
    ChannelFactory channelFactory = new NioServerSocketChannelFactory(boss, worker, getUrl().getPositiveParameter(Constants.IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS));
    bootstrap = new ServerBootstrap(channelFactory);
    
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    channels = nettyHandler.getChannels();
    // https://issues.jboss.org/browse/NETTY-365
    // https://issues.jboss.org/browse/NETTY-379
    // final Timer timer = new HashedWheelTimer(new NamedThreadFactory("NettyIdleTimer", true));
    bootstrap.setPipelineFactory(() -> {
        NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec() ,getUrl(), NettyServer.this);
        ChannelPipeline pipeline = Channels.pipeline();
        /*int idleTimeout = getIdleTimeout();
        if (idleTimeout > 10000) {
            pipeline.addLast("timer", new IdleStateHandler(timer, idleTimeout / 1000, 0, 0));
        }*/
        pipeline.addLast("decoder", adapter.getDecoder());
        pipeline.addLast("encoder", adapter.getEncoder());
        pipeline.addLast("handler", nettyHandler);
        return pipeline;
    });
    // bind
    channel = bootstrap.bind(getBindAddress());
}
 
Example #29
Source File: TSOClientRaw.java    From phoenix-omid with Apache License 2.0 5 votes vote down vote up
public TSOClientRaw(String host, int port) throws InterruptedException, ExecutionException {
    // Start client with Nb of active threads = 3 as maximum.
    ChannelFactory factory = new NioClientSocketChannelFactory(
            Executors.newCachedThreadPool(
                    new ThreadFactoryBuilder().setNameFormat("tsoclient-boss-%d").build()),
            Executors.newCachedThreadPool(
                    new ThreadFactoryBuilder().setNameFormat("tsoclient-worker-%d").build()), 3);
    // Create the bootstrap
    ClientBootstrap bootstrap = new ClientBootstrap(factory);

    InetSocketAddress addr = new InetSocketAddress(host, port);

    ChannelPipeline pipeline = bootstrap.getPipeline();
    pipeline.addLast("lengthbaseddecoder",
            new LengthFieldBasedFrameDecoder(8 * 1024, 0, 4, 0, 4));
    pipeline.addLast("lengthprepender", new LengthFieldPrepender(4));
    pipeline.addLast("protobufdecoder",
            new ProtobufDecoder(TSOProto.Response.getDefaultInstance()));
    pipeline.addLast("protobufencoder", new ProtobufEncoder());

    Handler handler = new Handler();
    pipeline.addLast("handler", handler);

    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("reuseAddress", true);
    bootstrap.setOption("connectTimeoutMillis", 100);

    ChannelFuture channelFuture = bootstrap.connect(addr).await();
    channel = channelFuture.getChannel();
}
 
Example #30
Source File: NettyServer.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    ExecutorService boss = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerBoss", true));
    ExecutorService worker = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerWorker", true));
    ChannelFactory channelFactory = new NioServerSocketChannelFactory(boss, worker, getUrl().getPositiveParameter(Constants.IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS));
    bootstrap = new ServerBootstrap(channelFactory);
    
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    channels = nettyHandler.getChannels();
    // https://issues.jboss.org/browse/NETTY-365
    // https://issues.jboss.org/browse/NETTY-379
    // final Timer timer = new HashedWheelTimer(new NamedThreadFactory("NettyIdleTimer", true));
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec() ,getUrl(), NettyServer.this);
            ChannelPipeline pipeline = Channels.pipeline();
            /*int idleTimeout = getIdleTimeout();
            if (idleTimeout > 10000) {
                pipeline.addLast("timer", new IdleStateHandler(timer, idleTimeout / 1000, 0, 0));
            }*/
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
    // bind
    channel = bootstrap.bind(getBindAddress());
}