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.clustercontrol.nodemap.util.SearchConnectionExecutor.java

/**
 * ???ExecutorService<BR>/*from w  ww. ja v  a 2  s .c om*/
 *
 * @param scopeId ?ID
 * @param isL3 L3????true L2????false?
 * @throws HinemosUnknown
 */
public SearchConnectionExecutor(String scopeId, boolean isL3) throws HinemosUnknown {
    start = HinemosTime.currentTimeMillis();
    this.isL3 = isL3;

    // ??SNMP?
    int threadSize = HinemosPropertyUtil
            .getHinemosPropertyNum("nodemap.search.connection.thread", Long.valueOf(4)).intValue();
    m_log.info("static() : Thread Size = " + threadSize);
    m_log.info("SearchConnectionExecutor() : scopeId=" + scopeId + ",L3=" + isL3);

    // ??????OID?
    String oid = isL3 ? SearchConnectionProperties.DEFAULT_OID_ARP : SearchConnectionProperties.DEFAULT_OID_FDB;
    oidSet = new HashSet<String>();
    oidSet.add(oid);

    facilityIdList = bean.getFacilityIdList(scopeId, RepositoryControllerBean.ONE_LEVEL);

    _executor = new MonitoredThreadPoolExecutor(threadSize, threadSize, 0L, TimeUnit.MICROSECONDS,
            new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
                private volatile int _count = 0;

                @Override
                public Thread newThread(Runnable r) {
                    return new Thread(r, "SearchConnectionExecutor-" + _count++);
                }
            }, new ThreadPoolExecutor.AbortPolicy());

    now = HinemosTime.currentTimeMillis();
    m_log.debug("Constructer : " + (now - start) + "ms");
}

From source file:uk.ac.kcl.service.GateService.java

private void loadresources() throws GateException, IOException {
    List<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
    //if called assume resources are either new/damaged/stale etc and delete all
    Gate.getCreoleRegister().getAllInstances("gate.Resource").forEach(Factory::deleteResource);

    if (activeProfiles.contains("gate")) {
        File gateApp = new File(env.getProperty("gateApp"));
        annotationSets = new ArrayList<>();
        try {/*from w  w w . j  a v  a 2s  .co m*/
            annotationSets.addAll(Arrays.asList(env.getProperty("gateAnnotationSets").split(",")));
        } catch (NullPointerException ex) {
            LOG.info("No annotation sets listed for extraction. Using default set");
        }
        annotationTypes = new ArrayList<>();
        try {
            annotationTypes.addAll(Arrays.asList(env.getProperty("gateAnnotationTypes").split(",")));
        } catch (NullPointerException ex) {
            LOG.info("No annotation types listed for extraction. Extracting all types");
        }
        genericQueue = new LinkedBlockingQueue<>();
        Corpus corpus = Factory.newCorpus("Corpus");
        CorpusController pipeline = (CorpusController) PersistenceManager.loadObjectFromFile(gateApp);
        pipeline.setCorpus(corpus);
        genericQueue.add(pipeline);
        while (genericQueue.size() != poolSize) {
            genericQueue.add((CorpusController) Factory.duplicate(pipeline));
        }
    }

    if (activeProfiles.contains("deid")) {
        File deidApp = new File(env.getProperty("deIdApp"));
        deIdQueue = new LinkedBlockingQueue<>();
        Corpus corpus = Factory.newCorpus("Corpus");
        CorpusController pipeline = (CorpusController) PersistenceManager.loadObjectFromFile(deidApp);
        pipeline.setCorpus(corpus);
        deIdQueue.add(pipeline);
        while (deIdQueue.size() != poolSize) {
            deIdQueue.add((CorpusController) Factory.duplicate(pipeline));
        }
    }
}

From source file:net.sf.jabb.camel.CamelContextController.java

/**
 * Creates an instance which wraps a specified CamelContext, 
 * listens at a specified port and controls the CamelContext according to 
 * commands received from that port.<br>
 * ?CamelContext??//from  w  w w. j a v a2  s  .c om
 * ??CamelContext
 * 
 * @param camelContext   The CamelContext that will be controlled.<br>
 *                   CamelContext
 * @param serverUri      The listening port in Camel Netty URI format, for example: <br>
 *                   ??URI?????CamelContext?CamelNettyURI?
 *                   <p><code>tcp://localhost:99999?keepAlive=true</code> 
 * @param autoDisconnect   Whether disconnect the socket connection from server side after each control command was received.<br>
 *                      ????socket
 * @throws Exception
 */
