use of io.netty.channel.ChannelInitializer in project chuidiang-ejemplos by chuidiang.
the class MulticastServer method start.
public void start() throws InterruptedException, SocketException, UnknownHostException {
NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getByName("127.0.0.1"));
InetSocketAddress groupAddress = new InetSocketAddress("239.255.27.1", 1234);
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(group).channelFactory(new ChannelFactory<NioDatagramChannel>() {
@Override
public NioDatagramChannel newChannel() {
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
}
}).handler(new ChannelInitializer<NioDatagramChannel>() {
@Override
protected void initChannel(NioDatagramChannel ch) throws Exception {
ch.pipeline().addLast(new ServerHandler());
}
}).option(ChannelOption.SO_REUSEADDR, true);
NioDatagramChannel ch = (NioDatagramChannel) b.bind(groupAddress.getPort()).sync().channel();
ch.joinGroup(groupAddress, ni).sync();
ch.closeFuture().await();
}
use of io.netty.channel.ChannelInitializer in project chuidiang-ejemplos by chuidiang.
the class MulticastClient method start.
public void start() throws InterruptedException, SocketException, UnknownHostException {
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(group).channelFactory(new ChannelFactory<NioDatagramChannel>() {
@Override
public NioDatagramChannel newChannel() {
return new NioDatagramChannel(InternetProtocolFamily.IPv4);
}
}).handler(new ChannelInitializer<NioDatagramChannel>() {
@Override
protected void initChannel(NioDatagramChannel ch) throws Exception {
ch.pipeline().addLast(myHandler);
}
}).option(ChannelOption.SO_REUSEADDR, true);
NioDatagramChannel ch = (NioDatagramChannel) b.bind(1234).sync().channel();
new Thread() {
public void run() {
while (true) {
try {
Thread.sleep(1000);
myHandler.sendMessage("Hola desde netty");
System.out.println("Envio Hola");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
ch.closeFuture().await();
}
use of 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 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 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