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:it.infn.ct.futuregateway.apiserver.inframanager.MonitorQueue.java

/**
 * Build a monitor queue.//from w  w  w  .j  ava2s  .  c  om
 * A monitor queue is made of two elements: queue and monitor thread. Both
 * are created.
 *
 * @param bufferSize  A queue to store the activities
 * @param threadPoolSize A thread pool for the monitoring
 * @param checkInterval The minimum interval between checks in milliseconds
 */
public MonitorQueue(final int bufferSize, final int threadPoolSize, final int checkInterval) {
    log.debug("Creating the monitor queue");
    monitorQueue = new ArrayBlockingQueue<>(bufferSize);
    log.debug("Creating the monitor thread pool");
    monitorPool = Executors.newFixedThreadPool(threadPoolSize);
    for (int i = 0; i < threadPoolSize; i++) {
        monitorPool.submit(new Monitor(monitorQueue, checkInterval));
    }
}

From source file:org.apache.ranger.audit.provider.AsyncAuditProvider.java

public AsyncAuditProvider(String name, int maxQueueSize, int maxFlushInterval) {
    LOG.info("AsyncAuditProvider(" + name + "): creating..");

    if (maxQueueSize < 1) {
        LOG.warn("AsyncAuditProvider(" + name + "): invalid maxQueueSize=" + maxQueueSize
                + ". will use default " + mMaxQueueSize);

        maxQueueSize = mMaxQueueSize;//from  ww w.  j  av  a 2 s  .c  o  m
    }

    mName = name;
    mMaxQueueSize = maxQueueSize;
    mMaxFlushInterval = maxFlushInterval;

    mQueue = new ArrayBlockingQueue<AuditEventBase>(mMaxQueueSize);
}

From source file:com.graphaware.importer.data.access.QueueDbDataReader.java

/**
 * {@inheritDoc}/*ww w .  j av  a 2  s  .c o  m*/
 */
@Override
public final void read(final String query, final String hint) {
    if (records != null) {
        throw new IllegalStateException("Previous reader hasn't been closed");
    }

    LOG.info("Start query: \n" + query);

    if (query.startsWith("alter")) {
        jdbcTemplate.execute(query);
        noMoreRecords = true;
        return;
    }

    records = new ArrayBlockingQueue<>(queueCapacity());

    new Thread(new Runnable() {
        @Override
        public void run() {
            Date d1 = Calendar.getInstance().getTime();

            try {
                jdbcTemplate.query(query, new ResultSetExtractor<Void>() {
                    @Override
                    public Void extractData(ResultSet rs) throws SQLException, DataAccessException {
                        ResultSetMetaData metaData = rs.getMetaData();
                        int colCount = metaData.getColumnCount();

                        while (rs.next()) {
                            Map<String, String> columns = new HashMap<>();
                            for (int i = 1; i <= colCount; i++) {
                                columns.put(metaData.getColumnLabel(i), rs.getString(i));
                            }
                            columns.put(ROW, String.valueOf(rs.getRow()));

                            try {
                                records.offer(columns, 1, TimeUnit.HOURS);
                            } catch (InterruptedException e) {
                                LOG.warn(
                                        "Was waiting for more than 1 hour to insert a record for processing, had to drop it");
                            }
                        }

                        return null;
                    }
                });
            } finally {
                noMoreRecords = true;
            }

            long diffInSeconds = TimeUnit.MILLISECONDS
                    .toSeconds(Calendar.getInstance().getTime().getTime() - d1.getTime());

            LOG.info("Finished querying for " + hint + " in " + diffInSeconds + " seconds");
        }
    }, "DB READER - " + hint).start();
}

From source file:uk.dsxt.voting.common.utils.web.JettyRunner.java

