Java 类org.jboss.netty.channel.ChannelPipelineFactory 实例源码

项目:EatDubbo    文件:NettyClient.java   
@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    bootstrap = new ClientBootstrap(channelFactory);
    // config
    // @see org.jboss.netty.channel.socket.SocketChannelConfig
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("connectTimeoutMillis", getTimeout());
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this);
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
}
项目:dubbo2    文件:NettyClient.java   
@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    bootstrap = new ClientBootstrap(channelFactory);
    // config
    // @see org.jboss.netty.channel.socket.SocketChannelConfig
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("connectTimeoutMillis", getTimeout());
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this);
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
}
项目:https-github.com-apache-zookeeper    文件:NettyServerCnxnFactory.java   
NettyServerCnxnFactory() {
    bootstrap = new ServerBootstrap(
            new NioServerSocketChannelFactory(
                    Executors.newCachedThreadPool(),
                    Executors.newCachedThreadPool()));
    // parent channel
    bootstrap.setOption("reuseAddress", true);
    // child channels
    bootstrap.setOption("child.tcpNoDelay", true);
    /* set socket linger to off, so that socket close does not block */
    bootstrap.setOption("child.soLinger", -1);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        @Override
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline p = Channels.pipeline();
            if (secure) {
                initSSL(p);
            }
            p.addLast("servercnxnfactory", channelHandler);

            return p;
        }
    });
}
项目:dubbox-hystrix    文件:NettyClient.java   
@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    bootstrap = new ClientBootstrap(channelFactory);
    // config
    // @see org.jboss.netty.channel.socket.SocketChannelConfig
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("connectTimeoutMillis", getTimeout());
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this);
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
}
项目:hadoop    文件:TestDelegationTokenRemoteFetcher.java   
private ServerBootstrap startHttpServer(int port,
    final Token<DelegationTokenIdentifier> token, final URI url) {
  ServerBootstrap bootstrap = new ServerBootstrap(
      new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
          Executors.newCachedThreadPool()));

  bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    @Override
    public ChannelPipeline getPipeline() throws Exception {
      return Channels.pipeline(new HttpRequestDecoder(),
          new HttpChunkAggregator(65536), new HttpResponseEncoder(),
          new CredentialsLogicHandler(token, url.toString()));
    }
  });
  bootstrap.bind(new InetSocketAddress("localhost", port));
  return bootstrap;
}
项目:athena    文件:Controller.java   
/**
 * Tell controller that we're ready to accept switches loop.
 */
public void run() {

    try {
        final ServerBootstrap bootstrap = createServerBootStrap();

        bootstrap.setOption("reuseAddr", true);
        bootstrap.setOption("child.keepAlive", true);
        bootstrap.setOption("child.tcpNoDelay", true);
        bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);

        ChannelPipelineFactory pfact =
                new OpenflowPipelineFactory(this, null, sslContext);
        bootstrap.setPipelineFactory(pfact);
        cg = new DefaultChannelGroup();
        openFlowPorts.forEach(port -> {
            InetSocketAddress sa = new InetSocketAddress(port);
            cg.add(bootstrap.bind(sa));
            log.info("Listening for switch connections on {}", sa);
        });

    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}
项目:athena    文件:Controller.java   
/**
 * Tell controller that we're ready to accept pcc connections.
 */
