List of usage examples for com.google.common.util.concurrent RateLimiter acquire
public double acquire()
From source file:com.yahoo.pulsar.client.cli.CmdConsume.java
/** * Run the consume command.//from w ww . ja v a2 s . c o m * * @return 0 for success, < 0 otherwise */ public int run() throws PulsarClientException, IOException { if (mainOptions.size() != 1) throw (new ParameterException("Please provide one and only one topic name.")); if (this.serviceURL == null || this.serviceURL.isEmpty()) throw (new ParameterException("Broker URL is not provided.")); if (this.subscriptionName == null || this.subscriptionName.isEmpty()) throw (new ParameterException("Subscription name is not provided.")); if (this.numMessagesToConsume < 0) throw (new ParameterException("Number of messages should be zero or positive.")); String topic = this.mainOptions.get(0); int numMessagesConsumed = 0; int returnCode = 0; try { ConsumerConfiguration consumerConf = new ConsumerConfiguration(); consumerConf.setSubscriptionType(this.subscriptionType); PulsarClient client = PulsarClient.create(this.serviceURL, this.clientConfig); Consumer consumer = client.subscribe(topic, this.subscriptionName, consumerConf); RateLimiter limiter = (this.consumeRate > 0) ? RateLimiter.create(this.consumeRate) : null; while (this.numMessagesToConsume == 0 || numMessagesConsumed < this.numMessagesToConsume) { if (limiter != null) { limiter.acquire(); } Message msg = consumer.receive(5, TimeUnit.SECONDS); if (msg == null) { LOG.warn("No message to consume after waiting for 20 seconds."); } else { numMessagesConsumed += 1; System.out.println(MESSAGE_BOUNDARY); String output = this.interpretMessage(msg, displayHex); System.out.println(output); consumer.acknowledge(msg); } } client.close(); } catch (Exception e) { LOG.error("Error while consuming messages"); LOG.error(e.getMessage(), e); returnCode = -1; } finally { LOG.info("{} messages successfully consumed", numMessagesConsumed); } return returnCode; }
From source file:com.amazonaws.service.iot.mqttloadapp.runtime.LoadGeneratorThread.java
@Override public void run() { MqttConnection connection = MqttConnection.getInstance(); running = true;// w w w. jav a 2 s . co m started = System.currentTimeMillis(); // TODO simplification with only one metrics series supported int ratePerMinute = config.getRate(); RateLimiter limiter = com.google.common.util.concurrent.RateLimiter.create(ratePerMinute / (double) 60.0); while (running && connection.isConnected()) { long start = System.currentTimeMillis(); numberTicks++; // Wait for the configured amount of time to maintain the rate limiter.acquire(); // int numberOfPayloads = calculatePayloadsPerTick(ratePerMinute, timeForLastTick, MIN_TICK_TIME_MS); byte[] payload = formatPayload(config.getTemplateId(), functions); try { connection.publish(config.getTopic(), 0, payload, config.getId()); } catch (MqttException ex) { LOG.log(Level.SEVERE, "Could not publish the MQTT-message", ex); } long timeForLastTick = System.currentTimeMillis() - start; LOG.log(Level.INFO, "Took {0}ms to execute tick #: {1}", new Object[] { timeForLastTick, numberTicks }); } LOG.info("Stopped metrics thread"); }
From source file:cherry.foundation.mail.SendMailBatch.java
private void sendMail() { try {/*w ww. j av a 2 s .co m*/ RateLimiter rateLimiter = null; if (sendPerSecond != null && sendPerSecond.doubleValue() > 0.0) { rateLimiter = RateLimiter.create(sendPerSecond.doubleValue()); } LocalDateTime now = bizDateTime.now(); for (long messageId : mailSendHandler.listMessage(now)) { if (rateLimiter != null) { rateLimiter.acquire(); } mailSendHandler.sendMessage(messageId); } } catch (MailException | DataAccessException ex) { if (log.isDebugEnabled()) { log.debug(ex, "failed to send mail"); } } }
From source file:org.apache.bookkeeper.tools.cli.commands.bookieid.SearchReplaceBookieIdCommand.java
@Override protected void run(BookKeeper bk, Flags flags) throws Exception { try (BookKeeperAdmin admin = new BookKeeperAdmin((org.apache.bookkeeper.client.BookKeeper) bk)) { LedgerManager ledgerManager = ((org.apache.bookkeeper.client.BookKeeper) bk).getLedgerManager(); long i = 0; BookieSocketAddress fromAddr = new BookieSocketAddress(flags.from); BookieSocketAddress toAddr = new BookieSocketAddress(flags.to); System.out.println(String.format("Replacing bookie id %s with %s in metadata", fromAddr, toAddr)); RateLimiter limiter = RateLimiter.create(flags.rate); for (Long lid : admin.listLedgers()) { Versioned<LedgerMetadata> md = ledgerManager.readLedgerMetadata(lid).get(); if (md.getValue().getAllEnsembles().entrySet().stream() .anyMatch(e -> e.getValue().contains(fromAddr))) { limiter.acquire(); LedgerMetadataBuilder builder = LedgerMetadataBuilder.from(md.getValue()); md.getValue().getAllEnsembles().entrySet().stream().filter(e -> e.getValue().contains(fromAddr)) .forEach(e -> { List<BookieSocketAddress> ensemble = new ArrayList<>(e.getValue()); ensemble.replaceAll((a) -> { if (a.equals(fromAddr)) { return toAddr; } else { return a; }/*from w ww . jav a2 s . co m*/ }); builder.replaceEnsembleEntry(e.getKey(), ensemble); }); LedgerMetadata newMeta = builder.build(); if (flags.verbose) { System.out.println("Replacing ledger " + lid + " metadata ..."); System.out.println(md.getValue().toSafeString()); System.out.println("with ..."); System.out.println(newMeta.toSafeString()); } i++; if (!flags.dryRun) { ledgerManager.writeLedgerMetadata(lid, newMeta, md.getVersion()).get(); } } if (i >= flags.max) { System.out.println("Max number of ledgers processed, exiting"); break; } } System.out.println("Replaced bookie ID in " + i + " ledgers"); } }
From source file:com.example.app.service.NotificationService.java
/** * Send an SMS message delaying until an appropriate time if necessary. * * @param user the user./*from w w w . ja va 2 s. com*/ * @param phoneNumber the phone number to send too. * @param content the content of the message. */ public void sendSMS(User user, PhoneNumber phoneNumber, String content) { // FIXME : make sure SMS messages are delayed if necessary based on user timezone, etc. final Optional<EmailAddress> emailAddress = ContactUtil.getEmailAddress(user.getPrincipal().getContact(), ContactDataCategory.values()); if (!emailAddress.isPresent()) { _logger.error( "Users are required to have an email address. Not sending SMS. User.id = " + user.getId()); return; } if (!_emailValidationService.checkForValidDomain(emailAddress.get().getEmail(), false)) { _logger.info( "User's email address is being filtered/sanitized. Not sending SMS. User.id = " + user.getId()); return; } if (content.length() < SMS_MESSAGE_LIMIT) _smsService.sendSms(phoneNumber, content); else { RateLimiter rateLimiter = RateLimiter.create(1.5); // Space out split message so it isn't interleaved final List<String> parts = splitContent(content); for (String part : parts) { rateLimiter.acquire(); _smsService.sendSms(phoneNumber, part); } } }
From source file:org.apache.pulsar.client.cli.CmdProduce.java
/** * Run the producer./*w w w. j a va2 s . c o m*/ * * @return 0 for success, < 0 otherwise * @throws Exception */ public int run() throws PulsarClientException { if (mainOptions.size() != 1) throw (new ParameterException("Please provide one and only one topic name.")); if (this.serviceURL == null || this.serviceURL.isEmpty()) throw (new ParameterException("Broker URL is not provided.")); if (this.numTimesProduce <= 0) throw (new ParameterException("Number of times need to be positive number.")); if (messages.size() == 0 && messageFileNames.size() == 0) throw (new ParameterException("Please supply message content with either --messages or --files")); int totalMessages = (messages.size() + messageFileNames.size()) * numTimesProduce; if (totalMessages > MAX_MESSAGES) { String msg = "Attempting to send " + totalMessages + " messages. Please do not send more than " + MAX_MESSAGES + " messages"; throw new ParameterException(msg); } String topic = this.mainOptions.get(0); int numMessagesSent = 0; int returnCode = 0; try { PulsarClient client = PulsarClient.create(this.serviceURL, this.clientConfig); Producer producer = client.createProducer(topic); List<byte[]> messageBodies = generateMessageBodies(this.messages, this.messageFileNames); RateLimiter limiter = (this.publishRate > 0) ? RateLimiter.create(this.publishRate) : null; for (int i = 0; i < this.numTimesProduce; i++) { List<Message> messages = generateMessages(messageBodies); for (Message msg : messages) { if (limiter != null) limiter.acquire(); producer.send(msg); numMessagesSent++; } } client.close(); } catch (Exception e) { LOG.error("Error while producing messages"); LOG.error(e.getMessage(), e); returnCode = -1; } finally { LOG.info("{} messages successfully produced", numMessagesSent); } return returnCode; }
From source file:com.example.app.communication.service.NotificationService.java
/** * Send an SMS message delaying until an appropriate time if necessary. * * @param user the user.//from w ww . j a v a 2 s . c om * @param phoneNumber the phone number to send too. * @param content the content of the message. * @param delayMillis the delay millis */ public void sendSMS(User user, PhoneNumber phoneNumber, String content, @Nullable Long delayMillis) { // FUTURE : make sure SMS messages are delayed if necessary based on user timezone, etc. final Optional<EmailAddress> emailAddress = ContactUtil.getEmailAddress(user.getPrincipal().getContact(), ContactDataCategory.values()); if (!emailAddress.isPresent()) { _logger.error( "Users are required to have an email address. Not sending SMS. User.id = " + user.getId()); return; } if (!_emailValidationService.checkForValidDomain(emailAddress.get().getEmail(), false)) { _logger.info( "User's email address is being filtered/sanitized. Not sending SMS. User.id = " + user.getId()); return; } if (content.length() < SMS_MESSAGE_LIMIT) _smsService.sendSms(phoneNumber, content); else { RateLimiter rateLimiter = RateLimiter.create(1.5); // Space out split message so it isn't interleaved final List<String> parts = splitContent(content); for (String part : parts) { rateLimiter.acquire(); _smsService.sendSms(phoneNumber, part); } } }
From source file:com.ibm.og.scheduling.ConcurrentRequestScheduler.java
/** * Constructs an instance with the provided concurrency * // w w w . j a v a 2 s. c o m * @param concurrentRequests the number of concurrent requests allowed * @throws IllegalArgumentException if concurrentRequests is negative or zero */ public ConcurrentRequestScheduler(final int concurrentRequests, final double rampup, final TimeUnit rampupUnit) { checkArgument(concurrentRequests > 0, "concurrentRequests must be > 0"); checkArgument(rampup >= 0.0, "rampup must be >= 0.0 [%s]", rampup); checkNotNull(rampupUnit); this.concurrentRequests = concurrentRequests; this.rampup = rampup; this.rampupUnit = rampupUnit; this.started = new CountDownLatch(1); if (DoubleMath.fuzzyEquals(rampup, 0.0, Math.pow(0.1, 6))) { this.permits = new Semaphore(concurrentRequests); } else { this.permits = new Semaphore(0); final Thread rampupThread = new Thread(new Runnable() { @Override public void run() { final double rampSeconds = (rampup * rampupUnit.toNanos(1)) / TimeUnit.SECONDS.toNanos(1); _logger.debug("Ramp seconds [{}]", rampSeconds); final RateLimiter ramp = RateLimiter.create(concurrentRequests / rampSeconds); _logger.debug("Ramp rate [{}]", ramp.getRate()); _logger.debug("Awaiting start latch"); Uninterruptibles.awaitUninterruptibly(ConcurrentRequestScheduler.this.started); _logger.info("Starting ramp"); for (int i = 0; i < concurrentRequests; i++) { _logger.debug("Acquiring RateLimiter permit"); ramp.acquire(); _logger.debug("Releasing semaphore permit"); ConcurrentRequestScheduler.this.permits.release(); } _logger.info("Finished ramp"); } }, "concurrent-scheduler-ramp"); rampupThread.setDaemon(true); rampupThread.start(); _logger.debug("Starting permits [{}]", this.permits.availablePermits()); } }
From source file:org.opennms.netmgt.icmp.proxy.PingSweepRpcModule.java
@Override public CompletableFuture<PingSweepResponseDTO> execute(PingSweepRequestDTO request) { final Pinger pinger = pingerFactory.getInstance(); final PingSweepResultTracker tracker = new PingSweepResultTracker(); String location = request.getLocation(); int packetSize = request.getPacketSize(); List<IPPollRange> ranges = new ArrayList<>(); for (IPRangeDTO dto : request.getIpRanges()) { IPPollRange pollRange = new IPPollRange(null, location, dto.getBegin(), dto.getEnd(), dto.getTimeout(), dto.getRetries());/*from w ww. jav a 2 s.co m*/ ranges.add(pollRange); } // Use a RateLimiter to limit the ping packets per second that we send RateLimiter limiter = RateLimiter.create(request.getPacketsPerSecond()); List<IPPollAddress> addresses = StreamSupport.stream(getAddresses(ranges).spliterator(), false) .filter(j -> j.getAddress() != null).collect(Collectors.toList()); return CompletableFuture.supplyAsync(() -> { addresses.stream().forEach(pollAddress -> { try { tracker.expectCallbackFor(pollAddress.getAddress()); limiter.acquire(); pinger.ping(pollAddress.getAddress(), pollAddress.getTimeout(), pollAddress.getRetries(), packetSize, 1, tracker); } catch (Exception e) { tracker.handleError(pollAddress.getAddress(), null, e); tracker.completeExceptionally(e); } }); try { tracker.getLatch().await(); } catch (InterruptedException e) { throw Throwables.propagate(e); } tracker.complete(); return tracker.getResponse(); }, executor); }
From source file:org.apache.bookkeeper.client.UpdateLedgerOp.java
/** * Update the bookie id present in the ledger metadata. * * @param oldBookieId/* w ww . ja v a 2s . c om*/ * current bookie id * @param newBookieId * new bookie id * @param rate * number of ledgers updating per second (default 5 per sec) * @param limit * maximum number of ledgers to update (default: no limit). Stop * update if reaching limit * @param progressable * report progress of the ledger updates * @throws IOException * if there is an error when updating bookie id in ledger * metadata * @throws InterruptedException * interrupted exception when update ledger meta */ public void updateBookieIdInLedgers(final BookieSocketAddress oldBookieId, final BookieSocketAddress newBookieId, final int rate, final int limit, final UpdateLedgerNotifier progressable) throws BKException, IOException { final ThreadFactoryBuilder tfb = new ThreadFactoryBuilder().setNameFormat("UpdateLedgerThread") .setDaemon(true); final ExecutorService executor = Executors.newSingleThreadExecutor(tfb.build()); final AtomicInteger issuedLedgerCnt = new AtomicInteger(); final AtomicInteger updatedLedgerCnt = new AtomicInteger(); final Future<?> updateBookieCb = executor.submit(new Runnable() { @Override public void run() { updateLedgers(oldBookieId, newBookieId, rate, limit, progressable); } private void updateLedgers(final BookieSocketAddress oldBookieId, final BookieSocketAddress newBookieId, final int rate, final int limit, final UpdateLedgerNotifier progressable) { try { final AtomicBoolean stop = new AtomicBoolean(false); final Set<Long> outstandings = Collections .newSetFromMap(new ConcurrentHashMap<Long, Boolean>()); final RateLimiter throttler = RateLimiter.create(rate); final Iterator<Long> ledgerItr = admin.listLedgers().iterator(); final CountDownLatch syncObj = new CountDownLatch(1); // iterate through all the ledgers while (ledgerItr.hasNext() && !stop.get()) { // throttler to control updates per second throttler.acquire(); final Long lId = ledgerItr.next(); final ReadLedgerMetadataCb readCb = new ReadLedgerMetadataCb(bkc, lId, oldBookieId, newBookieId); outstandings.add(lId); FutureCallback<Void> updateLedgerCb = new UpdateLedgerCb(lId, stop, issuedLedgerCnt, updatedLedgerCnt, outstandings, syncObj, progressable); Futures.addCallback(readCb.getFutureListener(), updateLedgerCb); issuedLedgerCnt.incrementAndGet(); if (limit != Integer.MIN_VALUE && issuedLedgerCnt.get() >= limit || !ledgerItr.hasNext()) { stop.set(true); } bkc.getLedgerManager().readLedgerMetadata(lId, readCb); } // waiting till all the issued ledgers are finished syncObj.await(); } catch (IOException ioe) { LOG.error("Exception while updating ledger", ioe); throw new RuntimeException("Exception while updating ledger", ioe.getCause()); } catch (InterruptedException ie) { LOG.error("Exception while updating ledger metadata", ie); Thread.currentThread().interrupt(); throw new RuntimeException("Exception while updating ledger", ie.getCause()); } } }); try { // Wait to finish the issued ledgers. updateBookieCb.get(); } catch (ExecutionException ee) { throw new IOException("Exception while updating ledger", ee); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); throw new IOException("Exception while updating ledger", ie); } finally { executor.shutdown(); } }