public static Server run(ResourceConfig application, Properties properties, int port, String originFilter,
        String aliasName, File keystoreFile, String password, String frontendRoot, String apiPathPattern,
        boolean copyWebDir) {
    try {/*from  w  w w  .ja v a  2  s  .c o  m*/
        QueuedThreadPool threadPool = new QueuedThreadPool(
                Integer.valueOf(properties.getProperty("jetty.maxThreads")),
                Integer.valueOf(properties.getProperty("jetty.minThreads")),
                Integer.valueOf(properties.getProperty("jetty.idleTimeout")),
                new ArrayBlockingQueue<>(Integer.valueOf(properties.getProperty("jetty.maxQueueSize"))));
        Server server = new Server(threadPool);
        HttpConfiguration config = new HttpConfiguration();

        if (keystoreFile != null) {
            log.info("Jetty runner {}. SSL enabled.", application.getClass());
            SslContextFactory sslFactory = new SslContextFactory();
            sslFactory.setCertAlias(aliasName);

            String path = keystoreFile.getAbsolutePath();
            if (!keystoreFile.exists()) {
                log.error("Couldn't load keystore file: {}", path);
                return null;
            }
            sslFactory.setKeyStorePath(path);
            sslFactory.setKeyStorePassword(password);
            sslFactory.setKeyManagerPassword(password);
            sslFactory.setTrustStorePath(path);
            sslFactory.setTrustStorePassword(password);

            config.setSecureScheme("https");
            config.setSecurePort(port);
            config.addCustomizer(new SecureRequestCustomizer());

            ServerConnector https = new ServerConnector(server,
                    new SslConnectionFactory(sslFactory, "http/1.1"), new HttpConnectionFactory(config));
            https.setPort(port);
            server.setConnectors(new Connector[] { https });
        } else {
            ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(config));
            http.setPort(port);
            server.setConnectors(new Connector[] { http });
        }

        Handler handler = ContainerFactory.createContainer(JettyHttpContainer.class, application);
        if (originFilter != null)
            handler = new CrossDomainFilter(handler, originFilter);
        if (frontendRoot != null) {
            WebAppContext htmlHandler = new WebAppContext();
            htmlHandler.setResourceBase(frontendRoot);
            htmlHandler.setCopyWebDir(copyWebDir);
            Map<Pattern, Handler> pathToHandler = new HashMap<>();
            pathToHandler.put(Pattern.compile(apiPathPattern), handler);

            SessionManager sm = new HashSessionManager();
            SessionHandler sh = new SessionHandler(sm);
            htmlHandler.setSessionHandler(sh);

            DefaultServlet defaultServlet = new DefaultServlet();
            ServletHolder holder = new ServletHolder(defaultServlet);
            holder.setInitParameter("useFileMappedBuffer", Boolean.toString(!copyWebDir));
            holder.setInitParameter("cacheControl", "no-store,no-cache,must-revalidate,max-age=-1,public");
            htmlHandler.addServlet(holder, "/");

            handler = new RequestsRouter(htmlHandler, pathToHandler, frontendRoot);
        }
        server.setHandler(handler);
        server.start();

        while (!server.isStarted()) {
            Thread.sleep(50);
        }
        log.info("Jetty server started {} on port {}", application.getClass(), port);
        return server;
    } catch (Exception e) {
        log.error(String.format("Jetty start failed %s.", application.getClass()), e);
        return null;
    }
}

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

/**
 * Checks that the getUrl method does not return an empty string
 * // w w  w.j av  a  2  s  .c o  m
 * @throws InterruptedException
 */
@Test
public void testMethodGetUrl() throws InterruptedException {
    final BlockingQueue<String> events = new ArrayBlockingQueue<String>(1);
    httpEp.getUrl(new Continuation<String>() {

        @Override
        public void onSuccess(String result) {
            events.add(result);
        }

        @Override
        public void onError(Throwable cause) {
            throw new KurentoMediaFrameworkException(cause);
        }
    });

    String url = events.poll(500, MILLISECONDS);
    Assert.assertTrue(!(url == null || url.isEmpty()));
}

From source file:ubic.gemma.apps.ExpressionExperimentDataFileGeneratorCli.java

@Override
protected Exception doWork(String[] args) {

    Exception exp = processCommandLine(DESCRIPTION, args);
    if (exp != null) {
        return exp;
    }//from w  ww  .  ja  v a  2s. c  o m

    BlockingQueue<BioAssaySet> queue = new ArrayBlockingQueue<BioAssaySet>(expressionExperiments.size());

    // Add the Experiments to the queue for processing
    for (BioAssaySet ee : expressionExperiments) {
        if (ee instanceof ExpressionExperiment) {
            try {
                queue.put(ee);
            } catch (InterruptedException ie) {
                log.info(ie);
            }
        } else {
            throw new UnsupportedOperationException("Can't handle non-EE BioAssaySets yet");
        }

    }

    // Inner class for processing the experiments
    class Worker extends Thread {
        BlockingQueue<BioAssaySet> q;
        SecurityContext context;

        Worker(BlockingQueue<BioAssaySet> q, SecurityContext context) {
            this.context = context;
            this.q = q;
        }

        @Override
        public void run() {

            SecurityContextHolder.setContext(this.context);

            while (true) {
                BioAssaySet ee = q.poll();
                if (ee == null) {
                    break;
                }
                log.info("Processing Experiment: " + ee.getName());
                processExperiment((ExpressionExperiment) ee);

            }

        }
    }

    final SecurityContext context = SecurityContextHolder.getContext();

    Collection<Thread> threads = new ArrayList<Thread>();

    for (int i = 1; i <= this.numThreads; i++) {
        Worker worker = new Worker(queue, context);
        threads.add(worker);
        log.info("Starting thread " + i);
        worker.start();
    }

    waitForThreadPoolCompletion(threads);

    summarizeProcessing();

    return null;

}

From source file:org.jodconverter.office.AbstractOfficeManagerPool.java

/** Constructs a new instance of the class with the specified settings. */
protected AbstractOfficeManagerPool(final int poolSize, final OfficeManagerPoolConfig config) {
    super();/*ww  w. j a  v a 2 s.  c  o  m*/

    this.config = config;

    // Create the pool
    pool = new ArrayBlockingQueue<>(poolSize);

    // Initialize the temp file counter
    tempFileCounter = new AtomicLong(0);
}

