/** * 启动 cim server * * @return * @throws InterruptedException */ @PostConstruct public void start() throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap() .group(boss, work) .channel(NioServerSocketChannel.class) .localAddress(new InetSocketAddress(nettyPort)) //保持长连接 .childOption(ChannelOption.SO_KEEPALIVE, true) .childHandler(new CIMServerInitializer()); ChannelFuture future = bootstrap.bind().sync(); if (future.isSuccess()) { LOGGER.info("启动 cim server 成功"); } }
public void start() throws Exception { final EchoServerHandler serverHandler = new EchoServerHandler(); EventLoopGroup group = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(group) .channel(NioServerSocketChannel.class) .localAddress(new InetSocketAddress(port)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(serverHandler); } }); ChannelFuture f = b.bind().sync(); System.out.println(EchoServer.class.getName() + " started and listening for connections on " + f.channel().localAddress()); f.channel().closeFuture().sync(); } finally { group.shutdownGracefully().sync(); } } }
nettyServerConfig.getWriteBufferHighWaterMark()) .childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, nettyServerConfig.getWriteBufferLowWaterMark()) .localAddress(new InetSocketAddress(listenPort)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override
.group(parentGroup, childGroup) .channel(NioServerSocketChannel.class) .localAddress(new InetSocketAddress(ServerContains.port)) .childHandler(new DefaultChannelInitializer());
b.group(group) .channel(OioServerSocketChannel.class) .localAddress(new InetSocketAddress(port)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override
ServerBootstrap b = new ServerBootstrap(); b.group(group).channel(NioServerSocketChannel.class) .localAddress(new InetSocketAddress(port)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override
.childOption(ChannelOption.SO_SNDBUF, nettyServerConfig.getServerSocketSndBufSize()) .childOption(ChannelOption.SO_RCVBUF, nettyServerConfig.getServerSocketRcvBufSize()) .localAddress(new InetSocketAddress(this.nettyServerConfig.getListenPort())) .childHandler(new ChannelInitializer<SocketChannel>() { @Override
.option(ChannelOption.SO_REUSEADDR, true) .childOption(ChannelOption.TCP_NODELAY, true) .localAddress(new InetSocketAddress(this.remotingServerConfig.getListenPort())) .childHandler(new ChannelInitializer<SocketChannel>() { @Override
.option(ChannelOption.SO_REUSEADDR, true) .childOption(ChannelOption.TCP_NODELAY, true) .localAddress(new InetSocketAddress(this.remotingServerConfig.getListenPort())) .childHandler(new ChannelInitializer<SocketChannel>() { @Override
private void init() { bossGroup = new NioEventLoopGroup(MQTTBridgeConfiguration.threadNumOfBossGroup()); workerGroup = new NioEventLoopGroup(MQTTBridgeConfiguration.threadNumOfWorkerGroup()); serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup) .localAddress(MQTTBridgeConfiguration.host(), MQTTBridgeConfiguration.port()) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, MQTTBridgeConfiguration.socketBacklog()) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("mqtt-decoder", new MqttDecoder()); pipeline.addLast("mqtt-encoder", MqttEncoder.INSTANCE); pipeline.addLast("channel-idle-handler", new MqttIdleHandler()); pipeline.addLast("message-dispatcher", messageDispatcher); pipeline.addLast("connection-manager", connectionHandler); } }); subscriptionStore = new InMemorySubscriptionStore(); clientManager = new ClientManagerImpl(); messageDispatcher = new MessageDispatcher(clientManager); connectionHandler = new MqttConnectionHandler(clientManager, subscriptionStore); registerMessageHandlers(); }
.childOption(ChannelOption.SO_SNDBUF, nettyServerConfig.getServerSocketSndBufSize()) .childOption(ChannelOption.SO_RCVBUF, nettyServerConfig.getServerSocketRcvBufSize()) .localAddress(new InetSocketAddress(this.nettyServerConfig.getListenPort())) .childHandler(new ChannelInitializer<SocketChannel>() { @Override
.childOption(ChannelOption.SO_SNDBUF, nettyServerConfig.getServerSocketSndBufSize()) .childOption(ChannelOption.SO_RCVBUF, nettyServerConfig.getServerSocketRcvBufSize()) .localAddress(new InetSocketAddress(this.nettyServerConfig.getListenPort())) .childHandler(new ChannelInitializer<SocketChannel>() { @Override
.childHandler( PipelineUtils.SERVER_CHILD ) .group( eventLoops ) .localAddress( info.getHost() ) .bind().addListener( listener );
@Override protected void configure(ServerBootstrap serverBootstrap, Bootstrap bootstrap, ByteBufAllocator allocator) { serverBootstrap.localAddress(new InetSocketAddress(NetUtil.LOCALHOST, 0)); serverBootstrap.option(ChannelOption.ALLOCATOR, allocator); serverBootstrap.childOption(ChannelOption.ALLOCATOR, allocator); bootstrap.option(ChannelOption.ALLOCATOR, allocator); } }
/** * The address to which this server should bind on subscribe. * * @param bindingAddressSupplier A supplier of the address to bind to. * * @return a new {@link TcpServer} */ public final TcpServer addressSupplier(Supplier<? extends SocketAddress> bindingAddressSupplier) { Objects.requireNonNull(bindingAddressSupplier, "bindingAddressSupplier"); return bootstrap(b -> b.localAddress(bindingAddressSupplier.get())); }
/** * The address to which this server should bind on subscribe. * * @param bindingAddressSupplier A supplier of the address to bind to. * * @return a new {@link TcpServer} */ public final TcpServer addressSupplier(Supplier<? extends SocketAddress> bindingAddressSupplier) { Objects.requireNonNull(bindingAddressSupplier, "bindingAddressSupplier"); return bootstrap(b -> b.localAddress(bindingAddressSupplier.get())); }
@Override protected void configure(ServerBootstrap bootstrap, Bootstrap bootstrap2, ByteBufAllocator allocator) { bootstrap.localAddress(newSocketAddress()); bootstrap.option(ChannelOption.ALLOCATOR, allocator); bootstrap.childOption(ChannelOption.ALLOCATOR, allocator); bootstrap2.option(ChannelOption.ALLOCATOR, allocator); }
public void start() throws InterruptedException { ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap .channel(NioServerSocketChannel.class) .group(this.group) .localAddress(this.host, this.port) .childHandler(createChildHandler()); ChannelFuture future = serverBootstrap.bind(); future.sync(); }
private void bootstrapServer(SocketAddress localAddress, NioEventLoopGroup parentGroup, NioEventLoopGroup childGroup) { new ServerBootstrap().group(parentGroup, childGroup) .channel(NioServerSocketChannel.class) .localAddress(localAddress) .childHandler(new SimpleHttpServerInitializer()) .bind().awaitUninterruptibly(); // Make sure the server is bound before the constructor returns. LOGGER.info("Server listening on {}", localAddress); }
public void run() throws Exception { ServerBootstrap server = new ServerBootstrap(); try { server.group(new NioEventLoopGroup(), new NioEventLoopGroup()) .channel(NioServerSocketChannel.class).localAddress(port) .childHandler(new DispatcherServletChannelInitializer()); server.bind().sync().channel().closeFuture().sync(); } finally { server.shutdown(); } }