public void run() {
    try {
        final ServerBootstrap bootstrap = createServerBootStrap();

        bootstrap.setOption("reuseAddr", true);
        bootstrap.setOption("child.keepAlive", true);
        bootstrap.setOption("child.tcpNoDelay", true);
        bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);

        ChannelPipelineFactory pfact = new PcepPipelineFactory(this);

        bootstrap.setPipelineFactory(pfact);
        InetSocketAddress sa = new InetSocketAddress(pcepPort);
        cg = new DefaultChannelGroup();
        cg.add(bootstrap.bind(sa));
        log.info("Listening for PCC connection on {}", sa);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
项目:athena    文件:BgpControllerImplTest.java   
/**
  * 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);
}
项目:athena    文件:BgpControllerImplTest.java   
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;
}
项目:iTAP-controller    文件:RPCService.java   
/**
 * Connect to remote servers.  We'll initiate the connection to
 * any nodes with a lower ID so that there will be a single connection
 * between each pair of nodes which we'll use symmetrically
 */
protected void startClients(ChannelPipelineFactory pipelineFactory) {
    final ClientBootstrap bootstrap =
            new ClientBootstrap(
                 new NioClientSocketChannelFactory(bossExecutor,
                                                   workerExecutor));
    bootstrap.setOption("child.reuseAddr", true);
    bootstrap.setOption("child.keepAlive", true);
    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setOption("child.sendBufferSize", SEND_BUFFER_SIZE);
    bootstrap.setOption("child.connectTimeoutMillis", CONNECT_TIMEOUT);
    bootstrap.setPipelineFactory(pipelineFactory);
    clientBootstrap = bootstrap;

    ScheduledExecutorService ses = 
            syncManager.getThreadPool().getScheduledExecutor();
    reconnectTask = new SingletonTask(ses, new ConnectTask());
    reconnectTask.reschedule(0, TimeUnit.SECONDS);
}
项目:iTAP-controller    文件:OFSwitchManager.java   
/**
 * Bootstraps netty, the server that handles all openflow connections
 */
public void bootstrapNetty() {
    try {
        final ServerBootstrap bootstrap = createServerBootStrap();

        bootstrap.setOption("reuseAddr", true);
        bootstrap.setOption("child.keepAlive", true);
        bootstrap.setOption("child.tcpNoDelay", true);
        bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);

        ChannelPipelineFactory pfact = useSsl ? new OpenflowPipelineFactory(this, floodlightProvider.getTimer(), this, debugCounterService, keyStore, keyStorePassword) :
            new OpenflowPipelineFactory(this, floodlightProvider.getTimer(), this, debugCounterService);

        bootstrap.setPipelineFactory(pfact);
        InetSocketAddress sa = new InetSocketAddress(floodlightProvider.getOFPort());
        final ChannelGroup cg = new DefaultChannelGroup();
        cg.add(bootstrap.bind(sa));

        log.info("Listening for switch connections on {}", sa);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
项目:dubbocloud    文件:NettyClient.java   
@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    bootstrap = new ClientBootstrap(channelFactory);
    // config
    // @see org.jboss.netty.channel.socket.SocketChannelConfig
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("connectTimeoutMillis", getTimeout());
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this);
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
}
项目:dubbos    文件:NettyClient.java   
@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    bootstrap = new ClientBootstrap(channelFactory);
    // config
    // @see org.jboss.netty.channel.socket.SocketChannelConfig
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("connectTimeoutMillis", getTimeout());
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this);
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
}
项目:bigstreams    文件:ServerUtil.java   
/**
 * Startup a ServerBootstrap with NioServerSocketChannelFactory using the
 * portNo specified in the constructor.
 * 
 * @return
 */
public ServerBootstrap connect() {

    bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
            Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool()));

    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        @Override
        public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(new MessageFrameDecoder(),  new MessageEventBagHandler(bagList));
        }
    });

    System.out.println("Binding to: localhost:" + portNo);
    bootstrap.bind(new InetSocketAddress("localhost", portNo));

    return bootstrap;

}
项目:bigstreams    文件:TestFilesSendWorker.java   
private ServerBootstrap connectServer(boolean simulateConflict,
        boolean simulateConflictErrorPointer) {

    ServerBootstrap bootstrap = new ServerBootstrap(
            new NioServerSocketChannelFactory(
                    Executors.newCachedThreadPool(),
                    Executors.newCachedThreadPool()));

    final MessageEventBagHandler messagEventBagHandler = new MessageEventBagHandler(
            bagList, simulateConflict, simulateConflictErrorPointer);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        @Override
        public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(new MessageFrameDecoder(),
                    messagEventBagHandler);
        }
    });

    bootstrap.bind(new InetSocketAddress(testPort));

    return bootstrap;

}
项目:bigstreams    文件:TestFilesSendWorkerConflict.java   
private ServerBootstrap connectServer() {

        ServerBootstrap bootstrap = new ServerBootstrap(
                new NioServerSocketChannelFactory(
                        Executors.newCachedThreadPool(),
                        Executors.newCachedThreadPool()));

        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            @Override
            public ChannelPipeline getPipeline() throws Exception {
                return Channels.pipeline(new MessageFrameDecoder(),
                        new MessageEventBagHandler(bagList));
            }
        });

        bootstrap.bind(new InetSocketAddress(testPort));

        return bootstrap;

    }
