use of org.motechproject.event.MotechEvent in project motech by motech.
the class TestListener method scheduleJobs.
private void scheduleJobs(MotechSchedulerFactoryBean schedulerFactoryBean, SettingsFacade settings) throws InterruptedException, SchedulerException {
Scheduler scheduler = schedulerFactoryBean.getQuartzScheduler();
scheduler.clear();
try {
Map<String, Object> params = new HashMap<>();
params.put("foo", "bar");
params.put("fii", "bor");
params.put("fuu", "baz");
String eventSubject = "test_event";
MotechEvent motechEvent = new MotechEvent(eventSubject, params);
int maxJobs = 1;
int repeatCount = 1;
TestListener listener = new TestListener("test_listener", maxJobs, repeatCount);
eventListenerRegistryService.registerListener(listener, eventSubject);
DateTime startTime = now().plusSeconds(0);
Integer repeatInterval = 1;
System.out.println("startTime: " + startTime);
NanoStopWatch timeToSchedule = new NanoStopWatch().start();
for (int i = 0; i < maxJobs; i++) {
params.put(MotechSchedulerService.JOB_ID_KEY, "test_job_" + String.valueOf(i));
RepeatingSchedulableJob repeatingJob = new RepeatingSchedulableJob(motechEvent, repeatCount, repeatInterval, startTime, null, false);
repeatingJob.setUseOriginalFireTimeAfterMisfire(false);
schedulerService.scheduleRepeatingJob(repeatingJob);
}
System.out.println(format("Time to schedule : %dms", timeToSchedule.duration()));
scheduler.start();
synchronized (listener) {
while (scheduler.getTriggerKeys(GroupMatcher.triggerGroupContains("default")).size() > 0) {
listener.wait(5000);
}
}
System.out.println(listener.getReport());
assertEquals(maxJobs * (repeatCount + 1), listener.getCount());
} finally {
eventListenerRegistryService.clearListenersForBean("test_listener");
schedulerFactoryBean.getQuartzScheduler().standby();
}
}
use of org.motechproject.event.MotechEvent in project motech by motech.
the class JobIdTest method initializeUsingMotechEvent.
@Test
public void initializeUsingMotechEvent() {
MotechEvent motechEvent = new MotechEvent(SUBJECT_VALUE);
motechEvent.getParameters().put(MotechSchedulerService.JOB_ID_KEY, JOB_ID_VALUE);
JobId jobId = new CronJobId(motechEvent);
assertEquals(String.format("%s-%s", SUBJECT_VALUE, JOB_ID_VALUE), jobId.value());
jobId = new RepeatingJobId(motechEvent);
assertEquals(String.format("%s-%s%s", SUBJECT_VALUE, JOB_ID_VALUE, RepeatingJobId.SUFFIX_REPEATJOBID), jobId.value());
}
use of org.motechproject.event.MotechEvent in project motech by motech.
the class TestMotechScheduledEvent method equalsTest.
@Test
public void equalsTest() throws Exception {
MotechEvent motechEvent = new MotechEvent("testEvent", null);
MotechEvent motechEventSame = new MotechEvent("testEvent", null);
MotechEvent motechEventDifferentJobId = new MotechEvent("testEvent", null);
MotechEvent scheduledEventDifferentEventType = new MotechEvent("testEvent2", null);
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("One", 1);
MotechEvent nonNullParams = new MotechEvent("testEvent", hashMap);
MotechEvent nonNullParams2 = new MotechEvent("testEvent", hashMap);
assertTrue(motechEvent.equals(motechEvent));
assertTrue(motechEvent.equals(motechEventSame));
assertTrue(nonNullParams.equals(nonNullParams2));
assertFalse(motechEvent.equals(null));
assertFalse(motechEvent.equals(uuidStr));
assertFalse(motechEvent.equals(scheduledEventDifferentEventType));
assertFalse(motechEvent.equals(nonNullParams));
assertFalse(nonNullParams.equals(motechEvent));
}
use of org.motechproject.event.MotechEvent in project motech by motech.
the class TestMotechScheduledEvent method testGetParameters.
@Test
public void testGetParameters() {
MotechEvent motechEvent = new MotechEvent("testEvent", null);
Map<String, Object> params = motechEvent.getParameters();
assertNotNull("Expecting param object", params);
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("One", 1);
MotechEvent nonNullParams = new MotechEvent("testEvent", hashMap);
params = nonNullParams.getParameters();
assertTrue(params.equals(hashMap));
// assertFalse(params == hashMap); // not wrapped collection anymore.
}
use of org.motechproject.event.MotechEvent in project motech by motech.
the class TestEventListener method shouldGetPreviousFireTime.
@Test
public void shouldGetPreviousFireTime() throws InterruptedException {
try {
fakeNow(new DateTime());
Map<String, Object> params = new HashMap<>();
MotechEvent event = new MotechEvent("test_event", params);
final String jobId = id("jobId");
params.put(MotechSchedulerService.JOB_ID_KEY, jobId);
DateTime now = new DateTime();
StringBuilder cron = new StringBuilder();
cron.append(now.getSecondOfMinute()).append(" ").append(now.getMinuteOfHour()).append(" ");
cron.append(now.getHourOfDay()).append(" * * ?");
schedulerService.scheduleJob(new CronSchedulableJob(event, cron.toString()));
Thread.sleep(1000);
DateTime dateTime = schedulerService.getPreviousFireDate(new CronJobId(event));
assertEquals(dateTime.getHourOfDay(), now.getHourOfDay());
assertEquals(dateTime.getMinuteOfHour(), now.getMinuteOfHour());
assertEquals(dateTime.getSecondOfMinute(), now.getSecondOfMinute());
} finally {
stopFakingTime();
}
}
Aggregations