Search in sources :

Example 6 with EzyFuture

use of com.tvd12.ezyfox.concurrent.EzyFuture in project ezyhttp by youngmonkeys.

the class HttpClientProxy method request.

public ResponseEntity request(Request request, int timeout) throws Exception {
    EzyFuture future = new EzyFutureTask();
    futures.addFuture(request, future);
    try {
        addRequest(request);
    } catch (Exception e) {
        futures.removeFuture(request);
        throw e;
    }
    return future.get(timeout);
}
Also used : EzyFutureTask(com.tvd12.ezyfox.concurrent.EzyFutureTask) 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 7 with EzyFuture

use of com.tvd12.ezyfox.concurrent.EzyFuture in project ezyhttp by youngmonkeys.

the class ResourceUploadManager method loop.

private void loop() {
    byte[] buffer = new byte[bufferSize];
    while (active) {
        Entry entry = null;
        boolean done = true;
        boolean isMaxUploaded = false;
        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 (entry.increaseUploadedSize(read)) {
                if (read > 0) {
                    outputStream.write(buffer, 0, read);
                    done = false;
                }
            } else {
                isMaxUploaded = true;
            }
        } catch (Exception e) {
            exception = e;
            logger.info("upload error", e);
        } catch (Throwable e) {
            exception = new IllegalStateException(e);
            logger.info("upload fatal error", e);
        }
        if (entry == null) {
            continue;
        }
        if (isMaxUploaded) {
            exception = new MaxUploadSizeException(entry.maxUploadSize);
        }
        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 MaxResourceUploadCapacity(capacity));
                    }
                }
            }
        } catch (Throwable e) {
            logger.info("handle upload result error", e);
        }
    }
}
Also used : MaxResourceUploadCapacity(com.tvd12.ezyhttp.core.exception.MaxResourceUploadCapacity) InputStream(java.io.InputStream) MaxUploadSizeException(com.tvd12.ezyhttp.core.exception.MaxUploadSizeException) OutputStream(java.io.OutputStream) MaxUploadSizeException(com.tvd12.ezyhttp.core.exception.MaxUploadSizeException)

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