Example usage for java.util.concurrent LinkedBlockingQueue add

List of usage examples for java.util.concurrent LinkedBlockingQueue add

Introduction

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

Prototype

boolean add(E e);

Source Link

Document

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

Usage

From source file:org.apache.distributedlog.auditor.DLAuditor.java

private void collectLedgersFromDL(final URI uri, final Namespace namespace, final Set<Long> ledgers)
        throws IOException {
    logger.info("Enumerating {} to collect streams.", uri);
    Iterator<String> streams = namespace.getLogs();
    final LinkedBlockingQueue<String> streamQueue = new LinkedBlockingQueue<String>();
    while (streams.hasNext()) {
        streamQueue.add(streams.next());
    }//from  ww w  .jav a  2  s .c  om

    logger.info("Collected {} streams from uri {} : {}", new Object[] { streamQueue.size(), uri, streams });

    executeAction(streamQueue, 10, new Action<String>() {
        @Override
        public void execute(String stream) throws IOException {
            collectLedgersFromStream(namespace, stream, ledgers);
        }
    });
}

From source file:org.apache.distributedlog.auditor.DLAuditor.java

private Map<String, Long> calculateStreamSpaceUsage(final URI uri, final Namespace namespace)
        throws IOException {
    Iterator<String> streams = namespace.getLogs();
    final LinkedBlockingQueue<String> streamQueue = new LinkedBlockingQueue<String>();
    while (streams.hasNext()) {
        streamQueue.add(streams.next());
    }/*from  ww  w . j  av  a2s .c  o  m*/

    final Map<String, Long> streamSpaceUsageMap = new ConcurrentSkipListMap<String, Long>();
    final AtomicInteger numStreamsCollected = new AtomicInteger(0);

    executeAction(streamQueue, 10, new Action<String>() {
        @Override
        public void execute(String stream) throws IOException {
            streamSpaceUsageMap.put(stream, calculateStreamSpaceUsage(namespace, stream));
            if (numStreamsCollected.incrementAndGet() % 1000 == 0) {
                logger.info("Calculated {} streams from uri {}.", numStreamsCollected.get(), uri);
            }
        }
    });

    return streamSpaceUsageMap;
}

From source file:org.apache.hadoop.hbase.trace.IntegrationTestSendTraceRequests.java

private LinkedBlockingQueue<Long> insertData() throws IOException, InterruptedException {
    LinkedBlockingQueue<Long> rowKeys = new LinkedBlockingQueue<Long>(25000);
    HTable ht = new HTable(util.getConfiguration(), this.tableName);
    byte[] value = new byte[300];
    for (int x = 0; x < 5000; x++) {
        TraceScope traceScope = Trace.startSpan("insertData", Sampler.ALWAYS);
        try {//from w w  w. j  a va 2 s  . com
            ht.setAutoFlush(false, true);
            for (int i = 0; i < 5; i++) {
                long rk = random.nextLong();
                rowKeys.add(rk);
                Put p = new Put(Bytes.toBytes(rk));
                for (int y = 0; y < 10; y++) {
                    random.nextBytes(value);
                    p.add(Bytes.toBytes(familyName), Bytes.toBytes(random.nextLong()), value);
                }
                ht.put(p);
            }
            if ((x % 1000) == 0) {
                admin.flush(Bytes.toBytes(tableName));
            }
        } finally {
            traceScope.close();
        }
    }
    admin.flush(Bytes.toBytes(tableName));
    return rowKeys;
}

From source file:com.offbynull.portmapper.natpmp.NatPmpController.java

