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.alfresco.extension.bulkfilesystemimport.impl.BulkFilesystemImporterThreadPoolExecutor.java

public BulkFilesystemImporterThreadPoolExecutor(final int corePoolSize, final int maximumPoolSize,
        final long keepAliveTime, final TimeUnit keepAliveTimeUnit, final int blockingQueueSize) {
    super(corePoolSize <= 0 ? DEFAULT_CORE_POOL_SIZE : corePoolSize,
            maximumPoolSize <= 0 ? DEFAULT_MAXIMUM_CORE_POOL_SIZE : maximumPoolSize,
            keepAliveTime <= 0 ? DEFAULT_KEEP_ALIVE_TIME : keepAliveTime,
            keepAliveTimeUnit == null ? DEFAULT_KEEP_ALIVE_TIME_UNIT : keepAliveTimeUnit,
            new LinkedBlockingQueue<Runnable>(
                    blockingQueueSize <= 0 ? DEFAULT_BLOCKING_QUEUE_SIZE : blockingQueueSize),
            new BulkFilesystemImporterThreadFactory());

    // This won't work, since it allows for out-of-order execution
    // It also doesn't allow the thread pool to be cleanly shutdown - see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
    //        this.setRejectedExecutionHandler(new CallerRunsPolicy());

    if (log.isDebugEnabled())
        log.debug("Creating new bulk import thread pool." + "\n\tcorePoolSize = " + corePoolSize
                + "\n\tmaximumPoolSize = " + maximumPoolSize + "\n\tkeepAliveTime = " + keepAliveTime + " "
                + String.valueOf(keepAliveTimeUnit) + "\n\tblockingQueueSize = " + blockingQueueSize);
}

From source file:com.dbay.apns4j.impl.ApnsServiceImpl.java

private ApnsServiceImpl(ApnsConfig config, ErrorProcessHandler errorProcessHandler)
        throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException,
        CertificateException, CertificateExpiredException, IOException {
    this.name = config.getName();
    int poolSize = config.getPoolSize();
    // this.service = Executors.newFixedThreadPool(poolSize);
    this.service = new ThreadPoolExecutor(poolSize, poolSize, 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(queueSize));

    SocketFactory factory = ApnsTools.createSocketFactory(config.getKeyStore(), config.getPassword(),
            KEYSTORE_TYPE, ALGORITHM, PROTOCOL);
    this.connPool = ApnsConnectionPool.newConnPool(config, factory, errorProcessHandler);
    this.feedbackConn = new ApnsFeedbackConnectionImpl(config, factory);
}

From source file:org.yamj.core.service.ArtworkProcessScheduler.java

@Scheduled(initialDelay = 30000, fixedDelay = 60000)
public void processArtwork() throws Exception {
    int maxThreads = configService.getIntProperty("yamj3.scheduler.artworkprocess.maxThreads", 1);
    if (maxThreads <= 0) {
        if (!messageDisabled) {
            messageDisabled = Boolean.TRUE;
            LOG.info("Artwork processing is disabled");
        }/*ww  w .  jav a  2  s  .  c  o  m*/
        return;
    } else {
        messageDisabled = Boolean.FALSE;
    }

    int maxResults = configService.getIntProperty("yamj3.scheduler.artworkprocess.maxResults", 20);
    List<QueueDTO> queueElements = artworkStorageService.getArtworLocatedQueue(maxResults);
    if (CollectionUtils.isEmpty(queueElements)) {
        LOG.debug("No artwork found to process");
        return;
    }

    LOG.info("Found {} artwork objects to process; process with {} threads", queueElements.size(), maxThreads);
    BlockingQueue<QueueDTO> queue = new LinkedBlockingQueue<QueueDTO>(queueElements);

    ExecutorService executor = Executors.newFixedThreadPool(maxThreads);
    for (int i = 0; i < maxThreads; i++) {
        ArtworkProcessRunner worker = new ArtworkProcessRunner(queue, artworkProcessorService);
        executor.execute(worker);
    }
    executor.shutdown();

    // run until all workers have finished
    while (!executor.isTerminated()) {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException ignore) {
        }
    }

    LOG.debug("Finished artwork processing");
}

From source file:com.amazonaws.mobileconnectors.pinpoint.internal.event.EventRecorder.java

public static EventRecorder newInstance(final PinpointContext pinpointContext, final PinpointDBUtil dbUtil) {
    final ExecutorService submissionRunnableQueue = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(MAX_EVENT_OPERATIONS), new ThreadPoolExecutor.DiscardPolicy());
    return new EventRecorder(pinpointContext, dbUtil, submissionRunnableQueue);
}

From source file:com.alibaba.otter.shared.communication.core.impl.DefaultCommunicationClientImpl.java

public void initial() {
    RejectedExecutionHandler handler = null;
    if (discard) {
        handler = new ThreadPoolExecutor.DiscardPolicy();
    } else {//from   w  w  w . j a  v a 2  s . co  m
        handler = new ThreadPoolExecutor.AbortPolicy();
    }

    executor = new ThreadPoolExecutor(poolSize, poolSize, 60 * 1000L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(10 * 1000), new NamedThreadFactory("communication-async"),
            handler);
}

