Example usage for java.lang AutoCloseable close

List of usage examples for java.lang AutoCloseable close

Introduction

In this page you can find the example usage for java.lang AutoCloseable close.

Prototype

void close() throws Exception;

Source Link

Document

Closes this resource, relinquishing any underlying resources.

Usage

From source file:com.joyent.manta.client.MantaClient.java

@Override
public void close() {
    if (this.closed) {
        return;//from  w w  w . j ava2 s  . c  o  m
    }

    this.closed = true;

    final List<Exception> exceptions = new ArrayList<>();

    /* We explicitly close all streams that may have been opened when
     * this class (MantaClient) is closed. This helps to alleviate problems
     * where resources haven't been closed properly. In particular, this
     * is useful for the streamingIterator() method that returns an
     * iterator that must be closed after consumption. */
    for (AutoCloseable closeable : danglingStreams) {
        try {
            if (closeable == null) {
                continue;
            }

            closeable.close();
        } catch (InterruptedException ie) {
            /* Do nothing, but we won't capture the interrupted exception
             * because even if we are interrupted, we want to close all open
             * resources. */
        } catch (Exception e) {
            exceptions.add(e);
        }
    }

    try {
        this.httpHelper.close();
    } catch (InterruptedException ie) {
        /* Do nothing, but we won't capture the interrupted exception
         * because even if we are interrupted, we want to close all open
         * resources. */
    } catch (Exception e) {
        exceptions.add(e);
    }

    // Deregister associated MBeans
    try {
        if (this.agent != null) {
            this.agent.close();
        }
    } catch (Exception e) {
        exceptions.add(e);
    }

    try {
        this.config.close();
    } catch (final Exception e) {
        exceptions.add(e);
    }

    // Shut down the ForkJoinPool that may be executing find() operations
    try {
        this.findForkJoinPool.shutdownNow();
    } catch (Exception e) {
        exceptions.add(e);
    }

    if (!exceptions.isEmpty()) {
        String msg = "At least one exception was thrown when performing close()";
        OnCloseAggregateException exception = new OnCloseAggregateException(msg);

        exceptions.forEach(exception::aggregateException);

        throw exception;
    }
}