Skip to content

Commit fa23583

Browse files
committed
Adding programmatic timer sample
1 parent b3ae40f commit fa23583

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.javaee7.ejb.timer;
2+
3+
import javax.annotation.PostConstruct;
4+
import javax.annotation.Resource;
5+
import javax.ejb.*;
6+
import javax.enterprise.event.Event;
7+
import javax.inject.Inject;
8+
9+
/**
10+
* author: Jacek Jackowiak
11+
*/
12+
@Startup
13+
@Singleton
14+
public class ProgrammaticTimerBean {
15+
16+
@Inject
17+
Event<Ping> pingEvent;
18+
19+
@Resource
20+
TimerService timerService;
21+
22+
@PostConstruct
23+
public void initialize(){
24+
ScheduleExpression scheduleExpression = new ScheduleExpression();
25+
scheduleExpression.hour("*");
26+
scheduleExpression.minute("*");
27+
scheduleExpression.second("*/5");
28+
29+
TimerConfig timerConfig = new TimerConfig();
30+
timerConfig.setInfo("Every 5 second timer");
31+
32+
timerService.createCalendarTimer(scheduleExpression, timerConfig);
33+
}
34+
35+
@Timeout
36+
public void programmaticTimout(Timer timer) {
37+
pingEvent.fire(new Ping(timer.getInfo().toString()));
38+
}
39+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.javaee7.ejb.timer;
2+
3+
import org.hamcrest.BaseMatcher;
4+
import org.hamcrest.Description;
5+
import org.hamcrest.Matcher;
6+
import org.jboss.arquillian.container.test.api.Deployment;
7+
import org.jboss.arquillian.junit.Arquillian;
8+
import org.jboss.shrinkwrap.api.ShrinkWrap;
9+
import org.jboss.shrinkwrap.api.spec.WebArchive;
10+
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
14+
import javax.inject.Inject;
15+
import java.io.File;
16+
17+
import static com.jayway.awaitility.Awaitility.await;
18+
import static com.jayway.awaitility.Awaitility.to;
19+
import static org.hamcrest.MatcherAssert.assertThat;
20+
import static org.hamcrest.Matchers.equalTo;
21+
import static org.hamcrest.Matchers.is;
22+
23+
/**
24+
* author: Jacek Jackowiak
25+
*/
26+
@RunWith(Arquillian.class)
27+
public class ProgrammaticTimerBeanTest {
28+
29+
final static long TIMEOUT = 5000l;
30+
final static long TOLERANCE = 1000l;
31+
32+
@Inject
33+
PingsListener pings;
34+
35+
@Deployment
36+
public static WebArchive deploy() {
37+
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
38+
.resolve("com.jayway.awaitility:awaitility")
39+
.withTransitivity().asFile();
40+
41+
return ShrinkWrap.create(WebArchive.class)
42+
.addAsLibraries(jars)
43+
.addClasses(Ping.class, PingsListener.class, ProgrammaticTimerBean.class);
44+
}
45+
46+
@Test
47+
public void should_receive_two_pings() {
48+
await().untilCall(to(pings.getPings()).size(), equalTo(2));
49+
50+
Ping firstPing = pings.getPings().get(0);
51+
Ping secondPing = pings.getPings().get(1);
52+
53+
long delay = secondPing.getTime() - firstPing.getTime();
54+
System.out.println("Actual timeout = " + delay);
55+
assertThat(delay, is(withinWindow(TIMEOUT, TOLERANCE)));
56+
}
57+
58+
private Matcher<Long> withinWindow(final long timeout, final long tolerance) {
59+
return new BaseMatcher<Long>() {
60+
@Override
61+
public boolean matches(Object item) {
62+
final Long actual = (Long) item;
63+
return Math.abs(actual - timeout) < tolerance;
64+
}
65+
66+
@Override
67+
public void describeTo(Description description) {
68+
//To change body of implemented methods use File | Settings | File Templates.
69+
}
70+
};
71+
}
72+
73+
}

0 commit comments

Comments
 (0)