项目:bigstreams    文件:CollectorServerImpl.java   
@Override
public void connect() {

    workerService = createWorkerService(getThreadPoolType(CollectorProperties.WRITER.COLLECTOR_WORKER_THREAD_POOL));

    workerbossService = createWorkderBossService(getThreadPoolType(CollectorProperties.WRITER.COLLECTOR_WORKERBOSS_THREAD_POOL));
    channelFactory = new NioServerSocketChannelFactory(workerbossService,
            workerService);

    bootstrap = new ServerBootstrap(channelFactory);

    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        @Override
        public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(ipFilterHandler,
                    new MessageFrameDecoder(), new ReadTimeoutHandler(
                            HashedWheelTimerFactory.getInstance(),
                            readTimeout, TimeUnit.MILLISECONDS),
                    metricsHandler, channelHandler);
        }
    });

    bootstrap.bind(new InetSocketAddress(port));

}
项目:bigstreams    文件:CoordinationServerImpl.java   
/**
 * Startup a ServerBootstrap with NioServerSocketChannelFactory using the
 * portNo specified in the constructor.
 * 
 */
private void connectLockBootstrap() {

    lockBootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
            Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool()));

    lockBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        @Override
        public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(new MessageFrameDecoder(),
                    metricHandler, lockHandler);
        }
    });

    lockBootstrap.bind(new InetSocketAddress(lockPort));

}
项目:bigstreams    文件:CoordinationServerImpl.java   
/**
 * Startup a ServerBootstrap with NioServerSocketChannelFactory using the
 * portNo specified in the constructor.
 * 
 */
private void connectUnlockBootstrap() {

    unlockBootstrap = new ServerBootstrap(
            new NioServerSocketChannelFactory(
                    Executors.newCachedThreadPool(),
                    Executors.newCachedThreadPool()));

    unlockBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        @Override
        public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(new MessageFrameDecoder(),
                    unlockHandler);
        }
    });

    unlockBootstrap.bind(new InetSocketAddress(releaseLockPort));

}
项目:QoS-floodlight    文件:RPCService.java   
/**
 * Connect to remote servers.  We'll initiate the connection to
 * any nodes with a lower ID so that there will be a single connection
 * between each pair of nodes which we'll use symmetrically
 */