From source file:gobblin.service.StreamingKafkaSpecExecutorInstanceConsumer.java

public StreamingKafkaSpecExecutorInstanceConsumer(Config config, MutableJobCatalog jobCatalog,
        Optional<Logger> log) {
    super(config, log);
    String topic = config.getString(SPEC_KAFKA_TOPICS_KEY);
    Config defaults = ConfigFactory.parseMap(ImmutableMap.of(AvroJobSpecKafkaJobMonitor.TOPIC_KEY, topic,
            KafkaJobMonitor.KAFKA_AUTO_OFFSET_RESET_KEY, KafkaJobMonitor.KAFKA_AUTO_OFFSET_RESET_SMALLEST));

    try {//from  ww  w .j  a  v a2  s  .  c o m
        _jobMonitor = (AvroJobSpecKafkaJobMonitor) (new AvroJobSpecKafkaJobMonitor.Factory())
                .forConfig(config.withFallback(defaults), jobCatalog);
    } catch (IOException e) {
        throw new RuntimeException("Could not create job monitor", e);
    }

    _jobSpecQueue = new LinkedBlockingQueue<>(ConfigUtils.getInt(config, "SPEC_STREAMING_BLOCKING_QUEUE_SIZE",
            DEFAULT_SPEC_STREAMING_BLOCKING_QUEUE_SIZE));

    // listener will add job specs to a blocking queue to send to callers of changedSpecs()
    jobCatalog.addListener(new JobSpecListener());
}

From source file:com.amazonaws.kinesis.dataviz.twitter.TwitterProducer.java

public void run(String propFilePath) {
    loadFileProperties(propFilePath, DEFAULT_PROP_FILE_NAME);

    String consumerKey = System.getProperty(TWIT_CONSUMER_KEY);
    String consumerSecret = System.getProperty(TWIT_CONSUMER_SECRET);
    String token = System.getProperty(TWIT_TOKEN);
    String secret = System.getProperty(TWIT_SECRET);
    String streamName = System.getProperty(STREAM_NAME);
    String regionName = System.getProperty(REGION_NAME);

    while (true) {
        /**/* w  w w.  j av a2  s.c o m*/
         * Set up your blocking queues: Be sure to size these properly based
         * on expected TPS of your stream
         */
        BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>(10000);

        /**
         * Declare the host you want to connect to, the endpoint, and
         * authentication (basic auth or oauth)
         */
        StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();

        // Track  anything that is geo-tagged
        endpoint.addQueryParameter("locations", "-180,-90,180,90");

        // These secrets should be read from a config file
        Authentication hosebirdAuth = new OAuth1(consumerKey, consumerSecret, token, secret);

        // create a new basic client - by default gzip is enabled
        Client client = new ClientBuilder().hosts(Constants.STREAM_HOST).endpoint(endpoint)
                .authentication(hosebirdAuth).processor(new StringDelimitedProcessor(msgQueue)).build();

        client.connect();

        LOG.info("Got connection to Twitter");

        // create producer
        ProducerClient producer = new ProducerBuilder().withName("Twitter").withStreamName(streamName)
                .withRegion(regionName).withThreads(10).build();

        producer.connect();

        LOG.info("Got connection to Kinesis");

        try {
            if (process(msgQueue, producer)) {
                break;
            }

        } catch (Exception e) {
            // if we get here, our client has broken, throw away and
            // recreate
            e.printStackTrace();
        }
        // also, if we make it here, we have had a problem, so start again
        client.stop();
    }
}

From source file:nuclei.task.TaskPool.java

TaskPool(Looper mainLooper, final String name, int maxThreads, List<TaskInterceptor> interceptors) {
    this.name = name;
    TASK_POOLS.put(name, this);
    this.interceptors = interceptors;
    handler = new Handler(mainLooper, this);
    BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(128);
    taskRunnablePool = new Pools.SimplePool<>(maxThreads);
    taskQueues = new Pools.SimplePool<>(10);
    maxThreads = Math.max(CORE_POOL_SIZE, maxThreads);
    poolExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE, maxThreads, 1, TimeUnit.SECONDS, workQueue,
            new ThreadFactory() {
                private final AtomicInteger mCount = new AtomicInteger(1);

                @Override/*  w  w  w .ja  v a 2s.  c  o  m*/
                public Thread newThread(@NonNull Runnable r) {
                    return new Thread(r, name + " #" + mCount.incrementAndGet());
                }
            });

    if (LISTENER != null)
        LISTENER.onCreated(this);
}

From source file:com.haulmont.cuba.core.app.ClusterManager.java

@PostConstruct
protected void init() {
    int nThreads = clusterConfig.getClusterMessageSendingThreadPoolSize();
    executor = new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(clusterConfig.getClusterMessageSendingQueueCapacity()),
            new ThreadFactoryBuilder().setNameFormat("ClusterManagerMessageSender-%d").build(),
            new RejectedExecutionHandler() {
                @Override/*from   w w w.  j av  a  2 s  .com*/
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    SendMessageRunnable sendMessageRunnable = (SendMessageRunnable) r;
                    log.info("Queue capacity is exceeded. Message: {}: {}",
                            sendMessageRunnable.message.getClass(), sendMessageRunnable.message);
                }
            });
}