Search in sources :

Example 31 with Channel

use of io.netty.channel.Channel in project neo4j by neo4j.

the class BoltProtocolV1Test method shouldNotTalkToChannelDirectlyOnFatalError.

@Test
public void shouldNotTalkToChannelDirectlyOnFatalError() throws Throwable {
    // Given
    Channel outputChannel = newChannelMock();
    BoltStateMachine machine = mock(BoltStateMachine.class);
    BoltProtocolV1 protocol = new BoltProtocolV1(new SynchronousBoltWorker(machine), outputChannel, NullLogService.getInstance());
    verify(outputChannel).alloc();
    // And given inbound data that'll explode when the protocol tries to interpret it
    ByteBuf bomb = mock(ByteBuf.class);
    doThrow(IOException.class).when(bomb).readableBytes();
    // When
    protocol.handle(mock(ChannelHandlerContext.class), bomb);
    // Then the protocol should not mess with the channel (because it runs on the IO thread, and only the worker thread should produce writes)
    verifyNoMoreInteractions(outputChannel);
    // But instead make sure the state machine is shut down
    verify(machine).close();
}
Also used : SynchronousBoltWorker(org.neo4j.bolt.v1.runtime.SynchronousBoltWorker) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 32 with Channel

use of io.netty.channel.Channel in project neo4j by neo4j.

the class BoltProtocolV1Test method newChannelMock.

private static Channel newChannelMock() {
    Channel channel = mock(Channel.class);
    ByteBufAllocator allocator = mock(ByteBufAllocator.class, RETURNS_MOCKS);
    when(channel.alloc()).thenReturn(allocator);
    return channel;
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Channel(io.netty.channel.Channel)

Example 33 with Channel

use of io.netty.channel.Channel in project cradle by BingLau7.

the class ChannelOperationExamples method writingToChannelManyThreads.

// Channel 是线程安全的,可以在多个线程中使用它,需要注意的是,消息将会被保证按顺序发送
public static void writingToChannelManyThreads() {
    // Get the channel reference from somewhere
    final Channel channel = null;
    // 创建持有要写数据的 ByteBuf
    final ByteBuf buf = Unpooled.copiedBuffer("your data", CharsetUtil.UTF_8).retain();
    // 创建将数据写到 Channel 的 Runnable
    Runnable writer = new Runnable() {

        @Override
        public void run() {
            channel.writeAndFlush(buf.duplicate());
        }
    };
    // 获取线程池引用
    Executor executor = Executors.newCachedThreadPool();
    // 递交写任务给线程池以便在某个线程中执行
    executor.execute(writer);
    // 递交另一个写任务以便在另一个线程中执行
    executor.execute(writer);
}
Also used : Executor(java.util.concurrent.Executor) Channel(io.netty.channel.Channel) ByteBuf(io.netty.buffer.ByteBuf)

Example 34 with Channel

use of io.netty.channel.Channel in project intellij-community by JetBrains.

the class BuildManager method startListening.

private int startListening() throws Exception {
    EventLoopGroup group;
    BuiltInServer mainServer = StartupUtil.getServer();
    boolean isOwnEventLoopGroup = !Registry.is("compiler.shared.event.group", true) || mainServer == null || mainServer.getEventLoopGroup() instanceof OioEventLoopGroup;
    if (isOwnEventLoopGroup) {
        group = new NioEventLoopGroup(1, ConcurrencyUtil.newNamedThreadFactory("External compiler"));
    } else {
        group = mainServer.getEventLoopGroup();
    }
    final ServerBootstrap bootstrap = serverBootstrap(group);
    bootstrap.childHandler(new ChannelInitializer() {

        @Override
        protected void initChannel(@NotNull Channel channel) throws Exception {
            channel.pipeline().addLast(myChannelRegistrar, new ProtobufVarint32FrameDecoder(), new ProtobufDecoder(CmdlineRemoteProto.Message.getDefaultInstance()), new ProtobufVarint32LengthFieldPrepender(), new ProtobufEncoder(), myMessageDispatcher);
        }
    });
    Channel serverChannel = bootstrap.bind(InetAddress.getLoopbackAddress(), 0).syncUninterruptibly().channel();
    myChannelRegistrar.setServerChannel(serverChannel, isOwnEventLoopGroup);
    return ((InetSocketAddress) serverChannel.localAddress()).getPort();
}
Also used : ProtobufEncoder(io.netty.handler.codec.protobuf.ProtobufEncoder) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) ProtobufDecoder(io.netty.handler.codec.protobuf.ProtobufDecoder) ProtobufVarint32LengthFieldPrepender(io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender) BuiltInServer(org.jetbrains.io.BuiltInServer) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) IOException(java.io.IOException) ExecutionException(com.intellij.execution.ExecutionException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) ChannelInitializer(io.netty.channel.ChannelInitializer) ProtobufVarint32FrameDecoder(io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 35 with Channel

use of io.netty.channel.Channel in project intellij-community by JetBrains.

the class BuildMessageDispatcher method sendBuildParameters.

public boolean sendBuildParameters(@NotNull final UUID preloadedSessionId, @NotNull CmdlineRemoteProto.Message.ControllerMessage params) {
    boolean succeeded = false;
    final SessionData sessionData = mySessionDescriptors.get(preloadedSessionId);
    if (sessionData != null) {
        //noinspection SynchronizationOnLocalVariableOrMethodParameter
        synchronized (sessionData) {
            if (sessionData.state == SessionData.State.WAITING_PARAMS) {
                sessionData.state = SessionData.State.RUNNING;
                final Channel channel = sessionData.channel;
                if (channel != null && channel.isActive()) {
                    sessionData.handler.buildStarted(preloadedSessionId);
                    channel.writeAndFlush(CmdlineProtoUtil.toMessage(preloadedSessionId, params));
                    succeeded = true;
                }
            } else {
                if (sessionData.state == SessionData.State.INITIAL) {
                    sessionData.params = params;
                    succeeded = true;
                }
            }
        }
    }
    return succeeded;
}
Also used : Channel(io.netty.channel.Channel)

Aggregations

Channel (io.netty.channel.Channel)886 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)226 ChannelFuture (io.netty.channel.ChannelFuture)204 Test (org.junit.Test)203 Bootstrap (io.netty.bootstrap.Bootstrap)199 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)191 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)177 InetSocketAddress (java.net.InetSocketAddress)165 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)151 EventLoopGroup (io.netty.channel.EventLoopGroup)142 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)138 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)132 IOException (java.io.IOException)126 ByteBuf (io.netty.buffer.ByteBuf)112 SocketChannel (io.netty.channel.socket.SocketChannel)106 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)99 ChannelPipeline (io.netty.channel.ChannelPipeline)98 CountDownLatch (java.util.concurrent.CountDownLatch)96 LocalChannel (io.netty.channel.local.LocalChannel)93 LocalServerChannel (io.netty.channel.local.LocalServerChannel)89