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.impl.bootstrap;
29  
30  import java.io.IOException;
31  import java.net.InetSocketAddress;
32  import java.util.List;
33  import java.util.concurrent.Future;
34  
35  import org.apache.hc.core5.annotation.Internal;
36  import org.apache.hc.core5.concurrent.DefaultThreadFactory;
37  import org.apache.hc.core5.concurrent.FutureCallback;
38  import org.apache.hc.core5.function.Callback;
39  import org.apache.hc.core5.function.Decorator;
40  import org.apache.hc.core5.function.Resolver;
41  import org.apache.hc.core5.http.HttpHost;
42  import org.apache.hc.core5.http.impl.DefaultAddressResolver;
43  import org.apache.hc.core5.io.CloseMode;
44  import org.apache.hc.core5.reactor.ConnectionInitiator;
45  import org.apache.hc.core5.reactor.DefaultConnectingIOReactor;
46  import org.apache.hc.core5.reactor.ExceptionEvent;
47  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
48  import org.apache.hc.core5.reactor.IOReactorConfig;
49  import org.apache.hc.core5.reactor.IOReactorService;
50  import org.apache.hc.core5.reactor.IOReactorStatus;
51  import org.apache.hc.core5.reactor.IOSession;
52  import org.apache.hc.core5.reactor.IOSessionListener;
53  import org.apache.hc.core5.util.Args;
54  import org.apache.hc.core5.util.TimeValue;
55  
56  /**
57   * Protocol agnostic client side I/O session initiator.
58   *
59   * @since 5.0
60   */
61  public class AsyncRequester extends AbstractConnectionInitiatorBase implements IOReactorService {
62  
63      private final DefaultConnectingIOReactor ioReactor;
64      private final Resolver<HttpHost, InetSocketAddress> addressResolver;
65  
66      @Internal
67      public AsyncRequester(
68              final IOEventHandlerFactory eventHandlerFactory,
69              final IOReactorConfig ioReactorConfig,
70              final Decorator<IOSession> ioSessionDecorator,
71              final IOSessionListener sessionListener,
72              final Callback<IOSession> sessionShutdownCallback,
73              final Resolver<HttpHost, InetSocketAddress> addressResolver) {
74          this.ioReactor = new DefaultConnectingIOReactor(
75                  eventHandlerFactory,
76                  ioReactorConfig,
77                  new DefaultThreadFactory("requester-dispatch", true),
78                  ioSessionDecorator,
79                  sessionListener,
80                  sessionShutdownCallback);
81          this.addressResolver = addressResolver != null ? addressResolver : DefaultAddressResolver.INSTANCE;
82      }
83  
84      @Override
85      ConnectionInitiator getIOReactor() {
86          return ioReactor;
87      }
88  
89      public Future<IOSession> requestSession(
90              final HttpHost host,
91              final TimeValue timeout,
92              final Object attachment,
93              final FutureCallback<IOSession> callback) {
94          Args.notNull(host, "Host");
95          Args.notNull(timeout, "Timeout");
96          return connect(host, addressResolver.resolve(host), null, timeout, attachment, callback);
97      }
98  
99      @Override
100     public void start() {
101         ioReactor.start();
102     }
103 
104     @Override
105     public IOReactorStatus getStatus() {
106         return ioReactor.getStatus();
107     }
108 
109     @Override
110     public List<ExceptionEvent> getExceptionLog() {
111         return ioReactor.getExceptionLog();
112     }
113 
114     @Override
115     public void initiateShutdown() {
116         ioReactor.initiateShutdown();
117     }
118 
119     @Override
120     public void awaitShutdown(final TimeValue waitTime) throws InterruptedException {
121         ioReactor.awaitShutdown(waitTime);
122     }
123 
124     @Override
125     public void close(final CloseMode closeMode) {
126         ioReactor.close(closeMode);
127     }
128 
129     @Override
130     public void close() throws IOException {
131         ioReactor.close();
132     }
133 
134 }