private <T extends NatPmpResponse> T attemptRequest(ByteBuffer sendBuffer, int attempt, Creator<T> creator)
        throws InterruptedException {

    final LinkedBlockingQueue<ByteBuffer> recvBufferQueue = new LinkedBlockingQueue<>();

    UdpCommunicatorListener listener = new UdpCommunicatorListener() {

        @Override//from   w  ww. j  a va 2 s .  c  om
        public void incomingPacket(InetSocketAddress sourceAddress, DatagramChannel channel,
                ByteBuffer packet) {
            if (channel != unicastChannel) {
                return;
            }

            recvBufferQueue.add(packet);
        }
    };

    // timeout duration should double each iteration, starting from 250 according to spec
    // i = 1, maxWaitTime = (1 << (1-1)) * 250 = (1 << 0) * 250 = 1 * 250 = 250
    // i = 2, maxWaitTime = (1 << (2-1)) * 250 = (1 << 1) * 250 = 2 * 250 = 500
    // i = 3, maxWaitTime = (1 << (3-1)) * 250 = (1 << 2) * 250 = 4 * 250 = 1000
    // i = 4, maxWaitTime = (1 << (4-1)) * 250 = (1 << 3) * 250 = 8 * 250 = 2000
    // ...
    try {
        communicator.addListener(listener);
        communicator.send(unicastChannel, gateway, sendBuffer);

        int maxWaitTime = (1 << (attempt - 1)) * 250; // NOPMD

        T pcpResponse = null;

        long endTime = System.currentTimeMillis() + maxWaitTime;
        long waitTime;
        while ((waitTime = endTime - System.currentTimeMillis()) > 0L) {
            waitTime = Math.max(waitTime, 0L); // must be at least 0, probably should never happen

            ByteBuffer recvBuffer = recvBufferQueue.poll(waitTime, TimeUnit.MILLISECONDS);

            if (recvBuffer != null) {
                pcpResponse = creator.create(recvBuffer);
                if (pcpResponse != null) {
                    break;
                }
            }
        }

        return pcpResponse;
    } finally {
        communicator.removeListener(listener);
    }
}

From source file:com.offbynull.portmapper.pcp.PcpController.java

private <T extends PcpResponse> T attemptRequest(ByteBuffer sendBuffer, int attempt, Creator<T> creator)
        throws InterruptedException {

    final LinkedBlockingQueue<ByteBuffer> recvBufferQueue = new LinkedBlockingQueue<>();

    UdpCommunicatorListener listener = new UdpCommunicatorListener() {

        @Override/*from   ww  w  .  j a  va 2s  .c o m*/
        public void incomingPacket(InetSocketAddress sourceAddress, DatagramChannel channel,
                ByteBuffer packet) {
            if (channel != unicastChannel) {
                return;
            }

            recvBufferQueue.add(packet);
        }
    };

    // timeout duration should double each iteration, starting from 250 according to spec
    // i = 1, maxWaitTime = (1 << (1-1)) * 250 = (1 << 0) * 250 = 1 * 250 = 250
    // i = 2, maxWaitTime = (1 << (2-1)) * 250 = (1 << 1) * 250 = 2 * 250 = 500
    // i = 3, maxWaitTime = (1 << (3-1)) * 250 = (1 << 2) * 250 = 4 * 250 = 1000
    // i = 4, maxWaitTime = (1 << (4-1)) * 250 = (1 << 3) * 250 = 8 * 250 = 2000
    // ...
    try {
        communicator.addListener(listener);
        communicator.send(unicastChannel, gateway, sendBuffer);

        int maxWaitTime = (1 << (attempt - 1)) * 250; // NOPMD

        T pcpResponse = null;

        long endTime = System.currentTimeMillis() + maxWaitTime;
        long waitTime;
        while ((waitTime = endTime - System.currentTimeMillis()) > 0L) {
            waitTime = Math.max(waitTime, 0L); // must be at least 0, probably should never happen

            ByteBuffer recvBuffer = recvBufferQueue.poll(waitTime, TimeUnit.MILLISECONDS);

            if (recvBuffer != null) {
                pcpResponse = creator.create(recvBuffer);
                if (pcpResponse != null) {
                    break;
                }
            }
        }

        return pcpResponse;
    } finally {
        communicator.removeListener(listener);
    }
}

From source file:com.test.HibernateDerbyLockingTest.java

public void runTest(final SessionFactory sessionFactory) throws Exception {
    Person person = new Person();

    Session session = sessionFactory.openSession();
    session.save(person);//from  www .  j av  a  2 s.co  m
    session.flush();
    session.close();

    final String id = person.getId();
    final LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>();

    ExecutorService executorService = Executors.newCachedThreadPool();
    Future<?> submit = executorService.submit(new Runnable() {
        public void run() {
            Session session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.load(Person.class, id, LockMode.UPGRADE);
            try {
                Thread.sleep(2000);
            } catch (Throwable t) {
            }
            System.out.println("one");
            queue.add("one");
            try {
                Thread.sleep(500);
            } catch (Throwable t) {
            }
            transaction.commit();
            session.flush();
            session.close();
        }
    });
    Thread.sleep(500);
    Future<?> submit2 = executorService.submit(new Runnable() {
        public void run() {
            Session session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.load(Person.class, id, LockMode.UPGRADE);
            queue.add("two");
            System.out.println("two");
            transaction.commit();
            session.flush();
            session.close();
        }
    });
    submit.get();
    submit2.get();
    assertEquals("one", queue.poll(3, TimeUnit.SECONDS));
    assertEquals("two", queue.poll(3, TimeUnit.SECONDS));
}

