jee7schedule
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import javax.ejb.Schedule;
|
||||||
|
import javax.ejb.Singleton;
|
||||||
|
import javax.ejb.Startup;
|
||||||
|
import javax.enterprise.event.Event;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
|
||||||
|
@Startup
|
||||||
|
@Singleton
|
||||||
|
public class AutomaticTimerBean {
|
||||||
|
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Event<TimerEvent> event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will be called every 10 second and will fire an @TimerEvent
|
||||||
|
*/
|
||||||
|
@Schedule(hour = "*", minute = "*", second = "*/10", info = "Every 10 second timer")
|
||||||
|
public void printDate() {
|
||||||
|
event.fire(new TimerEvent("TimerEvent sent at :" + new Date()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import javax.ejb.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by ccristianchiovari on 5/2/16.
|
||||||
|
*/
|
||||||
|
@Singleton
|
||||||
|
public class FixedDelayTimerBean {
|
||||||
|
|
||||||
|
@EJB
|
||||||
|
private WorkerBean workerBean;
|
||||||
|
|
||||||
|
@Lock(LockType.READ)
|
||||||
|
@Schedule(second = "*/5", minute = "*", hour = "*", persistent = false)
|
||||||
|
public void atSchedule() throws InterruptedException {
|
||||||
|
workerBean.doTimerWork();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.ejb.*;
|
||||||
|
import javax.enterprise.event.Event;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* author: Cristian Chiovari
|
||||||
|
*/
|
||||||
|
@Startup
|
||||||
|
@Singleton
|
||||||
|
public class ProgrammaticAtFixedRateTimerBean {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Event<TimerEvent> event;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
TimerService timerService;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void initialize() {
|
||||||
|
timerService.createTimer(0,1000, "Every second timer");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Timeout
|
||||||
|
public void programmaticTimout(Timer timer) {
|
||||||
|
event.fire(new TimerEvent(timer.getInfo().toString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.ejb.*;
|
||||||
|
import javax.enterprise.event.Event;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* author: Jacek Jackowiak
|
||||||
|
*/
|
||||||
|
@Startup
|
||||||
|
@Singleton
|
||||||
|
public class ProgrammaticTimerBean {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Event<TimerEvent> event;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
TimerService timerService;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void initialize() {
|
||||||
|
ScheduleExpression scheduleExpression = new ScheduleExpression()
|
||||||
|
.hour("*")
|
||||||
|
.minute("*")
|
||||||
|
.second("*/5");
|
||||||
|
|
||||||
|
TimerConfig timerConfig = new TimerConfig();
|
||||||
|
timerConfig.setInfo("Every 5 second timer");
|
||||||
|
|
||||||
|
timerService.createCalendarTimer(scheduleExpression, timerConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Timeout
|
||||||
|
public void programmaticTimout(Timer timer) {
|
||||||
|
event.fire(new TimerEvent(timer.getInfo().toString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
+31
@@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.ejb.*;
|
||||||
|
import javax.enterprise.event.Event;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* author: Cristian Chiovari
|
||||||
|
*/
|
||||||
|
@Startup
|
||||||
|
@Singleton
|
||||||
|
public class ProgrammaticWithInitialFixedDelayTimerBean {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Event<TimerEvent> event;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
TimerService timerService;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void initialize() {
|
||||||
|
timerService.createTimer(10000l,5000l, "Delay 10 seconds then every 5 second timer");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Timeout
|
||||||
|
public void programmaticTimout(Timer timer) {
|
||||||
|
event.fire(new TimerEvent(timer.getInfo().toString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import javax.ejb.Schedule;
|
||||||
|
import javax.ejb.Singleton;
|
||||||
|
import javax.ejb.Startup;
|
||||||
|
import javax.ejb.Timer;
|
||||||
|
import javax.enterprise.event.Event;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
|
||||||
|
@Startup
|
||||||
|
@Singleton
|
||||||
|
public class ScheduleTimerBean {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Event<TimerEvent> event;
|
||||||
|
|
||||||
|
@Schedule(hour = "*", minute = "*", second = "*/5", info = "Every 5 second timer")
|
||||||
|
public void automaticallyScheduled(Timer timer) {
|
||||||
|
fireEvent(timer);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void fireEvent(Timer timer) {
|
||||||
|
event.fire(new TimerEvent(timer.getInfo().toString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
public class TimerEvent {
|
||||||
|
|
||||||
|
private String eventInfo;
|
||||||
|
private long time = System.currentTimeMillis();
|
||||||
|
|
||||||
|
public TimerEvent(String s) {
|
||||||
|
this.eventInfo = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getTime() {
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getEventInfo() {
|
||||||
|
return eventInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "TimerEvent {" +
|
||||||
|
"eventInfo='" + eventInfo + '\'' +
|
||||||
|
", time=" + time +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import javax.ejb.Singleton;
|
||||||
|
import javax.ejb.Startup;
|
||||||
|
import javax.enterprise.event.Observes;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class will listen to all TimerEvent and will collect them
|
||||||
|
*/
|
||||||
|
@Startup
|
||||||
|
@Singleton
|
||||||
|
public class TimerEventListener {
|
||||||
|
|
||||||
|
final List<TimerEvent> events = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
|
public void listen(@Observes TimerEvent event) {
|
||||||
|
System.out.println("event = " + event);
|
||||||
|
events.add(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<TimerEvent> getEvents() {
|
||||||
|
return events;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import javax.ejb.Lock;
|
||||||
|
import javax.ejb.LockType;
|
||||||
|
import javax.ejb.Singleton;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by cristianchiovari on 5/2/16.
|
||||||
|
*/
|
||||||
|
@Singleton
|
||||||
|
public class WorkerBean {
|
||||||
|
|
||||||
|
private AtomicBoolean busy = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
@Lock(LockType.READ)
|
||||||
|
public void doTimerWork() throws InterruptedException {
|
||||||
|
|
||||||
|
System.out.println("Timer method called but not started yet !");
|
||||||
|
|
||||||
|
if (!busy.compareAndSet(false, true)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
System.out.println("Timer work started");
|
||||||
|
Thread.sleep(12000);
|
||||||
|
System.out.println("Timer work done");
|
||||||
|
} finally {
|
||||||
|
busy.set(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import com.jayway.awaitility.Awaitility;
|
||||||
|
import org.hamcrest.Matchers;
|
||||||
|
import org.jboss.arquillian.container.test.api.Deployment;
|
||||||
|
import org.jboss.arquillian.junit.Arquillian;
|
||||||
|
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||||
|
import org.jboss.shrinkwrap.api.spec.WebArchive;
|
||||||
|
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static com.jayway.awaitility.Awaitility.await;
|
||||||
|
import static com.jayway.awaitility.Awaitility.to;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
||||||
|
|
||||||
|
@RunWith(Arquillian.class)
|
||||||
|
public class AutomaticTimerBeanTest {
|
||||||
|
|
||||||
|
//the @AutomaticTimerBean has a method called every 10 seconds
|
||||||
|
//testing the difference ==> 100000
|
||||||
|
final static long TIMEOUT = 10000l;
|
||||||
|
|
||||||
|
//the tolerance accepted , so if between two consecutive calls there has to be at least 9 or max 11 seconds.
|
||||||
|
//because the timer service is not intended for real-time applications so it will not be exactly 10 seconds
|
||||||
|
final static long TOLERANCE = 1000l;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
TimerEventListener timerEventListener;
|
||||||
|
|
||||||
|
@Deployment
|
||||||
|
public static WebArchive deploy() {
|
||||||
|
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
|
||||||
|
.resolve("com.jayway.awaitility:awaitility")
|
||||||
|
.withTransitivity().asFile();
|
||||||
|
|
||||||
|
//only @AutomaticTimerBean is deployed not the other timers
|
||||||
|
return ShrinkWrap.create(WebArchive.class)
|
||||||
|
.addAsLibraries(jars)
|
||||||
|
.addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, AutomaticTimerBean.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_receive_two_pings() {
|
||||||
|
Awaitility.setDefaultTimeout(30, TimeUnit.SECONDS);
|
||||||
|
//the test will wait here until two events are triggered
|
||||||
|
await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(2));
|
||||||
|
|
||||||
|
TimerEvent firstEvent = timerEventListener.getEvents().get(0);
|
||||||
|
TimerEvent secondEvent = timerEventListener.getEvents().get(1);
|
||||||
|
|
||||||
|
long delay = secondEvent.getTime() - firstEvent.getTime();
|
||||||
|
System.out.println("Actual timeout = " + delay);
|
||||||
|
|
||||||
|
//ensure that the delay between the events is more or less 10 seconds (no real time precision)
|
||||||
|
assertThat(delay, Matchers.is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||||
|
}
|
||||||
|
}
|
||||||
+56
@@ -0,0 +1,56 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import com.jayway.awaitility.Awaitility;
|
||||||
|
import org.jboss.arquillian.container.test.api.Deployment;
|
||||||
|
import org.jboss.arquillian.junit.Arquillian;
|
||||||
|
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||||
|
import org.jboss.shrinkwrap.api.spec.WebArchive;
|
||||||
|
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static com.jayway.awaitility.Awaitility.await;
|
||||||
|
import static com.jayway.awaitility.Awaitility.to;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
|
||||||
|
|
||||||
|
@RunWith(Arquillian.class)
|
||||||
|
public class ProgrammaticAtFixedRateTimerBeanTest {
|
||||||
|
|
||||||
|
final static long TIMEOUT = 1000;
|
||||||
|
final static long TOLERANCE = 500l;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
TimerEventListener timerEventListener;
|
||||||
|
|
||||||
|
@Deployment
|
||||||
|
public static WebArchive deploy() {
|
||||||
|
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
|
||||||
|
.resolve("com.jayway.awaitility:awaitility")
|
||||||
|
.withTransitivity().asFile();
|
||||||
|
|
||||||
|
return ShrinkWrap.create(WebArchive.class)
|
||||||
|
.addAsLibraries(jars)
|
||||||
|
.addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, ProgrammaticAtFixedRateTimerBean.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_receive_ten_pings() {
|
||||||
|
|
||||||
|
Awaitility.setDefaultTimeout(30, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(10));
|
||||||
|
TimerEvent firstEvent = timerEventListener.getEvents().get(0);
|
||||||
|
TimerEvent secondEvent = timerEventListener.getEvents().get(1);
|
||||||
|
|
||||||
|
long delay = secondEvent.getTime() - firstEvent.getTime();
|
||||||
|
System.out.println("Actual timeout = " + delay);
|
||||||
|
assertThat(delay, is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import org.jboss.arquillian.container.test.api.Deployment;
|
||||||
|
import org.jboss.arquillian.junit.Arquillian;
|
||||||
|
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||||
|
import org.jboss.shrinkwrap.api.spec.WebArchive;
|
||||||
|
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import static com.jayway.awaitility.Awaitility.await;
|
||||||
|
import static com.jayway.awaitility.Awaitility.to;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
|
||||||
|
|
||||||
|
@RunWith(Arquillian.class)
|
||||||
|
public class ProgrammaticTimerBeanTest {
|
||||||
|
|
||||||
|
final static long TIMEOUT = 5000l;
|
||||||
|
final static long TOLERANCE = 1000l;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
TimerEventListener timerEventListener;
|
||||||
|
|
||||||
|
@Deployment
|
||||||
|
public static WebArchive deploy() {
|
||||||
|
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
|
||||||
|
.resolve("com.jayway.awaitility:awaitility")
|
||||||
|
.withTransitivity().asFile();
|
||||||
|
|
||||||
|
return ShrinkWrap.create(WebArchive.class)
|
||||||
|
.addAsLibraries(jars)
|
||||||
|
.addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, ProgrammaticTimerBean.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_receive_two_pings() {
|
||||||
|
|
||||||
|
await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(2));
|
||||||
|
|
||||||
|
TimerEvent firstEvent = timerEventListener.getEvents().get(0);
|
||||||
|
TimerEvent secondEvent = timerEventListener.getEvents().get(1);
|
||||||
|
|
||||||
|
long delay = secondEvent.getTime() - firstEvent.getTime();
|
||||||
|
System.out.println("Actual timeout = " + delay);
|
||||||
|
assertThat(delay, is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||||
|
}
|
||||||
|
}
|
||||||
+61
@@ -0,0 +1,61 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import com.jayway.awaitility.Awaitility;
|
||||||
|
import org.jboss.arquillian.container.test.api.Deployment;
|
||||||
|
import org.jboss.arquillian.junit.Arquillian;
|
||||||
|
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||||
|
import org.jboss.shrinkwrap.api.spec.WebArchive;
|
||||||
|
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static com.jayway.awaitility.Awaitility.await;
|
||||||
|
import static com.jayway.awaitility.Awaitility.to;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
|
||||||
|
|
||||||
|
@RunWith(Arquillian.class)
|
||||||
|
public class ProgrammaticWithFixedDelayTimerBeanTest {
|
||||||
|
|
||||||
|
final static long TIMEOUT = 15000l;
|
||||||
|
final static long TOLERANCE = 1000l;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
TimerEventListener timerEventListener;
|
||||||
|
|
||||||
|
@Deployment
|
||||||
|
public static WebArchive deploy() {
|
||||||
|
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
|
||||||
|
.resolve("com.jayway.awaitility:awaitility")
|
||||||
|
.withTransitivity().asFile();
|
||||||
|
|
||||||
|
return ShrinkWrap.create(WebArchive.class)
|
||||||
|
.addAsLibraries(jars)
|
||||||
|
.addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, ProgrammaticWithInitialFixedDelayTimerBean.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_receive_two_pings() {
|
||||||
|
|
||||||
|
Awaitility.setDefaultTimeout(30, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
// 10 seconds pause so we get the startTime and it will trigger first event
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(2));
|
||||||
|
TimerEvent firstEvent = timerEventListener.getEvents().get(0);
|
||||||
|
TimerEvent secondEvent = timerEventListener.getEvents().get(1);
|
||||||
|
|
||||||
|
long delay = secondEvent.getTime() - startTime;
|
||||||
|
System.out.println("Actual timeout = " + delay);
|
||||||
|
|
||||||
|
//apx 15 seconds = 10 delay + 2 timers (first after a pause of 10 seconds and the next others every 5 seconds)
|
||||||
|
assertThat(delay, is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import com.jayway.awaitility.Awaitility;
|
||||||
|
import org.hamcrest.Matchers;
|
||||||
|
import org.jboss.arquillian.container.test.api.Deployment;
|
||||||
|
import org.jboss.arquillian.junit.Arquillian;
|
||||||
|
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||||
|
import org.jboss.shrinkwrap.api.spec.WebArchive;
|
||||||
|
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static com.jayway.awaitility.Awaitility.await;
|
||||||
|
import static com.jayway.awaitility.Awaitility.to;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
|
||||||
|
|
||||||
|
@RunWith(Arquillian.class)
|
||||||
|
public class ScheduleTimerBeanTest {
|
||||||
|
|
||||||
|
final static long TIMEOUT = 5000l;
|
||||||
|
final static long TOLERANCE = 1000l;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
TimerEventListener timerEventListener;
|
||||||
|
|
||||||
|
@Deployment
|
||||||
|
public static WebArchive deploy() {
|
||||||
|
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
|
||||||
|
.resolve("com.jayway.awaitility:awaitility")
|
||||||
|
.withTransitivity().asFile();
|
||||||
|
|
||||||
|
return ShrinkWrap.create(WebArchive.class)
|
||||||
|
.addAsLibraries(jars)
|
||||||
|
.addClasses(WithinWindowMatcher.class, TimerEvent.class, TimerEventListener.class, ScheduleTimerBean.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void should_receive_three_pings() {
|
||||||
|
|
||||||
|
Awaitility.setDefaultTimeout(30, TimeUnit.SECONDS);
|
||||||
|
await().untilCall(to(timerEventListener.getEvents()).size(), equalTo(3));
|
||||||
|
|
||||||
|
TimerEvent firstEvent = timerEventListener.getEvents().get(0);
|
||||||
|
TimerEvent secondEvent = timerEventListener.getEvents().get(1);
|
||||||
|
TimerEvent thirdEvent = timerEventListener.getEvents().get(2);
|
||||||
|
|
||||||
|
long delay = secondEvent.getTime() - firstEvent.getTime();
|
||||||
|
assertThat(delay, Matchers.is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||||
|
delay = thirdEvent.getTime() - secondEvent.getTime();
|
||||||
|
assertThat(delay, Matchers.is(WithinWindowMatcher.withinWindow(TIMEOUT, TOLERANCE)));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.baeldung.timer;
|
||||||
|
|
||||||
|
import org.hamcrest.BaseMatcher;
|
||||||
|
import org.hamcrest.Description;
|
||||||
|
import org.hamcrest.Matcher;
|
||||||
|
|
||||||
|
class WithinWindowMatcher extends BaseMatcher<Long> {
|
||||||
|
private final long timeout;
|
||||||
|
private final long tolerance;
|
||||||
|
|
||||||
|
public WithinWindowMatcher(long timeout, long tolerance) {
|
||||||
|
this.timeout = timeout;
|
||||||
|
this.tolerance = tolerance;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matches(Object item) {
|
||||||
|
final Long actual = (Long) item;
|
||||||
|
return Math.abs(actual - timeout) < tolerance;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void describeTo(Description description) {
|
||||||
|
//To change body of implemented methods use File | Settings | File Templates.
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Matcher<Long> withinWindow(final long timeout, final long tolerance) {
|
||||||
|
return new WithinWindowMatcher(timeout, tolerance);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,6 +23,7 @@
|
|||||||
<module>jooq-spring</module>
|
<module>jooq-spring</module>
|
||||||
<module>json-path</module>
|
<module>json-path</module>
|
||||||
<module>mockito</module>
|
<module>mockito</module>
|
||||||
|
<module>jee7schedule</module>
|
||||||
<!-- <module>jpa-storedprocedure</module> -->
|
<!-- <module>jpa-storedprocedure</module> -->
|
||||||
<module>querydsl</module>
|
<module>querydsl</module>
|
||||||
<!-- <module>raml</module> -->
|
<!-- <module>raml</module> -->
|
||||||
|
|||||||
Reference in New Issue
Block a user