Example usage for io.netty.channel ChannelPromise cause

List of usage examples for io.netty.channel ChannelPromise cause

Introduction

In this page you can find the example usage for io.netty.channel ChannelPromise cause.

Prototype

Throwable cause();

Source Link

Document

Returns the cause of the failed I/O operation if the I/O operation has failed.

Usage

From source file:io.aos.netty5.http2.client.HttpResponseHandler.java

License:Apache License

/**
 * Wait (sequentially) for a time duration for each anticipated response
 *
 * @param timeout Value of time to wait for each response
 * @param unit Units associated with {@code timeout}
 * @see {@link io.netty.example.http2.client.HttpResponseHandler#put put}
 *//*w w  w  .ja  v a 2  s.c om*/
public void awaitResponses(long timeout, TimeUnit unit) {
    Iterator<Entry<Integer, ChannelPromise>> itr = streamidPromiseMap.entrySet().iterator();
    while (itr.hasNext()) {
        Entry<Integer, ChannelPromise> entry = itr.next();
        ChannelPromise promise = entry.getValue();
        if (!promise.awaitUninterruptibly(timeout, unit)) {
            throw new IllegalStateException("Timed out waiting for response on stream id " + entry.getKey());
        }
        if (!promise.isSuccess()) {
            throw new RuntimeException(promise.cause());
        }
        System.out.println("---Stream id: " + entry.getKey() + " received---");
        itr.remove();
    }
}

From source file:jmeter.plugins.http2.sampler.HttpResponseHandler.java

License:Apache License

/**
 * Wait (sequentially) for a time duration for each anticipated response
 *
 * @param timeout Value of time to wait for each response
 * @param unit Units associated with {@code timeout}
 * @see HttpResponseHandler#put(int, ChannelPromise)
 *//*from www  . j  av  a 2  s.c  o m*/
public SortedMap<Integer, FullHttpResponse> awaitResponses(long timeout, TimeUnit unit) {
    Iterator<Entry<Integer, ChannelPromise>> itr = streamidPromiseMap.entrySet().iterator();

    while (itr.hasNext()) {
        Entry<Integer, ChannelPromise> entry = itr.next();
        ChannelPromise promise = entry.getValue();
        if (!promise.awaitUninterruptibly(timeout, unit)) {
            throw new IllegalStateException("Timed out waiting for response on stream id " + entry.getKey());
        }
        if (!promise.isSuccess()) {
            throw new RuntimeException(promise.cause());
        }
        System.out.println("---Stream id: " + entry.getKey() + " received---");
        itr.remove();
    }

    return streamidResponseMap;
}

From source file:org.ballerinalang.test.util.http2.HTTP2ResponseHandler.java

License:Open Source License

/**
 * Provide asynchronous response to HTTP2 request.
 *
 * @param streamId StreamID/*www. ja  va 2s.  c o m*/
 * @return Response string
 */
public FullHttpResponse getResponse(int streamId) {

    FullHttpResponse message = streamIdResponseMap.get(streamId);
    if (message != null) {
        return message;
    } else {
        Entry<ChannelFuture, ChannelPromise> channelFutureChannelPromiseEntry = streamIdPromiseMap
                .get(streamId);
        if (channelFutureChannelPromiseEntry != null) {
            ChannelFuture writeFuture = channelFutureChannelPromiseEntry.getKey();
            if (!writeFuture.awaitUninterruptibly(TestConstant.HTTP2_RESPONSE_TIME_OUT,
                    TestConstant.HTTP2_RESPONSE_TIME_UNIT)) {
                streamIdPromiseMap.remove(streamId);
                throw new IllegalStateException("Timed out waiting to write for stream id " + streamId);
            }
            if (!writeFuture.isSuccess()) {
                streamIdPromiseMap.remove(streamId);
                throw new RuntimeException(writeFuture.cause());
            }
            ChannelPromise promise = channelFutureChannelPromiseEntry.getValue();
            if (!promise.awaitUninterruptibly(TestConstant.HTTP2_RESPONSE_TIME_OUT,
                    TestConstant.HTTP2_RESPONSE_TIME_UNIT)) {
                streamIdPromiseMap.remove(streamId);
                throw new IllegalStateException("Timed out waiting for response on stream id " + streamId);
            }
            if (!promise.isSuccess()) {
                streamIdPromiseMap.remove(streamId);
                throw new RuntimeException(promise.cause());
            }
        }
    }
    return streamIdResponseMap.get(streamId);
}

From source file:org.elasticsearch.http.netty4.Netty4HttpPipeliningHandlerTests.java

License:Apache License