From source file:com.twitter.distributedlog.auditor.DLAuditor.java

private void collectLedgersFromAllocator(final URI uri,
        final com.twitter.distributedlog.DistributedLogManagerFactory factory,
        final List<String> allocationPaths, final Set<Long> ledgers) throws IOException {
    final LinkedBlockingQueue<String> poolQueue = new LinkedBlockingQueue<String>();
    for (String allocationPath : allocationPaths) {
        String rootPath = uri.getPath() + "/" + allocationPath;
        try {// www. j a v  a 2  s. com
            List<String> pools = getZooKeeperClient(factory).get().getChildren(rootPath, false);
            for (String pool : pools) {
                poolQueue.add(rootPath + "/" + pool);
            }
        } catch (KeeperException e) {
            throw new ZKException("Failed to get list of pools from " + rootPath, e);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new DLInterruptedException("Interrupted on getting list of pools from " + rootPath, e);
        }
    }

    logger.info("Collecting ledgers from allocators for {} : {}", uri, poolQueue);

    executeAction(poolQueue, 10, new Action<String>() {
        @Override
        public void execute(String poolPath) throws IOException {
            try {
                collectLedgersFromPool(poolPath);
            } catch (InterruptedException e) {
                throw new DLInterruptedException(
                        "Interrupted on collecting ledgers from allocation pool " + poolPath, e);
            } catch (KeeperException e) {
                throw new ZKException("Failed to collect ledgers from allocation pool " + poolPath, e.code());
            }
        }

        private void collectLedgersFromPool(String poolPath)
                throws InterruptedException, ZooKeeperClient.ZooKeeperConnectionException, KeeperException {
            List<String> allocators = getZooKeeperClient(factory).get().getChildren(poolPath, false);
            for (String allocator : allocators) {
                String allocatorPath = poolPath + "/" + allocator;
                byte[] data = getZooKeeperClient(factory).get().getData(allocatorPath, false, new Stat());
                if (null != data && data.length > 0) {
                    try {
                        long ledgerId = DLUtils.bytes2LedgerId(data);
                        synchronized (ledgers) {
                            ledgers.add(ledgerId);
                        }
                    } catch (NumberFormatException nfe) {
                        logger.warn("Invalid ledger found in allocator path {} : ", allocatorPath, nfe);
                    }
                }
            }
        }
    });

    logger.info("Collected ledgers from allocators for {}.", uri);
}

From source file:org.apache.distributedlog.auditor.DLAuditor.java

private void collectLedgersFromAllocator(final URI uri, final Namespace namespace,
        final List<String> allocationPaths, final Set<Long> ledgers) throws IOException {
    final LinkedBlockingQueue<String> poolQueue = new LinkedBlockingQueue<String>();
    for (String allocationPath : allocationPaths) {
        String rootPath = uri.getPath() + "/" + allocationPath;
        try {//  w w  w . j a va 2  s.  co m
            List<String> pools = getZooKeeperClient(namespace).get().getChildren(rootPath, false);
            for (String pool : pools) {
                poolQueue.add(rootPath + "/" + pool);
            }
        } catch (KeeperException e) {
            throw new ZKException("Failed to get list of pools from " + rootPath, e);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new DLInterruptedException("Interrupted on getting list of pools from " + rootPath, e);
        }
    }

    logger.info("Collecting ledgers from allocators for {} : {}", uri, poolQueue);

    executeAction(poolQueue, 10, new Action<String>() {
        @Override
        public void execute(String poolPath) throws IOException {
            try {
                collectLedgersFromPool(poolPath);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new DLInterruptedException(
                        "Interrupted on collecting" + " ledgers from allocation pool " + poolPath, e);
            } catch (KeeperException e) {
                throw new ZKException("Failed to collect ledgers from allocation pool " + poolPath, e.code());
            }
        }

        private void collectLedgersFromPool(String poolPath)
                throws InterruptedException, ZooKeeperClient.ZooKeeperConnectionException, KeeperException {
            List<String> allocators = getZooKeeperClient(namespace).get().getChildren(poolPath, false);
            for (String allocator : allocators) {
                String allocatorPath = poolPath + "/" + allocator;
                byte[] data = getZooKeeperClient(namespace).get().getData(allocatorPath, false, new Stat());
                if (null != data && data.length > 0) {
                    try {
                        long ledgerId = DLUtils.bytes2LogSegmentId(data);
                        synchronized (ledgers) {
                            ledgers.add(ledgerId);
                        }
                    } catch (NumberFormatException nfe) {
                        logger.warn("Invalid ledger found in allocator path {} : ", allocatorPath, nfe);
                    }
                }
            }
        }
    });

    logger.info("Collected ledgers from allocators for {}.", uri);
}

