Search in sources :

Example 96 with Bootstrap

use of io.netty.bootstrap.Bootstrap in project neo4j by neo4j.

the class SenderService method start.

@Override
public synchronized void start() {
    serviceLock.writeLock().lock();
    try {
        eventLoopGroup = new NioEventLoopGroup(0, new NamedThreadFactory("sender-service"));
        bootstrap = new Bootstrap().group(eventLoopGroup).channel(NioSocketChannel.class).handler(channelInitializer);
        senderServiceRunning = true;
    } finally {
        serviceLock.writeLock().unlock();
    }
}
Also used : NamedThreadFactory(org.neo4j.helpers.NamedThreadFactory) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 97 with Bootstrap

use of io.netty.bootstrap.Bootstrap in project netty by netty.

the class ReentrantChannelTest method testWriteFlushPingPong.

@Test
public void testWriteFlushPingPong() throws Exception {
    LocalAddress addr = new LocalAddress("testWriteFlushPingPong");
    ServerBootstrap sb = getLocalServerBootstrap();
    sb.bind(addr).sync().channel();
    Bootstrap cb = getLocalClientBootstrap();
    setInterest(Event.WRITE, Event.FLUSH, Event.CLOSE, Event.EXCEPTION);
    Channel clientChannel = cb.connect(addr).sync().channel();
    clientChannel.pipeline().addLast(new ChannelOutboundHandlerAdapter() {

        int writeCount;

        int flushCount;

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            if (writeCount < 5) {
                writeCount++;
                ctx.channel().flush();
            }
            super.write(ctx, msg, promise);
        }

        @Override
        public void flush(ChannelHandlerContext ctx) throws Exception {
            if (flushCount < 5) {
                flushCount++;
                ctx.channel().write(createTestBuf(2000));
            }
            super.flush(ctx);
        }
    });
    clientChannel.writeAndFlush(createTestBuf(2000));
    clientChannel.close().sync();
    assertLog("WRITE\n" + "FLUSH\n" + "WRITE\n" + "FLUSH\n" + "WRITE\n" + "FLUSH\n" + "WRITE\n" + "FLUSH\n" + "WRITE\n" + "FLUSH\n" + "WRITE\n" + "FLUSH\n" + "CLOSE\n");
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ClosedChannelException(java.nio.channels.ClosedChannelException) Test(org.junit.Test)

Example 98 with Bootstrap

use of io.netty.bootstrap.Bootstrap in project netty by netty.

the class ReentrantChannelTest method testFlushFailure.

@Test
public void testFlushFailure() throws Exception {
    LocalAddress addr = new LocalAddress("testFlushFailure");
    ServerBootstrap sb = getLocalServerBootstrap();
    sb.bind(addr).sync().channel();
    Bootstrap cb = getLocalClientBootstrap();
    setInterest(Event.WRITE, Event.FLUSH, Event.CLOSE, Event.EXCEPTION);
    Channel clientChannel = cb.connect(addr).sync().channel();
    clientChannel.pipeline().addLast(new ChannelOutboundHandlerAdapter() {

        @Override
        public void flush(ChannelHandlerContext ctx) throws Exception {
            throw new Exception("intentional failure");
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            ctx.close();
        }
    });
    try {
        clientChannel.writeAndFlush(createTestBuf(2000)).sync();
        fail();
    } catch (Throwable cce) {
        // FIXME:  shouldn't this contain the "intentional failure" exception?
        assertEquals(ClosedChannelException.class, cce.getClass());
    }
    clientChannel.closeFuture().sync();
    assertLog("WRITE\nCLOSE\n");
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) LocalAddress(io.netty.channel.local.LocalAddress) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ClosedChannelException(java.nio.channels.ClosedChannelException) Test(org.junit.Test)

Example 99 with Bootstrap

use of io.netty.bootstrap.Bootstrap in project netty by netty.

the class ReentrantChannelTest method testFlushInWritabilityChanged.