public CamelContextController(final CamelContext camelContext, final String serverUri,
        final boolean autoDisconnect) throws Exception {
    context = camelContext;
    commandQueue = new LinkedBlockingQueue<String>();

    myContext = new DefaultCamelContext();
    myContext.setName("Controller of '" + context.getName() + "'");
    myContext.addRoutes(new RouteBuilder() {
        @Override
        public void configure() {
            StringBuilder sb = new StringBuilder();
            sb.append("netty:").append(serverUri);
            sb.append(serverUri.contains("?") ? '&' : '?');
            sb.append("sync=true&textline=true&disconnect=");
            sb.append(autoDisconnect ? "true" : "false");
            from(sb.toString()).process(new Processor() {
                public void process(Exchange exchange) throws Exception {
                    exchange.getOut().setBody(command(exchange.getIn().getBody(String.class)));
                }
            });
        }
    });
    myContext.start();
}

From source file:com.idlegandalf.ledd.helper.LedDHelper.java

public LedDHelper(LedDDaemon ledDDaemon, Context appl) {
    this.context = appl;
    this.dRequests = new LinkedBlockingQueue<>();
    this.ledDDaemon = ledDDaemon;

    Intent intent = new Intent(appl, ColorService.class);
    appl.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

From source file:com.amazon.janusgraph.example.MarvelGraphFactory.java

public static void load(final JanusGraph graph, final int rowsToLoad, final boolean report) throws Exception {

    JanusGraphManagement mgmt = graph.openManagement();
    if (mgmt.getGraphIndex(CHARACTER) == null) {
        final PropertyKey characterKey = mgmt.makePropertyKey(CHARACTER).dataType(String.class).make();
        mgmt.buildIndex(CHARACTER, Vertex.class).addKey(characterKey).unique().buildCompositeIndex();
    }/* w  w w .  j  a  v  a2s .  c  o  m*/
    if (mgmt.getGraphIndex(COMIC_BOOK) == null) {
        final PropertyKey comicBookKey = mgmt.makePropertyKey(COMIC_BOOK).dataType(String.class).make();
        mgmt.buildIndex(COMIC_BOOK, Vertex.class).addKey(comicBookKey).unique().buildCompositeIndex();
        mgmt.makePropertyKey(WEAPON).dataType(String.class).make();
        mgmt.makeEdgeLabel(APPEARED).multiplicity(Multiplicity.MULTI).make();
    }
    mgmt.commit();

    ClassLoader classLoader = MarvelGraphFactory.class.getClassLoader();
    URL resource = classLoader.getResource("META-INF/marvel.csv");
    int line = 0;
    Map<String, Set<String>> comicToCharacter = new HashMap<>();
    Map<String, Set<String>> characterToComic = new HashMap<>();
    Set<String> characters = new HashSet<>();
    BlockingQueue<Runnable> creationQueue = new LinkedBlockingQueue<>();
    try (CSVReader reader = new CSVReader(new InputStreamReader(resource.openStream()))) {
        String[] nextLine;
        while ((nextLine = reader.readNext()) != null && line < rowsToLoad) {
            line++;
            String comicBook = nextLine[1];
            String[] characterNames = nextLine[0].split("/");
            if (!comicToCharacter.containsKey(comicBook)) {
                comicToCharacter.put(comicBook, new HashSet<String>());
            }
            List<String> comicCharacters = Arrays.asList(characterNames);
            comicToCharacter.get(comicBook).addAll(comicCharacters);
            characters.addAll(comicCharacters);

        }
    }

    for (String character : characters) {
        creationQueue.add(new CharacterCreationCommand(character, graph));
    }

    BlockingQueue<Runnable> appearedQueue = new LinkedBlockingQueue<>();
    for (String comicBook : comicToCharacter.keySet()) {
        creationQueue.add(new ComicBookCreationCommand(comicBook, graph));
        Set<String> comicCharacters = comicToCharacter.get(comicBook);
        for (String character : comicCharacters) {
            AppearedCommand lineCommand = new AppearedCommand(graph, new Appeared(character, comicBook));
            appearedQueue.add(lineCommand);
            if (!characterToComic.containsKey(character)) {
                characterToComic.put(character, new HashSet<String>());
            }
            characterToComic.get(character).add(comicBook);
        }
        REGISTRY.histogram("histogram.comic-to-character").update(comicCharacters.size());
    }

    int maxAppearances = 0;
    String maxCharacter = "";
    for (String character : characterToComic.keySet()) {
        Set<String> comicBookSet = characterToComic.get(character);
        int numberOfAppearances = comicBookSet.size();
        REGISTRY.histogram("histogram.character-to-comic").update(numberOfAppearances);
        if (numberOfAppearances > maxAppearances) {
            maxCharacter = character;
            maxAppearances = numberOfAppearances;
        }
    }
    LOG.info("Character {} has most appearances at {}", maxCharacter, maxAppearances);

    ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
    for (int i = 0; i < POOL_SIZE; i++) {
        executor.execute(new BatchCommand(graph, creationQueue));
    }
    executor.shutdown();
    while (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
        LOG.info("Awaiting:" + creationQueue.size());
        if (report) {
            REPORTER.report();
        }
    }

    executor = Executors.newSingleThreadExecutor();
    executor.execute(new BatchCommand(graph, appearedQueue));

    executor.shutdown();
    while (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
        LOG.info("Awaiting:" + appearedQueue.size());
        if (report) {
            REPORTER.report();
        }
    }
    LOG.info("MarvelGraphFactory.load complete");
}

From source file:com.cloudant.sync.replication.BasicPullStrategy.java

public BasicPullStrategy(PullReplication pullReplication, ExecutorService executorService,
        PullConfiguration config) {//from w  ww .  j  a v a  2 s  .co  m
    Preconditions.checkNotNull(pullReplication, "PullReplication must not be null.");

    if (executorService == null) {
        executorService = new ThreadPoolExecutor(4, 4, 1, TimeUnit.MINUTES,
                new LinkedBlockingQueue<Runnable>());
    }

    if (config == null) {
        config = new PullConfiguration();
    }

    this.executor = executorService;
    this.config = config;
    this.filter = pullReplication.filter;

    String dbName = pullReplication.getSourceDbName();
    CouchConfig couchConfig = pullReplication.getCouchConfig();
    this.sourceDb = new CouchClientWrapper(dbName, couchConfig);
    this.targetDb = new DatastoreWrapper((DatastoreExtended) pullReplication.target);
    this.name = String.format("%s [%s]", LOG_TAG, pullReplication.getReplicatorName());
}

From source file:com.microsoft.rest.serializer.FlatteningSerializer.java

@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    if (value == null) {
        jgen.writeNull();/* w w w  .  j a  va  2s .c  om*/
        return;
    }

    // BFS for all collapsed properties
    ObjectNode root = mapper.valueToTree(value);
    ObjectNode res = root.deepCopy();
    Queue<ObjectNode> source = new LinkedBlockingQueue<ObjectNode>();
    Queue<ObjectNode> target = new LinkedBlockingQueue<ObjectNode>();
    source.add(root);
    target.add(res);
    while (!source.isEmpty()) {
        ObjectNode current = source.poll();
        ObjectNode resCurrent = target.poll();
        Iterator<Map.Entry<String, JsonNode>> fields = current.fields();
        while (fields.hasNext()) {
            Map.Entry<String, JsonNode> field = fields.next();
            ObjectNode node = resCurrent;
            String key = field.getKey();
            JsonNode outNode = resCurrent.get(key);
            if (field.getKey().matches(".+[^\\\\]\\..+")) {
                String[] values = field.getKey().split("((?<!\\\\))\\.");
                for (int i = 0; i < values.length; ++i) {
                    values[i] = values[i].replace("\\.", ".");
                    if (i == values.length - 1) {
                        break;
                    }
                    String val = values[i];
                    if (node.has(val)) {
                        node = (ObjectNode) node.get(val);
                    } else {
                        ObjectNode child = new ObjectNode(JsonNodeFactory.instance);
                        node.put(val, child);
                        node = child;
                    }
                }
                node.set(values[values.length - 1], resCurrent.get(field.getKey()));
                resCurrent.remove(field.getKey());
                outNode = node.get(values[values.length - 1]);
            }
            if (field.getValue() instanceof ObjectNode) {
                source.add((ObjectNode) field.getValue());
                target.add((ObjectNode) outNode);
            } else if (field.getValue() instanceof ArrayNode && (field.getValue()).size() > 0
                    && (field.getValue()).get(0) instanceof ObjectNode) {
                Iterator<JsonNode> sourceIt = field.getValue().elements();
                Iterator<JsonNode> targetIt = outNode.elements();
                while (sourceIt.hasNext()) {
                    source.add((ObjectNode) sourceIt.next());
                    target.add((ObjectNode) targetIt.next());
                }
            }
        }
    }
    jgen.writeTree(res);
}

