List of usage examples for io.netty.bootstrap ServerBootstrap ServerBootstrap
public ServerBootstrap()
From source file:com.sohu.jafka.http.HttpServer.java
License:Apache License
public void run() { // Configure the server. ///* ww w . j a va 2 s . c o m*/ try { ServerBootstrap b = new ServerBootstrap(); b.option(ChannelOption.SO_BACKLOG, 1024); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new HttpServerInitializer(this)); Channel ch = b.bind(port).sync().channel(); //System.err.println("Open your web browser and navigate to " + "http://127.0.0.1:" + port + '/'); logger.info("Jafka HttpServer start at port {}", port); ch.closeFuture().sync(); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); throw new RuntimeException(ie.getMessage(), ie); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } logger.warn("Jafka HttpServer run over"); }
From source file:com.splicemachine.stream.StreamListenerServer.java
License:Apache License
public void start() throws StandardException { ThreadFactory tf = new ThreadFactoryBuilder().setDaemon(true) .setNameFormat("StreamerListenerServer-boss-%s").build(); this.bossGroup = new NioEventLoopGroup(4, tf); tf = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("StreamerListenerServer-worker-%s").build(); this.workerGroup = new NioEventLoopGroup(4, tf); try {// w w w . j av a2 s .c o m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new OpenHandler(this)) .option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); // Bind and start to accept incoming connections. ChannelFuture f = b.bind(port).sync(); this.serverChannel = f.channel(); InetSocketAddress socketAddress = (InetSocketAddress) this.serverChannel.localAddress(); String host = InetAddress.getLocalHost().getHostName(); int port = socketAddress.getPort(); this.hostAndPort = HostAndPort.fromParts(host, port); LOG.info("StreamListenerServer listening on " + hostAndPort); } catch (IOException e) { throw Exceptions.parseException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } }
From source file:com.spotify.ffwd.debug.NettyDebugServer.java
License:Apache License
public AsyncFuture<Void> start() { final ResolvableFuture<Void> future = async.future(); final ServerBootstrap s = new ServerBootstrap(); s.channel(NioServerSocketChannel.class); s.group(boss, worker);//from www .ja va2 s.c o m s.childHandler(new ChannelInitializer<Channel>() { @Override protected void initChannel(final Channel ch) throws Exception { connected.add(ch); log.info("Connected {}", ch); ch.closeFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { connected.remove(ch); log.info("Disconnected {}", ch); } }); } }); s.bind(localAddress).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture f) throws Exception { if (!f.isSuccess()) { future.fail(f.cause()); return; } log.info("Bound to {}", localAddress); if (!server.compareAndSet(null, f.channel())) { f.channel().close(); future.fail(new IllegalStateException("server already started")); return; } future.resolve(null); } }); return future; }
From source file:com.spotify.ffwd.protocol.ProtocolServersImpl.java
License:Apache License
private AsyncFuture<ProtocolConnection> bindTCP(final Logger log, final Protocol protocol, ProtocolServer server, RetryPolicy policy) { final ServerBootstrap b = new ServerBootstrap(); b.group(boss, worker);/*from w ww. j a va 2 s .c o m*/ b.channel(NioServerSocketChannel.class); b.childHandler(server.initializer()); b.option(ChannelOption.SO_BACKLOG, 128); if (protocol.getReceiveBufferSize() != null) { b.childOption(ChannelOption.SO_RCVBUF, protocol.getReceiveBufferSize()); } b.childOption(ChannelOption.SO_KEEPALIVE, true); final String host = protocol.getAddress().getHostString(); final int port = protocol.getAddress().getPort(); final RetryingProtocolConnection connection = new RetryingProtocolConnection(async, timer, log, policy, new ProtocolChannelSetup() { @Override public ChannelFuture setup() { return b.bind(host, port); } @Override public String toString() { return String.format("bind tcp://%s:%d", host, port); } }); return connection.getInitialFuture(); }
From source file:com.spotify.heroic.rpc.nativerpc.NativeRpcProtocolServer.java
License:Apache License
private AsyncFuture<Void> start() { final ServerBootstrap s = new ServerBootstrap(); s.channel(NioServerSocketChannel.class); s.group(bossGroup, workerGroup);/*from w w w. jav a 2s . co m*/ s.childHandler(new NativeRpcServerSession(timer, mapper, container, maxFrameSize, encoding)); final ChannelFuture bind = s.bind(address); bind.addListener(new ChannelFutureListener() { @Override public void operationComplete(final ChannelFuture f) throws Exception { if (!f.isSuccess()) { bindFuture.fail(f.cause()); return; } serverChannel.set(f.channel()); final InetSocketAddress address = (InetSocketAddress) f.channel().localAddress(); bindFuture.resolve(address); } }); return bindFuture.directTransform(a -> null); }
From source file:com.spotify.netty.handler.codec.zmtp.ZMQIntegrationTest.java
License:Apache License
@Before public void setup() throws InterruptedException { bossGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(); serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override/*w w w. ja v a2 s .co m*/ protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ZMTP20Codec(new ZMTPSession(ZMTPConnectionType.Addressed, 1024, identity.getBytes(), ZMTPSocketType.REQ), false)); ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { @Override public void channelActive(final ChannelHandlerContext ctx) throws Exception { super.channelActive(ctx); channelsConnected.add(ctx.channel()); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { incomingMessages.put((ZMTPIncomingMessage) msg); } }); } }); serverChannel = serverBootstrap.bind(0).sync().channel(); serverAddress = (InetSocketAddress) serverChannel.localAddress(); }
From source file:com.spotify.netty4.handler.codec.zmtp.benchmarks.CustomReqRepBenchmark.java
License:Apache License
public static void main(final String... args) throws InterruptedException { final ProgressMeter meter = new ProgressMeter("requests", true); // Codecs// w ww .j a v a 2 s .c om final ZMTPCodec serverCodec = ZMTPCodec.builder().socketType(ROUTER).encoder(ReplyEncoder.class) .decoder(RequestDecoder.class).build(); final ZMTPCodec clientCodec = ZMTPCodec.builder().socketType(DEALER).encoder(RequestEncoder.class) .decoder(ReplyDecoder.class).build(); // Server final Executor serverExecutor = new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory, UNCAUGHT_EXCEPTION_HANDLER, true); final ServerBootstrap serverBootstrap = new ServerBootstrap() .group(new NioEventLoopGroup(1), new NioEventLoopGroup()).channel(NioServerSocketChannel.class) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.MESSAGE_SIZE_ESTIMATOR, ByteBufSizeEstimator.INSTANCE) .childHandler(new ChannelInitializer<NioSocketChannel>() { @Override protected void initChannel(final NioSocketChannel ch) throws Exception { ch.pipeline().addLast(serverCodec); ch.pipeline().addLast(new ServerRequestTracker()); ch.pipeline().addLast(new ServerHandler(serverExecutor)); } }); final Channel server = serverBootstrap.bind(ANY_PORT).awaitUninterruptibly().channel(); // Client final Executor clientExecutor = new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory, UNCAUGHT_EXCEPTION_HANDLER, true); final SocketAddress address = server.localAddress(); final Bootstrap clientBootstrap = new Bootstrap().group(new NioEventLoopGroup()) .channel(NioSocketChannel.class).option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, ByteBufSizeEstimator.INSTANCE) .handler(new ChannelInitializer<NioSocketChannel>() { @Override protected void initChannel(final NioSocketChannel ch) throws Exception { ch.pipeline().addLast(clientCodec); ch.pipeline().addLast(new ClientRequestTracker()); ch.pipeline().addLast(new ClientHandler(meter, clientExecutor)); } }); final Channel client = clientBootstrap.connect(address).awaitUninterruptibly().channel(); // Run until client is closed client.closeFuture().await(); }
From source file:com.spotify.netty4.handler.codec.zmtp.benchmarks.ReqRepBenchmark.java
License:Apache License
public static void main(final String... args) throws InterruptedException { final ProgressMeter meter = new ProgressMeter("requests"); // Codecs/*from w w w . jav a2s . c o m*/ // Server final ServerBootstrap serverBootstrap = new ServerBootstrap() .group(new NioEventLoopGroup(1), new NioEventLoopGroup()).channel(NioServerSocketChannel.class) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childHandler(new ChannelInitializer<NioSocketChannel>() { @Override protected void initChannel(final NioSocketChannel ch) throws Exception { ch.pipeline().addLast(ZMTPCodec.builder().socketType(ROUTER).build()); ch.pipeline().addLast(new ServerHandler()); } }); final Channel server = serverBootstrap.bind(ANY_PORT).awaitUninterruptibly().channel(); // Client final SocketAddress address = server.localAddress(); final Bootstrap clientBootstrap = new Bootstrap().group(new NioEventLoopGroup()) .channel(NioSocketChannel.class).option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .handler(new ChannelInitializer<NioSocketChannel>() { @Override protected void initChannel(final NioSocketChannel ch) throws Exception { ch.pipeline().addLast(ZMTPCodec.builder().socketType(DEALER).build()); ch.pipeline().addLast(new ClientHandler(meter)); } }); final Channel client = clientBootstrap.connect(address).awaitUninterruptibly().channel(); // Run until client is closed client.closeFuture().await(); }
From source file:com.spotify.netty4.handler.codec.zmtp.benchmarks.ThroughputBenchmark.java
License:Apache License
public static void main(final String... args) throws InterruptedException { final ProgressMeter meter = new ProgressMeter("messages"); // Server/* w ww. j a va2 s. c om*/ final ServerBootstrap serverBootstrap = new ServerBootstrap() .group(new NioEventLoopGroup(1), new NioEventLoopGroup(1)).channel(NioServerSocketChannel.class) .childOption(ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childHandler(new ChannelInitializer<NioSocketChannel>() { @Override protected void initChannel(final NioSocketChannel ch) throws Exception { ch.pipeline().addLast(ZMTPCodec.of(ROUTER)); ch.pipeline().addLast(new ServerHandler(meter)); } }); final Channel server = serverBootstrap.bind(ANY_PORT).awaitUninterruptibly().channel(); // Client final SocketAddress address = server.localAddress(); final Bootstrap clientBootstrap = new Bootstrap().group(new NioEventLoopGroup(1)) .channel(NioSocketChannel.class).option(ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, ByteBufSizeEstimator.INSTANCE) .handler(new ChannelInitializer<NioSocketChannel>() { @Override protected void initChannel(final NioSocketChannel ch) throws Exception { ch.pipeline().addLast(ZMTPCodec.of(DEALER)); ch.pipeline().addLast(new ClientHandler()); } }); final Channel client = clientBootstrap.connect(address).awaitUninterruptibly().channel(); // Run until client is closed client.closeFuture().await(); }
From source file:com.spotify.netty4.handler.codec.zmtp.EndToEndTest.java
License:Apache License
private Channel bind(final SocketAddress address, final ChannelHandler codec, final ChannelHandler handler) { final ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(new NioEventLoopGroup(1), new NioEventLoopGroup()); bootstrap.channel(NioServerSocketChannel.class); bootstrap.childHandler(new ChannelInitializer<NioSocketChannel>() { @Override//from www . ja v a 2 s. c o m protected void initChannel(final NioSocketChannel ch) throws Exception { ch.pipeline().addLast(codec, handler); } }); return bootstrap.bind(address).awaitUninterruptibly().channel(); }