Example usage for java.util.concurrent LinkedBlockingQueue LinkedBlockingQueue

List of usage examples for java.util.concurrent LinkedBlockingQueue LinkedBlockingQueue

Introduction

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

Prototype

public LinkedBlockingQueue() 

Source Link

Document

Creates a LinkedBlockingQueue with a capacity of Integer#MAX_VALUE .

Usage

From source file:com.miraclelinux.historygluon.HistoryStreamer.java

private HistoryStreamer() {
    m_log = LogFactory.getLog(HistoryStreamer.class);
    m_queue = new LinkedBlockingQueue<HistoryStreamElement>();
    m_semaphore = new Semaphore(1);
    m_semaphore.acquireUninterruptibly(); // This is never blocked.
    m_working_sem = new Semaphore(1);
}

From source file:com.datatorrent.lib.bucket.AbstractBucketManager.java

public AbstractBucketManager() {
    eventQueue = new LinkedBlockingQueue<Long>();
    evictionCandidates = Sets.newHashSet();
    dirtyBuckets = Maps.newConcurrentMap();
    bucketHeap = MinMaxPriorityQueue.orderedBy(new Comparator<AbstractBucket<T>>() {
        @Override/*from   w w  w.j  a  v  a  2s .  c  o m*/
        public int compare(AbstractBucket<T> bucket1, AbstractBucket<T> bucket2) {
            if (bucket1.lastUpdateTime() < bucket2.lastUpdateTime()) {
                return -1;
            }
            if (bucket1.lastUpdateTime() > bucket2.lastUpdateTime()) {
                return 1;
            }
            return 0;
        }

    }).create();
    lock = new Lock();
    committedWindow = -1;

    noOfBuckets = DEF_NUM_BUCKETS;
    noOfBucketsInMemory = DEF_NUM_BUCKETS_MEM;
    maxNoOfBucketsInMemory = DEF_NUM_BUCKETS_MEM + 100;
    millisPreventingBucketEviction = DEF_MILLIS_PREVENTING_EVICTION;
    writeEventKeysOnly = true;
    bucketsToDelete = Sets.newHashSet();
}

From source file:com.microsoft.live.unittest.DownloadTest.java

@Override
protected void setUp() throws Exception {
    super.setUp();

    this.responseQueue = new LinkedBlockingQueue<LiveDownloadOperation>();
    this.queueingListener = new DownloadOperationQueueingListener(this.exceptionQueue, this.responseQueue);
}

From source file:com.connection.factory.FtpConnectionApacheLib.java

@Override
public void readAllFilesWalkingPathWithListener(FileListener listener, String remotePath) {
    // List<RemoteFileObject> willReturnObject = new ArrayList<>();
    Queue<RemoteFileObject> directorylist = new LinkedBlockingQueue<>();
    RemoteFileObject object = null;/*  w w w  . j  ava  2s .  c o  m*/
    object = new FtpApacheFileObject(FileInfoEnum.DIRECTORY);
    object.setDirectPath(remotePath);
    directorylist.add(object);
    try {
        while (!directorylist.isEmpty()) {
            object = directorylist.poll();
            FTPFile[] fileListTemp = _ftpObj.listFiles(object.getPath());
            for (FTPFile each : fileListTemp) {
                RemoteFileObject objectTemp = null;
                if (each.isDirectory()) {
                    objectTemp = new FtpApacheFileObject(FileInfoEnum.DIRECTORY);
                    objectTemp.setFileName(each.getName());
                    objectTemp.setAbsolutePath(object.getPath());
                    directorylist.add(objectTemp);
                } else if (each.isFile()) {
                    objectTemp = new FtpApacheFileObject(FileInfoEnum.FILE);
                    objectTemp.setFileName(each.getName());
                    objectTemp.setAbsolutePath(object.getPath());
                    objectTemp.setFileSize(each.getSize());
                    objectTemp.setFileType();
                    objectTemp.setDate(each.getTimestamp().getTime());
                    listener.handleRemoteFile(object);
                }
            }
            object = null;
            fileListTemp = null;
        }

    } catch (IOException | ConnectionException ex) {
        //    return null;
    }
    //  return willReturnObject;

    //  return willReturnObject;
}

