Example usage for java.util.concurrent ArrayBlockingQueue ArrayBlockingQueue

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

Introduction

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

Prototype

public ArrayBlockingQueue(int capacity) 

Source Link

Document

Creates an ArrayBlockingQueue with the given (fixed) capacity and default access policy.

Usage

From source file:ubic.gemma.loader.association.NCBIGene2GOAssociationLoader.java

/**
 * @param inputStream/*from  ww  w .j av a 2s  .c om*/
 */
public void load(final InputStream inputStream) {
    final BlockingQueue<Gene2GOAssociation> queue = new ArrayBlockingQueue<Gene2GOAssociation>(QUEUE_SIZE);
    final SecurityContext context = SecurityContextHolder.getContext();
    final Authentication authentication = context.getAuthentication();

    Thread loadThread = new Thread(new Runnable() {
        @Override
        public void run() {
            log.info("Starting loading");
            SecurityContextHolder.setContext(context);
            load(queue);
        }
    });

    loadThread.start();

    Thread parseThread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                // NCBIGene2GOAssociationParser parser = new NCBIGene2GOAssociationParser();
                SecurityContextHolder.getContext().setAuthentication(authentication);
                parser.parse(inputStream, queue);
                setCount(parser.getCount());
            } catch (IOException e) {
                log.error(e, e);
                throw new RuntimeException(e);
            }
            log.info("Done parsing");
            producerDone.set(true);
        }
    });

    parseThread.start();

    while (!isProducerDone() || !isConsumerDone()) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.taobao.tddl.common.sync.RowBasedReplicater.java

public void init() {
    /**//from www  .  j  av a  2 s  .  c o  m
     * CallerRunsPolicy: execute
     */
    replicationExecutor = new ThreadPoolExecutor(threadPoolSize, threadPoolSize, 0L, TimeUnit.MILLISECONDS,
            new ArrayBlockingQueue<Runnable>(workQueueSize), new ThreadPoolExecutor.CallerRunsPolicy());

    /**
     * LogDiscardPolicy
     */
    deleteSyncLogExecutor = new ThreadPoolExecutor(1, 2, 0L, TimeUnit.MILLISECONDS,
            new ArrayBlockingQueue<Runnable>(10), new RejectedExecutionHandler() {
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    logger.warn("A DeleteSyncLogTask discarded");
                }
            });
    updateSyncLogExecutor = new ThreadPoolExecutor(1, 2, 0L, TimeUnit.MILLISECONDS,
            new ArrayBlockingQueue<Runnable>(10), new RejectedExecutionHandler() {
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    logger.warn("A UpdateSyncLogTask discarded");
                }
            });

    /**
     * 
     */
    final BucketTaker<RowBasedReplicationContext> deleteBucketTaker = new BucketTaker<RowBasedReplicationContext>(
            deleteSyncLogExecutor) {
        @Override
        public Runnable createTakeAwayTask(Collection<RowBasedReplicationContext> list) {
            return new DeleteSyncLogTask(list);
        }

    };
    final BucketTaker<RowBasedReplicationContext> updateBucketTaker = new BucketTaker<RowBasedReplicationContext>(
            updateSyncLogExecutor) {

        @Override
        public Runnable createTakeAwayTask(Collection<RowBasedReplicationContext> list) {
            return new UpdateSyncLogTask(list);
        }

    };
    deleteBucketSwitcher = new NoStrictBucketSwitcher<RowBasedReplicationContext>(deleteBucketTaker,
            DEFAULT_BATCH_DELETE_SIZE);
    updateBucketSwitcher = new NoStrictBucketSwitcher<RowBasedReplicationContext>(updateBucketTaker,
            DEFAULT_BATCH_UPDATE_SIZE);

    TDDLMBeanServer.registerMBean(this, "Replicater"); //JMX
}

From source file:org.opencredo.couchdb.inbound.CouchDbAllDocumentsMessageSource.java

