2626 */
2727package org .apache .hc .core5 .http2 .examples ;
2828
29- import java .io .BufferedReader ;
30- import java .io .InputStreamReader ;
31- import java .nio .charset .Charset ;
32- import java .nio .charset .StandardCharsets ;
3329import java .util .List ;
30+ import java .util .concurrent .CountDownLatch ;
3431import java .util .concurrent .Future ;
3532
3633import org .apache .hc .core5 .annotation .Experimental ;
37- import org .apache .hc .core5 .http .ClassicHttpRequest ;
38- import org .apache .hc .core5 .http .ClassicHttpResponse ;
39- import org .apache .hc .core5 .http .ContentType ;
34+ import org .apache .hc .core5 .concurrent .FutureCallback ;
4035import org .apache .hc .core5 .http .Header ;
4136import org .apache .hc .core5 .http .HttpConnection ;
42- import org .apache .hc .core5 .http .HttpEntity ;
4337import org .apache .hc .core5 .http .HttpHost ;
38+ import org .apache .hc .core5 .http .HttpResponse ;
39+ import org .apache .hc .core5 .http .Message ;
4440import org .apache .hc .core5 .http .impl .bootstrap .HttpAsyncRequester ;
45- import org .apache .hc .core5 .http .io . support . ClassicRequestBuilder ;
41+ import org .apache .hc .core5 .http .message . BasicHttpRequest ;
4642import org .apache .hc .core5 .http .nio .AsyncClientEndpoint ;
47- import org .apache .hc .core5 .http .nio .support .classic .ClassicToAsyncRequestProducer ;
48- import org .apache .hc .core5 .http .nio .support .classic .ClassicToAsyncResponseConsumer ;
49- import org .apache .hc .core5 .http .protocol .HttpCoreContext ;
43+ import org .apache .hc .core5 .http .nio .entity .StringAsyncEntityConsumer ;
44+ import org .apache .hc .core5 .http .nio .support .BasicRequestProducer ;
45+ import org .apache .hc .core5 .http .nio .support .BasicResponseConsumer ;
46+ import org .apache .hc .core5 .http .support .BasicRequestBuilder ;
5047import org .apache .hc .core5 .http2 .HttpVersionPolicy ;
5148import org .apache .hc .core5 .http2 .config .H2Config ;
5249import org .apache .hc .core5 .http2 .frame .RawFrame ;
5350import org .apache .hc .core5 .http2 .impl .H2Processors ;
5451import org .apache .hc .core5 .http2 .impl .nio .H2StreamListener ;
52+ import org .apache .hc .core5 .http2 .priority .PriorityFormatter ;
5553import org .apache .hc .core5 .http2 .priority .PriorityValue ;
56- import org .apache .hc .core5 .http2 .protocol .H2RequestPriority ;
5754import org .apache .hc .core5 .io .CloseMode ;
5855import org .apache .hc .core5 .util .Timeout ;
5956
6360 * Requires H2Processors to include H2RequestPriority (client chain) and an HTTP/2 connection.
6461 */
6562@ Experimental
66- public class ClassicH2PriorityExample {
63+ public class H2RequestExecutionWithPriorityExample {
6764
6865 public static void main (final String [] args ) throws Exception {
6966
@@ -120,50 +117,62 @@ public void onOutputFlowControl(final HttpConnection c, final int id, final int
120117 final Future <AsyncClientEndpoint > future = requester .connect (target , Timeout .ofSeconds (30 ));
121118 final AsyncClientEndpoint clientEndpoint = future .get ();
122119
120+ final CountDownLatch latch = new CountDownLatch (2 );
121+
123122 // ---- Request 1: Explicit non-default priority -> header MUST be emitted
124- executeWithPriority (clientEndpoint , target , "/httpbin/headers" , PriorityValue .of (0 , true ));
123+ executeWithPriority (clientEndpoint , target , "/httpbin/headers" , PriorityValue .of (0 , true ), latch );
125124
126125 // ---- Request 2: RFC defaults -> header MUST be omitted by the interceptor
127- executeWithPriority (clientEndpoint , target , "/httpbin/user-agent" , PriorityValue .defaults ());
126+ executeWithPriority (clientEndpoint , target , "/httpbin/user-agent" , PriorityValue .defaults (), latch );
128127
129128 System .out .println ("Shutting down I/O reactor" );
130129 requester .initiateShutdown ();
131130 }
132131
133132 private static void executeWithPriority (
134- final AsyncClientEndpoint endpoint ,
133+ final AsyncClientEndpoint clientEndpoint ,
135134 final HttpHost target ,
136- final String path ,
137- final PriorityValue priorityValue ) throws Exception {
135+ final String requestUri ,
136+ final PriorityValue priorityValue ,
137+ final CountDownLatch latch ) throws Exception {
138138
139- final ClassicHttpRequest request = ClassicRequestBuilder .get ()
139+ final BasicHttpRequest request = BasicRequestBuilder .get ()
140140 .setHttpHost (target )
141- .setPath (path )
141+ .setPath (requestUri )
142142 .build ();
143+ if (!PriorityValue .defaults ().equals (priorityValue )) {
144+ request .addHeader (PriorityFormatter .formatHeader (priorityValue ));
145+ }
146+
147+ clientEndpoint .execute (
148+ new BasicRequestProducer (request , null ),
149+ new BasicResponseConsumer <>(new StringAsyncEntityConsumer ()),
150+ new FutureCallback <Message <HttpResponse , String >>() {
143151
144- // Place the PriorityValue into the context so H2RequestPriority can format the header
145- final HttpCoreContext ctx = HttpCoreContext .create ();
146- ctx .setAttribute (H2RequestPriority .ATTR_HTTP2_PRIORITY_VALUE , priorityValue );
147-
148- final ClassicToAsyncRequestProducer requestProducer = new ClassicToAsyncRequestProducer (request , Timeout .ofMinutes (1 ));
149- final ClassicToAsyncResponseConsumer responseConsumer = new ClassicToAsyncResponseConsumer (Timeout .ofMinutes (1 ));
150-
151- endpoint .execute (requestProducer , responseConsumer , ctx , null );
152-
153- requestProducer .blockWaiting ().execute ();
154- try (ClassicHttpResponse response = responseConsumer .blockWaiting ()) {
155- System .out .println (path + " -> " + response .getCode ());
156- final HttpEntity entity = response .getEntity ();
157- if (entity != null ) {
158- final ContentType ct = ContentType .parse (entity .getContentType ());
159- final Charset cs = ContentType .getCharset (ct , StandardCharsets .UTF_8 );
160- try (BufferedReader reader = new BufferedReader (new InputStreamReader (entity .getContent (), cs ))) {
161- String line ;
162- while ((line = reader .readLine ()) != null ) {
163- System .out .println (line );
152+ @ Override
153+ public void completed (final Message <HttpResponse , String > message ) {
154+ clientEndpoint .releaseAndReuse ();
155+ final HttpResponse response = message .getHead ();
156+ final String body = message .getBody ();
157+ System .out .println (requestUri + "->" + response .getCode ());
158+ System .out .println (body );
159+ latch .countDown ();
164160 }
165- }
166- }
167- }
161+
162+ @ Override
163+ public void failed (final Exception ex ) {
164+ clientEndpoint .releaseAndDiscard ();
165+ System .out .println (requestUri + "->" + ex );
166+ latch .countDown ();
167+ }
168+
169+ @ Override
170+ public void cancelled () {
171+ clientEndpoint .releaseAndDiscard ();
172+ System .out .println (requestUri + " cancelled" );
173+ latch .countDown ();
174+ }
175+
176+ });
168177 }
169178}
0 commit comments