View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.nio;
29  
30  import java.util.concurrent.Future;
31  
32  import org.apache.hc.core5.annotation.Contract;
33  import org.apache.hc.core5.annotation.ThreadingBehavior;
34  import org.apache.hc.core5.concurrent.BasicFuture;
35  import org.apache.hc.core5.concurrent.FutureCallback;
36  import org.apache.hc.core5.http.nio.support.BasicClientExchangeHandler;
37  import org.apache.hc.core5.http.protocol.HttpContext;
38  import org.apache.hc.core5.http.protocol.HttpCoreContext;
39  
40  /**
41   * Client endpoint leased from a connection manager.
42   * <p>
43   * Once the endpoint is no longer needed it MUST be released with {@link #releaseAndReuse()}
44   * or {@link #releaseAndDiscard()}.
45   *
46   * @since 5.0
47   */
48  @Contract(threading = ThreadingBehavior.SAFE)
49  public abstract class AsyncClientEndpoint {
50  
51      /**
52       * Initiates a message exchange using the given handler.
53       * <p>
54       * Once the endpoint is no longer needed it MUST be released with {@link #releaseAndReuse()}
55       * or {@link #releaseAndDiscard()}.
56       */
57      public abstract void execute(AsyncClientExchangeHandler exchangeHandler, HttpContext context);
58  
59      /**
60       * Releases the underlying connection back to the connection pool as re-usable.
61       */
62      public abstract void releaseAndReuse();
63  
64      /**
65       * Shuts down the underlying connection and removes it from the connection pool.
66       */
67      public abstract void releaseAndDiscard();
68  
69      /**
70       * Initiates message exchange using the given request producer and response consumer.
71       * <p>
72       * Once the endpoint is no longer needed it MUST be released with {@link #releaseAndReuse()}
73       * or {@link #releaseAndDiscard()}.
74       */
75      public final <T> Future<T> execute(
76              final AsyncRequestProducer requestProducer,
77              final AsyncResponseConsumer<T> responseConsumer,
78              final HttpContext context,
79              final FutureCallback<T> callback) {
80          final BasicFuture<T> future = new BasicFuture<>(callback);
81          execute(new BasicClientExchangeHandler<>(requestProducer, responseConsumer,
82                          new FutureCallback<T>() {
83  
84                              @Override
85                              public void completed(final T result) {
86                                  future.completed(result);
87                              }
88  
89                              @Override
90                              public void failed(final Exception ex) {
91                                  future.failed(ex);
92                              }
93  
94                              @Override
95                              public void cancelled() {
96                                  future.cancel();
97                              }
98  
99                          }),
100                 context != null ? context : HttpCoreContext.create());
101         return future;
102     }
103 
104     /**
105      * Initiates a message exchange using the given request producer and response consumer.
106      * <p>
107      * Once the endpoint is no longer needed it MUST be released with {@link #releaseAndReuse()}
108      * or {@link #releaseAndDiscard()}.
109      */
110     public final <T> Future<T> execute(
111             final AsyncRequestProducer requestProducer,
112             final AsyncResponseConsumer<T> responseConsumer,
113             final FutureCallback<T> callback) {
114         return execute(requestProducer, responseConsumer, null, callback);
115     }
116 
117 }