| 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 |
package org.apache.http.impl.nio.client; |
| 28 |
|
| 29 |
import java.io.IOException; |
| 30 |
import java.util.concurrent.CancellationException; |
| 31 |
import java.util.concurrent.ThreadFactory; |
| 32 |
import java.util.concurrent.atomic.AtomicReference; |
| 33 |
|
| 34 |
import org.apache.commons.logging.Log; |
| 35 |
import org.apache.commons.logging.LogFactory; |
| 36 |
import org.apache.http.nio.NHttpClientEventHandler; |
| 37 |
import org.apache.http.nio.conn.NHttpClientConnectionManager; |
| 38 |
import org.apache.http.nio.reactor.IOEventDispatch; |
| 39 |
|
| 40 |
abstract class CloseableHttpAsyncClientBase extends CloseableHttpPipeliningClient { |
| 41 |
|
| 42 |
private final Log log = LogFactory.getLog(getClass()); |
| 43 |
|
| 44 |
static enum Status {INACTIVE, ACTIVE, STOPPED} |
| 45 |
|
| 46 |
private final NHttpClientConnectionManager connmgr; |
| 47 |
private final Thread reactorThread; |
| 48 |
|
| 49 |
private final AtomicReference<Status> status; |
| 50 |
|
| 51 |
public CloseableHttpAsyncClientBase( |
| 52 |
final NHttpClientConnectionManager connmgr, |
| 53 |
final ThreadFactory threadFactory, |
| 54 |
final NHttpClientEventHandler handler) { |
| 55 |
super(); |
| 56 |
this.connmgr = connmgr; |
| 57 |
if (threadFactory != null && handler != null) { |
| 58 |
this.reactorThread = threadFactory.newThread(new Runnable() { |
| 59 |
|
| 60 |
@Override |
| 61 |
public void run() { |
| 62 |
try { |
| 63 |
final IOEventDispatch ioEventDispatch = new InternalIODispatch(handler); |
| 64 |
connmgr.execute(ioEventDispatch); |
| 65 |
} catch (final Exception ex) { |
| 66 |
log.error("I/O reactor terminated abnormally", ex); |
| 67 |
} finally { |
| 68 |
status.set(Status.STOPPED); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
}); |
| 73 |
} else { |
| 74 |
this.reactorThread = null; |
| 75 |
} |
| 76 |
this.status = new AtomicReference<Status>(Status.INACTIVE); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void start() { |
| 81 |
if (this.status.compareAndSet(Status.INACTIVE, Status.ACTIVE)) { |
| 82 |
if (this.reactorThread != null) { |
| 83 |
this.reactorThread.start(); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public void close() { |
| 90 |
if (this.status.compareAndSet(Status.ACTIVE, Status.STOPPED)) { |
| 91 |
if (this.reactorThread != null) { |
| 92 |
try { |
| 93 |
this.connmgr.shutdown(); |
| 94 |
} catch (final IOException ex) { |
| 95 |
this.log.error("I/O error shutting down connection manager", ex); |
| 96 |
} |
| 97 |
try { |
| 98 |
this.reactorThread.join(); |
| 99 |
} catch (final InterruptedException ex) { |
| 100 |
Thread.currentThread().interrupt(); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public boolean isRunning() { |
| 108 |
return this.status.get() == Status.ACTIVE; |
| 109 |
} |
| 110 |
|
| 111 |
final void execute(final AbstractClientExchangeHandler handler) { |
| 112 |
try { |
| 113 |
if (!isRunning()) { |
| 114 |
throw new CancellationException("Request execution cancelled"); |
| 115 |
} |
| 116 |
handler.start(); |
| 117 |
} catch (final Exception ex) { |
| 118 |
handler.failed(ex); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
} |