From source file:org.openspaces.core.map.LockManager.java

/**
 * Creates a new Lock Manager based on the {@link com.j_spaces.map.IMap}.
 *//*from   ww  w.  j a  v  a  2 s. com*/
public LockManager(IMap map) {
    this.map = map;
    this.masterSpace = map.getMasterSpace();
    try {
        transactionManagerProvider = new DistributedTransactionManagerProvider();
    } catch (TransactionException e) {
        throw new CannotCreateTransactionException("Failed to obtain transaction lock manager", e);
    }

    templatePool = new ArrayBlockingQueue<SpaceMapEntry>(1000);
    for (int i = 0; i < 1000; i++) {
        templatePool.add(MapEntryFactory.create());
    }
}

From source file:com.kdab.daytona.Logger.java

@Override
public void init(ServletConfig cfg) {
    m_logger = new Logger(cfg);
    Configuration config = null;/*from  w  ww.  j a v  a  2 s.com*/
    try {
        Properties props = new Properties();
        props.load(new FileInputStream("/Users/frank/daytona-config.xml")); //TODO where to put the file?
        config = new Configuration(props);
    } catch (IOException e) {
        System.err.println(e.getMessage());
        cfg.getServletContext().log("Could not load Daytona configuration file", e);
        m_error = true;
        return;
    } catch (InvalidConfigurationException e) {
        cfg.getServletContext().log("Could not parse Daytona configuration file", e);
        m_error = true;
        return;
    }
    BlockingQueue<byte[]> rawXml = new ArrayBlockingQueue<byte[]>(1000);
    BlockingQueue<byte[]> rawJson = new ArrayBlockingQueue<byte[]>(1000);
    m_queuesByFormat.put("xml", rawXml);
    m_queuesByFormat.put("json", rawJson);
    BlockingQueue<Message> parsed = new ArrayBlockingQueue<Message>(1000);
    BlockingQueue<Message> routed = new ArrayBlockingQueue<Message>(5000);
    ParserRunnable xp = new ParserRunnable(new XmlParser(), rawXml, parsed, m_logger);
    ParserRunnable jp = new ParserRunnable(new JsonParser(), rawJson, parsed, m_logger);
    Router r = new Router(parsed, routed, config.routingRules());
    JabberBot b = new JabberBot(routed, config.account(), config.nick(), config.admins(), config.roomsToJoin(),
            m_logger);
    m_workers = new Vector<Thread>();
    m_workers.add(new Thread(xp));
    m_workers.add(new Thread(jp));
    m_workers.add(new Thread(r));
    m_workers.add(new Thread(b));
    for (Thread i : m_workers)
        i.start();
}

From source file:org.datacleaner.api.AnalyzerResultFutureTest.java

public void testMultiThreadedListenerScenario() throws Exception {
    final int threadCount = 10;

    final Thread[] threads = new Thread[threadCount];
    @SuppressWarnings({ "unchecked" })
    final Listener<NumberResult>[] listeners = new Listener[threadCount];
    final ArrayBlockingQueue<Object> resultQueue = new ArrayBlockingQueue<>(threadCount);

    for (int i = 0; i < listeners.length; i++) {
        listeners[i] = new Listener<NumberResult>() {
            @Override//from www  .j  a  v  a 2  s. co m
            public void onSuccess(NumberResult result) {
                resultQueue.add(result);
            }

            @Override
            public void onError(RuntimeException error) {
                resultQueue.add(error);
            }
        };
    }

    final Ref<NumberResult> resultRef = new LazyRef<NumberResult>() {
        @Override
        protected NumberResult fetch() throws Throwable {
            final long randomSleepTime = (long) (1000 * Math.random());
            Thread.sleep(randomSleepTime);
            return new NumberResult(43);
        }
    };

    final AnalyzerResultFuture<NumberResult> future = new AnalyzerResultFutureImpl<>("foo", resultRef);

    for (int i = 0; i < threads.length; i++) {
        final Listener<NumberResult> listener = listeners[i];
        threads[i] = new Thread() {
            @Override
            public void run() {
                future.addListener(listener);
            }
        };
    }

    final int halfOfTheThreads = threads.length / 2;
    for (int i = 0; i < halfOfTheThreads; i++) {
        threads[i].start();
    }
    for (int i = 0; i < halfOfTheThreads; i++) {
        threads[i].join();
    }

    future.get();

    // to avoid any race conditions we use the drainTo method before calling
    // toString().
    final List<Object> result = new ArrayList<>();
    resultQueue.drainTo(result);

    assertEquals("[43, 43, 43, 43, 43]", result.toString());
    assertEquals(halfOfTheThreads, result.size());

    for (int i = halfOfTheThreads; i < threads.length; i++) {
        threads[i].start();
    }
    for (int i = halfOfTheThreads; i < threads.length; i++) {
        threads[i].join();
    }

    resultQueue.drainTo(result);

    assertEquals("[43, 43, 43, 43, 43, 43, 43, 43, 43, 43]", result.toString());
    assertEquals(threads.length, result.size());
}