/**
 * Creates an instance with a default database to connect to.
 * //from   w  w w  .ja  v a2 s .  com
 * @throws URISyntaxException
 */
public CouchDbAllDocumentsMessageSource(String databaseUrl, int limit) throws URISyntaxException {
    this.couchDbDocumentOperations = new CouchDbDocumentTemplate(databaseUrl);
    this.limit = limit;
    this.toBeReceived = new ArrayBlockingQueue<URI>(limit);
    this.databaseUri = new URI(databaseUrl);
    int ind = databaseUrl.indexOf("/_all_docs");
    if (ind != -1) {
        this.baseUri = databaseUrl.substring(0, ind);
    } else {
        ind = databaseUrl.indexOf("/_design/");
        if (ind != -1) {
            this.baseUri = databaseUrl.substring(0, ind);
        } else {
            throw new IllegalArgumentException(
                    "databaseUrl must be a /_design/../_view/... or a /_all_docs URL");
        }
    }
}

From source file:cf.randers.scd.CommandLineInterface.java

private void run() {
    if (params == null)
        return;// w ww  . j av a  2s. co  m
    LOGGER.info("Making temp dir...");
    File tmpDir = new File("tmp/");
    File outDir = new File(outputDirectory);
    //noinspection ResultOfMethodCallIgnored
    tmpDir.mkdirs();
    //noinspection ResultOfMethodCallIgnored
    outDir.mkdirs();
    BlockingQueue<Runnable> tasks = new ArrayBlockingQueue<>(params.size());
    maximumConcurrentConnections = Math.min(params.size(),
            maximumConcurrentConnections > params.size() ? params.size() : maximumConcurrentConnections);
    ThreadPoolExecutor executor = new ThreadPoolExecutor(maximumConcurrentConnections,
            maximumConcurrentConnections, 0, TimeUnit.NANOSECONDS, tasks);
    LOGGER.info("Starting to execute " + params.size() + " thread(s)...");
    for (String param : params) {
        executor.execute(() -> {
            LOGGER.info("Started thread for " + param);
            Map json;
            byte[] artworkBytes = new byte[0];
            List<Track> toProcess = new ArrayList<>();
            LOGGER.info("Resolving and querying track info...");
            try (CloseableHttpClient client = HttpClients.createDefault();
                    CloseableHttpResponse response = client.execute(new HttpGet(new URIBuilder()
                            .setScheme("https").setHost("api.soundcloud.com").setPath("/resolve")
                            .addParameter("url", param).addParameter("client_id", clientID).build()));
                    InputStreamReader inputStreamReader = new InputStreamReader(
                            response.getEntity().getContent())) {

                final int bufferSize = 1024;
                final char[] buffer = new char[bufferSize];
                final StringBuilder out = new StringBuilder();
                for (;;) {
                    int rsz = inputStreamReader.read(buffer, 0, buffer.length);
                    if (rsz < 0)
                        break;
                    out.append(buffer, 0, rsz);
                }
                String rawJson = out.toString();
                Album a = new Gson().fromJson(rawJson, Album.class);

                if (a.getTrackCount() == null) {
                    Track tr = new Gson().fromJson(rawJson, Track.class);
                    toProcess.add(tr);
                }
                toProcess.addAll(a.getTracks());
                EntityUtils.consumeQuietly(response.getEntity());
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
            for (Track track : toProcess) {
                System.out.println(track.getId());
                System.out.println(track.getTitle());
            }
            for (Track track : toProcess) {
                LOGGER.info("Downloading mp3 to file...");
                File tmpFile = new File("tmp/" + String.format("%d", track.getId()) + ".mp3");

                try (CloseableHttpClient client = HttpClients.createDefault();
                        CloseableHttpResponse response = client
                                .execute(new HttpGet(track.getStreamUrl() + "?client_id=" + clientID))) {
                    IOUtils.copy(response.getEntity().getContent(), new FileOutputStream(tmpFile));
                    EntityUtils.consumeQuietly(response.getEntity());
                } catch (Exception e) {
                    e.printStackTrace();
                    return;
                }

                boolean hasArtwork = track.getArtworkUrl() != null;

                if (hasArtwork) {
                    LOGGER.info("Downloading artwork jpg into memory...");
                    try (CloseableHttpClient client = HttpClients.createDefault();
                            CloseableHttpResponse response = client.execute(
                                    new HttpGet(track.getArtworkUrl().replace("-large.jpg", "-t500x500.jpg")
                                            + "?client_id=" + clientID))) {
                        artworkBytes = IOUtils.toByteArray(response.getEntity().getContent());
                        EntityUtils.consumeQuietly(response.getEntity());
                    } catch (Exception e) {
                        e.printStackTrace();
                        return;
                    }
                }

                try {
                    LOGGER.info("Reading temp file into AudioFile object...");
                    // Read audio file from tmp directory
                    AudioFile audioFile = AudioFileIO.read(tmpFile);

                    // Set Artwork
                    Tag tag = audioFile.getTagAndConvertOrCreateAndSetDefault();
                    if (hasArtwork) {
                        StandardArtwork artwork = new StandardArtwork();
                        artwork.setBinaryData(artworkBytes);
                        artwork.setImageFromData();
                        tag.addField(artwork);
                    }
                    tag.addField(FieldKey.TITLE, track.getTitle());
                    tag.addField(FieldKey.ARTIST, track.getUser().getUsername());
                    LOGGER.info("Saving audio file...");
                    System.out.println(
                            outDir.getAbsolutePath() + "/" + String.format(outputformat, track.getId()));
                    new AudioFileIO().writeFile(audioFile,
                            outDir.getAbsolutePath() + "/" + String.format(outputformat, track.getId()));
                    tmpFile.deleteOnExit();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            File[] listFiles = tmpDir.listFiles();
            if (listFiles == null) {
                return;
            }
            for (File file : listFiles) {
                file.delete();
            }
        });
    }
    executor.shutdown();
}

From source file:com.kurento.kmf.media.PointerDetectorFilterTest.java

/**
 * Test if a {@link PointerDetectorFilter} can be created in the KMS. The
 * filter is pipelined with a {@link PlayerEndpoint}, which feeds video to
 * the filter. This test depends on the correct behaviour of the player and
 * its events.//from   w w  w .  j  a va 2 s. com
 * 
 * @throws InterruptedException
 */
@Test
public void testPointerDetectorFilter() throws InterruptedException {
    player.connect(filter);

    filter.addWindow(new PointerDetectorWindowMediaParam("goal", 50, 50, 150, 150));
    final BlockingQueue<WindowInEvent> events = new ArrayBlockingQueue<WindowInEvent>(1);
    filter.addWindowInListener(new MediaEventListener<WindowInEvent>() {
        @Override
        public void onEvent(WindowInEvent event) {
            events.add(event);
        }
    });

    player.play();

    Assert.assertNotNull(events.poll(20, SECONDS));

    player.stop();
}

From source file:com.turn.griffin.data.GriffinUploadTask.java

private BitSet getAvailableBitmap(FileInfo fileInfo) {

    String filename = fileInfo.getFilename();
    long fileVersion = fileInfo.getVersion();
    long blockCount = fileInfo.getBlockCount();

    Optional<GriffinConsumer> consumer = Optional.absent();
    BitSet availableBlockBitmap = new BitSet((int) blockCount);
    try {/*ww w  . j  a  v a2s .c  o m*/
        BlockingQueue<byte[]> dataQueue = new ArrayBlockingQueue<>(
                GriffinDownloadTask.DOWNLOAD_CONSUMER_QUEUE_SIZE);
        Properties properties = new Properties();
        properties.put("auto.offset.reset", "smallest");

        /* The groupId should be unique to avoid conflict with other consumers running on this machine */
        String consumerGroupId = GriffinKafkaTopicNameUtil.getDataTopicConsumerGroupId(filename, fileVersion,
                new String[] { dataManager.getMyServerId(), this.getClass().getSimpleName(),
                        UUID.randomUUID().toString() });
        String dataTopicNameForConsumer = GriffinKafkaTopicNameUtil.getDataTopicNameForConsumer(filename,
                fileVersion);

        consumer = Optional.fromNullable(new GriffinConsumer(GriffinModule.ZOOKEEPER, consumerGroupId,
                dataTopicNameForConsumer, GriffinDownloadTask.DOWNLOAD_THREAD_COUNT, properties, dataQueue));

        /* TODO: Change this to a better bitmap (Check out RoaringBitmap) */
        while (availableBlockBitmap.nextClearBit(0) != blockCount) {
            Optional<byte[]> message = Optional.fromNullable(dataQueue
                    .poll(GriffinLeaderSelectionTask.LEADER_SELECTION_PERIOD_MS, TimeUnit.MILLISECONDS));
            if (!message.isPresent()) {
                /* We know how much of the file is available in Kafka */
                break;
            }
            DataMessage dataMessage = DataMessage.parseFrom(message.get());
            availableBlockBitmap.set((int) dataMessage.getBlockSeqNo());
        }
    } catch (Exception e) {
        logger.warn(String.format("Unable to download file %s to get available bitmap ", filename), e);
        /* Work with whatever information we have gathered till now */
    } finally {
        if (consumer.isPresent()) {
            consumer.get().shutdown(true);
        }
    }

    return availableBlockBitmap;
}

From source file:com.predic8.membrane.core.interceptor.schemavalidation.AbstractXMLSchemaValidator.java

public AbstractXMLSchemaValidator(ResolverMap resourceResolver, String location,
        ValidatorInterceptor.FailureHandler failureHandler, boolean skipFaults) throws Exception {
    this.location = location;
    this.resourceResolver = resourceResolver;
    this.failureHandler = failureHandler;
    this.skipFaults = skipFaults;
    int concurrency = Runtime.getRuntime().availableProcessors() * 2;
    validators = new ArrayBlockingQueue<List<Validator>>(concurrency);
    for (int i = 0; i < concurrency; i++)
        validators.add(createValidators());
    xopr = new XOPReconstitutor();
}

From source file:com.slytechs.utils.work.JobWorker.java

/**
 * Constructor for JobWorker that uses its own private job queue
 * @param name ResolvableName of the worker and execution thread
 *//*  w w  w.j a  v  a 2 s .  c om*/
public JobWorker(String name) {
    this.thread = new WorkerThread(name, this);

    queue = new ArrayBlockingQueue<Job>(JOB_QUEUE_SIZE);
}

From source file:com.optimizely.ab.event.AsyncEventHandler.java

public AsyncEventHandler(int queueCapacity, int numWorkers, int maxConnections, int connectionsPerRoute,
        int validateAfter, long closeTimeout, TimeUnit closeTimeoutUnit) {
    if (queueCapacity <= 0) {
        throw new IllegalArgumentException("queue capacity must be > 0");
    }/*from   ww  w .ja va 2 s  .  c om*/

    this.httpClient = OptimizelyHttpClient.builder().withMaxTotalConnections(maxConnections)
            .withMaxPerRoute(connectionsPerRoute).withValidateAfterInactivity(validateAfter).build();

    this.workerExecutor = new ThreadPoolExecutor(numWorkers, numWorkers, 0L, TimeUnit.MILLISECONDS,
            new ArrayBlockingQueue<Runnable>(queueCapacity),
            new NamedThreadFactory("optimizely-event-dispatcher-thread-%s", true));

    this.closeTimeout = closeTimeout;
    this.closeTimeoutUnit = closeTimeoutUnit;
}