From source file:com.microsoft.live.unittest.DeleteTest.java

@Override
protected void setUp() throws Exception {
    super.setUp();

    this.calendarId = "calendar.013123";
    this.responseQueue = new LinkedBlockingQueue<LiveOperation>();
    this.queueingListener = new OperationQueueingListener(this.exceptionQueue, this.responseQueue);
}

From source file:com.alibaba.otter.node.etl.common.pipe.impl.http.archive.ArchiveBean.java

/**
 * /*from   ww  w  .  j ava  2s . co m*/
 */
@SuppressWarnings("resource")
private boolean doPack(final File targetArchiveFile, List<FileData> fileDatas,
        final ArchiveRetriverCallback<FileData> callback) {
    // ?
    if (true == targetArchiveFile.exists() && false == NioUtils.delete(targetArchiveFile, 3)) {
        throw new ArchiveException(
                String.format("[%s] exist and delete failed", targetArchiveFile.getAbsolutePath()));
    }

    boolean exist = false;
    ZipOutputStream zipOut = null;
    Set<String> entryNames = new HashSet<String>();
    BlockingQueue<Future<ArchiveEntry>> queue = new LinkedBlockingQueue<Future<ArchiveEntry>>(); // ?
    ExecutorCompletionService completionService = new ExecutorCompletionService(executor, queue);

    final File targetDir = new File(targetArchiveFile.getParentFile(),
            FilenameUtils.getBaseName(targetArchiveFile.getPath()));
    try {
        // 
        FileUtils.forceMkdir(targetDir);

        zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(targetArchiveFile)));
        zipOut.setLevel(Deflater.BEST_SPEED);
        // ??
        for (final FileData fileData : fileDatas) {
            if (fileData.getEventType().isDelete()) {
                continue; // delete??
            }

            String namespace = fileData.getNameSpace();
            String path = fileData.getPath();
            boolean isLocal = StringUtils.isBlank(namespace);
            String entryName = null;
            if (true == isLocal) {
                entryName = FilenameUtils.getPath(path) + FilenameUtils.getName(path);
            } else {
                entryName = namespace + File.separator + path;
            }

            // ????
            if (entryNames.contains(entryName) == false) {
                entryNames.add(entryName);
            } else {
                continue;
            }

            final String name = entryName;
            if (true == isLocal && !useLocalFileMutliThread) {
                // ??
                queue.add(new DummyFuture(new ArchiveEntry(name, callback.retrive(fileData))));
            } else {
                completionService.submit(new Callable<ArchiveEntry>() {

                    public ArchiveEntry call() throws Exception {
                        // ??
                        InputStream input = null;
                        OutputStream output = null;
                        try {
                            input = callback.retrive(fileData);

                            if (input instanceof LazyFileInputStream) {
                                input = ((LazyFileInputStream) input).getInputSteam();// ?stream
                            }

                            if (input != null) {
                                File tmp = new File(targetDir, name);
                                NioUtils.create(tmp.getParentFile(), false, 3);// ?
                                output = new FileOutputStream(tmp);
                                NioUtils.copy(input, output);// ?
                                return new ArchiveEntry(name, new File(targetDir, name));
                            } else {
                                return new ArchiveEntry(name);
                            }
                        } finally {
                            IOUtils.closeQuietly(input);
                            IOUtils.closeQuietly(output);
                        }
                    }
                });
            }
        }

        for (int i = 0; i < entryNames.size(); i++) {
            // ?
            ArchiveEntry input = null;
            InputStream stream = null;
            try {
                input = queue.take().get();
                if (input == null) {
                    continue;
                }

                stream = input.getStream();
                if (stream == null) {
                    continue;
                }

                if (stream instanceof LazyFileInputStream) {
                    stream = ((LazyFileInputStream) stream).getInputSteam();// ?stream
                }

                exist = true;
                zipOut.putNextEntry(new ZipEntry(input.getName()));
                NioUtils.copy(stream, zipOut);// ?
                zipOut.closeEntry();
            } finally {
                IOUtils.closeQuietly(stream);
            }
        }

        if (exist) {
            zipOut.finish();
        }
    } catch (Exception e) {
        throw new ArchiveException(e);
    } finally {
        IOUtils.closeQuietly(zipOut);
        try {
            FileUtils.deleteDirectory(targetDir);// 
        } catch (IOException e) {
            // ignore
        }
    }

    return exist;
}