/**
     * Similar to {@link #testWritabilityChanged()} with slight variation.
     */
@Test
public void testFlushInWritabilityChanged() throws Exception {
    LocalAddress addr = new LocalAddress("testFlushInWritabilityChanged");
    ServerBootstrap sb = getLocalServerBootstrap();
    sb.bind(addr).sync().channel();
    Bootstrap cb = getLocalClientBootstrap();
    setInterest(Event.WRITE, Event.FLUSH, Event.WRITABILITY);
    Channel clientChannel = cb.connect(addr).sync().channel();
    clientChannel.config().setWriteBufferLowWaterMark(512);
    clientChannel.config().setWriteBufferHighWaterMark(1024);
    clientChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
            if (!ctx.channel().isWritable()) {
                ctx.channel().flush();
            }
            ctx.fireChannelWritabilityChanged();
        }
    });
    assertTrue(clientChannel.isWritable());
    clientChannel.write(createTestBuf(2000)).sync();
    clientChannel.close().sync();
    assertLog(// Case 1:
    "WRITABILITY: writable=false\n" + "FLUSH\n" + "WRITE\n" + "WRITABILITY: writable=false\n" + "WRITABILITY: writable=false\n" + "FLUSH\n" + "WRITABILITY: writable=true\n", // Case 2:
    "WRITABILITY: writable=false\n" + "FLUSH\n" + "WRITE\n" + "WRITABILITY: writable=false\n" + "FLUSH\n" + "WRITABILITY: writable=true\n" + "WRITABILITY: writable=true\n");
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ClosedChannelException(java.nio.channels.ClosedChannelException) Test(org.junit.Test)

Example 100 with Bootstrap

use of io.netty.bootstrap.Bootstrap in project netty by netty.

the class SimpleChannelPool method acquireHealthyFromPoolOrNew.

/**
     * Tries to retrieve healthy channel from the pool if any or creates a new channel otherwise.
     * @param promise the promise to provide acquire result.
     * @return future for acquiring a channel.
     */
private Future<Channel> acquireHealthyFromPoolOrNew(final Promise<Channel> promise) {
    try {
        final Channel ch = pollChannel();
        if (ch == null) {
            // No Channel left in the pool bootstrap a new Channel
            Bootstrap bs = bootstrap.clone();
            bs.attr(POOL_KEY, this);
            ChannelFuture f = connectChannel(bs);
            if (f.isDone()) {
                notifyConnect(f, promise);
            } else {
                f.addListener(new ChannelFutureListener() {

                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        notifyConnect(future, promise);
                    }
                });
            }
            return promise;
        }
        EventLoop loop = ch.eventLoop();
        if (loop.inEventLoop()) {
            doHealthCheck(ch, promise);
        } else {
            loop.execute(new Runnable() {

                @Override
                public void run() {
                    doHealthCheck(ch, promise);
                }
            });
        }
    } catch (Throwable cause) {
        promise.tryFailure(cause);
    }
    return promise;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EventLoop(io.netty.channel.EventLoop) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)163 Channel (io.netty.channel.Channel)81 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)73 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)68 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)68 ChannelFuture (io.netty.channel.ChannelFuture)62 EventLoopGroup (io.netty.channel.EventLoopGroup)61 Test (org.junit.Test)61 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)49 InetSocketAddress (java.net.InetSocketAddress)49 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)37 SocketChannel (io.netty.channel.socket.SocketChannel)33 ChannelPipeline (io.netty.channel.ChannelPipeline)31 LocalAddress (io.netty.channel.local.LocalAddress)26 ClosedChannelException (java.nio.channels.ClosedChannelException)26 LocalChannel (io.netty.channel.local.LocalChannel)22 LocalServerChannel (io.netty.channel.local.LocalServerChannel)22 CountDownLatch (java.util.concurrent.CountDownLatch)22 ChannelFutureListener (io.netty.channel.ChannelFutureListener)20 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)18