Search in sources :

Example 1 with EzyFuture

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;
    }
}
Also used : EzyFuture(com.tvd12.ezyfox.concurrent.EzyFuture) RequestFutureTask(com.tvd12.ezyhttp.client.concurrent.RequestFutureTask) EzyProcessor.processWithException(com.tvd12.ezyfox.util.EzyProcessor.processWithException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) IOException(java.io.IOException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException)

Example 2 with EzyFuture

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);
    }
}
Also used : ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) Request(com.tvd12.ezyhttp.client.request.Request) DownloadRequest(com.tvd12.ezyhttp.client.request.DownloadRequest) EzyFuture(com.tvd12.ezyfox.concurrent.EzyFuture) EzyProcessor.processWithException(com.tvd12.ezyfox.util.EzyProcessor.processWithException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) IOException(java.io.IOException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException)

Example 3 with EzyFuture

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);
        }
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) MaxResourceDownloadCapacity(com.tvd12.ezyhttp.core.exception.MaxResourceDownloadCapacity) EzyFuture(com.tvd12.ezyfox.concurrent.EzyFuture)

Example 4 with EzyFuture

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);
}
Also used : EzyFutureTask(com.tvd12.ezyfox.concurrent.EzyFutureTask) EzyFuture(com.tvd12.ezyfox.concurrent.EzyFuture)

Example 5 with EzyFuture

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();
    }
}
Also used : Request(com.tvd12.ezyhttp.client.request.Request) DownloadRequest(com.tvd12.ezyhttp.client.request.DownloadRequest) EzyFuture(com.tvd12.ezyfox.concurrent.EzyFuture)

Aggregations

EzyFuture (com.tvd12.ezyfox.concurrent.EzyFuture)6 EzyProcessor.processWithException (com.tvd12.ezyfox.util.EzyProcessor.processWithException)3 ClientNotActiveException (com.tvd12.ezyhttp.client.exception.ClientNotActiveException)3 RequestQueueFullException (com.tvd12.ezyhttp.client.exception.RequestQueueFullException)3 IOException (java.io.IOException)3 EzyFutureTask (com.tvd12.ezyfox.concurrent.EzyFutureTask)2 DownloadRequest (com.tvd12.ezyhttp.client.request.DownloadRequest)2 Request (com.tvd12.ezyhttp.client.request.Request)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 RequestFutureTask (com.tvd12.ezyhttp.client.concurrent.RequestFutureTask)1 MaxResourceDownloadCapacity (com.tvd12.ezyhttp.core.exception.MaxResourceDownloadCapacity)1 MaxResourceUploadCapacity (com.tvd12.ezyhttp.core.exception.MaxResourceUploadCapacity)1 MaxUploadSizeException (com.tvd12.ezyhttp.core.exception.MaxUploadSizeException)1 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)1