当前位置: 首页>>代码示例>>Java>>正文


Java HandlerSubscriber类代码示例

本文整理汇总了Java中com.typesafe.netty.HandlerSubscriber的典型用法代码示例。如果您正苦于以下问题:Java HandlerSubscriber类的具体用法?Java HandlerSubscriber怎么用?Java HandlerSubscriber使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


HandlerSubscriber类属于com.typesafe.netty包,在下文中一共展示了HandlerSubscriber类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: subscribe

import com.typesafe.netty.HandlerSubscriber; //导入依赖的package包/类
@Override
public void subscribe(Subscriber<? super HttpResponse> s) {
    System.out.println("subscribe: " + s);
    Subscription subscriberSubscription = new Subscription() {

        @Override
        public void cancel() {
            System.out.println("cancel: " + this);
        }

        @Override
        public void request(long n) {
            System.out.println("request: " + this);
        }

    };

    this.subscriber = (HandlerSubscriber<? super HttpResponse>) s;
    this.subscriber.onSubscribe(subscriberSubscription);
}
 
开发者ID:restlet,项目名称:restlet-framework,代码行数:21,代码来源:HttpServerHelper.java

示例2: start

import com.typesafe.netty.HandlerSubscriber; //导入依赖的package包/类
@Override
public void start() throws Exception {
    super.start();
    setBossGroup(new NioEventLoopGroup());
    setWorkerGroup(new NioEventLoopGroup());
    setServerBootstrap(new ServerBootstrap());
    getServerBootstrap().option(ChannelOption.SO_BACKLOG, 1024);
    getServerBootstrap().group(getBossGroup(), getWorkerGroup()).channel(NioServerSocketChannel.class)
            .childOption(ChannelOption.AUTO_READ, false).localAddress(getHelped().getPort())
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();

                    pipeline.addLast(new HttpRequestDecoder(), new HttpResponseEncoder())
                            .addLast("serverStreamsHandler", new HttpStreamsServerHandler());

                    HandlerSubscriber<HttpResponse> subscriber = new HandlerSubscriber<>(ch.eventLoop(), 2, 4);
                    HandlerPublisher<HttpRequest> publisher = new HandlerPublisher<>(ch.eventLoop(),
                            HttpRequest.class);

                    pipeline.addLast("serverSubscriber", subscriber);
                    pipeline.addLast("serverPublisher", publisher);

                    publisher.subscribe(NettyServerHelper.this);
                    NettyServerHelper.this.subscribe(subscriber);
                }
            });

    setServerChannel(getServerBootstrap().bind().sync().channel());
    setEphemeralPort(((InetSocketAddress) getServerChannel().localAddress()).getPort());
    getLogger().info("Starting the Netty " + getProtocols() + " server on port " + getHelped().getPort());
}
 
开发者ID:restlet,项目名称:restlet-framework,代码行数:34,代码来源:NettyServerHelper.java

示例3: handleWebSocketResponse

import com.typesafe.netty.HandlerSubscriber; //导入依赖的package包/类
private void handleWebSocketResponse(ChannelHandlerContext ctx, Outgoing out) {
    WebSocketHttpResponse response = (WebSocketHttpResponse) out.message;
    WebSocketServerHandshaker handshaker = response.handshakerFactory().newHandshaker(lastRequest);

    if (handshaker == null) {
        HttpResponse res = new DefaultFullHttpResponse(
                HttpVersion.HTTP_1_1,
                HttpResponseStatus.UPGRADE_REQUIRED);
        res.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue());
        HttpUtil.setContentLength(res, 0);
        super.unbufferedWrite(ctx, new Outgoing(res, out.promise));
        response.subscribe(new CancelledSubscriber<>());
    } else {
        // First, insert new handlers in the chain after us for handling the websocket
        ChannelPipeline pipeline = ctx.pipeline();
        HandlerPublisher<WebSocketFrame> publisher = new HandlerPublisher<>(ctx.executor(), WebSocketFrame.class);
        HandlerSubscriber<WebSocketFrame> subscriber = new HandlerSubscriber<>(ctx.executor());
        pipeline.addAfter(ctx.executor(), ctx.name(), "websocket-subscriber", subscriber);
        pipeline.addAfter(ctx.executor(), ctx.name(), "websocket-publisher", publisher);

        // Now remove ourselves from the chain
        ctx.pipeline().remove(ctx.name());

        // Now do the handshake
        // Wrap the request in an empty request because we don't need the WebSocket handshaker ignoring the body,
        // we already have handled the body.
        handshaker.handshake(ctx.channel(), new EmptyHttpRequest(lastRequest));

        // And hook up the subscriber/publishers
        response.subscribe(subscriber);
        publisher.subscribe(response);
    }

}
 
