use of org.apache.flink.shaded.netty4.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(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 org.apache.flink.shaded.netty4.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 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 org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project ambry by linkedin.
the class MultiplexedChannelRecordTest method setup.
@Before
public void setup() throws Exception {
loopGroup = new NioEventLoopGroup(4);
channel = new MockChannel();
idleTimeoutMillis = 500L;
streamChannelInitializer = new ChannelInitializer() {
@Override
protected void initChannel(Channel ch) throws Exception {
// do nothing
}
};
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project ambry by linkedin.
the class FrontendNettyFactoryTest method doGetNettyServerTest.
/**
* Test that a {@link NettyServer} can be constructed by the factory.
* @param properties the {@link Properties} to use.
* @param defaultSslFactory the default {@link SSLFactory} to pass into the constructor.
*/
private void doGetNettyServerTest(Properties properties, SSLFactory defaultSslFactory) throws Exception {
VerifiableProperties verifiableProperties = new VerifiableProperties(properties);
NettyConfig nettyConfig = new NettyConfig(verifiableProperties);
FrontendNettyFactory nettyServerFactory = new FrontendNettyFactory(verifiableProperties, new MetricRegistry(), REST_REQUEST_HANDLER, PUBLIC_ACCESS_LOGGER, REST_SERVER_STATE, defaultSslFactory, null);
NioServer nioServer = nettyServerFactory.getNioServer();
assertNotNull("No NioServer returned", nioServer);
assertEquals("Did not receive a NettyServer instance", NettyServer.class.getCanonicalName(), nioServer.getClass().getCanonicalName());
Map<Integer, ChannelInitializer<SocketChannel>> channelInitializers = nettyServerFactory.channelInitializers;
if (nettyConfig.nettyServerEnableSSL && defaultSslFactory != null) {
assertEquals("Expected two ChannelInitializers when SSLFactory is not null", 2, channelInitializers.size());
assertNotNull("No ChannelInitializer for SSL port", channelInitializers.get(nettyConfig.nettyServerSSLPort));
} else {
assertEquals("Expected one ChannelInitializer when SSLFactory is null", 1, channelInitializers.size());
}
assertNotNull("No ChannelInitializer for plaintext port", channelInitializers.get(nettyConfig.nettyServerPort));
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project ambry by linkedin.
the class NettyServer method start.
@Override
public void start() throws InstantiationException {
long startupBeginTime = System.currentTimeMillis();
try {
logger.trace("Starting NettyServer deployment");
if (Epoll.isAvailable()) {
logger.trace("Using EpollEventLoopGroup in NettyServer.");
bossGroup = new EpollEventLoopGroup(nettyConfig.nettyServerBossThreadCount);
workerGroup = new EpollEventLoopGroup(nettyConfig.nettyServerWorkerThreadCount);
} else {
bossGroup = new NioEventLoopGroup(nettyConfig.nettyServerBossThreadCount);
workerGroup = new NioEventLoopGroup(nettyConfig.nettyServerWorkerThreadCount);
}
for (Map.Entry<Integer, ChannelInitializer<SocketChannel>> entry : channelInitializers.entrySet()) {
bindServer(entry.getKey(), entry.getValue(), bossGroup, workerGroup);
}
nettyMetrics.registerNettyPendingTasksGauge(bossGroup, "Boss");
nettyMetrics.registerNettyPendingTasksGauge(workerGroup, "Worker");
} catch (InterruptedException e) {
logger.error("NettyServer start await was interrupted", e);
nettyMetrics.nettyServerStartError.inc();
throw new InstantiationException("Netty server bind to port [" + nettyConfig.nettyServerPort + "] was interrupted");
} finally {
long startupTime = System.currentTimeMillis() - startupBeginTime;
logger.info("NettyServer start took {} ms", startupTime);
nettyMetrics.nettyServerStartTimeInMs.update(startupTime);
}
}
Aggregations