HttpResponseHandler Class — netty Architecture
Architecture documentation for the HttpResponseHandler class in HttpResponseHandler.java from the netty codebase.
Entity Profile
Dependency Diagram
graph TD 3bbd42a8_8af4_e370_e14a_958c09fd930b["HttpResponseHandler"] ba96ffeb_428e_d56b_48f5_e5b274ba4e94["HttpResponseHandler.java"] 3bbd42a8_8af4_e370_e14a_958c09fd930b -->|defined in| ba96ffeb_428e_d56b_48f5_e5b274ba4e94 a6c6debb_00a8_2db4_c2cc_049ee5dd5054["HttpResponseHandler()"] 3bbd42a8_8af4_e370_e14a_958c09fd930b -->|method| a6c6debb_00a8_2db4_c2cc_049ee5dd5054 56807c7b_4ee8_ce91_8c5e_e8bf1fea8257["put()"] 3bbd42a8_8af4_e370_e14a_958c09fd930b -->|method| 56807c7b_4ee8_ce91_8c5e_e8bf1fea8257 d0c83892_0140_a4ea_6a94_4c83b599410d["awaitResponses()"] 3bbd42a8_8af4_e370_e14a_958c09fd930b -->|method| d0c83892_0140_a4ea_6a94_4c83b599410d f5ec575c_59aa_bcfd_4a59_c0a1b3fea3c1["channelRead0()"] 3bbd42a8_8af4_e370_e14a_958c09fd930b -->|method| f5ec575c_59aa_bcfd_4a59_c0a1b3fea3c1
Relationship Graph
Source Code
example/src/main/java/io/netty/example/http2/helloworld/client/HttpResponseHandler.java lines 36–113
public class HttpResponseHandler extends SimpleChannelInboundHandler<FullHttpResponse> {
private final Map<Integer, Entry<ChannelFuture, ChannelPromise>> streamidPromiseMap;
public HttpResponseHandler() {
// Use a concurrent map because we add and iterate from the main thread (just for the purposes of the example),
// but Netty also does a get on the map when messages are received in a EventLoop thread.
streamidPromiseMap = new ConcurrentHashMap<>();
}
/**
* Create an association between an anticipated response stream id and a {@link io.netty.channel.ChannelPromise}
*
* @param streamId The stream for which a response is expected
* @param writeFuture A future that represent the request write operation
* @param promise The promise object that will be used to wait/notify events
* @return The previous object associated with {@code streamId}
* @see HttpResponseHandler#awaitResponses(long, java.util.concurrent.TimeUnit)
*/
public Entry<ChannelFuture, ChannelPromise> put(int streamId, ChannelFuture writeFuture, ChannelPromise promise) {
return streamidPromiseMap.put(streamId, new SimpleEntry<ChannelFuture, ChannelPromise>(writeFuture, promise));
}
/**
* 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)
*/
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());
}
System.out.println("---Stream id: " + entry.getKey() + " received---");
itr.remove();
}
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
Integer streamId = msg.headers().getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text());
if (streamId == null) {
System.err.println("HttpResponseHandler unexpected message received: " + msg);
return;
}
Entry<ChannelFuture, ChannelPromise> entry = streamidPromiseMap.get(streamId);
if (entry == null) {
System.err.println("Message received for unknown stream id " + streamId);
} else {
// Do stuff with the message (for now just print it)
ByteBuf content = msg.content();
if (content.isReadable()) {
int contentLength = content.readableBytes();
byte[] arr = new byte[contentLength];
content.readBytes(arr);
System.out.println(new String(arr, 0, contentLength, CharsetUtil.UTF_8));
}
entry.getValue().setSuccess();
}
}
}
Source
Frequently Asked Questions
What is the HttpResponseHandler class?
HttpResponseHandler is a class in the netty codebase, defined in example/src/main/java/io/netty/example/http2/helloworld/client/HttpResponseHandler.java.
Where is HttpResponseHandler defined?
HttpResponseHandler is defined in example/src/main/java/io/netty/example/http2/helloworld/client/HttpResponseHandler.java at line 36.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free