public void testPipeliningRequestsAreReleased() throws InterruptedException {
    final int numberOfRequests = 10;
    final EmbeddedChannel embeddedChannel = new EmbeddedChannel(
            new Netty4HttpPipeliningHandler(logger, numberOfRequests + 1));

    for (int i = 0; i < numberOfRequests; i++) {
        embeddedChannel.writeInbound(createHttpRequest("/" + i));
    }//from   w ww  .  j  a  v a 2  s  .  co m

    HttpPipelinedRequest<FullHttpRequest> inbound;
    ArrayList<HttpPipelinedRequest<FullHttpRequest>> requests = new ArrayList<>();
    while ((inbound = embeddedChannel.readInbound()) != null) {
        requests.add(inbound);
    }

    ArrayList<ChannelPromise> promises = new ArrayList<>();
    for (int i = 1; i < requests.size(); ++i) {
        ChannelPromise promise = embeddedChannel.newPromise();
        promises.add(promise);
        HttpPipelinedRequest<FullHttpRequest> pipelinedRequest = requests.get(i);
        Netty4HttpRequest nioHttpRequest = new Netty4HttpRequest(pipelinedRequest.getRequest(),
                pipelinedRequest.getSequence());
        Netty4HttpResponse resp = nioHttpRequest.createResponse(RestStatus.OK, BytesArray.EMPTY);
        embeddedChannel.writeAndFlush(resp, promise);
    }

    for (ChannelPromise promise : promises) {
        assertFalse(promise.isDone());
    }
    embeddedChannel.close().syncUninterruptibly();
    for (ChannelPromise promise : promises) {
        assertTrue(promise.isDone());
        assertTrue(promise.cause() instanceof ClosedChannelException);
    }
}

From source file:org.elasticsearch.http.nio.NioHttpPipeliningHandlerTests.java

License:Apache License

public void testPipeliningRequestsAreReleased() throws InterruptedException {
    final int numberOfRequests = 10;
    final EmbeddedChannel embeddedChannel = new EmbeddedChannel(
            new NioHttpPipeliningHandler(logger, numberOfRequests + 1));

    for (int i = 0; i < numberOfRequests; i++) {
        embeddedChannel.writeInbound(createHttpRequest("/" + i));
    }// w  ww.  jav a  2 s .  c  om

    HttpPipelinedRequest<FullHttpRequest> inbound;
    ArrayList<HttpPipelinedRequest<FullHttpRequest>> requests = new ArrayList<>();
    while ((inbound = embeddedChannel.readInbound()) != null) {
        requests.add(inbound);
    }

    ArrayList<ChannelPromise> promises = new ArrayList<>();
    for (int i = 1; i < requests.size(); ++i) {
        ChannelPromise promise = embeddedChannel.newPromise();
        promises.add(promise);
        HttpPipelinedRequest<FullHttpRequest> pipelinedRequest = requests.get(i);
        NioHttpRequest nioHttpRequest = new NioHttpRequest(pipelinedRequest.getRequest(),
                pipelinedRequest.getSequence());
        NioHttpResponse resp = nioHttpRequest.createResponse(RestStatus.OK, BytesArray.EMPTY);
        embeddedChannel.writeAndFlush(resp, promise);
    }

    for (ChannelPromise promise : promises) {
        assertFalse(promise.isDone());
    }
    embeddedChannel.close().syncUninterruptibly();
    for (ChannelPromise promise : promises) {
        assertTrue(promise.isDone());
        assertTrue(promise.cause() instanceof ClosedChannelException);
    }
}

From source file:org.infinispan.rest.http2.HttpResponseHandler.java

License:Apache License

/**
 * Wait (sequentially) for a time duration for each anticipated response
 *
 * @param timeout Value of time to wait for each response
 * @param unit Units associated with {@code timeout}
 * @see HttpResponseHandler#put(int, io.netty.channel.ChannelFuture, io.netty.channel.ChannelPromise)
 *//*from w  w  w .ja v  a  2s  .  c o  m*/
public void awaitResponses(long timeout, TimeUnit unit) {
    Iterator<Entry<Integer, Entry<ChannelFuture, ChannelPromise>>> itr = streamidPromiseMap.entrySet()
            .iterator();
    while (itr.hasNext()) {
        Entry<Integer, Entry<ChannelFuture, ChannelPromise>> entry = itr.next();
        ChannelFuture writeFuture = entry.getValue().getKey();
        if (!writeFuture.awaitUninterruptibly(timeout, unit)) {
            throw new IllegalStateException("Timed out waiting to write for stream id " + entry.getKey());
        }
        if (!writeFuture.isSuccess()) {
            throw new RuntimeException(writeFuture.cause());
        }
        ChannelPromise promise = entry.getValue().getValue();
        if (!promise.awaitUninterruptibly(timeout, unit)) {
            throw new IllegalStateException("Timed out waiting for response on stream id " + entry.getKey());
        }
        if (!promise.isSuccess()) {
            throw new RuntimeException(promise.cause());
        }
        itr.remove();
    }
}

