Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions ejb/timer/src/main/java/org/javaee7/ejb/timer/Ping.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.javaee7.ejb.timer;

public class Ping {

private String timeInfo;
private long time = System.currentTimeMillis();

public Ping(String s) {
this.timeInfo = s;
}

public long getTime() {
return time;
}

public String getTimeInfo() {
return timeInfo;
}

@Override
public String toString() {
return "Ping {" +
"timeInfo='" + timeInfo + '\'' +
", time=" + time +
'}';
}
}
23 changes: 23 additions & 0 deletions ejb/timer/src/main/java/org/javaee7/ejb/timer/PingsListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.javaee7.ejb.timer;

import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.enterprise.event.Observes;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Startup
@Singleton
public class PingsListener {

final List<Ping> pings = new CopyOnWriteArrayList<>();

public void listen(@Observes Ping ping) {
System.out.println("ping = " + ping);
pings.add(ping);
}

public List<Ping> getPings() {
return pings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,34 @@
*/
package org.javaee7.ejb.timer;

import java.util.Collection;
import java.util.Date;
import javax.annotation.Resource;
import javax.ejb.Schedule;
import javax.ejb.SessionContext;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Timer;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import java.util.Collection;

/**
* @author Arun Gupta
*/
@Startup
@Singleton
public class TimerSessionBean {

@Resource SessionContext ctx;

@Schedule(hour="*", minute="*", second="*/5", info = "Every 5 second timer")
@Resource
SessionContext ctx;

@Inject
Event<Ping> pingEvent;

@Schedule(hour = "*", minute = "*", second = "*/5", info = "Every 5 second timer")
public void printDate() {
System.out.println(new Date());
Collection<Timer> timers = ctx.getTimerService().getAllTimers();
for (Timer t : timers) {
System.out.println("Timer info: " + t.getInfo());
pingEvent.fire(new Ping(t.getInfo().toString()));
}
}

Expand Down
56 changes: 0 additions & 56 deletions ejb/timer/src/main/webapp/index.jsp

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

/**
* author: Jakub Marchwicki
*/
@RunWith(Arquillian.class)
public class TimerSessionBeanTest {

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, TimerSessionBean.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<Long> withinWindow(final long timeout, final long tolerance) {
return new BaseMatcher<Long>() {
@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.
}
};
}

}