From source file:au.org.ala.layers.dao.LayerIntersectDAOImpl.java

ArrayList<String> localSampling(IntersectionFile[] intersectionFiles, double[][] points,
        IntersectCallback callback) {//  ww w .j av  a 2s  .  c om
    logger.info("begin LOCAL sampling, number of threads " + intersectConfig.getThreadCount()
            + ", number of layers=" + intersectionFiles.length + ", number of coordinates=" + points.length);
    long start = System.currentTimeMillis();
    int threadCount = intersectConfig.getThreadCount();
    SamplingThread[] threads = new SamplingThread[threadCount];
    LinkedBlockingQueue<Integer> lbq = new LinkedBlockingQueue();
    CountDownLatch cdl = new CountDownLatch(intersectionFiles.length);
    ArrayList<String> output = new ArrayList<String>();
    for (int i = 0; i < intersectionFiles.length; i++) {
        output.add("");
        lbq.add(i);
    }

    callback.setLayersToSample(intersectionFiles);
    logger.info("Initialising sampling threads: " + threadCount);
    for (int i = 0; i < threadCount; i++) {
        threads[i] = new SamplingThread(lbq, cdl, intersectionFiles, points, output,
                intersectConfig.getThreadCount(), intersectConfig.getShapeFileCache(),
                intersectConfig.getGridBufferSize(), callback);
        threads[i].start();
    }

    try {
        cdl.await();
    } catch (InterruptedException ex) {
        logger.error(ex.getMessage(), ex);
    } finally {
        for (int i = 0; i < threadCount; i++) {
            try {
                threads[i].interrupt();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    logger.info("End sampling, threads=" + threadCount + " layers=" + intersectionFiles.length + " in "
            + (System.currentTimeMillis() - start) + "ms");
    return output;
}

From source file:org.kurento.rabbitmq.RabbitTemplate.java

@Override
public void onMessage(Message message) {
    try {//from   w ww . jav a2 s . c om
        String messageTag;
        if (this.correlationKey == null) { // using standard correlationId
            // property
            messageTag = new String(message.getMessageProperties().getCorrelationId(), this.encoding);
        } else {
            messageTag = (String) message.getMessageProperties().getHeaders().get(this.correlationKey);
        }
        if (messageTag == null) {
            logger.error("No correlation header in reply");
            return;
        }

        PendingReply pendingReply = this.replyHolder.get(messageTag);
        if (pendingReply == null) {
            if (logger.isWarnEnabled()) {
                logger.warn("Reply received after timeout for " + messageTag);
            }
        } else {
            // Restore the inbound correlation data
            String savedCorrelation = pendingReply.getSavedCorrelation();
            if (this.correlationKey == null) {
                if (savedCorrelation == null) {
                    message.getMessageProperties().setCorrelationId(null);
                } else {
                    message.getMessageProperties().setCorrelationId(savedCorrelation.getBytes(this.encoding));
                }
            } else {
                if (savedCorrelation != null) {
                    message.getMessageProperties().setHeader(this.correlationKey, savedCorrelation);
                } else {
                    message.getMessageProperties().getHeaders().remove(this.correlationKey);
                }
            }
            // Restore any inbound replyTo
            String savedReplyTo = pendingReply.getSavedReplyTo();
            message.getMessageProperties().setReplyTo(savedReplyTo);
            LinkedBlockingQueue<Message> queue = pendingReply.getQueue();
            queue.add(message);
            if (logger.isDebugEnabled()) {
                logger.debug("Reply received for " + messageTag);
                if (savedReplyTo != null) {
                    logger.debug("Restored replyTo to " + savedReplyTo);
                }
            }
        }
    } catch (UnsupportedEncodingException e) {
        throw new AmqpIllegalStateException("Invalid Character Set:" + this.encoding, e);
    }
}