use of com.tvd12.ezyfox.concurrent.EzyFuture in project ezyhttp by youngmonkeys.
the class HttpClientProxy method execute.
public void execute(Request request, RequestCallback<ResponseEntity> callback) {
EzyFuture future = new RequestFutureTask(callback);
futures.addFuture(request, future);
try {
addRequest(request);
} catch (Exception e) {
futures.removeFuture(request);
throw e;
}
}
use of com.tvd12.ezyfox.concurrent.EzyFuture in project ezyhttp by youngmonkeys.
the class HttpClientProxy method handleRequests.
protected void handleRequests() {
Request request = null;
EzyFuture future = null;
Exception exception = null;
ResponseEntity response = null;
try {
request = requestQueue.take();
future = futures.removeFuture(request);
response = client.request(request);
} catch (Exception e) {
exception = e;
}
try {
if (future != null) {
if (exception != null) {
future.setException(exception);
} else {
future.setResult(response);
}
} else {
if (exception != null) {
logger.info("handled request: {} with exception, but there is no future", request, exception);
} else {
logger.info("handled request: {} with response: {}, but there is no future", request, response);
}
}
} catch (Throwable e) {
logger.info("handle request result error", e);
}
}
use of com.tvd12.ezyfox.concurrent.EzyFuture in project ezyhttp by youngmonkeys.
the class ResourceDownloadManager method loop.
private void loop() {
byte[] buffer = new byte[bufferSize];
while (active) {
Entry entry = null;
boolean done = true;
Exception exception = null;
try {
entry = queue.take();
if (entry == POISON) {
break;
}
InputStream inputStream = entry.inputStream;
OutputStream outputStream = entry.outputStream;
int read = inputStream.read(buffer);
if (read > 0) {
outputStream.write(buffer, 0, read);
done = false;
}
} catch (Exception e) {
exception = e;
logger.info("download error", e);
} catch (Throwable e) {
exception = new IllegalStateException(e);
logger.info("download fatal error", e);
}
if (entry == null) {
continue;
}
try {
if (done) {
EzyFuture future = futureMap.removeFuture(entry);
if (future == null) {
continue;
}
if (exception != null) {
future.setException(exception);
} else {
future.setResult(Boolean.TRUE);
}
} else {
if (!queue.offer(entry)) {
EzyFuture future = futureMap.removeFuture(entry);
if (future != null) {
future.setException(new MaxResourceDownloadCapacity(capacity));
}
}
}
} catch (Throwable e) {
logger.info("handle download result error", e);
}
}
}
use of com.tvd12.ezyfox.concurrent.EzyFuture in project ezyhttp by youngmonkeys.
the class ResourceDownloadManager method drain.
public void drain(InputStream from, OutputStream to) throws Exception {
Entry entry = new Entry(from, to);
EzyFuture future = new EzyFutureTask();
drain(entry, future);
future.get(DEFAULT_TIMEOUT);
}
use of com.tvd12.ezyfox.concurrent.EzyFuture in project ezyhttp by youngmonkeys.
the class HttpClientProxy method close.
@Override
public void close() {
this.active = false;
this.requestQueue.clear();
Map<Request, EzyFuture> undoneTasks = futures.clear();
for (Request undoneRequest : undoneTasks.keySet()) {
EzyFuture undoneTask = undoneTasks.get(undoneRequest);
undoneTask.cancel("HttpClientProxy close, request to: " + undoneRequest.getURL() + " has cancelled");
}
if (executorService != null) {
this.executorService.shutdownNow();
}
}
Aggregations