List of usage examples for java.util.concurrent LinkedBlockingQueue poll
public E poll()
From source file:twitter4j.internal.json.z_T4JInternalParseUtil.java
public static Date getDate(String dateString, String format) throws TwitterException { LinkedBlockingQueue<SimpleDateFormat> simpleDateFormats = formatMapQueue.get(format); if (simpleDateFormats == null) { simpleDateFormats = new LinkedBlockingQueue<SimpleDateFormat>(); formatMapQueue.put(format, simpleDateFormats); }/* w w w.j av a 2s . c om*/ SimpleDateFormat sdf = simpleDateFormats.poll(); if (null == sdf) { sdf = new SimpleDateFormat(format, Locale.US); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); } try { return sdf.parse(dateString); } catch (ParseException pe) { throw new TwitterException("Unexpected date format(" + dateString + ") returned from twitter.com", pe); } finally { try { simpleDateFormats.put(sdf); } catch (InterruptedException ignore) { // the size of LinkedBlockingQueue is Integer.MAX by default. // there is no need to concern about this situation } } }
From source file:org.apache.hadoop.hbase.trigger.WritePrepared.java
/** * Flush current action's pending Puts.//from w w w . ja va2s. c om * TODO I am pretty sure there is an error while processing the flush operations * It seems that the for statement does not work at all, and different flush may cause * interacts. I need more information about Java Multi-Thread programming! * @param action */ public static void flush(HTriggerAction action) { System.out.println("enter Flush"); int triggerId = action.getHTrigger().getTriggerId(); long round = action.getCurrentRound(); LinkedBlockingQueue<WriteUnit> writes = cachedElements.get(triggerId); //AtomicBoolean flag = flags.get(triggerId); //if other thread is flushing, we should just leave. /* * It is not necessary to stop other flushes as there is no possible other flush existing. if (!flag.compareAndSet(false, true)) return; */ try { WriteUnit w = writes.poll(); HTable ins = null; while (w != null) { System.out.println("start flush: " + w + "remain: " + writes.size()); ins = getOrNewHTableInstance(w.getTableName()); ins.put(w.getPut()); w = writes.poll(); System.out.println("end flush: " + w + "remain: " + writes.size()); } ins.flushCommits(); } catch (Exception e) { LOG.info("Exceptions While Calling HTable's Put"); } //record successful flush for future recovery. //In fact, there should be a watcher monitoring on these dir and //delete entries written by recordZKActionRound. recordZKWritesFlushed(CurrentRS, triggerId, round); //LOG.info("Trigger" + triggerId + " at round" + round + " Flush OK"); }
From source file:com.twitter.distributedlog.auditor.DLAuditor.java
static <T> void executeAction(final LinkedBlockingQueue<T> queue, final int numThreads, final Action<T> action) throws IOException { final CountDownLatch failureLatch = new CountDownLatch(1); final CountDownLatch doneLatch = new CountDownLatch(queue.size()); final AtomicInteger numFailures = new AtomicInteger(0); final AtomicInteger completedThreads = new AtomicInteger(0); ExecutorService executorService = Executors.newFixedThreadPool(numThreads); try {//from w w w . j a v a 2 s . c om for (int i = 0; i < numThreads; i++) { executorService.submit(new Runnable() { @Override public void run() { while (true) { T item = queue.poll(); if (null == item) { break; } try { action.execute(item); } catch (IOException ioe) { logger.error("Failed to execute action on item '{}'", item, ioe); numFailures.incrementAndGet(); failureLatch.countDown(); break; } doneLatch.countDown(); } if (numFailures.get() == 0 && completedThreads.incrementAndGet() == numThreads) { failureLatch.countDown(); } } }); } try { failureLatch.await(); if (numFailures.get() > 0) { throw new IOException("Encountered " + numFailures.get() + " failures on executing action."); } doneLatch.await(); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); logger.warn("Interrupted on executing action", ie); throw new DLInterruptedException("Interrupted on executing action", ie); } } finally { executorService.shutdown(); } }
From source file:com.offbynull.portmapper.common.UdpCommunicator.java
@Override protected void run() throws Exception { ByteBuffer recvBuffer = ByteBuffer.allocate(1100); while (true) { selector.select();// w ww.j ava2 s . c o m if (stopFlag) { return; } for (DatagramChannel channel : sendQueue.keySet()) { if (!sendQueue.get(channel).isEmpty()) { channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE); } else { channel.register(selector, SelectionKey.OP_READ); } } for (SelectionKey key : selector.selectedKeys()) { if (!key.isValid()) { continue; } DatagramChannel channel = (DatagramChannel) key.channel(); if (key.isReadable()) { recvBuffer.clear(); InetSocketAddress incomingAddress = (InetSocketAddress) channel.receive(recvBuffer); recvBuffer.flip(); for (UdpCommunicatorListener listener : listeners) { try { listener.incomingPacket(incomingAddress, channel, recvBuffer.asReadOnlyBuffer()); } catch (RuntimeException re) { // NOPMD // do nothing } } } else if (key.isWritable()) { LinkedBlockingQueue<ImmutablePair<InetSocketAddress, ByteBuffer>> queue = sendQueue .get(channel); ImmutablePair<InetSocketAddress, ByteBuffer> next = queue.poll(); if (next != null) { try { channel.send(next.getValue(), next.getKey()); } catch (RuntimeException re) { // NOPMD // do nothing } } } } } }
From source file:org.wso2.carbon.databridge.agent.thrift.lb.ReceiverGroup.java
public void resendEvents(LinkedBlockingQueue<Event> events) { if (null != events) { if (events.size() > 0) { log.info("Resending the failed events...."); }// w ww . ja v a 2 s . co m while (true) { Event event = events.poll(); if (null != event) { publish(event); } else { break; } } } }
From source file:org.wso2.carbon.databridge.agent.thrift.lb.ReceiverGroup.java
public void resendPublishedData(LinkedBlockingQueue<PublishData> publishDatas) { if (null != publishDatas) { if (publishDatas.size() > 0) { log.info("Resending the failed published data..."); }/*w ww .jav a2 s . c o m*/ while (true) { PublishData data = publishDatas.poll(); if (null != data) { try { if (data.getStreamName() == null) { publish(data.getEvent()); } else { publish(data.getStreamName(), data.getStreamVersion(), data.getEvent()); } } catch (AgentException e) { log.error(e); } } else { break; } } } }
From source file:se.vgregion.pubsub.push.impl.DefaultPushSubscriberVerifyTest.java
@Test @Transactional//from www. ja v a 2s. c o m @Rollback public void verify() throws Exception { final LinkedBlockingQueue<HttpRequest> requests = new LinkedBlockingQueue<HttpRequest>(); server.register("/*", new HttpRequestHandler() { @Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { requests.add(request); response.setEntity( new StringEntity(getQueryParamValue(request.getRequestLine().getUri(), "hub.challenge"))); } }); subscriber.verify(SubscriptionMode.SUBSCRIBE); Assert.assertEquals(1, requests.size()); HttpRequest actualRequest = requests.poll(); String requestUri = actualRequest.getRequestLine().getUri(); Assert.assertEquals("subscribe", getQueryParamValue(requestUri, "hub.mode")); Assert.assertEquals(subscriber.getTopic().toString(), URLDecoder.decode(getQueryParamValue(requestUri, "hub.topic"), "UTF-8")); Assert.assertNotNull(getQueryParamValue(requestUri, "hub.challenge")); Assert.assertEquals("123", getQueryParamValue(requestUri, "hub.lease_seconds")); Assert.assertEquals(subscriber.getVerifyToken(), getQueryParamValue(requestUri, "hub.verify_token")); }
From source file:com.ibm.crail.tools.CrailBenchmark.java
void getFileAsync(String filename, int loop, int batch) throws Exception, InterruptedException { System.out.println("getFileAsync, filename " + filename + ", loop " + loop + ", batch " + batch); //warmup// ww w.java 2 s . com ConcurrentLinkedQueue<CrailBuffer> bufferQueue = new ConcurrentLinkedQueue<CrailBuffer>(); CrailBuffer buf = fs.allocateBuffer(); bufferQueue.add(buf); warmUp(filename, warmup, bufferQueue); fs.freeBuffer(buf); //benchmark System.out.println("starting benchmark..."); fs.getStatistics().reset(); LinkedBlockingQueue<Future<CrailNode>> fileQueue = new LinkedBlockingQueue<Future<CrailNode>>(); long start = System.currentTimeMillis(); for (int i = 0; i < loop; i++) { //single operation == loop for (int j = 0; j < batch; j++) { Future<CrailNode> future = fs.lookup(filename); fileQueue.add(future); } for (int j = 0; j < batch; j++) { Future<CrailNode> future = fileQueue.poll(); future.get(); } } long end = System.currentTimeMillis(); double executionTime = ((double) (end - start)); double latency = executionTime * 1000.0 / ((double) batch); System.out.println("execution time [ms] " + executionTime); System.out.println("latency [us] " + latency); fs.getStatistics().print("close"); }
From source file:org.springframework.integration.jms.JmsOutboundGateway.java
private javax.jms.Message obtainReplyFromContainer(String correlationId, LinkedBlockingQueue<javax.jms.Message> replyQueue) { javax.jms.Message reply = null;// w ww .j a v a2 s . co m if (this.receiveTimeout < 0) { reply = replyQueue.poll(); } else { try { reply = replyQueue.poll(this.receiveTimeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { logger.error("Interrupted while awaiting reply; treated as a timeout", e); Thread.currentThread().interrupt(); } } if (logger.isDebugEnabled()) { if (reply == null) { if (logger.isDebugEnabled()) { logger.debug(this.getComponentName() + " Timed out waiting for reply with CorrelationId " + correlationId); } } else { if (logger.isDebugEnabled()) { logger.debug(this.getComponentName() + " Obtained reply with CorrelationId " + correlationId); } } } return reply; }
From source file:com.ibm.crail.tools.CrailBenchmark.java
void createFile(String filename, int loop) throws Exception, InterruptedException { System.out.println("createFile, filename " + filename + ", loop " + loop); //warmup// w ww . j a v a2s . c om ConcurrentLinkedQueue<CrailBuffer> bufferQueue = new ConcurrentLinkedQueue<CrailBuffer>(); CrailBuffer buf = fs.allocateBuffer(); bufferQueue.add(buf); warmUp(filename, warmup, bufferQueue); fs.freeBuffer(buf); //benchmark System.out.println("starting benchmark..."); fs.getStatistics().reset(); LinkedBlockingQueue<String> pathQueue = new LinkedBlockingQueue<String>(); fs.create(filename, CrailNodeType.DIRECTORY, CrailStorageClass.DEFAULT, CrailLocationClass.DEFAULT).get() .syncDir(); int filecounter = 0; for (int i = 0; i < loop; i++) { String name = "" + filecounter++; String f = filename + "/" + name; pathQueue.add(f); } double ops = 0; long start = System.currentTimeMillis(); while (!pathQueue.isEmpty()) { String path = pathQueue.poll(); fs.create(path, CrailNodeType.DATAFILE, CrailStorageClass.DEFAULT, CrailLocationClass.DEFAULT).get() .syncDir(); } long end = System.currentTimeMillis(); double executionTime = ((double) (end - start)) / 1000.0; double latency = 0.0; if (executionTime > 0) { latency = 1000000.0 * executionTime / ops; } System.out.println("execution time " + executionTime); System.out.println("ops " + ops); System.out.println("latency " + latency); fs.getStatistics().print("close"); }