protected void startClients(ChannelPipelineFactory pipelineFactory) {
    final ClientBootstrap bootstrap =
            new ClientBootstrap(
                 new NioClientSocketChannelFactory(bossExecutor,
                                                   workerExecutor));
    bootstrap.setOption("child.reuseAddr", true);
    bootstrap.setOption("child.keepAlive", true);
    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setOption("child.sendBufferSize", SEND_BUFFER_SIZE);
    bootstrap.setOption("child.connectTimeoutMillis", CONNECT_TIMEOUT);
    bootstrap.setPipelineFactory(pipelineFactory);
    clientBootstrap = bootstrap;

    ScheduledExecutorService ses = 
            syncManager.getThreadPool().getScheduledExecutor();
    reconnectTask = new SingletonTask(ses, new ConnectTask());
    reconnectTask.reschedule(0, TimeUnit.SECONDS);
}
项目:dubbo-comments    文件:NettyClient.java   
@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    bootstrap = new ClientBootstrap(channelFactory);
    // config
    // @see org.jboss.netty.channel.socket.SocketChannelConfig
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("connectTimeoutMillis", getTimeout());
    //下面才是正确的
    //bootstrap.setOption("connectTimeoutMillis", getConnectTimeout());
    //netty handler
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this);
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
}
项目:nfs-rpc    文件:NettyServer.java   
public void start(int listenPort, final ExecutorService threadPool) throws Exception {
  if (!startFlag.compareAndSet(false, true)) {
    return;
  }
  bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    public ChannelPipeline getPipeline() throws Exception {
      ChannelPipeline pipeline = new DefaultChannelPipeline();
      pipeline.addLast("decoder", new NettyProtocolDecoder());
      pipeline.addLast("encoder", new NettyProtocolEncoder());
      pipeline.addLast("handler", new NettyServerHandler(threadPool));
      return pipeline;
    }
  });
  bootstrap.bind(new InetSocketAddress(listenPort));
  LOGGER.warn("Server started,listen at: " + listenPort);
}
项目:dubbox    文件:NettyClient.java   
@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    bootstrap = new ClientBootstrap(channelFactory);
    // config
    // @see org.jboss.netty.channel.socket.SocketChannelConfig
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("connectTimeoutMillis", getTimeout());
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this);
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
}
项目:dubbo    文件:NettyClient.java   
@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    bootstrap = new ClientBootstrap(channelFactory);
    // config
    // @see org.jboss.netty.channel.socket.SocketChannelConfig
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("connectTimeoutMillis", getTimeout());
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this);
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
}
项目:big-c    文件:TestDelegationTokenRemoteFetcher.java   
private ServerBootstrap startHttpServer(int port,
    final Token<DelegationTokenIdentifier> token, final URI url) {
  ServerBootstrap bootstrap = new ServerBootstrap(
      new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
          Executors.newCachedThreadPool()));

  bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    @Override
    public ChannelPipeline getPipeline() throws Exception {
      return Channels.pipeline(new HttpRequestDecoder(),
          new HttpChunkAggregator(65536), new HttpResponseEncoder(),
          new CredentialsLogicHandler(token, url.toString()));
    }
  });
  bootstrap.bind(new InetSocketAddress("localhost", port));
  return bootstrap;
}
项目:opentsdb-flume    文件:OpenTSDBSource.java   
@Override
public void start() {
  org.jboss.netty.channel.ChannelFactory factory = new NioServerSocketChannelFactory(
          Executors.newCachedThreadPool(), Executors.newCachedThreadPool());

  ServerBootstrap serverBootstrap = new ServerBootstrap(factory);
  serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    @Override
    public ChannelPipeline getPipeline() {
      EventHandler handler = new EventHandler();
      final ChannelPipeline pipeline = Channels.pipeline(handler);
      pipeline.addFirst("decoder", new LineBasedFrameDecoder(1024));
      pipeline.addLast("encoder", new StringEncoder(Charsets.UTF_8));
      return pipeline;
    }
  });

  logger.info("OpenTSDB Source starting...");

  if (host == null) {
    nettyChannel = serverBootstrap.bind(new InetSocketAddress(port));
  } else {
    nettyChannel = serverBootstrap.bind(new InetSocketAddress(host, port));
  }
  super.start();
}
项目:dubbo-learning    文件:NettyClient.java   
@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    bootstrap = new ClientBootstrap(channelFactory);
    // config
    // @see org.jboss.netty.channel.socket.SocketChannelConfig
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("connectTimeoutMillis", getTimeout());
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this);
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
}
项目:dubbo-learning    文件:NettyServer.java   
@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    //设置线程池(但是线程池中的线程都是守护线程,为的就是当JVM退出时候不用考虑守护线程是否已经结束)
    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); //Netty启动类
    //定义NettyHandler(这个应该是通用的Handler,只有在服务启动的时候生效一次)
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    channels = nettyHandler.getChannels();
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec() ,getUrl(), 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());
}
项目:DubboCode    文件:NettyClient.java   
@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    bootstrap = new ClientBootstrap(channelFactory);
    // config
    // @see org.jboss.netty.channel.socket.SocketChannelConfig
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("connectTimeoutMillis", getTimeout());
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this);
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
}
项目:navi    文件:NaviNettyServer.java   
@Override
public ChannelPipelineFactory getPipelineFactory() {
    executionHandler = new NaviExecutionHandler();

    return new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("httpCodec", new NaviHttpServerCodec());
            pipeline.addLast("inflater", new HttpContentDecompressor());

            pipeline.addLast("GLOBAL_TRAFFIC_SHAPING", globalTcHandler);

            String chunkSize = ServerConfigure.get(NaviDefine.CHUNK_AGGR_SIZE);
            if (StringUtils.isNumeric(chunkSize)) {
                pipeline.addLast("aggregator", new HttpChunkAggregator(Integer.valueOf(chunkSize)));
            }

            // pipeline.addLast("encoder", new HttpResponseEncoder());
            pipeline.addLast("deflater", new HttpContentCompressor());
            pipeline.addLast("execution", executionHandler);
            pipeline.addLast("idleState", new IdleStateHandler(timer, getChildChannelIdleTime(), getChildChannelIdleTime(), getChildChannelIdleTime()));
            pipeline.addLast("handler", getNaviHttpHandler());
            return pipeline;
        }
    };
}
项目:navi    文件:NaviNettyTCPServer.java   
public ChannelPipelineFactory getPipelineFactory() {
        executionHandler = new NaviExecutionHandler();
//      execution = new ExecutionHandler(Executors.newCachedThreadPool());
        return new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                ChannelPipeline pipeline = Channels.pipeline();
                pipeline.addLast("idleState", new IdleStateHandler(timer,
                    getChildChannelIdleTime(), getChildChannelIdleTime(),
                    getChildChannelIdleTime()));
                //StateCheckChannelHandler加入心跳机制  读空闲 断开连接 写空闲发送心跳数据
//              pipeline.addLast("idleHandler", new StateCheckChannelHandler());
                pipeline.addLast("decoder", new DelimiterBasedFrameDecoder(getMaxPacketSize(), getDelimiter()));
                pipeline.addLast("execution", executionHandler);
//              pipeline.addLast("execution", execution);
                pipeline.addLast("handler", getNaviTCPHandler());
                return pipeline;
            }
        };
    }
