use of io.netty.channel.EventLoopGroup 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.EventLoopGroup in project cxf by apache.
the class NettyHttpConduitFactory method createConduit.
@Override
public HTTPConduit createConduit(HTTPTransportFactory f, Bus bus, EndpointInfo localInfo, EndpointReferenceType target) throws IOException {
// need to check if the EventLoopGroup is created or not
// if not create a new EventLoopGroup for it
EventLoopGroup eventLoopGroup = bus.getExtension(EventLoopGroup.class);
if (eventLoopGroup == null) {
final EventLoopGroup group = new NioEventLoopGroup();
// register a BusLifeCycleListener for it
bus.setExtension(group, EventLoopGroup.class);
registerBusLifeListener(bus, group);
}
return new NettyHttpConduit(bus, localInfo, target, this);
}
use of io.netty.channel.EventLoopGroup in project cxf by apache.
the class NettyHttpConduitFactoryTest method testShutdownEventLoopGroup.
@Test
public void testShutdownEventLoopGroup() throws Exception {
bus = BusFactory.getDefaultBus(true);
assertNotNull("Cannot get bus", bus);
// Make sure we got the Transport Factory.
NettyHttpTransportFactory factory = bus.getExtension(NettyHttpTransportFactory.class);
assertNotNull("Cannot get NettyHttpTransportFactory", factory);
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setName(new QName("bla", "Service"));
EndpointInfo ei = new EndpointInfo(serviceInfo, "");
ei.setName(new QName("bla", "Port"));
ei.setAddress("netty://foo");
// The EventLoopGroup is put into bus when create a new netty http conduit
factory.getConduit(ei, null, bus);
bus.shutdown(true);
EventLoopGroup eventLoopGroup = bus.getExtension(EventLoopGroup.class);
assertNotNull("We should find the EventLoopGroup here.", eventLoopGroup);
assertTrue("The eventLoopGroup should be shutdown.", eventLoopGroup.isShutdown());
}
use of io.netty.channel.EventLoopGroup in project pravega by pravega.
the class ConnectionFactoryImplTest method setUp.
@Before
public void setUp() throws Exception {
// Configure SSL.
port = TestUtils.getAvailableListenPort();
final SslContext sslCtx;
if (ssl) {
try {
sslCtx = SslContextBuilder.forServer(new File("../config/cert.pem"), new File("../config/key.pem")).build();
} catch (SSLException e) {
throw new RuntimeException(e);
}
} else {
sslCtx = null;
}
boolean nio = false;
EventLoopGroup bossGroup;
EventLoopGroup workerGroup;
try {
bossGroup = new EpollEventLoopGroup(1);
workerGroup = new EpollEventLoopGroup();
} catch (ExceptionInInitializerError | UnsatisfiedLinkError | NoClassDefFoundError e) {
nio = true;
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
}
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(nio ? NioServerSocketChannel.class : EpollServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
if (sslCtx != null) {
SslHandler handler = sslCtx.newHandler(ch.alloc());
SSLEngine sslEngine = handler.engine();
SSLParameters sslParameters = sslEngine.getSSLParameters();
sslParameters.setEndpointIdentificationAlgorithm("LDAPS");
sslEngine.setSSLParameters(sslParameters);
p.addLast(handler);
}
}
});
// Start the server.
serverChannel = b.bind("localhost", port).awaitUninterruptibly().channel();
}
use of io.netty.channel.EventLoopGroup in project web3sdk by FISCO-BCOS.
the class ChannelConnections method startListen.
public void startListen(Integer port) {
if (running) {
logger.debug("服务已启动");
return;
}
logger.debug("初始化connections listen");
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
final ChannelConnections selfService = this;
final ThreadPoolTaskExecutor selfThreadPool = threadPool;
try {
serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
KeyStore ks = KeyStore.getInstance("JKS");
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource keystoreResource = resolver.getResource(getClientKeystorePath());
Resource caResource = resolver.getResource(getCaCertPath());
ks.load(keystoreResource.getInputStream(), getKeystorePassWord().toCharArray());
/*
* 每次连接使用新的handler
* 连接信息从socketChannel中获取
*/
ChannelHandler handler = new ChannelHandler();
handler.setConnections(selfService);
handler.setIsServer(true);
handler.setThreadPool(selfThreadPool);
SslContext sslCtx = SslContextBuilder.forServer((PrivateKey) ks.getKey("client", getClientCertPassWord().toCharArray()), (X509Certificate) ks.getCertificate("client")).trustManager(caResource.getFile()).build();
ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()), new LengthFieldBasedFrameDecoder(1024 * 1024 * 4, 0, 4, -4, 0), new IdleStateHandler(idleTimeout, idleTimeout, idleTimeout, TimeUnit.MILLISECONDS), handler);
}
});
ChannelFuture future = serverBootstrap.bind(port);
future.get();
running = true;
} catch (Exception e) {
logger.error("系统错误", e);
}
}
Aggregations