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(Collection<? extends E> c) 

Source Link

Document

Creates a LinkedBlockingQueue with a capacity of Integer#MAX_VALUE , initially containing the elements of the given collection, added in traversal order of the collection's iterator.

Usage

From source file:org.jasig.cas.util.http.SimpleHttpClientFactoryBean.java

/**
 * Build a {@link FutureRequestExecutionService} from the current properties and a HTTP client.
 *
 * @param httpClient the provided HTTP client
 * @return the built request executor service
 *//*from  w w  w . j a  v a  2s  .  co  m*/
private FutureRequestExecutionService buildRequestExecutorService(final CloseableHttpClient httpClient) {

    final ExecutorService definedExecutorService;
    // no executor service provided -> create a default one
    if (this.executorService == null) {
        definedExecutorService = new ThreadPoolExecutor(this.threadsNumber, this.threadsNumber, 0L,
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(this.queueSize));
    } else {
        definedExecutorService = this.executorService;
    }

    return new FutureRequestExecutionService(httpClient, definedExecutorService);
}

From source file:com.safi.asterisk.handler.SafletEngine.java

private void initThreadPool() {
    //     loadPoolProperties();
    if (log.isDebugEnabled())
        log.debug("Starting threadpool executor with poolsize " + fastAgiMaxPoolsize + " and queuesize "
                + fastAgiPoolsize);// www.j ava  2  s  .com
    threadPool = new ThreadPoolExecutor(fastAgiMaxPoolsize, fastAgiMaxPoolsize, 0L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(fastAgiMaxPoolsize + fastAgiPoolsize));
}

From source file:com.ebay.jetstream.messaging.transport.zookeeper.ZooKeeperTransport.java

/**
 * Method connects to zookeeper emsemble and register the context to the
 * transport//w  w  w.  ja va 2  s.co  m
 */
public void init(TransportConfig transportConfig, NICUsage nicUsage, DNSMap dnsMap, MessageServiceProxy proxy)
        throws Exception {

    if (m_initialized.get()) {
        return;
    }

    //in case , if transport is re-initialised, flipping shutdown flag
    m_shutdown.set(false);
    m_transportConfig = transportConfig;

    if (transportConfig instanceof ZooKeeperTransportConfig) {
        ZooKeeperTransportConfig config = (ZooKeeperTransportConfig) transportConfig;
        m_cxnWaitInMillis = config.getCxnWaitInMillis();
        m_retrycount = config.getRetrycount();
        m_retrywaitTimeInMillis = config.getRetryWaitTimeInMillis();
        m_sessionTimeoutInMillis = config.getSessionTimeoutInMillis();
        m_numZKnodes = config.getZknodes().size();

        m_NettyDiscoverableTopics.addAll(config.getNettyDiscoveryProtocolTopics());

        m_requestQueueDepth = config.getRequestQueueDepth();

        if (!config.getZknodes().isEmpty()) {
            m_connectionStr = prepareConnectString(config.getZknodes());
        } else {
            throw new Exception("ZooKeeper Servers are NOT configured");
        }

        m_cnxnBalanceIntervalInMs = config.getCnxnBalanceIntervalInHrs() * 3600 * 1000;
        m_pingerNotConnectedThreshold = config.getPingerCnxnLostThreshold();

        pingerTask = new PingerTask();
        MessageServiceTimer.sInstance().getTimer().scheduleAtFixedRate(pingerTask,
                (long) config.getPingerIntervalInMins() * 60 * 1000,
                (long) config.getPingerIntervalInMins() * 60 * 1000);

    }

    m_requestQueue = new LinkedBlockingQueue<Request>(m_requestQueueDepth);
    LOGGER.info("Registering Context -" + m_context);
    if (getZKHandle() == null) {
        connect();
    }

    createContext(m_context);

    Management.addBean((new StringBuilder()).append("MessageService/Transport/ZooKeeper/context/")
            .append(m_context).append("/stats").toString(), new TransportStatsController(this));

    Management.addBean((new StringBuilder()).append("MessageService/Transport/ZooKeeper/context/")
            .append(m_context).append("/registry").toString(), this);

    m_serviceProxy = proxy;
    m_reqWorker = new Thread(this, "ZKTransportRequestWorker");
    m_reqWorker.start();
    m_initialized.set(true);
}

From source file:com.amazonaws.services.simpleworkflow.flow.worker.GenericWorker.java