项目:jahhan    文件:NettyClient.java   
@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    bootstrap = new ClientBootstrap(channelFactory);
    // config
    // @see org.jboss.netty.channel.socket.SocketChannelConfig
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("connectTimeoutMillis", getTimeout());
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this);
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
}
项目:fast-failover-demo    文件:RPCService.java   
/**
 * Connect to remote servers.  We'll initiate the connection to
 * any nodes with a lower ID so that there will be a single connection
 * between each pair of nodes which we'll use symmetrically
 */
protected void startClients(ChannelPipelineFactory pipelineFactory) {
    final ClientBootstrap bootstrap =
            new ClientBootstrap(
                 new NioClientSocketChannelFactory(bossExecutor,
                                                   workerExecutor));
    bootstrap.setOption("child.reuseAddr", true);
    bootstrap.setOption("child.keepAlive", true);
    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setOption("child.sendBufferSize", SEND_BUFFER_SIZE);
    bootstrap.setOption("child.connectTimeoutMillis", CONNECT_TIMEOUT);
    bootstrap.setPipelineFactory(pipelineFactory);
    clientBootstrap = bootstrap;

    ScheduledExecutorService ses = 
            syncManager.getThreadPool().getScheduledExecutor();
    reconnectTask = new SingletonTask(ses, new ConnectTask());
    reconnectTask.reschedule(0, TimeUnit.SECONDS);
}
项目:fast-failover-demo    文件:OFSwitchManager.java   
/**
 * Bootstraps netty, the server that handles all openflow connections
 */
