org.apache.activemq.artemis.utils.TokenBucketLimiter

Here are the examples of the java api class org.apache.activemq.artemis.utils.TokenBucketLimiter taken from open source projects.

1. SoakSender#run()

Project: activemq-artemis
File: SoakSender.java
public void run() throws Exception {
    connect();
    boolean runInfinitely = perfParams.getDurationInMinutes() == -1;
    BytesMessage message = session.createBytesMessage();
    byte[] payload = SoakBase.randomByteArray(perfParams.getMessageSize());
    message.writeBytes(payload);
    final int modulo = 10000;
    TokenBucketLimiter tbl = perfParams.getThrottleRate() != -1 ? new TokenBucketLimiterImpl(perfParams.getThrottleRate(), false) : null;
    boolean transacted = perfParams.isSessionTransacted();
    int txBatchSize = perfParams.getBatchSize();
    boolean display = true;
    long start = System.currentTimeMillis();
    long moduleStart = start;
    AtomicLong count = new AtomicLong(0);
    while (true) {
        try {
            producer.send(message);
            count.incrementAndGet();
            if (transacted) {
                if (count.longValue() % txBatchSize == 0) {
                    session.commit();
                }
            }
            long totalDuration = System.currentTimeMillis() - start;
            if (display && count.longValue() % modulo == 0) {
                double duration = (1.0 * System.currentTimeMillis() - moduleStart) / 1000;
                moduleStart = System.currentTimeMillis();
                SoakSender.log.info(String.format("sent %s messages in %2.2fs (time: %.0fs)", modulo, duration, totalDuration / 1000.0));
            }
            if (tbl != null) {
                tbl.limit();
            }
            if (!runInfinitely && totalDuration > perfParams.getDurationInMinutes() * SoakBase.TO_MILLIS) {
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    SoakSender.log.info(String.format("Sent %s messages in %s minutes", count, perfParams.getDurationInMinutes()));
    SoakSender.log.info("END OF RUN");
    if (connection != null) {
        connection.close();
        connection = null;
    }
}

2. PerfBase#sendMessages()

Project: activemq-artemis
File: PerfBase.java
private void sendMessages(final int numberOfMessages, final int txBatchSize, final boolean durable, final boolean transacted, final boolean display, final int throttleRate, final int messageSize) throws Exception {
    MessageProducer producer = session.createProducer(destination);
    producer.setDeliveryMode(perfParams.isDurable() ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);
    producer.setDisableMessageID(perfParams.isDisableMessageID());
    producer.setDisableMessageTimestamp(perfParams.isDisableTimestamp());
    BytesMessage message = session.createBytesMessage();
    byte[] payload = PerfBase.randomByteArray(messageSize);
    message.writeBytes(payload);
    final int modulo = 2000;
    TokenBucketLimiter tbl = throttleRate != -1 ? new TokenBucketLimiterImpl(throttleRate, false) : null;
    boolean committed = false;
    for (int i = 1; i <= numberOfMessages; i++) {
        producer.send(message);
        if (transacted) {
            if (i % txBatchSize == 0) {
                session.commit();
                committed = true;
            } else {
                committed = false;
            }
        }
        if (display && i % modulo == 0) {
            double duration = (1.0 * System.currentTimeMillis() - start) / 1000;
            PerfBase.log.info(String.format("sent %6d messages in %2.2fs", i, duration));
        }
        if (tbl != null) {
            tbl.limit();
        }
    }
    if (transacted && !committed) {
        session.commit();
    }
}