Example usage for java.util.concurrent ExecutorCompletionService take

List of usage examples for java.util.concurrent ExecutorCompletionService take

Introduction

In this page you can find the example usage for java.util.concurrent ExecutorCompletionService take.

Prototype

public Future<V> take() throws InterruptedException 

Source Link

Usage

From source file:org.geoserver.bkprst.RestoreTask.java

@Override
public void run() {

    // If previous' backup info cannot be read, aborts the restore
    // Writes info about backup in a file
    BackupTask backupInfo = this.readBackupInfo(this.path);
    if (backupInfo == null) {
        LOGGER.severe("Backup data info were not written properly, the restore will not start");
        this.state = BrTaskState.FAILED;
        return;/*from www . j  ava  2s.co  m*/
    }

    // Sets up the filter to exclude some directories according to the previous backup info
    IOFileFilter excludeFilter = this.getExcludeFilter(backupInfo.includeData, backupInfo.includeGwc,
            backupInfo.includeLog);

    // Sets up source and destination
    File srcMount = new File(this.path);
    File trgMount = this.dataRoot.root();

    // Sets transaction
    this.trans = new RestoreTransaction(this, srcMount, trgMount, excludeFilter);

    try {
        // Start transanction
        this.trans.start();
        if (checkForHalt()) {
            return;
        }

        // Sets up the copy task
        ExecutorService ex = Executors.newFixedThreadPool(2);
        if (ex == null || ex.isTerminated()) {
            throw new IllegalArgumentException(
                    "Unable to run asynchronously using a terminated or null ThreadPoolExecutor");
        }
        ExecutorCompletionService<File> cs = new ExecutorCompletionService<File>(ex);

        this.act = new CopyTree(excludeFilter, cs, srcMount, trgMount);
        this.act.addCopyListener(new DefaultProgress(this.id.toString()) {
            public void onUpdateProgress(float percent) {
                super.onUpdateProgress(percent);
                progress = percent;
            }
        });

        // Starts restore
        int workSize = this.act.copy();
        LOGGER.info("Restore " + this.id + " has started");
        this.startTime = new Date();
        this.state = BrTaskState.RUNNING;

        // This is to keep track af restore advancement
        while (workSize-- > 0) {
            Future<File> future = cs.take();
            try {
                LOGGER.info("copied file: " + future.get());
            } catch (ExecutionException e) {

                LOGGER.log(Level.INFO, e.getLocalizedMessage(), e);
            }
            if (checkForHalt()) {
                ex.shutdown();
                if (!ex.awaitTermination(5, TimeUnit.SECONDS)) {
                    throw new RuntimeException("Unable to stop backup task");
                }
                return;
            }
        }

        // Restore completed
        this.trans.commit();

        // reload the config from disk
        getGeoServer().reload();
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);

        // In case of errors, rollback
        this.trans.rollback();
    } finally {
        haltSemaphore.release();
    }
}