Search in sources :

Example 66 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project tutorials-java by Artister.

the class HelloWorldClient method start.

public void start() {
    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(group).channel(NioSocketChannel.class).handler(new ClientChannelInitializer());
    try {
        ChannelFuture future = bootstrap.connect(address, port).sync();
        future.channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ClientChannelInitializer(org.ko.netty.t3.initializer.ClientChannelInitializer) ChannelFuture(io.netty.channel.ChannelFuture) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 67 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project autobahn-java by crossbario.

the class NettyWebSocket method connect.

@Override
public void connect(ITransportHandler transportHandler, TransportOptions options) throws Exception {
    if (options == null) {
        if (mOptions == null) {
            options = new TransportOptions();
        } else {
            options = new TransportOptions();
            options.setAutoPingInterval(mOptions.getAutoPingInterval());
            options.setAutoPingTimeout(mOptions.getAutoPingTimeout());
            options.setMaxFramePayloadSize(mOptions.getMaxFramePayloadSize());
        }
    }
    URI uri;
    uri = new URI(mUri);
    int port = validateURIAndGetPort(uri);
    String scheme = uri.getScheme();
    String host = uri.getHost();
    final SslContext sslContext = getSSLContext(scheme);
    WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, mSerializers, true, new DefaultHttpHeaders(), options.getMaxFramePayloadSize());
    mHandler = new NettyWebSocketClientHandler(handshaker, this, transportHandler);
    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(group);
    bootstrap.channel(NioSocketChannel.class);
    TransportOptions opt = options;
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline channelPipeline = ch.pipeline();
            if (sslContext != null) {
                channelPipeline.addLast(sslContext.newHandler(ch.alloc(), host, port));
            }
            channelPipeline.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), WebSocketClientCompressionHandler.INSTANCE, new IdleStateHandler(opt.getAutoPingInterval() + opt.getAutoPingTimeout(), opt.getAutoPingInterval(), 0, TimeUnit.SECONDS), mHandler);
        }
    });
    mChannel = bootstrap.connect(uri.getHost(), port).sync().channel();
    mHandler.getHandshakeFuture().sync();
}
Also used : WebSocketClientHandshaker(io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) TransportOptions(io.crossbar.autobahn.wamp.types.TransportOptions) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) URI(java.net.URI) SSLException(javax.net.ssl.SSLException) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 68 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project LogHub by fbacchella.

the class ClientFactory method getBootStrap.

@Override
public AbstractBootstrap<Bootstrap, Channel> getBootStrap() {
    bootstrap = new Bootstrap();
    bootstrap.channelFactory(getInstance());
    return bootstrap;
}
Also used : Bootstrap(io.netty.bootstrap.Bootstrap) AbstractBootstrap(io.netty.bootstrap.AbstractBootstrap)

Example 69 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project LogHub by fbacchella.

the class TestServer method testSimple.

@Test(timeout = 2000)
public void testSimple() throws InterruptedException {
    Properties empty = new Properties(Collections.emptyMap());
    BlockingQueue<Event> receiver = new ArrayBlockingQueue<>(1);
    TesterReceiver r = new TesterReceiver(receiver, new Pipeline(Collections.emptyList(), "testone", null));
    r.configure(empty);
    final ChannelFuture[] sent = new ChannelFuture[1];
    EventLoopGroup workerGroup = new DefaultEventLoopGroup();
    Bootstrap b = new Bootstrap();
    b.group(workerGroup);
    b.channel(LocalChannel.class);
    b.handler(new SimpleChannelInboundHandler<ByteBuf>() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            sent[0] = ctx.writeAndFlush(Unpooled.copiedBuffer("Message\r\n", CharsetUtil.UTF_8));
        }

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        }
    });
    // Start the client.
    ChannelFuture f = b.connect(new LocalAddress(TestServer.class.getCanonicalName())).sync();
    Thread.sleep(100);
    sent[0].sync();
    f.channel().close();
    // Wait until the connection is closed.
    f.channel().closeFuture().sync();
    Event e = receiver.poll();
    Assert.assertEquals("Message", e.get("message"));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) LocalAddress(io.netty.channel.local.LocalAddress) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Properties(loghub.configuration.Properties) ByteBuf(io.netty.buffer.ByteBuf) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) IOException(java.io.IOException) Pipeline(loghub.Pipeline) ChannelPipeline(io.netty.channel.ChannelPipeline) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Event(loghub.Event) AbstractBootstrap(io.netty.bootstrap.AbstractBootstrap) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Test(org.junit.Test)

Example 70 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project jim-framework by jiangmin168168.

the class RpcClientInvokerManager method connect.

public void connect() {
    InetSocketAddress remotePeer = new InetSocketAddress(this.referenceConfig.getHost(), this.referenceConfig.getPort());
    Bootstrap b = new Bootstrap();
    b.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new RpcClientInitializer()).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) this.referenceConfig.getConnectTimeoutMillis());
    ChannelFuture channelFuture = b.connect(remotePeer);
    channelFuture.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(final ChannelFuture channelFuture) throws Exception {
            if (channelFuture.isSuccess()) {
                RpcClientInvoker handler = channelFuture.channel().pipeline().get(RpcClientInvoker.class);
                addHandler(handler);
            }
        }
    });
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ChannelFuture(io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelFutureListener(io.netty.channel.ChannelFutureListener) RpcException(com.jim.framework.rpc.exception.RpcException)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)429 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)207 Channel (io.netty.channel.Channel)194 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)189 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)169 ChannelFuture (io.netty.channel.ChannelFuture)152 EventLoopGroup (io.netty.channel.EventLoopGroup)139 SocketChannel (io.netty.channel.socket.SocketChannel)123 InetSocketAddress (java.net.InetSocketAddress)119 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)113 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)108 Test (org.junit.jupiter.api.Test)89 ChannelPipeline (io.netty.channel.ChannelPipeline)76 LocalChannel (io.netty.channel.local.LocalChannel)74 LocalServerChannel (io.netty.channel.local.LocalServerChannel)71 LocalAddress (io.netty.channel.local.LocalAddress)66 CountDownLatch (java.util.concurrent.CountDownLatch)62 IOException (java.io.IOException)60 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)56 ChannelInitializer (io.netty.channel.ChannelInitializer)45