@Override
public void start() {
    if (log.isInfoEnabled()) {
        log.info("start: " + toString());
    }//  ww w.ja  v  a2 s .c o m
    checkStarted();
    checkRequiredProperty(service, "service");
    checkRequiredProperty(domain, "domain");
    checkRequiredProperty(taskListToPoll, "taskListToPoll");
    checkRequredProperties();

    if (registerDomain) {
        registerDomain();
    }

    if (!disableTypeRegitrationOnStart) {
        registerTypesToPoll();
    }

    if (maximumPollRatePerSecond > 0.0) {
        pollRateThrottler = new Throttler("pollRateThrottler " + taskListToPoll, maximumPollRatePerSecond,
                maximumPollRateIntervalMilliseconds);
    }

    pollExecutor = new ThreadPoolExecutor(pollThreadCount, pollThreadCount, 1, TimeUnit.MINUTES,
            new LinkedBlockingQueue<Runnable>(pollThreadCount));
    ExecutorThreadFactory pollExecutorThreadFactory = getExecutorThreadFactory();
    pollExecutor.setThreadFactory(pollExecutorThreadFactory);

    pollBackoffThrottler = new BackoffThrottler(pollBackoffInitialInterval, pollBackoffMaximumInterval,
            pollBackoffCoefficient);
    poller = createPoller();
    for (int i = 0; i < pollThreadCount; i++) {
        pollExecutor.execute(new PollServiceTask(poller));
    }
}

From source file:com.serphacker.serposcope.task.google.GoogleTask.java

protected void initializeSearches() {
    List<GoogleSearch> searchList;
    if (updateRun) {
        searchList = googleDB.search.listUnchecked(run.getId());
    } else if (run.getGroup() != null) {
        Collection<Integer> groups = new ArrayList<Integer>();
        groups.add(run.getGroup().getId());
        searchList = googleDB.search.listByGroup(groups);
    } else {//from  w ww  .j a v a2 s.c o m
        searchList = googleDB.search.list();
    }
    if (shuffle) {
        Collections.shuffle(searchList);
    }
    searches = new LinkedBlockingQueue<>(searchList);
    LOG.info("{} searches to do", searches.size());
}

From source file:org.opennms.newts.gsod.ImportRunner.java

private Observable<Boolean> parMap(Observable<List<Sample>> samples, MetricRegistry metrics,
        Func1<List<Sample>, Boolean> insert) {

    final Timer waitTime = metrics.timer("wait-time");

    @SuppressWarnings("serial")
    final BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(
            m_maxThreadQueueSize == 0 ? m_threadCount * 3 : m_maxThreadQueueSize) {

        @Override//from  www  .j  a v  a2  s.c o m
        public boolean offer(Runnable r) {
            try (Context time = waitTime.time()) {
                this.put(r);
                return true;
            } catch (InterruptedException e) {
                throw Exceptions.propagate(e);
            }
        }

        @Override
        public boolean add(Runnable r) {
            try (Context time = waitTime.time()) {
                this.put(r);
                return true;
            } catch (InterruptedException e) {
                throw Exceptions.propagate(e);
            }
        }

    };
    final ThreadPoolExecutor executor = new ThreadPoolExecutor(m_threadCount, m_threadCount, 0L,
            TimeUnit.MILLISECONDS, workQueue);

    metrics.register("active-threads", new Gauge<Integer>() {

        @Override
        public Integer getValue() {
            return executor.getActiveCount();
        }

    });

    metrics.register("pool-size", new Gauge<Integer>() {

        @Override
        public Integer getValue() {
            return executor.getPoolSize();
        }

    });
    metrics.register("largest-pool-size", new Gauge<Integer>() {

        @Override
        public Integer getValue() {
            return executor.getLargestPoolSize();
        }

    });

    metrics.register("work-queue-size", new Gauge<Integer>() {

        @Override
        public Integer getValue() {
            return workQueue.size();
        }

    });

    return parMap(samples, executor, metrics, insert);
}

From source file:com.groksolutions.grok.mobile.service.GrokDataSyncService.java

/**
 * Loads metric data from the server/*  w  w w  .  j  a v  a 2s.  c om*/
 *
 * @param metricId (optional) The metric Id to get the data. If metricId is {@code null} then
 *                 loads data for all metrics at once.
 * @param from     return records from this date
 * @param to       return records up to this date
 * @see HTMClient#getMetricData
 */