From source file:io.seldon.api.state.ZkAlgorithmUpdater.java

@Override
public void run() {
    logger.info("Starting");
    try {/*from   w w w .  j a  va2s  .c om*/
        boolean error = false;
        while (keepRunning) {
            try {

                CuratorFramework client = null;
                boolean ok = false;
                for (int attempts = 0; attempts < 4 && !ok; attempts++) {
                    client = curatorHandler.getCurator().usingNamespace(clientName);
                    logger.info("Waiting until zookeeper connected on attempt " + attempts);
                    ok = client.getZookeeperClient().blockUntilConnectedOrTimedOut();
                    if (ok)
                        logger.info("zookeeper connected");
                    else {
                        logger.error("Timed out waiting for zookeeper connect : attempt " + attempts);
                    }
                }
                if (!ok) {
                    logger.error("Failed to connect to zookeeper after multiple attempts - STOPPING");
                    return;
                }
                queue = new LinkedBlockingQueue<>();
                final Watcher watcher = new Watcher() {
                    boolean expired = false;

                    @Override
                    public void process(WatchedEvent event) {
                        try {
                            if (event.getPath() != null)
                                queue.put(event.getPath());
                            else {
                                logger.warn("Unexpected event " + event.getType().name() + " -> "
                                        + event.toString());
                                switch (event.getState()) {
                                case SyncConnected: {
                                }
                                    break;
                                case Expired: {
                                    queue.put("");
                                }
                                    break;
                                case Disconnected: {
                                    logger.warn("Disconnected from server");
                                    //queue.put("");                                      
                                }
                                    break;
                                }

                            }
                        } catch (InterruptedException e) {
                            throw new Error(e);
                        }
                    }
                };

                logger.info("Checking path " + ALG_PATH + " exists");
                if (client.checkExists().forPath(ALG_PATH) == null) {
                    logger.warn(
                            "Path " + ALG_PATH + " does not exist for client " + clientName + " creating...");
                    client.create().forPath(ALG_PATH);
                } else
                    logger.info("Path " + ALG_PATH + " exists");

                //client.getConnectionStateListenable().addListener(stateListener);
                boolean restart = false;
                while (keepRunning && !restart) {

                    client.getData().usingWatcher(watcher).forPath(ALG_PATH);

                    String path = queue.take();
                    if (!StringUtils.isEmpty(path)) {
                        logger.info("Alg Path changed " + path);

                        byte[] bytes = client.getData().forPath(ALG_PATH);

                        try {
                            ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
                            CFAlgorithm alg = (CFAlgorithm) in.readObject();
                            in.close();
                            //Update Algorithm for client
                            logger.info(
                                    "Updating algorithm options for " + clientName + " to " + alg.toString());
                            Util.getAlgorithmService().setAlgorithmOptions(clientName, alg);
                            numUpdates++;
                        } catch (ClassNotFoundException e) {
                            logger.error("Can't find class ", e);
                        } catch (IOException e) {
                            logger.error("Failed to deserialize algorithm for client " + clientName, e);
                        }
                    } else {
                        //logger.warn("Empty path - maybe zookeeper connection state change watcher will be reset");
                        logger.warn("Will try to restart");
                        restart = true;
                    }
                }

            } catch (IOException e) {
                logger.error("Exception trying to create sk client ", e);
                error = true;
            } catch (Exception e) {
                logger.error("Exception from zookeeper client ", e);
                error = true;
            } finally {

            }

            if (keepRunning && error) {
                logger.info("Sleeping " + sleepTime);
                Thread.sleep(sleepTime);
                if (sleepTime * 2 < maxSleepTime)
                    sleepTime = sleepTime * 2;
                error = false;
            }

        }
    } catch (InterruptedException e) {
        logger.warn("Sleep interuppted ", e);
    }
    logger.info("Stopping");
}

From source file:io.cloudslang.worker.management.services.WorkerManager.java

@PostConstruct
private void init() {
    logger.info("Initialize worker with UUID: " + workerUuid);
    System.setProperty("worker.uuid", workerUuid); //do not remove!!!
    inBuffer = new LinkedBlockingQueue<>();

    executorService = new ThreadPoolExecutor(numberOfThreads, numberOfThreads, Long.MAX_VALUE,
            TimeUnit.NANOSECONDS, inBuffer,
            new WorkerThreadFactory((++threadPoolVersion) + "_WorkerExecutionThread"));

    mapOfRunningTasks = new ConcurrentHashMap<>(numberOfThreads);
}