From source file:org.tinygroup.nettyremote.impl.ClientImpl.java

License:GNU General Public License

public void write(Object o) {
    if (o instanceof Event) {
        Event event = (Event) o;
        LOGGER.logMessage(LogLevel.DEBUG, "?:eventId:{},serviceId:{}", event.getEventId(),
                event.getServiceRequest().getServiceId());
    }// w ww  .j  a  v  a  2s.com
    if (future == null || future.channel() == null) {
        throw new RuntimeException("");
    }
    ChannelFuture f = future.channel().writeAndFlush(o);
    if (f instanceof ChannelPromise) {
        ChannelPromise p = (ChannelPromise) f;
        try {
            p.await();
        } catch (InterruptedException e) {
            LOGGER.logMessage(LogLevel.WARN, "?");
        }
        if (p.isSuccess()) {
            LOGGER.logMessage(LogLevel.DEBUG, "??{}", p.isSuccess());
        } else {
            LOGGER.logMessage(LogLevel.WARN, "??false");
            throw new RuntimeException(p.cause());
        }

    }

}

From source file:org.wso2.carbon.transport.http.netty.util.client.http2.HTTP2ResponseHandler.java

License:Open Source License

/**
 * Provide asynchronous response to HTTP2 request
 *
 * @param streamId StreamID//  w w  w. j av  a  2  s  . co m
 * @return Response string
 */
public String getResponse(int streamId) {

    String message = streamIdResponseMap.get(streamId);
    if (message != null) {
        return message;
    } else {
        Entry<ChannelFuture, ChannelPromise> channelFutureChannelPromiseEntry = streamIdPromiseMap
                .get(streamId);
        if (channelFutureChannelPromiseEntry != null) {
            ChannelFuture writeFuture = channelFutureChannelPromiseEntry.getKey();
            if (!writeFuture.awaitUninterruptibly(TestUtil.HTTP2_RESPONSE_TIME_OUT,
                    TestUtil.HTTP2_RESPONSE_TIME_UNIT)) {
                streamIdPromiseMap.remove(streamId);
                throw new IllegalStateException("Timed out waiting to write for stream id " + streamId);
            }
            if (!writeFuture.isSuccess()) {
                streamIdPromiseMap.remove(streamId);
                throw new RuntimeException(writeFuture.cause());
            }
            ChannelPromise promise = channelFutureChannelPromiseEntry.getValue();
            if (!promise.awaitUninterruptibly(TestUtil.HTTP2_RESPONSE_TIME_OUT,
                    TestUtil.HTTP2_RESPONSE_TIME_UNIT)) {
                streamIdPromiseMap.remove(streamId);
                throw new IllegalStateException("Timed out waiting for response on stream id " + streamId);
            }
            if (!promise.isSuccess()) {
                streamIdPromiseMap.remove(streamId);
                throw new RuntimeException(promise.cause());
            }
        }
    }
    return streamIdResponseMap.get(streamId);
}

From source file:org.wso2.esb.integration.common.utils.clients.http2client.HttpResponseHandler.java

License:Open Source License

/**
 * Wait (sequentially) for a time duration for each anticipated response
 *
 * @param timeout Value of time to wait for each response
 * @param unit Units associated with {@code timeout}
 * @see HttpResponseHandler#put(int, io.netty.channel.ChannelFuture, io.netty.channel.ChannelPromise)
 *//*from w w  w .j  a  v a2s. c om*/
public void awaitResponses(long timeout, TimeUnit unit) {
    Iterator<Entry<Integer, Entry<ChannelFuture, ChannelPromise>>> itr = streamidPromiseMap.entrySet()
            .iterator();
    while (itr.hasNext()) {
        Entry<Integer, Entry<ChannelFuture, ChannelPromise>> entry = itr.next();
        ChannelFuture writeFuture = entry.getValue().getKey();
        if (!writeFuture.awaitUninterruptibly(timeout, unit)) {
            throw new IllegalStateException("Timed out waiting to write for stream id " + entry.getKey());
        }
        if (!writeFuture.isSuccess()) {
            throw new RuntimeException(writeFuture.cause());
        }
        ChannelPromise promise = entry.getValue().getValue();
        if (!promise.awaitUninterruptibly(timeout, unit)) {
            throw new IllegalStateException("Timed out waiting for response on stream id " + entry.getKey());
        }
        if (!promise.isSuccess()) {
            throw new RuntimeException(promise.cause());
        }
        log.debug("---Stream id: " + entry.getKey() + " received---");
        itr.remove();
    }
}