private void loadMetricData(final String metricId, final long from, final long to)
        throws HTMException, IOException {

    if (getClient() == null) {
        Log.w(TAG, "Not connected to any server yet");
        return;
    }
    final CoreDatabase database = HTMITApplication.getDatabase();

    // Blocking queue holding metric data waiting to be saved to the
    // database. This queue will be filled by the HTMClient as it downloads
    // the metric data and it will be emptied by the databaseTask as is
    // saves the data to the database
    final LinkedBlockingQueue<MetricData> pending = new LinkedBlockingQueue<>(
            MAX_PENDING_METRIC_DATA_IO_BUFFER);

    // Background task used save metric data to the database. This task will
    // wait for metric data to arrive from the server and save them to the
    // database in batches until it finds the end of the queue marked by
    // METRIC_DATA_EOF or it times out after 60 seconds
    final Future<?> databaseTask = getService().getIOThreadPool().submit(new Runnable() {
        @Override
        public void run() {

            // Make the batch size 1 hour for all metrics or one week for
            // single metric
            int batchSize = metricId == null ? DataUtils.MILLIS_PER_HOUR : 24 * 7 * DataUtils.MILLIS_PER_HOUR;

            // Save metrics in batches, 24 hours at the time
            final List<MetricData> batch = new ArrayList<>();

            // Tracks batch timestamp. Once the metric timestamp is greater
            // than the batch timestamp, a new batch is created
            long batchTimestamp = 0;

            try {
                // Process all pending metric data until the METRIC_DATA_EOF
                // is found or a timeout is reached
                MetricData metricData;
                while ((metricData = pending.poll(60, TimeUnit.SECONDS)) != METRIC_DATA_EOF
                        && metricData != null) {
                    // Add metric data to batch regardless of the timestamp.
                    // At this point we may receive stale metric data with
                    // lower timestamp after we receive the latest data with
                    // the current timestamp. As a side effect, you may see
                    // gaps in the data as described in MER-1524
                    batch.add(metricData);
                    // Process batches
                    if (metricData.getTimestamp() > batchTimestamp) {
                        // Calculate next batch timestamp
                        batchTimestamp = metricData.getTimestamp() + batchSize;
                        if (database.addMetricDataBatch(batch)) {
                            Log.d(TAG, "Saving " + batch.size() + " new records");
                            // Notify receivers new data has arrived
                            fireMetricDataChangedEvent();
                        }
                        batch.clear();
                    }
                }
                // Last batch
                if (!batch.isEmpty()) {
                    if (database.addMetricDataBatch(batch)) {
                        Log.d(TAG, "Received " + batch.size() + " records");
                        // Notify receivers new data has arrived
                        fireMetricDataChangedEvent();
                    }
                }
            } catch (InterruptedException e) {
                Log.w(TAG, "Interrupted while loading metric data");
            }
        }
    });

    try {
        // Get new data from server
        getClient().getMetricData(metricId, new Date(from), new Date(to),
                new HTMClient.DataCallback<MetricData>() {
                    @Override
                    public boolean onData(MetricData metricData) {
                        // enqueue data for saving
                        try {
                            Metric metric = database.getMetric(metricData.getMetricId());
                            if (metric == null) {
                                Log.w(TAG, "Received data for unknown metric:" + metricData.getMetricId());
                                return true;
                            }
                            pending.put(metricData);
                        } catch (InterruptedException e) {
                            pending.clear();
                            Log.w(TAG, "Interrupted while loading metric data");
                            return false;
                        }
                        return true;
                    }
                });
        // Mark the end of the records
        pending.add(METRIC_DATA_EOF);
        // Wait for the database task to complete
        databaseTask.get();
    } catch (InterruptedException e) {
        Log.w(TAG, "Interrupted while loading metric data");
    } catch (ExecutionException e) {
        Log.e(TAG, "Failed to load metric data", e);
    }
}

From source file:com.YOMPsolutions.YOMP.mobile.service.YOMPDataSyncService.java

/**
 * Loads metric data from the server/*www.  j ava 2s  . co m*/
 *
 * @param metricId (optional) The metric Id to get the data. If metricId is {@code null} then
 *                 loads data for all metrics at once.
 * @param from     return records from this date
 * @param to       return records up to this date
 * @see com.numenta.core.service.YOMPClient#getMetricData
 */