From source file:org.alfresco.solr.client.SOLRAPIClientTest.java

@Override
public void setUp() throws Exception {
    if (client == null) {
        TenantService tenantService = new SingleTServiceImpl();

        dictionaryDAO = new DictionaryDAOImpl();
        NamespaceDAO namespaceDAO = dictionaryDAO;
        dictionaryDAO.setTenantService(tenantService);

        CompiledModelsCache compiledModelsCache = new CompiledModelsCache();
        compiledModelsCache.setDictionaryDAO(dictionaryDAO);
        compiledModelsCache.setTenantService(tenantService);
        compiledModelsCache.setRegistry(new DefaultAsynchronouslyRefreshedCacheRegistry());
        TraceableThreadFactory threadFactory = new TraceableThreadFactory();
        threadFactory.setThreadDaemon(true);
        threadFactory.setThreadPriority(Thread.NORM_PRIORITY);

        ThreadPoolExecutor threadPoolExecutor = new DynamicallySizedThreadPoolExecutor(20, 20, 90,
                TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory,
                new ThreadPoolExecutor.CallerRunsPolicy());
        compiledModelsCache.setThreadPoolExecutor(threadPoolExecutor);
        dictionaryDAO.setDictionaryRegistryCache(compiledModelsCache);
        // TODO: use config ....
        dictionaryDAO.setDefaultAnalyserResourceBundleName("alfresco/model/dataTypeAnalyzers");
        dictionaryDAO.setResourceClassLoader(getResourceClassLoader());
        dictionaryDAO.init();//w ww .  ja  v  a2  s .c om

        DictionaryComponent dictionaryComponent = new DictionaryComponent();
        dictionaryComponent.setDictionaryDAO(dictionaryDAO);
        dictionaryComponent.setMessageLookup(new StaticMessageLookup());

        // cmis dictionary
        CMISMapping cmisMapping = new CMISMapping();
        cmisMapping.setCmisVersion(CmisVersion.CMIS_1_0);
        DictionaryNamespaceComponent namespaceService = new DictionaryNamespaceComponent();
        namespaceService.setNamespaceDAO(namespaceDAO);
        cmisMapping.setNamespaceService(namespaceService);
        cmisMapping.setDictionaryService(dictionaryComponent);
        cmisMapping.afterPropertiesSet();

        cmisDictionaryService = new CMISStrictDictionaryService();
        cmisDictionaryService.setCmisMapping(cmisMapping);
        cmisDictionaryService.setDictionaryService(dictionaryComponent);
        cmisDictionaryService.setDictionaryDAO(dictionaryDAO);
        cmisDictionaryService.setSingletonCache(new MemoryCache<String, CMISDictionaryRegistry>());
        cmisDictionaryService.setTenantService(tenantService);
        cmisDictionaryService.init();

        RuntimePropertyLuceneBuilderMapping luceneBuilderMapping = new RuntimePropertyLuceneBuilderMapping();
        luceneBuilderMapping.setDictionaryService(dictionaryComponent);
        luceneBuilderMapping.setCmisDictionaryService(cmisDictionaryService);
        cmisDictionaryService.setPropertyLuceneBuilderMapping(luceneBuilderMapping);

        luceneBuilderMapping.afterPropertiesSet();

        // Load the key store from the classpath
        ClasspathKeyResourceLoader keyResourceLoader = new ClasspathKeyResourceLoader();
        client = new SOLRAPIClient(getRepoClient(keyResourceLoader), dictionaryComponent, dictionaryDAO);
        trackModels();
    }
}

