Skip to content

Commit 7018631

Browse files
committed
Merge pull request #3 from javaee-samples/master
Update from upstream
2 parents 53bd58c + b89f18f commit 7018631

File tree

10 files changed

+313
-19
lines changed

10 files changed

+313
-19
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Some samples/tests have documentation otherwise read the code. The [Java EE 7 Es
88

99
Samples are tested on Wildfly and GlassFish using the Arquillian ecosystem.
1010

11+
A brief instruction how to clone, build, import and run the samples on your local machine @radcortez provides in this sample video https://www.youtube.com/watch?v=BB4b-Yz9cF0
12+
1113
Only one container profile and one profile for browser can be active at a given time otherwise there will be dependency conflicts.
1214

1315
There are 4 available container profiles:

ejb/timer/src/main/java/org/javaee7/ejb/timer/TimerSessionBean.java renamed to ejb/timer/src/main/java/org/javaee7/ejb/timer/AutomaticTimerBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
@Startup
1717
@Singleton
18-
public class TimerSessionBean {
18+
public class AutomaticTimerBean {
1919

2020
@Resource
2121
SessionContext ctx;
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 fastAutomaticallyScheduled(Timer timer) {
22+
fireEvent(timer);
23+
}
24+
25+
@Schedule(hour = "*", minute = "*", second = "*/10", info = "Every 10 second timer")
26+
public void slowlyAutomaticallyScheduled(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: 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+
.hour("*")
26+
.minute("*")
27+
.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: 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+
}

ejb/timer/src/test/java/org/javaee7/ejb/timer/TimerSessionBeanTest.java renamed to ejb/timer/src/test/java/org/javaee7/ejb/timer/AutomaticTimerBeanTest.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
import static com.jayway.awaitility.Awaitility.*;
1818
import static org.hamcrest.MatcherAssert.*;
1919
import static org.hamcrest.Matchers.*;
20+
import static org.javaee7.ejb.timer.WithinWindowMatcher.withinWindow;
2021

2122
/**
2223
* author: Jakub Marchwicki
2324
*/
2425
@RunWith(Arquillian.class)
25-
public class TimerSessionBeanTest {
26+
public class AutomaticTimerBeanTest {
2627

2728
final static long TIMEOUT = 5000l;
2829
final static long TOLERANCE = 1000l;
@@ -38,7 +39,7 @@ public static WebArchive deploy() {
3839

3940
return ShrinkWrap.create(WebArchive.class)
4041
.addAsLibraries(jars)
41-
.addClasses(Ping.class, PingsListener.class, TimerSessionBean.class);
42+
.addClasses(WithinWindowMatcher.class, Ping.class, PingsListener.class, AutomaticTimerBean.class);
4243
}
4344

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

0 commit comments

Comments
 (0)