private void loadMetricData(final String metricId, final long from, final long to)
        throws YOMPException, IOException {

    if (getClient() == null) {
        Log.w(TAG, "Not connected to any server yet");
        return;
    }
    final CoreDatabase database = YOMPApplication.getDatabase();

    // Blocking queue holding metric data waiting to be saved to the
    // database. This queue will be filled by the YOMPClient as it downloads
    // the metric data and it will be emptied by the databaseTask as is
    // saves the data to the database
    final LinkedBlockingQueue<MetricData> pending = new LinkedBlockingQueue<>(
            MAX_PENDING_METRIC_DATA_IO_BUFFER);

    // Background task used save metric data to the database. This task will
    // wait for metric data to arrive from the server and save them to the
    // database in batches until it finds the end of the queue marked by
    // METRIC_DATA_EOF or it times out after 60 seconds
    final Future<?> databaseTask = getService().getIOThreadPool().submit(new Runnable() {
        @Override
        public void run() {

            // Make the batch size 1 hour for all metrics or one week for
            // single metric
            int batchSize = metricId == null ? DataUtils.MILLIS_PER_HOUR : 24 * 7 * DataUtils.MILLIS_PER_HOUR;

            // Save metrics in batches, 24 hours at the time
            final List<MetricData> batch = new ArrayList<>();

            // Tracks batch timestamp. Once the metric timestamp is greater
            // than the batch timestamp, a new batch is created
            long batchTimestamp = 0;

            try {
                // Process all pending metric data until the METRIC_DATA_EOF
                // is found or a timeout is reached
                MetricData metricData;
                while ((metricData = pending.poll(60, TimeUnit.SECONDS)) != METRIC_DATA_EOF
                        && metricData != null) {
                    // Add metric data to batch regardless of the timestamp.
                    // At this point we may receive stale metric data with
                    // lower timestamp after we receive the latest data with
                    // the current timestamp. As a side effect, you may see
                    // gaps in the data as described in MER-1524
                    batch.add(metricData);
                    // Process batches
                    if (metricData.getTimestamp() > batchTimestamp) {
                        // Calculate next batch timestamp
                        batchTimestamp = metricData.getTimestamp() + batchSize;
                        if (database.addMetricDataBatch(batch)) {
                            Log.d(TAG, "Saving " + batch.size() + " new records");
                            // Notify receivers new data has arrived
                            fireMetricDataChangedEvent();
                        }
                        batch.clear();
                    }
                }
                // Last batch
                if (!batch.isEmpty()) {
                    if (database.addMetricDataBatch(batch)) {
                        Log.d(TAG, "Received " + batch.size() + " records");
                        // Notify receivers new data has arrived
                        fireMetricDataChangedEvent();
                    }
                }
            } catch (InterruptedException e) {
                Log.w(TAG, "Interrupted while loading metric data");
            }
        }
    });

    try {
        // Get new data from server
        getClient().getMetricData(metricId, new Date(from), new Date(to),
                new YOMPClient.DataCallback<MetricData>() {
                    @Override
                    public boolean onData(MetricData metricData) {
                        // enqueue data for saving
                        try {
                            Metric metric = database.getMetric(metricData.getMetricId());
                            if (metric == null) {
                                Log.w(TAG, "Received data for unknown metric:" + metricData.getMetricId());
                                return true;
                            }
                            pending.put(metricData);
                        } catch (InterruptedException e) {
                            pending.clear();
                            Log.w(TAG, "Interrupted while loading metric data");
                            return false;
                        }
                        return true;
                    }
                });
        // Mark the end of the records
        pending.add(METRIC_DATA_EOF);
        // Wait for the database task to complete
        databaseTask.get();
    } catch (InterruptedException e) {
        Log.w(TAG, "Interrupted while loading metric data");
    } catch (ExecutionException e) {
        Log.e(TAG, "Failed to load metric data", e);
    }
}

From source file:com.inmobi.messaging.consumer.databus.AbstractMessagingDatabusConsumer.java

@Override
protected void doReset() throws IOException {
    // restart the service, consumer will start streaming from the last saved
    // checkpoint
    close();//from w  w w  . j  av  a2  s  .c om
    currentCheckpoint.read(checkpointProvider, getChkpointKey());
    LOG.info("Resetting to checkpoint:" + currentCheckpoint);
    buffer = new LinkedBlockingQueue<QueueEntry>(bufferSize);
    start();
}