Example usage for java.util.concurrent LinkedBlockingDeque putFirst

List of usage examples for java.util.concurrent LinkedBlockingDeque putFirst

Introduction

In this page you can find the example usage for java.util.concurrent LinkedBlockingDeque putFirst.

Prototype

public void putFirst(E e) throws InterruptedException 

Source Link

Usage

From source file:org.commoncrawl.service.listcrawler.DataTransferAgent.java

static Thread startTransferThread(final int threadIndex, final CCBridgeServerMapping mapping,
        final File shutdownFile, final FileSystem fs, final Configuration conf,
        final LinkedBlockingDeque<ProxyTransferItem> itemQueue, final EventLoop eventLoop,
        final Semaphore shutdownSemaphore) {
    Thread thread = new Thread(new Runnable() {

        @Override//  w  ww  . j  av  a2  s.  c o m
        public void run() {
            try {
                while (true) {
                    if (shutdownFile.exists()) {
                        LOG.info("Exiting due to shutdown file existense!");
                        break;
                    }
                    ProxyTransferItem item = itemQueue.take();

                    if (item.hdfsFilePath == null) {
                        LOG.info("Transfer Thread:" + Thread.currentThread().getId() + " Exiting");
                    } else {
                        try {
                            LOG.info("Transfer Thread:" + threadIndex + " for Host:" + mapping._internalName
                                    + " Transferring File:" + item.hdfsFilePath);
                            int result = uploadSingeFile(mapping, fs, conf, item.hdfsFilePath, item.uploadName,
                                    eventLoop);
                            if (result == 200) {
                                LOG.info("Transfer Thread:" + threadIndex + "for Host:" + mapping._internalName
                                        + " Done Transferring File:" + item.hdfsFilePath);
                                //item.logFilePath.createNewFile();
                            } else if (result == 409) {
                                LOG.info("Transfer Thread:" + threadIndex + "for Host:" + mapping._internalName
                                        + " File Already Exists for Path:" + item.hdfsFilePath);
                                //item.logFilePath.createNewFile();
                            } else {
                                LOG.error("Transfer Thread:" + threadIndex + "for Host:" + mapping._internalName
                                        + " File Transfer Failed with Error:" + result + " for Path:"
                                        + item.hdfsFilePath);
                                itemQueue.putFirst(item);
                            }
                        } catch (IOException e) {
                            LOG.error("Transfer Failed for Thread:" + threadIndex + "Host:"
                                    + mapping._internalName + " File: " + item.hdfsFilePath);
                            LOG.fatal(CCStringUtils.stringifyException(e));
                        }
                    }
                }
            } catch (InterruptedException e) {
            } finally {
                shutdownSemaphore.release();
            }
        }

    });
    thread.start();
    return thread;
}