From b3ae40f937deb1664f54485a8526b98a1a4413be Mon Sep 17 00:00:00 2001 From: Jacek Jackowiak Date: Fri, 27 Mar 2015 23:13:03 +0100 Subject: [PATCH 1/6] Renaming oryginal samples --- .../timer/{TimerSessionBean.java => AutomaticTimerBean.java} | 2 +- ...{TimerSessionBeanTest.java => AutomaticTimerBeanTest.java} | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename ejb/timer/src/main/java/org/javaee7/ejb/timer/{TimerSessionBean.java => AutomaticTimerBean.java} (95%) rename ejb/timer/src/test/java/org/javaee7/ejb/timer/{TimerSessionBeanTest.java => AutomaticTimerBeanTest.java} (94%) diff --git a/ejb/timer/src/main/java/org/javaee7/ejb/timer/TimerSessionBean.java b/ejb/timer/src/main/java/org/javaee7/ejb/timer/AutomaticTimerBean.java similarity index 95% rename from ejb/timer/src/main/java/org/javaee7/ejb/timer/TimerSessionBean.java rename to ejb/timer/src/main/java/org/javaee7/ejb/timer/AutomaticTimerBean.java index 473e3bb73..88a9da361 100644 --- a/ejb/timer/src/main/java/org/javaee7/ejb/timer/TimerSessionBean.java +++ b/ejb/timer/src/main/java/org/javaee7/ejb/timer/AutomaticTimerBean.java @@ -15,7 +15,7 @@ */ @Startup @Singleton -public class TimerSessionBean { +public class AutomaticTimerBean { @Resource SessionContext ctx; diff --git a/ejb/timer/src/test/java/org/javaee7/ejb/timer/TimerSessionBeanTest.java b/ejb/timer/src/test/java/org/javaee7/ejb/timer/AutomaticTimerBeanTest.java similarity index 94% rename from ejb/timer/src/test/java/org/javaee7/ejb/timer/TimerSessionBeanTest.java rename to ejb/timer/src/test/java/org/javaee7/ejb/timer/AutomaticTimerBeanTest.java index 0882d1c03..6cfb2b78b 100644 --- a/ejb/timer/src/test/java/org/javaee7/ejb/timer/TimerSessionBeanTest.java +++ b/ejb/timer/src/test/java/org/javaee7/ejb/timer/AutomaticTimerBeanTest.java @@ -22,7 +22,7 @@ * author: Jakub Marchwicki */ @RunWith(Arquillian.class) -public class TimerSessionBeanTest { +public class AutomaticTimerBeanTest { final static long TIMEOUT = 5000l; final static long TOLERANCE = 1000l; @@ -38,7 +38,7 @@ public static WebArchive deploy() { return ShrinkWrap.create(WebArchive.class) .addAsLibraries(jars) - .addClasses(Ping.class, PingsListener.class, TimerSessionBean.class); + .addClasses(Ping.class, PingsListener.class, AutomaticTimerBean.class); } @Test From fa235833926cf24f6fed3413cbdb11b5843244cc Mon Sep 17 00:00:00 2001 From: Jacek Jackowiak Date: Fri, 27 Mar 2015 23:13:29 +0100 Subject: [PATCH 2/6] Adding programmatic timer sample --- .../ejb/timer/ProgrammaticTimerBean.java | 39 ++++++++++ .../ejb/timer/ProgrammaticTimerBeanTest.java | 73 +++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 ejb/timer/src/main/java/org/javaee7/ejb/timer/ProgrammaticTimerBean.java create mode 100644 ejb/timer/src/test/java/org/javaee7/ejb/timer/ProgrammaticTimerBeanTest.java diff --git a/ejb/timer/src/main/java/org/javaee7/ejb/timer/ProgrammaticTimerBean.java b/ejb/timer/src/main/java/org/javaee7/ejb/timer/ProgrammaticTimerBean.java new file mode 100644 index 000000000..2d83158aa --- /dev/null +++ b/ejb/timer/src/main/java/org/javaee7/ejb/timer/ProgrammaticTimerBean.java @@ -0,0 +1,39 @@ +package org.javaee7.ejb.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 pingEvent; + + @Resource + TimerService timerService; + + @PostConstruct + public void initialize(){ + ScheduleExpression scheduleExpression = new ScheduleExpression(); + scheduleExpression.hour("*"); + scheduleExpression.minute("*"); + scheduleExpression.second("*/5"); + + TimerConfig timerConfig = new TimerConfig(); + timerConfig.setInfo("Every 5 second timer"); + + timerService.createCalendarTimer(scheduleExpression, timerConfig); + } + + @Timeout + public void programmaticTimout(Timer timer) { + pingEvent.fire(new Ping(timer.getInfo().toString())); + } +} diff --git a/ejb/timer/src/test/java/org/javaee7/ejb/timer/ProgrammaticTimerBeanTest.java b/ejb/timer/src/test/java/org/javaee7/ejb/timer/ProgrammaticTimerBeanTest.java new file mode 100644 index 000000000..5e0e1bd19 --- /dev/null +++ b/ejb/timer/src/test/java/org/javaee7/ejb/timer/ProgrammaticTimerBeanTest.java @@ -0,0 +1,73 @@ +package org.javaee7.ejb.timer; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +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; + +/** + * author: Jacek Jackowiak + */ +@RunWith(Arquillian.class) +public class ProgrammaticTimerBeanTest { + + final static long TIMEOUT = 5000l; + final static long TOLERANCE = 1000l; + + @Inject + PingsListener pings; + + @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(Ping.class, PingsListener.class, ProgrammaticTimerBean.class); + } + + @Test + public void should_receive_two_pings() { + await().untilCall(to(pings.getPings()).size(), equalTo(2)); + + Ping firstPing = pings.getPings().get(0); + Ping secondPing = pings.getPings().get(1); + + long delay = secondPing.getTime() - firstPing.getTime(); + System.out.println("Actual timeout = " + delay); + assertThat(delay, is(withinWindow(TIMEOUT, TOLERANCE))); + } + + private Matcher withinWindow(final long timeout, final long tolerance) { + return new BaseMatcher() { + @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. + } + }; + } + +} \ No newline at end of file From e32c3317b22d6aa5f737fe7c512aa6a43289b710 Mon Sep 17 00:00:00 2001 From: Jacek Jackowiak Date: Sat, 28 Mar 2015 00:08:54 +0100 Subject: [PATCH 3/6] Adding multiple timers sample --- .../ejb/timer/MultipleScheduleTimerBean.java | 33 ++++++++ .../javaee7/ejb/timer/SchedulesTimerBean.java | 25 ++++++ .../timer/MultipleScheduleTimerBeanTest.java | 77 +++++++++++++++++++ .../ejb/timer/SchedulesTimerBeanTest.java | 77 +++++++++++++++++++ 4 files changed, 212 insertions(+) create mode 100644 ejb/timer/src/main/java/org/javaee7/ejb/timer/MultipleScheduleTimerBean.java create mode 100644 ejb/timer/src/main/java/org/javaee7/ejb/timer/SchedulesTimerBean.java create mode 100644 ejb/timer/src/test/java/org/javaee7/ejb/timer/MultipleScheduleTimerBeanTest.java create mode 100644 ejb/timer/src/test/java/org/javaee7/ejb/timer/SchedulesTimerBeanTest.java diff --git a/ejb/timer/src/main/java/org/javaee7/ejb/timer/MultipleScheduleTimerBean.java b/ejb/timer/src/main/java/org/javaee7/ejb/timer/MultipleScheduleTimerBean.java new file mode 100644 index 000000000..98b880eb5 --- /dev/null +++ b/ejb/timer/src/main/java/org/javaee7/ejb/timer/MultipleScheduleTimerBean.java @@ -0,0 +1,33 @@ +package org.javaee7.ejb.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; + +/** + * @author Jacek Jackowiak + */ +@Startup +@Singleton +public class MultipleScheduleTimerBean { + + @Inject + Event pingEvent; + + @Schedule(hour = "*", minute = "*", second = "*/5", info = "Every 5 second timer") + public void automaticallyScheduled(Timer timer) { + fireEvent(timer); + } + + @Schedule(hour = "*", minute = "*", second = "*/10", info = "Every 10 second timer") + public void automaticallyScheduled2(Timer timer) { + fireEvent(timer); + } + + private void fireEvent(Timer timer) { + pingEvent.fire(new Ping(timer.getInfo().toString())); + } +} diff --git a/ejb/timer/src/main/java/org/javaee7/ejb/timer/SchedulesTimerBean.java b/ejb/timer/src/main/java/org/javaee7/ejb/timer/SchedulesTimerBean.java new file mode 100644 index 000000000..826fc626b --- /dev/null +++ b/ejb/timer/src/main/java/org/javaee7/ejb/timer/SchedulesTimerBean.java @@ -0,0 +1,25 @@ +package org.javaee7.ejb.timer; + +import javax.ejb.*; +import javax.enterprise.event.Event; +import javax.inject.Inject; + +/** + * @author Jacek Jackowiak + */ +@Startup +@Singleton +public class SchedulesTimerBean { + + @Inject + Event pingEvent; + + @Schedules({ + @Schedule(hour = "*", minute = "*", second = "*/5", info = "Every 5 second timer"), + @Schedule(hour = "*", minute = "*", second = "*/10", info = "Every 10 second timer") + }) + public void automaticallyScheduled(Timer timer) { + pingEvent.fire(new Ping(timer.getInfo().toString())); + } + +} diff --git a/ejb/timer/src/test/java/org/javaee7/ejb/timer/MultipleScheduleTimerBeanTest.java b/ejb/timer/src/test/java/org/javaee7/ejb/timer/MultipleScheduleTimerBeanTest.java new file mode 100644 index 000000000..245490169 --- /dev/null +++ b/ejb/timer/src/test/java/org/javaee7/ejb/timer/MultipleScheduleTimerBeanTest.java @@ -0,0 +1,77 @@ +package org.javaee7.ejb.timer; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +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; + +/** + * author: Jacek Jackowiak + */ +@RunWith(Arquillian.class) +public class MultipleScheduleTimerBeanTest { + + final static long TIMEOUT = 0l; + final static long TOLERANCE = 1000l; + + @Inject + PingsListener pings; + + @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(Ping.class, PingsListener.class, MultipleScheduleTimerBean.class); + } + + @Test + public void should_receive_three_pings() { + await().untilCall(to(pings.getPings()).size(), equalTo(3)); + + Ping firstPing = pings.getPings().get(0); + Ping secondPing = pings.getPings().get(1); + Ping thirdPing = pings.getPings().get(2); + + long delay = secondPing.getTime() - firstPing.getTime(); + System.out.println("Actual timeout = " + delay); + long delay2 = thirdPing.getTime() - secondPing.getTime(); + System.out.println("Actual timeout = " + delay2); + long smallerDelay = Math.min(delay, delay2); + assertThat(smallerDelay, is(withinWindow(TIMEOUT, TOLERANCE))); + } + + private Matcher withinWindow(final long timeout, final long tolerance) { + return new BaseMatcher() { + @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. + } + }; + } + +} diff --git a/ejb/timer/src/test/java/org/javaee7/ejb/timer/SchedulesTimerBeanTest.java b/ejb/timer/src/test/java/org/javaee7/ejb/timer/SchedulesTimerBeanTest.java new file mode 100644 index 000000000..24992699d --- /dev/null +++ b/ejb/timer/src/test/java/org/javaee7/ejb/timer/SchedulesTimerBeanTest.java @@ -0,0 +1,77 @@ +package org.javaee7.ejb.timer; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +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; + +/** + * author: Jacek Jackowiak + */ +@RunWith(Arquillian.class) +public class SchedulesTimerBeanTest { + + final static long TIMEOUT = 0l; + final static long TOLERANCE = 1000l; + + @Inject + PingsListener pings; + + @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(Ping.class, PingsListener.class, SchedulesTimerBean.class); + } + + @Test + public void should_receive_three_pings() { + await().untilCall(to(pings.getPings()).size(), equalTo(3)); + + Ping firstPing = pings.getPings().get(0); + Ping secondPing = pings.getPings().get(1); + Ping thirdPing = pings.getPings().get(2); + + long delay = secondPing.getTime() - firstPing.getTime(); + System.out.println("Actual timeout = " + delay); + long delay2 = thirdPing.getTime() - secondPing.getTime(); + System.out.println("Actual timeout = " + delay2); + long smallerDelay = Math.min(delay, delay2); + assertThat(smallerDelay, is(withinWindow(TIMEOUT, TOLERANCE))); + } + + private Matcher withinWindow(final long timeout, final long tolerance) { + return new BaseMatcher() { + @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. + } + }; + } + +} From e24189d417843055cfd2c6e61198836cb4cb39c9 Mon Sep 17 00:00:00 2001 From: Jacek Jackowiak Date: Sat, 28 Mar 2015 00:21:10 +0100 Subject: [PATCH 4/6] Removing code duplications --- .../ejb/timer/AutomaticTimerBeanTest.java | 19 ++---------- .../timer/MultipleScheduleTimerBeanTest.java | 19 ++---------- .../ejb/timer/ProgrammaticTimerBeanTest.java | 19 ++---------- .../ejb/timer/SchedulesTimerBeanTest.java | 21 ++----------- .../ejb/timer/WithinWindowMatcher.java | 30 +++++++++++++++++++ 5 files changed, 38 insertions(+), 70 deletions(-) create mode 100644 ejb/timer/src/test/java/org/javaee7/ejb/timer/WithinWindowMatcher.java diff --git a/ejb/timer/src/test/java/org/javaee7/ejb/timer/AutomaticTimerBeanTest.java b/ejb/timer/src/test/java/org/javaee7/ejb/timer/AutomaticTimerBeanTest.java index 6cfb2b78b..563bd1701 100644 --- a/ejb/timer/src/test/java/org/javaee7/ejb/timer/AutomaticTimerBeanTest.java +++ b/ejb/timer/src/test/java/org/javaee7/ejb/timer/AutomaticTimerBeanTest.java @@ -17,6 +17,7 @@ import static com.jayway.awaitility.Awaitility.*; import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.Matchers.*; +import static org.javaee7.ejb.timer.WithinWindowMatcher.withinWindow; /** * author: Jakub Marchwicki @@ -38,7 +39,7 @@ public static WebArchive deploy() { return ShrinkWrap.create(WebArchive.class) .addAsLibraries(jars) - .addClasses(Ping.class, PingsListener.class, AutomaticTimerBean.class); + .addClasses(WithinWindowMatcher.class, Ping.class, PingsListener.class, AutomaticTimerBean.class); } @Test @@ -52,20 +53,4 @@ public void should_receive_two_pings() { System.out.println("Actual timeout = " + delay); assertThat(delay, is(withinWindow(TIMEOUT, TOLERANCE))); } - - private Matcher withinWindow(final long timeout, final long tolerance) { - return new BaseMatcher() { - @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. - } - }; - } - } diff --git a/ejb/timer/src/test/java/org/javaee7/ejb/timer/MultipleScheduleTimerBeanTest.java b/ejb/timer/src/test/java/org/javaee7/ejb/timer/MultipleScheduleTimerBeanTest.java index 245490169..ac2844829 100644 --- a/ejb/timer/src/test/java/org/javaee7/ejb/timer/MultipleScheduleTimerBeanTest.java +++ b/ejb/timer/src/test/java/org/javaee7/ejb/timer/MultipleScheduleTimerBeanTest.java @@ -19,6 +19,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; +import static org.javaee7.ejb.timer.WithinWindowMatcher.withinWindow; /** * author: Jacek Jackowiak @@ -40,7 +41,7 @@ public static WebArchive deploy() { return ShrinkWrap.create(WebArchive.class) .addAsLibraries(jars) - .addClasses(Ping.class, PingsListener.class, MultipleScheduleTimerBean.class); + .addClasses(WithinWindowMatcher.class, Ping.class, PingsListener.class, MultipleScheduleTimerBean.class); } @Test @@ -58,20 +59,4 @@ public void should_receive_three_pings() { long smallerDelay = Math.min(delay, delay2); assertThat(smallerDelay, is(withinWindow(TIMEOUT, TOLERANCE))); } - - private Matcher withinWindow(final long timeout, final long tolerance) { - return new BaseMatcher() { - @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. - } - }; - } - } diff --git a/ejb/timer/src/test/java/org/javaee7/ejb/timer/ProgrammaticTimerBeanTest.java b/ejb/timer/src/test/java/org/javaee7/ejb/timer/ProgrammaticTimerBeanTest.java index 5e0e1bd19..22fd534b6 100644 --- a/ejb/timer/src/test/java/org/javaee7/ejb/timer/ProgrammaticTimerBeanTest.java +++ b/ejb/timer/src/test/java/org/javaee7/ejb/timer/ProgrammaticTimerBeanTest.java @@ -19,6 +19,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; +import static org.javaee7.ejb.timer.WithinWindowMatcher.withinWindow; /** * author: Jacek Jackowiak @@ -40,7 +41,7 @@ public static WebArchive deploy() { return ShrinkWrap.create(WebArchive.class) .addAsLibraries(jars) - .addClasses(Ping.class, PingsListener.class, ProgrammaticTimerBean.class); + .addClasses(WithinWindowMatcher.class, Ping.class, PingsListener.class, ProgrammaticTimerBean.class); } @Test @@ -54,20 +55,4 @@ public void should_receive_two_pings() { System.out.println("Actual timeout = " + delay); assertThat(delay, is(withinWindow(TIMEOUT, TOLERANCE))); } - - private Matcher withinWindow(final long timeout, final long tolerance) { - return new BaseMatcher() { - @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. - } - }; - } - } \ No newline at end of file diff --git a/ejb/timer/src/test/java/org/javaee7/ejb/timer/SchedulesTimerBeanTest.java b/ejb/timer/src/test/java/org/javaee7/ejb/timer/SchedulesTimerBeanTest.java index 24992699d..ee0031b59 100644 --- a/ejb/timer/src/test/java/org/javaee7/ejb/timer/SchedulesTimerBeanTest.java +++ b/ejb/timer/src/test/java/org/javaee7/ejb/timer/SchedulesTimerBeanTest.java @@ -1,7 +1,5 @@ package org.javaee7.ejb.timer; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; import org.hamcrest.Matcher; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; @@ -19,6 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; +import static org.javaee7.ejb.timer.WithinWindowMatcher.withinWindow; /** * author: Jacek Jackowiak @@ -40,7 +39,7 @@ public static WebArchive deploy() { return ShrinkWrap.create(WebArchive.class) .addAsLibraries(jars) - .addClasses(Ping.class, PingsListener.class, SchedulesTimerBean.class); + .addClasses(WithinWindowMatcher.class, Ping.class, PingsListener.class, SchedulesTimerBean.class); } @Test @@ -58,20 +57,4 @@ public void should_receive_three_pings() { long smallerDelay = Math.min(delay, delay2); assertThat(smallerDelay, is(withinWindow(TIMEOUT, TOLERANCE))); } - - private Matcher withinWindow(final long timeout, final long tolerance) { - return new BaseMatcher() { - @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. - } - }; - } - } diff --git a/ejb/timer/src/test/java/org/javaee7/ejb/timer/WithinWindowMatcher.java b/ejb/timer/src/test/java/org/javaee7/ejb/timer/WithinWindowMatcher.java new file mode 100644 index 000000000..da432dbc1 --- /dev/null +++ b/ejb/timer/src/test/java/org/javaee7/ejb/timer/WithinWindowMatcher.java @@ -0,0 +1,30 @@ +package org.javaee7.ejb.timer; + +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; + +class WithinWindowMatcher extends BaseMatcher { + 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 withinWindow(final long timeout, final long tolerance) { + return new WithinWindowMatcher(timeout, tolerance); + } +} From 9ea4cd57e4e209981fc3265231ac006a0212530f Mon Sep 17 00:00:00 2001 From: Jacek Jackowiak Date: Sat, 28 Mar 2015 10:21:03 +0100 Subject: [PATCH 5/6] Switching to builder pattern when creating ScheduleExpression --- .../org/javaee7/ejb/timer/ProgrammaticTimerBean.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ejb/timer/src/main/java/org/javaee7/ejb/timer/ProgrammaticTimerBean.java b/ejb/timer/src/main/java/org/javaee7/ejb/timer/ProgrammaticTimerBean.java index 2d83158aa..0eb5e57ef 100644 --- a/ejb/timer/src/main/java/org/javaee7/ejb/timer/ProgrammaticTimerBean.java +++ b/ejb/timer/src/main/java/org/javaee7/ejb/timer/ProgrammaticTimerBean.java @@ -20,11 +20,11 @@ public class ProgrammaticTimerBean { TimerService timerService; @PostConstruct - public void initialize(){ - ScheduleExpression scheduleExpression = new ScheduleExpression(); - scheduleExpression.hour("*"); - scheduleExpression.minute("*"); - scheduleExpression.second("*/5"); + public void initialize() { + ScheduleExpression scheduleExpression = new ScheduleExpression() + .hour("*") + .minute("*") + .second("*/5"); TimerConfig timerConfig = new TimerConfig(); timerConfig.setInfo("Every 5 second timer"); From 9def070bb23fa94b81b6d6a5b4a1cd1ed0caba2d Mon Sep 17 00:00:00 2001 From: Jacek Jackowiak Date: Sat, 28 Mar 2015 10:21:34 +0100 Subject: [PATCH 6/6] Renaming methods in MultipleScheduleTimerBean --- .../java/org/javaee7/ejb/timer/MultipleScheduleTimerBean.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ejb/timer/src/main/java/org/javaee7/ejb/timer/MultipleScheduleTimerBean.java b/ejb/timer/src/main/java/org/javaee7/ejb/timer/MultipleScheduleTimerBean.java index 98b880eb5..a109c2466 100644 --- a/ejb/timer/src/main/java/org/javaee7/ejb/timer/MultipleScheduleTimerBean.java +++ b/ejb/timer/src/main/java/org/javaee7/ejb/timer/MultipleScheduleTimerBean.java @@ -18,12 +18,12 @@ public class MultipleScheduleTimerBean { Event pingEvent; @Schedule(hour = "*", minute = "*", second = "*/5", info = "Every 5 second timer") - public void automaticallyScheduled(Timer timer) { + public void fastAutomaticallyScheduled(Timer timer) { fireEvent(timer); } @Schedule(hour = "*", minute = "*", second = "*/10", info = "Every 10 second timer") - public void automaticallyScheduled2(Timer timer) { + public void slowlyAutomaticallyScheduled(Timer timer) { fireEvent(timer); }