Skip to content

Commit e32c331

Browse files
committed
Adding multiple timers sample
1 parent fa23583 commit e32c331

File tree

4 files changed

+212
-0
lines changed

4 files changed

+212
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.javaee7.ejb.timer;
2+
3+
import javax.ejb.Schedule;
4+
import javax.ejb.Singleton;
5+
import javax.ejb.Startup;
6+
import javax.ejb.Timer;
7+
import javax.enterprise.event.Event;
8+
import javax.inject.Inject;
9+
10+
/**
11+
* @author Jacek Jackowiak
12+
*/
13+
@Startup
14+
@Singleton
15+
public class MultipleScheduleTimerBean {
16+
17+
@Inject
18+
Event<Ping> pingEvent;
19+
20+
@Schedule(hour = "*", minute = "*", second = "*/5", info = "Every 5 second timer")
21+
public void automaticallyScheduled(Timer timer) {
22+
fireEvent(timer);
23+
}
24+
25+
@Schedule(hour = "*", minute = "*", second = "*/10", info = "Every 10 second timer")
26+
public void automaticallyScheduled2(Timer timer) {
27+
fireEvent(timer);
28+
}
29+
30+
private void fireEvent(Timer timer) {
31+
pingEvent.fire(new Ping(timer.getInfo().toString()));
32+
}
33+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.javaee7.ejb.timer;
2+
3+
import javax.ejb.*;
4+
import javax.enterprise.event.Event;
5+
import javax.inject.Inject;
6+
7+
/**
8+
* @author Jacek Jackowiak
9+
*/
10+
@Startup
11+
@Singleton
12+
public class SchedulesTimerBean {
13+
14+
@Inject
15+
Event<Ping> pingEvent;
16+
17+
@Schedules({
18+
@Schedule(hour = "*", minute = "*", second = "*/5", info = "Every 5 second timer"),
19+
@Schedule(hour = "*", minute = "*", second = "*/10", info = "Every 10 second timer")
20+
})
21+
public void automaticallyScheduled(Timer timer) {
22+
pingEvent.fire(new Ping(timer.getInfo().toString()));
23+
}
24+
25+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 MultipleScheduleTimerBeanTest {
28+
29+
final static long TIMEOUT = 0l;
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, MultipleScheduleTimerBean.class);
44+
}
45+
46+
@Test
47+
public void should_receive_three_pings() {
48+
await().untilCall(to(pings.getPings()).size(), equalTo(3));
49+
50+
Ping firstPing = pings.getPings().get(0);
51+
Ping secondPing = pings.getPings().get(1);
52+
Ping thirdPing = pings.getPings().get(2);
53+
54+
long delay = secondPing.getTime() - firstPing.getTime();
55+
System.out.println("Actual timeout = " + delay);
56+
long delay2 = thirdPing.getTime() - secondPing.getTime();
57+
System.out.println("Actual timeout = " + delay2);
58+
long smallerDelay = Math.min(delay, delay2);
59+
assertThat(smallerDelay, is(withinWindow(TIMEOUT, TOLERANCE)));
60+
}
61+
62+
private Matcher<Long> withinWindow(final long timeout, final long tolerance) {
63+
return new BaseMatcher<Long>() {
64+
@Override
65+
public boolean matches(Object item) {
66+
final Long actual = (Long) item;
67+
return Math.abs(actual - timeout) < tolerance;
68+
}
69+
70+
@Override
71+
public void describeTo(Description description) {
72+
//To change body of implemented methods use File | Settings | File Templates.
73+
}
74+
};
75+
}
76+
77+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 SchedulesTimerBeanTest {
28+
29+
final static long TIMEOUT = 0l;
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, SchedulesTimerBean.class);
44+
}
45+
46+
@Test
47+
public void should_receive_three_pings() {
48+
await().untilCall(to(pings.getPings()).size(), equalTo(3));
49+
50+
Ping firstPing = pings.getPings().get(0);
51+
Ping secondPing = pings.getPings().get(1);
52+
Ping thirdPing = pings.getPings().get(2);
53+
54+
long delay = secondPing.getTime() - firstPing.getTime();
55+
System.out.println("Actual timeout = " + delay);
56+
long delay2 = thirdPing.getTime() - secondPing.getTime();
57+
System.out.println("Actual timeout = " + delay2);
58+
long smallerDelay = Math.min(delay, delay2);
59+
assertThat(smallerDelay, is(withinWindow(TIMEOUT, TOLERANCE)));
60+
}
61+
62+
private Matcher<Long> withinWindow(final long timeout, final long tolerance) {
63+
return new BaseMatcher<Long>() {
64+
@Override
65+
public boolean matches(Object item) {
66+
final Long actual = (Long) item;
67+
return Math.abs(actual - timeout) < tolerance;
68+
}
69+
70+
@Override
71+
public void describeTo(Description description) {
72+
//To change body of implemented methods use File | Settings | File Templates.
73+
}
74+
};
75+
}
76+
77+
}

0 commit comments

Comments
 (0)