public void bootstrapNetty() {
    try {
        final ServerBootstrap bootstrap = createServerBootStrap();

        bootstrap.setOption("reuseAddr", true);
        bootstrap.setOption("child.keepAlive", true);
        bootstrap.setOption("child.tcpNoDelay", true);
        bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);

        ChannelPipelineFactory pfact = useSsl ? new OpenflowPipelineFactory(this, floodlightProvider.getTimer(), this, debugCounterService, keyStore, keyStorePassword) :
            new OpenflowPipelineFactory(this, floodlightProvider.getTimer(), this, debugCounterService);

        bootstrap.setPipelineFactory(pfact);
        InetSocketAddress sa = new InetSocketAddress(floodlightProvider.getOFPort());
        final ChannelGroup cg = new DefaultChannelGroup();
        cg.add(bootstrap.bind(sa));

        log.info("Listening for switch connections on {}", sa);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
项目:hadoop-2.6.0-cdh5.4.3    文件:TestDelegationTokenRemoteFetcher.java   
private ServerBootstrap startHttpServer(int port,
    final Token<DelegationTokenIdentifier> token, final URI url) {
  ServerBootstrap bootstrap = new ServerBootstrap(
      new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
          Executors.newCachedThreadPool()));

  bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    @Override
    public ChannelPipeline getPipeline() throws Exception {
      return Channels.pipeline(new HttpRequestDecoder(),
          new HttpChunkAggregator(65536), new HttpResponseEncoder(),
          new CredentialsLogicHandler(token, url.toString()));
    }
  });
  bootstrap.bind(new InetSocketAddress("localhost", port));
  return bootstrap;
}
项目:anima    文件:NettyClient.java   
@Override
public void doOpen() throws Throwable {
    bootstrap = new ClientBootstrap(channelFactory);
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("connectTimeoutMillis", getConnectTimeout());
    final NettyHandler nettyHandler = new NettyHandler(getConf(), this);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getConf(),getCodec(), NettyClient.this);
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
}
项目:anima    文件:NettyServer.java   
@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());
}
项目:ravikumaran201504    文件:Controller.java   
/**
 * Tell controller that we're ready to accept switches loop.
 */
public void run() {

    try {
        final ServerBootstrap bootstrap = createServerBootStrap();

        bootstrap.setOption("reuseAddr", true);
        bootstrap.setOption("child.keepAlive", true);
        bootstrap.setOption("child.tcpNoDelay", true);
        bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);

        ChannelPipelineFactory pfact =
                new OpenflowPipelineFactory(this, null);
        bootstrap.setPipelineFactory(pfact);
        InetSocketAddress sa = new InetSocketAddress(openFlowPort);
        cg = new DefaultChannelGroup();
        cg.add(bootstrap.bind(sa));

        log.info("Listening for switch connections on {}", sa);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}
项目:dataworks-zeus    文件:MasterServer.java   
public MasterServer(final ChannelHandler handler){
    NioServerSocketChannelFactory channelFactory=
        new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
    bootstrap=new ServerBootstrap(channelFactory);
    pipelineFactory=new ChannelPipelineFactory(){
        private final ProtobufVarint32LengthFieldPrepender frameEncoder = new ProtobufVarint32LengthFieldPrepender();
        private final ProtobufEncoder protobufEncoder = new ProtobufEncoder();
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline p = pipeline();
            p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
            p.addLast("protobufDecoder",new ProtobufDecoder(Protocol.SocketMessage.getDefaultInstance()));
            p.addLast("frameEncoder", frameEncoder);
            p.addLast("protobufEncoder", protobufEncoder);
            p.addLast("handler", handler);
            return p;
        }

    };
    try {
        bootstrap.setPipeline(pipelineFactory.getPipeline());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
项目:dataworks-zeus    文件:MasterServer.java   
public MasterServer(final ChannelHandler handler){
    NioServerSocketChannelFactory channelFactory=
        new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
    bootstrap=new ServerBootstrap(channelFactory);
    pipelineFactory=new ChannelPipelineFactory(){
        private final ProtobufVarint32LengthFieldPrepender frameEncoder = new ProtobufVarint32LengthFieldPrepender();
        private final ProtobufEncoder protobufEncoder = new ProtobufEncoder();
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline p = pipeline();
            p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
            p.addLast("protobufDecoder",new ProtobufDecoder(Protocol.SocketMessage.getDefaultInstance()));
            p.addLast("frameEncoder", frameEncoder);
            p.addLast("protobufEncoder", protobufEncoder);
            p.addLast("handler", handler);
            return p;
        }

    };
    try {
        bootstrap.setPipeline(pipelineFactory.getPipeline());
    } catch (Exception e) {
        e.printStackTrace();
    }
}