Search in sources :

Example 76 with EventLoopGroup

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();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 77 with EventLoopGroup

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);
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 78 with EventLoopGroup

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());
}
Also used : ServiceInfo(org.apache.cxf.service.model.ServiceInfo) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) EventLoopGroup(io.netty.channel.EventLoopGroup) QName(javax.xml.namespace.QName) Test(org.junit.Test)

Example 79 with EventLoopGroup

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();
}
Also used : EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) SSLEngine(javax.net.ssl.SSLEngine) SSLException(javax.net.ssl.SSLException) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) SSLException(javax.net.ssl.SSLException) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SSLParameters(javax.net.ssl.SSLParameters) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) File(java.io.File) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext) Before(org.junit.Before)

Example 80 with EventLoopGroup

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);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) PrivateKey(java.security.PrivateKey) Resource(org.springframework.core.io.Resource) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Aggregations

EventLoopGroup (io.netty.channel.EventLoopGroup)147 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)103 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)67 Channel (io.netty.channel.Channel)67 Bootstrap (io.netty.bootstrap.Bootstrap)66 ChannelFuture (io.netty.channel.ChannelFuture)42 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)38 SslContext (io.netty.handler.ssl.SslContext)38 Test (org.junit.Test)37 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)32 SocketChannel (io.netty.channel.socket.SocketChannel)29 LoggingHandler (io.netty.handler.logging.LoggingHandler)28 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)27 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)25 ChannelPipeline (io.netty.channel.ChannelPipeline)23 LocalAddress (io.netty.channel.local.LocalAddress)18 LocalChannel (io.netty.channel.local.LocalChannel)18 LocalServerChannel (io.netty.channel.local.LocalServerChannel)18 InetSocketAddress (java.net.InetSocketAddress)18 ByteBuf (io.netty.buffer.ByteBuf)17