final AtomicLong counter = new AtomicLong(0);
final AtomicLong insertCount = new AtomicLong(0);
final long max_count = 1000000;
final CountingQueueStats stats = new CountingQueueStats();
final ConsistencyLevel cl = ConsistencyLevel.CL_ONE;
final MessageQueue scheduler = new ShardedDistributedMessageQueue.Builder()
.withColumnFamily(SCHEDULER_NAME_CF_NAME)
.withQueueName("StressQueue"+qNameSfx)
.withKeyspace(keyspace)
.withConsistencyLevel(cl)
.withStats(stats)
.withTimeBuckets(10, 30, TimeUnit.SECONDS)
.withShardCount(100)
.withPollInterval(100L, TimeUnit.MILLISECONDS)
.withShardLockManager(slm)
.build();
scheduler.createStorage();
Thread.sleep(1000);
scheduler.createQueue();
final ConcurrentMap<String, Boolean> lookup = Maps.newConcurrentMap();
final int batchSize = 50;
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
MessageProducer producer = scheduler.createProducer();
for (int i = 0; i < max_count / batchSize; i++) {
long tm = System.currentTimeMillis();
List<Message> messages = Lists.newArrayList();
for (int j = 0; j < batchSize; j++) {
long id = insertCount.incrementAndGet();
messages.add(new Message()
.setKey("" + id)
.addParameter("data", "The quick brown fox jumped over the lazy cow " + id)
.setTimeout(0)
.setTrigger(new RunOnceTrigger.Builder()
.withDelay(j, TimeUnit.SECONDS)
.build()));
}
try {
producer.sendMessages(messages);
} catch (MessageQueueException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long sleep = 1000 - System.currentTimeMillis() - tm;
if (sleep > 0) {
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
// // Producer
// final AtomicLong iCounter = new AtomicLong(0);
// for (int j = 0; j < 1; j++) {
// executor.submit(new Runnable() {
// @Override
// public void run() {
// MessageProducer producer = scheduler.createProducer();
//
// List<Message> tasks = Lists.newArrayList();
// while (true) {
// long count = insertCount.incrementAndGet();
// if (count > max_count) {
// insertCount.decrementAndGet();
// break;
// }
// try {
// tasks.add(new Message()
// .setKey("" + count)
// .addParameter("data", "The quick brown fox jumped over the lazy cow " + count)
// // .setNextTriggerTime(TimeUnit.SECONDS.convert(tm, TimeUnit.MILLISECONDS))
//// .setTimeout(1L, TimeUnit.MINUTES)
// );
//
// if (tasks.size() == batchSize) {
// producer.sendMessages(tasks);
// tasks.clear();
// }
// } catch (Exception e) {
// LOG.error(e.getMessage());
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e1) {
// e1.printStackTrace();
// }
// }
// }
//
// if (tasks.size() == batchSize) {
// try {
// producer.sendMessages(tasks);
// } catch (MessageQueueException e) {
// e.printStackTrace();
// }
// tasks.clear();
// }
// }
// });
// }
// Status
final AtomicLong prevCount = new AtomicLong(0);
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
long newCount = insertCount.get();
// long newCount = counter.get();
// LOG.info("#### Processed : " + (newCount - prevCount.get()) + " of " + newCount + " (" + (insertCount.get() - newCount) + ")");
// LOG.info("#### Pending : " + scheduler.getTaskCount());
// for (Entry<String, Integer> shard : producer.getShardCounts().entrySet()) {
// LOG.info(" " + shard.getKey() + " : " + shard.getValue());
// }
LOG.info(stats.toString());
LOG.info("" + (newCount - prevCount.get()) + " /sec (" + newCount + ")");
prevCount.set(newCount);
// if (insertCount.get() >= max_count) {
// Map<String, Integer> counts;