use of io.netty.channel.ChannelInitializer in project chuidiang-ejemplos by chuidiang.
the class Client method run.
public void run() throws Exception {
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
// (2)
Bootstrap b = new Bootstrap();
b.group(workerGroup).channel(// (3)
NioSocketChannel.class).handler(new // (4)
ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new KryoDecoderHandler());
ch.pipeline().addLast(new KryoEncoderHandler());
ch.pipeline().addLast(handler);
}
}).option(ChannelOption.SO_KEEPALIVE, // (6)
true);
// Bind and start to accept incoming connections.
// (7)
ChannelFuture f = b.connect("localhost", port).sync();
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to gracefully
// shut down your server.
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
}
}
use of io.netty.channel.ChannelInitializer in project chuidiang-ejemplos by chuidiang.
the class Client method run.
public void run() throws Exception {
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
// (2)
Bootstrap b = new Bootstrap();
b.group(workerGroup).channel(// (3)
NioSocketChannel.class).handler(new // (4)
ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new FrameExtractor());
ch.pipeline().addLast(new FrameMaker());
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(clientHandler);
}
}).option(ChannelOption.SO_KEEPALIVE, // (6)
true);
// Bind and start to accept incoming connections.
// (7)
ChannelFuture f = b.connect("localhost", port).sync();
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to gracefully
// shut down your server.
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
}
}
use of io.netty.channel.ChannelInitializer in project ambry by linkedin.
the class NettyServerTest method getNettyServer.
// helpers
// general
/**
* Gets an instance of {@link NettyServer}.
* @param properties the in-memory {@link Properties} to use.
* @return an instance of {@link NettyServer}.
* @throws InstantiationException
* @throws IOException
*/
private NettyServer getNettyServer(Properties properties) throws InstantiationException, IOException {
if (properties == null) {
// dud properties. should pick up defaults
properties = new Properties();
}
VerifiableProperties verifiableProperties = new VerifiableProperties(properties);
final NettyConfig nettyConfig = new NettyConfig(verifiableProperties);
final PerformanceConfig performanceConfig = new PerformanceConfig(verifiableProperties);
Map<Integer, ChannelInitializer<SocketChannel>> channelInitializers = new HashMap<>();
channelInitializers.put(nettyConfig.nettyServerPort, new FrontendNettyChannelInitializer(nettyConfig, performanceConfig, NETTY_METRICS, CONNECTION_STATS_HANDLER, REQUEST_HANDLER, PUBLIC_ACCESS_LOGGER, REST_SERVER_STATE, null, null));
channelInitializers.put(nettyConfig.nettyServerSSLPort, new FrontendNettyChannelInitializer(nettyConfig, performanceConfig, NETTY_METRICS, CONNECTION_STATS_HANDLER, REQUEST_HANDLER, PUBLIC_ACCESS_LOGGER, REST_SERVER_STATE, SSL_FACTORY, null));
return new NettyServer(nettyConfig, NETTY_METRICS, channelInitializers);
}
use of io.netty.channel.ChannelInitializer in project weicoder by wdcode.
the class WebSocketServer method handler.
@Override
protected ChannelHandler handler() {
return new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(final SocketChannel ch) throws Exception {
ch.pipeline().addLast(new HttpServerCodec());
ch.pipeline().addLast(new HttpObjectAggregator(1024 * 1024));
ch.pipeline().addLast(new WebSocketHandler("websocket"));
ch.config().setAllocator(PooledByteBufAllocator.DEFAULT);
}
};
}
use of io.netty.channel.ChannelInitializer in project java by wavefrontHQ.
the class ProxyUtil method createInitializer.
/**
* Create a {@link ChannelInitializer} with multiple dynamically created
* {@link ChannelHandler} objects.
*
* @param channelHandlerSuppliers Suppliers of ChannelHandlers.
* @param port port number.
* @param idleTimeout idle timeout in seconds.
* @param sslContext SSL context.
* @return channel initializer
*/
static ChannelInitializer<SocketChannel> createInitializer(Iterable<Supplier<ChannelHandler>> channelHandlerSuppliers, int port, int idleTimeout, @Nullable SslContext sslContext) {
String strPort = String.valueOf(port);
ChannelHandler idleStateEventHandler = new IdleStateEventHandler(Metrics.newCounter(new TaggedMetricName("listeners", "connections.idle.closed", "port", strPort)));
ChannelHandler connectionTracker = new ConnectionTrackingHandler(Metrics.newCounter(new TaggedMetricName("listeners", "connections.accepted", "port", strPort)), Metrics.newCounter(new TaggedMetricName("listeners", "connections.active", "port", strPort)));
if (sslContext != null) {
logger.info("TLS enabled on port: " + port);
}
return new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
if (sslContext != null) {
pipeline.addLast(sslContext.newHandler(ch.alloc()));
}
pipeline.addFirst("idlehandler", new IdleStateHandler(idleTimeout, 0, 0));
pipeline.addLast("idlestateeventhandler", idleStateEventHandler);
pipeline.addLast("connectiontracker", connectionTracker);
channelHandlerSuppliers.forEach(x -> pipeline.addLast(x.get()));
}
};
}
Aggregations