From source file:org.psit.transwatcher.TransWatcher.java

private void startImageDownloaderQueue(final String cardIP) {
    if (downLoadQueue != null)
        downLoadQueue.interrupt();/*from   ww w .j ava 2s  .  c  om*/

    downLoadQueue = new Thread(new Runnable() {

        @Override
        public void run() {
            queue = new LinkedBlockingQueue<String>();
            InputStream input = null;
            FileOutputStream output = null;
            try {
                while (true) {
                    String fileName = queue.take();
                    setState(State.DOWNLOADING);
                    File file = new File(fileName);
                    String req = "/cgi-bin/wifi_download?fn=" + file.getName() + "&fd=" + file.getParent()
                            + "/";
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet("http://" + cardIP + req);
                    HttpResponse response = httpclient.execute(httpGet);

                    input = response.getEntity().getContent();
                    output = new FileOutputStream(fileDestinationPrefix + file.getName());
                    byte[] buffer = new byte[1024];

                    for (int length; (length = input.read(buffer)) > 0;) {
                        output.write(buffer, 0, length);
                    }
                    notifyDownload(fileDestinationPrefix + file.getName());
                    notifyMessage(file.getName() + " downloaded");
                    setState(State.LISTENING);
                }
            } catch (InterruptedException ex) {
                notifyMessage("Downloadqueue stopped.");
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (output != null)
                    try {
                        output.close();
                    } catch (IOException logOrIgnore) {
                    }
                if (input != null)
                    try {
                        input.close();
                    } catch (IOException logOrIgnore) {
                    }
            }
        }

    }, "ImageDownloaderQueue");

    downLoadQueue.start();
}

From source file:info.raack.appliancelabeler.machinelearning.appliancedetection.algorithms.ApplianceEnergyConsumptionDetectionAlgorithm.java

protected Queue<EnergyTimestep> getEnergyTimesteps(Calendar firstMeasurementDate,
        Calendar lastMeasurementDate) {
    Queue<EnergyTimestep> timesteps = new LinkedBlockingQueue<EnergyTimestep>();

    Calendar startDate = dateUtils.getPreviousFiveMinuteIncrement(firstMeasurementDate);
    Calendar endDate = dateUtils.getPreviousFiveMinuteIncrement(lastMeasurementDate);

    while (startDate.getTimeInMillis() < endDate.getTimeInMillis()) {
        EnergyTimestep timestep = new EnergyTimestep();

        timestep.setStartTime(startDate.getTime());

        startDate.add(Calendar.MINUTE, 5);
        startDate.add(Calendar.SECOND, -1);
        timestep.setEndTime(startDate.getTime());

        startDate.add(Calendar.SECOND, 1);

        //logger.debug("Adding timestep " + timestep.getStartTime() + " - " + timestep.getEndTime());
        timesteps.add(timestep);/*from   w w  w .jav a 2 s  .c o m*/
    }

    return timesteps;
}

From source file:com.akop.bach.ImageCache.java

private ImageCache() {
    mContext = App.getInstance();// ww w  . jav a2  s .c  o  m
    mTasks = new LinkedBlockingQueue<Task>();
    mListeners = new HashSet<OnImageReadyListener>();
    mCurrentTask = null;
    mCacheDir = mContext.getCacheDir();
    mSdCacheDir = null;
    mSdImageDir = null;

    try {
        if (android.os.Build.VERSION.SDK_INT >= 8) {
            Api8Helper helper = new Api8Helper();

            mSdCacheDir = helper.getExternalCacheDir();
            mSdImageDir = helper.getExternalPictureDir();
        } else {
            mSdCacheDir = new File(Environment.getExternalStorageDirectory(),
                    "/Android/data/" + mContext.getPackageName() + "/cache");

            mSdImageDir = new File(Environment.getExternalStorageDirectory(),
                    "/Android/data/" + mContext.getPackageName() + "/files/Pictures");
        }
    } catch (Exception e) {
        if (App.getConfig().logToConsole())
            e.printStackTrace();
    }

    mThread = new Thread(this);
    mThread.start();
}