I am trying to create a simple timer in EJB3, example taken from: http://www.adam-bien.com/roller/abien/entry/simplest_possible_ejb_3_16
I created a dynamic web project in eclipse and added the following code. I do not see any output
import javax.ejb.Schedule;
import javax.ejb.Stateless;
@Stateless
public class ShowCurrentTime {
@Schedule(second="*/1", minute="*",hour="*")
public void showTime() {
System.out.println("Time : " + System.currentTimeMillis());
}
}
This is also not working in my maven project where the following dependency was added:
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.1_spec</artifactId>
<scope>provided</scope>
</dependency>
What am I missing?
Edit: Updated my code to import javax.annotation.PostConstruct; import javax.ejb.Schedule; import javax.ejb.Singleton; import javax.ejb.Startup;
@Singleton
@Startup
public class ShowCurrentTime {
@Schedule(second="*/1", minute="*", hour="*", persistent = false)
public void showTime() {
System.out.println("Time : " + System.currentTimeMillis());
}
@PostConstruct
public void applicationStartup() {
showTime();
}
}
All I see in output is the initial print 00:48:15,150 INFO [stdout] (ServerService Thread Pool -- 882) Time : 1414903695150
00:48:15,167 INFO [org.wildfly.extension.undertow] (MSC service thread 1-7) JBAS017534: Registered web context: /Test-DWP
00:48:15,202 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018565: Replaced deployment "Test-DWP.war" with deployment "Test-DWP.war"
Edit: Changing to Singleton and Cleaning up deployment directory and redeploying the application worked.
There's a subtle but important difference between your example and the one presented by Adam.
His example uses a @Singleton EJB and your's is @Stateless.
The difference means that Adam's example will self start, whereas yours will not start until you create an instance of it.