Example usage for org.apache.http.nio.reactor IOReactorStatus INACTIVE

List of usage examples for org.apache.http.nio.reactor IOReactorStatus INACTIVE

Introduction

In this page you can find the example usage for org.apache.http.nio.reactor IOReactorStatus INACTIVE.

Prototype

IOReactorStatus INACTIVE

To view the source code for org.apache.http.nio.reactor IOReactorStatus INACTIVE.

Click Source Link

Document

The reactor is inactive / has not been started

Usage

From source file:com.google.acre.script.NHttpClient.java

public void wait_on_result(long time, TimeUnit tunit, AcreResponse res) throws NHttpException {

    if (_reactor != null && _reactor.getStatus() != IOReactorStatus.INACTIVE) {
        throw new NHttpException("Can not run wait_on_results while it is already running [current status: "
                + _reactor.getStatus() + "]");
    }/*  w ww.j a v  a 2 s .  co m*/

    start();

    long endtime = System.currentTimeMillis() + tunit.toMillis(time);

    int i = 0;
    while (_requests.size() > 0) {
        long pass_start_time = System.currentTimeMillis();

        if (i > _requests.size() - 1)
            i = 0;

        if (time != -1L && endtime <= System.currentTimeMillis()) {
            // If we've run out of time, kill off the reactor to
            // close our open connections, and throw an exception
            // to let the caller know we're out of time
            // 
            // XXX should probably use a real exception here
            try {
                _requests.clear();
                _reactor.shutdown();
                throw new NHttpTimeoutException();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        NHttpClientClosure closure = _requests.get(i);
        if (!closure.done() && closure.sreq() == null && _connection_lock.availablePermits() > 0) {
            closure.sreq(closure.connect(_proxy, _reactor));
        }

        if (closure.done()) {
            if (closure.response() != null) {
                closure.callback().finished(closure.url(), closure.response());
            } else if (closure.exceptions().size() > 0) {
                closure.exceptions().get(0).printStackTrace();
                closure.callback().error(closure.url(), new NHttpException(closure.exceptions().get(0)));
            } else {
                closure.callback().error(closure.url(), new NHttpException("Request failed"));
            }
            _requests.remove(i);
            continue;
        } else if (closure.timeout() != 0
                && (closure.start_time() + closure.timeout()) <= System.currentTimeMillis()) {
            // We pass a null response to the callback to let it know
            // that things broke
            if (closure.sreq() != null) {
                closure.sreq().cancel();
                _connection_lock.release();
            }

            closure.callback().error(closure.url(), new NHttpTimeoutException());
            _requests.remove(i);
            continue;
        }

        // Block for 15ms to not eat all the cpu
        try {
            Thread.sleep(15);
        } catch (InterruptedException e) {
            // pass
        }

        _costCollector.collect("auub", System.currentTimeMillis() - pass_start_time);
        i++;
    }

    // It's somewhat questionable to stop the reactor when we're done
    // but we can restart it if we need it again.
    stop();
}