private void deactivateStream() { if (handler instanceof TransportResponseHandler) { // we only have to do this for TransportResponseHandler as it exposes numOutstandingFetches // (there is no extra cleanup that needs to happen) ((TransportResponseHandler) handler).deactivateStream(); } }
private void deactivateStream() { if (handler instanceof TransportResponseHandler) { // we only have to do this for TransportResponseHandler as it exposes numOutstandingFetches // (there is no extra cleanup that needs to happen) ((TransportResponseHandler) handler).deactivateStream(); } }
@Override public void channelInactive() throws Exception { handler.deactivateStream(); callback.onFailure(streamId, new ClosedChannelException()); }
@Override public void channelInactive() throws Exception { handler.deactivateStream(); callback.onFailure(streamId, new ClosedChannelException()); }
@Override public void exceptionCaught(Throwable cause) throws Exception { handler.deactivateStream(); callback.onFailure(streamId, cause); }
@Override public void exceptionCaught(Throwable cause) throws Exception { handler.deactivateStream(); callback.onFailure(streamId, cause); }
@Override public boolean handle(ByteBuf buf) throws Exception { int toRead = (int) Math.min(buf.readableBytes(), byteCount - bytesRead); ByteBuffer nioBuffer = buf.readSlice(toRead).nioBuffer(); int available = nioBuffer.remaining(); callback.onData(streamId, nioBuffer); bytesRead += available; if (bytesRead > byteCount) { RuntimeException re = new IllegalStateException(String.format( "Read too many bytes? Expected %d, but read %d.", byteCount, bytesRead)); callback.onFailure(streamId, re); handler.deactivateStream(); throw re; } else if (bytesRead == byteCount) { handler.deactivateStream(); callback.onComplete(streamId); } return bytesRead != byteCount; }
@Override public boolean handle(ByteBuf buf) throws Exception { int toRead = (int) Math.min(buf.readableBytes(), byteCount - bytesRead); ByteBuffer nioBuffer = buf.readSlice(toRead).nioBuffer(); int available = nioBuffer.remaining(); callback.onData(streamId, nioBuffer); bytesRead += available; if (bytesRead > byteCount) { RuntimeException re = new IllegalStateException(String.format( "Read too many bytes? Expected %d, but read %d.", byteCount, bytesRead)); callback.onFailure(streamId, re); handler.deactivateStream(); throw re; } else if (bytesRead == byteCount) { handler.deactivateStream(); callback.onComplete(streamId); } return bytesRead != byteCount; }
} catch (Exception e) { logger.error("Error installing stream handler.", e); deactivateStream();
} catch (Exception e) { logger.error("Error installing stream handler.", e); deactivateStream();
} catch (Exception e) { logger.error("Error installing stream handler.", e); deactivateStream();
} catch (Exception e) { logger.error("Error installing stream handler.", e); deactivateStream();
@Test public void testActiveStreams() throws Exception { Channel c = new LocalChannel(); c.pipeline().addLast(TransportFrameDecoder.HANDLER_NAME, new TransportFrameDecoder()); TransportResponseHandler handler = new TransportResponseHandler(c); StreamResponse response = new StreamResponse("stream", 1234L, null); StreamCallback cb = mock(StreamCallback.class); handler.addStreamCallback("stream", cb); assertEquals(1, handler.numOutstandingRequests()); handler.handle(response); assertEquals(1, handler.numOutstandingRequests()); handler.deactivateStream(); assertEquals(0, handler.numOutstandingRequests()); StreamFailure failure = new StreamFailure("stream", "uh-oh"); handler.addStreamCallback("stream", cb); assertEquals(1, handler.numOutstandingRequests()); handler.handle(failure); assertEquals(0, handler.numOutstandingRequests()); }
@Test public void testActiveStreams() throws Exception { Channel c = new LocalChannel(); c.pipeline().addLast(TransportFrameDecoder.HANDLER_NAME, new TransportFrameDecoder()); TransportResponseHandler handler = new TransportResponseHandler(c); StreamResponse response = new StreamResponse("stream", 1234L, null); StreamCallback cb = mock(StreamCallback.class); handler.addStreamCallback("stream", cb); assertEquals(1, handler.numOutstandingRequests()); handler.handle(response); assertEquals(1, handler.numOutstandingRequests()); handler.deactivateStream(); assertEquals(0, handler.numOutstandingRequests()); StreamFailure failure = new StreamFailure("stream", "uh-oh"); handler.addStreamCallback("stream", cb); assertEquals(1, handler.numOutstandingRequests()); handler.handle(failure); assertEquals(0, handler.numOutstandingRequests()); }