Search in sources :

Example 86 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project swift by luastar.

the class HttpServer method start.

public void start() {
    // 设置线程名称
    ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
    EventLoopGroup bossGroup = new NioEventLoopGroup(HttpConstant.SWIFT_BOSS_THREADS, threadFactoryBuilder.setNameFormat("boss-group-%d").build());
    EventLoopGroup workerGroup = new NioEventLoopGroup(HttpConstant.SWIFT_WORKER_THREADS, threadFactoryBuilder.setNameFormat("worker-group-%d").build());
    // EventExecutorGroup executorGroup = new DefaultEventExecutorGroup(HttpConstant.SWIFT_BUSINESS_THREADS, threadFactoryBuilder.setNameFormat("executor-group-%d").build());
    try {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                if (ssl) {
                    SelfSignedCertificate ssc = new SelfSignedCertificate();
                    SslContext sslContext = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
                    pipeline.addLast(sslContext.newHandler(ch.alloc()));
                }
                // http request decode and response encode
                pipeline.addLast(new HttpServerCodec());
                // 将消息头和体聚合成FullHttpRequest和FullHttpResponse
                pipeline.addLast(new HttpObjectAggregator(HttpConstant.SWIFT_MAX_CONTENT_LENGTH));
                // 压缩处理
                pipeline.addLast(new HttpContentCompressor(HttpConstant.SWIFT_COMPRESSION_LEVEL));
                // 自定义http服务
                // pipeline.addLast(executorGroup, "http-handler", new HttpChannelHandler(handlerMapping));
                pipeline.addLast(new HttpChannelHandler(handlerMapping));
            }
        });
        Channel channel = bootstrap.bind(port).sync().channel();
        channel.closeFuture().sync();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
        // executorGroup.shutdownGracefully();
        HttpThreadPoolExecutor.shutdownGracefully();
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) SocketChannel(io.netty.channel.socket.SocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 87 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project async-http-client by AsyncHttpClient.

the class EventPipelineTest method asyncPipelineTest.

@Test
public void asyncPipelineTest() throws Exception {
    Consumer<Channel> httpAdditionalPipelineInitializer = channel -> channel.pipeline().addBefore("inflater", "copyEncodingHeader", new CopyEncodingHandler());
    try (AsyncHttpClient p = asyncHttpClient(config().setHttpAdditionalChannelInitializer(httpAdditionalPipelineInitializer))) {
        final CountDownLatch l = new CountDownLatch(1);
        p.executeRequest(get(getTargetUrl()), new AsyncCompletionHandlerAdapter() {

            @Override
            public Response onCompleted(Response response) {
                try {
                    assertEquals(response.getStatusCode(), 200);
                    assertEquals(response.getHeader("X-Original-Content-Encoding"), "<original encoding>");
                } finally {
                    l.countDown();
                }
                return response;
            }
        }).get();
        if (!l.await(TIMEOUT, TimeUnit.SECONDS)) {
            fail("Timeout out");
        }
    }
}
Also used : AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) HttpMessage(io.netty.handler.codec.http.HttpMessage) Assert.fail(org.testng.Assert.fail) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) Response(org.asynchttpclient.Response) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) Dsl(org.asynchttpclient.Dsl) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest) Response(org.asynchttpclient.Response) Channel(io.netty.channel.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test) AbstractBasicTest(org.asynchttpclient.AbstractBasicTest)

Example 88 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class ClientManageProcessorTest method processRequest_UnRegisterProducer.

@Test
public void processRequest_UnRegisterProducer() throws Exception {
    brokerController.getProducerManager().registerProducer(group, clientChannelInfo);
    HashMap<Channel, ClientChannelInfo> channelMap = brokerController.getProducerManager().getGroupChannelTable().get(group);
    assertThat(channelMap).isNotNull();
    assertThat(channelMap.get(channel)).isEqualTo(clientChannelInfo);
    RemotingCommand request = createUnRegisterProducerCommand();
    RemotingCommand response = clientManageProcessor.processRequest(handlerContext, request);
    assertThat(response).isNotNull();
    assertThat(response.getCode()).isEqualTo(ResponseCode.SUCCESS);
    channelMap = brokerController.getProducerManager().getGroupChannelTable().get(group);
    assertThat(channelMap).isNull();
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) ClientChannelInfo(org.apache.rocketmq.broker.client.ClientChannelInfo) Channel(io.netty.channel.Channel) Test(org.junit.Test)

Example 89 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class SendMessageProcessorTest method init.

@Before
public void init() {
    brokerController.setMessageStore(messageStore);
    when(messageStore.now()).thenReturn(System.currentTimeMillis());
    Channel mockChannel = mock(Channel.class);
    when(mockChannel.remoteAddress()).thenReturn(new InetSocketAddress(1024));
    when(handlerContext.channel()).thenReturn(mockChannel);
    when(messageStore.lookMessageByOffset(anyLong())).thenReturn(new MessageExt());
    sendMessageProcessor = new SendMessageProcessor(brokerController);
}
Also used : MessageExt(org.apache.rocketmq.common.message.MessageExt) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) Before(org.junit.Before)

Example 90 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class PullMessageProcessorTest method init.

@Before
public void init() {
    brokerController.setMessageStore(messageStore);
    pullMessageProcessor = new PullMessageProcessor(brokerController);
    Channel mockChannel = mock(Channel.class);
    when(mockChannel.remoteAddress()).thenReturn(new InetSocketAddress(1024));
    when(handlerContext.channel()).thenReturn(mockChannel);
    brokerController.getTopicConfigManager().getTopicConfigTable().put(topic, new TopicConfig());
    clientChannelInfo = new ClientChannelInfo(mockChannel);
    ConsumerData consumerData = createConsumerData(group, topic);
    brokerController.getConsumerManager().registerConsumer(consumerData.getGroupName(), clientChannelInfo, consumerData.getConsumeType(), consumerData.getMessageModel(), consumerData.getConsumeFromWhere(), consumerData.getSubscriptionDataSet(), false);
}
Also used : ClientChannelInfo(org.apache.rocketmq.broker.client.ClientChannelInfo) ConsumerData(org.apache.rocketmq.common.protocol.heartbeat.ConsumerData) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) TopicConfig(org.apache.rocketmq.common.TopicConfig) Before(org.junit.Before)

Aggregations

Channel (io.netty.channel.Channel)889 Test (org.junit.Test)242 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)227 ChannelFuture (io.netty.channel.ChannelFuture)204 Bootstrap (io.netty.bootstrap.Bootstrap)201 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)193 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)176 InetSocketAddress (java.net.InetSocketAddress)169 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)152 IOException (java.io.IOException)143 EventLoopGroup (io.netty.channel.EventLoopGroup)142 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)138 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)131 ByteBuf (io.netty.buffer.ByteBuf)112 SocketChannel (io.netty.channel.socket.SocketChannel)105 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)99 ChannelPipeline (io.netty.channel.ChannelPipeline)98 CountDownLatch (java.util.concurrent.CountDownLatch)97 LocalChannel (io.netty.channel.local.LocalChannel)93 LocalServerChannel (io.netty.channel.local.LocalServerChannel)88