开发者ID:playframework,项目名称:netty-reactive-streams,代码行数:35,代码来源:HttpStreamsServerHandler.java

示例4: bind

import com.typesafe.netty.HandlerSubscriber; //导入依赖的package包/类
public ChannelFuture bind(SocketAddress address, final Callable<Processor<HttpRequest, HttpResponse>> handler) {
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(eventLoop)
            .channel(NioServerSocketChannel.class)
            .childOption(ChannelOption.AUTO_READ, false)
            .localAddress(address)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();

                    pipeline.addLast(
                            new HttpRequestDecoder(),
                            new HttpResponseEncoder()
                    ).addLast("serverStreamsHandler", new HttpStreamsServerHandler());

                    HandlerSubscriber<HttpResponse> subscriber = new HandlerSubscriber<>(ch.eventLoop(), 2, 4);
                    HandlerPublisher<HttpRequest> publisher = new HandlerPublisher<>(ch.eventLoop(), HttpRequest.class);

                    pipeline.addLast("serverSubscriber", subscriber);
                    pipeline.addLast("serverPublisher", publisher);

                    Processor<HttpRequest, HttpResponse> processor = handler.call();
                    processor.subscribe(subscriber);
                    publisher.subscribe(processor);
                }
            });

    return bootstrap.bind();
}
 
开发者ID:playframework,项目名称:netty-reactive-streams,代码行数:31,代码来源:ProcessorHttpServer.java

示例5: setSubscriber

import com.typesafe.netty.HandlerSubscriber; //导入依赖的package包/类
public void setSubscriber(Subscriber<? super HttpResponse> subscriber) {
    this.subscriber = (HandlerSubscriber<? super HttpResponse>) subscriber;
}
 
开发者ID:restlet,项目名称:restlet-framework,代码行数:4,代码来源:HttpServerHelper.java

示例6: expect100ContinuePipelining

import com.typesafe.netty.HandlerSubscriber; //导入依赖的package包/类
@Test
public void expect100ContinuePipelining() throws Exception {
    start(new ChannelHandlerAdapter() {
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            HandlerPublisher<HttpRequest> publisher = new HandlerPublisher<>(ctx.executor(), HttpRequest.class);
            HandlerSubscriber<HttpResponse> subscriber = new HandlerSubscriber<>(ctx.executor());
            ctx.pipeline().addLast(publisher, subscriber);
            Processor<HttpRequest, HttpResponse> processor = AkkaStreamsUtil.flowToProcessor(Flow.<HttpRequest>create()
                    .mapAsync(4, new Function<HttpRequest, CompletionStage<String>>() {
                        public CompletionStage<String> apply(HttpRequest request) throws Exception {
                            return helper.extractBodyAsync(request);
                        }
                    }).map(new Function<String, HttpResponse>() {
                        public HttpResponse apply(String body) throws Exception {
                            HttpResponse response = helper.createFullResponse(body);
                            response.headers().set("Body", body);
                            return response;
                        }
                    }),
                materializer
            );
            publisher.subscribe(processor);
            processor.subscribe(subscriber);
        }
    });
    client.writeAndFlush(helper.createStreamedRequest("POST", "/", Collections.singletonList("request 1"), 9));
    client.writeAndFlush(helper.createStreamedRequest("POST", "/", Collections.singletonList("request 2"), 9));
    StreamedHttpRequest request3 = helper.createStreamedRequest("POST", "/", Collections.singletonList("request 3"), 9);
    HttpUtil.set100ContinueExpected(request3, true);
    client.writeAndFlush(request3);
    client.writeAndFlush(helper.createStreamedRequest("POST", "/", Collections.singletonList("request 4"), 9));
    client.writeAndFlush(helper.createStreamedRequest("POST", "/", Collections.singletonList("request 5"), 9));
    StreamedHttpRequest request6 = helper.createStreamedRequest("POST", "/", Collections.singletonList("request 6"), 9);
    HttpUtil.set100ContinueExpected(request6, true);
    client.writeAndFlush(request6);

    assertEquals(helper.extractBody(receiveStreamedResponse()), "request 1");
    assertEquals(helper.extractBody(receiveStreamedResponse()), "request 2");
    assertEquals(helper.extractBody(receiveStreamedResponse()), "request 3");
    assertEquals(helper.extractBody(receiveStreamedResponse()), "request 4");
    assertEquals(helper.extractBody(receiveStreamedResponse()), "request 5");
    assertEquals(helper.extractBody(receiveStreamedResponse()), "request 6");
}
 
开发者ID:playframework,项目名称:netty-reactive-streams,代码行数:44,代码来源:HttpStreamsTest.java


注:本文中的com.typesafe.netty.HandlerSubscriber类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。