use of org.apache.activemq.util.ByteSequence in project activemq-artemis by apache.
the class JobSchedulerTest method testAddStopThenDeliver.
@Test
public void testAddStopThenDeliver() throws Exception {
final int COUNT = 10;
final CountDownLatch latch = new CountDownLatch(COUNT);
long time = 2000;
for (int i = 0; i < COUNT; i++) {
String test = new String("test" + i);
scheduler.schedule("id" + i, new ByteSequence(test.getBytes()), "", time, 1000, -1);
}
File directory = store.getDirectory();
tearDown();
startStore(directory);
scheduler.addListener(new JobListener() {
@Override
public void scheduledJob(String id, ByteSequence job) {
latch.countDown();
}
});
assertTrue(latch.getCount() == COUNT);
latch.await(3000, TimeUnit.SECONDS);
assertTrue(latch.getCount() == 0);
}
use of org.apache.activemq.util.ByteSequence in project activemq-artemis by apache.
the class JobSchedulerTest method testgetAllJobs.
@Test
public void testgetAllJobs() throws Exception {
final int COUNT = 10;
final String ID = "id:";
long time = 20000;
for (int i = 0; i < COUNT; i++) {
String str = new String("test" + i);
scheduler.schedule(ID + i, new ByteSequence(str.getBytes()), "", time, 10 + i, -1);
}
List<Job> list = scheduler.getAllJobs();
assertEquals(list.size(), COUNT);
int count = 0;
for (Job job : list) {
assertEquals(job.getJobId(), ID + count);
count++;
}
}
use of org.apache.activemq.util.ByteSequence in project activemq-artemis by apache.
the class JobSchedulerTest method testAddCronAndByteSequence.
@Test
public void testAddCronAndByteSequence() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
scheduler.addListener(new JobListener() {
@Override
public void scheduledJob(String id, ByteSequence job) {
latch.countDown();
}
});
Calendar current = Calendar.getInstance();
current.add(Calendar.MINUTE, 1);
int minutes = current.get(Calendar.MINUTE);
int hour = current.get(Calendar.HOUR_OF_DAY);
int day = current.get(Calendar.DAY_OF_WEEK) - 1;
String cronTab = String.format("%d %d * * %d", minutes, hour, day);
String str = new String("test1");
scheduler.schedule("id:1", new ByteSequence(str.getBytes()), cronTab, 0, 0, 0);
// need a little slack so go over 60 seconds
assertTrue(latch.await(70, TimeUnit.SECONDS));
assertEquals(0, latch.getCount());
}
use of org.apache.activemq.util.ByteSequence in project activemq-artemis by apache.
the class JobSchedulerTest method testGetExecutionCount.
@Test
public void testGetExecutionCount() throws Exception {
final String jobId = "Job-1";
long time = 10000;
final CountDownLatch done = new CountDownLatch(10);
String str = new String("test");
scheduler.schedule(jobId, new ByteSequence(str.getBytes()), "", time, 1000, 10);
int size = scheduler.getAllJobs().size();
assertEquals(size, 1);
scheduler.addListener(new JobListener() {
@Override
public void scheduledJob(String id, ByteSequence job) {
LOG.info("Job executed: {}", 11 - done.getCount());
done.countDown();
}
});
List<Job> jobs = scheduler.getNextScheduleJobs();
assertEquals(1, jobs.size());
Job job = jobs.get(0);
assertEquals(jobId, job.getJobId());
assertEquals(0, job.getExecutionCount());
assertTrue("Should have fired ten times.", done.await(60, TimeUnit.SECONDS));
// The job is not updated on the last firing as it is removed from the store following
// it's last execution so the count will always be one less than the max firings.
assertTrue(job.getExecutionCount() >= 9);
}
use of org.apache.activemq.util.ByteSequence in project activemq-artemis by apache.
the class JobSchedulerStoreTest method testRestart.
@Test(timeout = 120 * 1000)
public void testRestart() throws Exception {
JobSchedulerStore store = new JobSchedulerStoreImpl();
File directory = new File("target/test/ScheduledDB");
IOHelper.mkdirs(directory);
IOHelper.deleteChildren(directory);
store.setDirectory(directory);
final int NUMBER = 1000;
store.start();
List<ByteSequence> list = new ArrayList<>();
for (int i = 0; i < NUMBER; i++) {
ByteSequence buff = new ByteSequence(new String("testjob" + i).getBytes());
list.add(buff);
}
JobScheduler js = store.getJobScheduler("test");
js.startDispatching();
int count = 0;
long startTime = 10 * 60 * 1000;
long period = startTime;
for (ByteSequence job : list) {
js.schedule("id:" + (count++), job, "", startTime, period, -1);
}
List<Job> test = js.getAllJobs();
LOG.debug("Found {} jobs in the store before restart", test.size());
assertEquals(list.size(), test.size());
store.stop();
store.start();
js = store.getJobScheduler("test");
test = js.getAllJobs();
LOG.debug("Found {} jobs in the store after restart", test.size());
assertEquals(list.size(), test.size());
for (int i = 0; i < list.size(); i++) {
String orig = new String(list.get(i).getData());
String payload = new String(test.get(i).getPayload());
assertEquals(orig, payload);
}
}
Aggregations