Static EJB Timer example
TweetPosted on Tuesday Feb 25, 2014 at 04:08PM in Technology
Environment
- WildFly8.0.0.Final
- Oracle JDK7u51
Resources
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.nailedtothex</groupId> <artifactId>ejbtimer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>ejb</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency> </dependencies> </project>
TimerService.java
package org.nailedtothex.ejbtimer; import javax.ejb.Schedule; import javax.ejb.Singleton; @Singleton public class TimerService { @Schedule(second = "*/1", minute = "*", hour = "*", persistent = false) public void periodic() { System.out.println("period"); } @Schedule(dayOfMonth = "25", hour = "16", minute = "10", persistent = false) public void schedule() { System.out.println("schedule"); } }
- Almost all of the code was taken from [1].
- There are some additional useful informations and conversations so I recommend to visit [1].
- According to [2], “persistent” attribute means whether timers that missed should fire or not during downtime of the application server.
- Default is true.
- I don't want many missed timers to fire at once when I launch the application server which stopped unexpectedly so I just set it false.
- Detail of specification of @Schedule here: [4]
Log
16:09:44,002 INFO [stdout] (EJB default - 8) period 16:09:45,001 INFO [stdout] (EJB default - 9) period 16:09:46,002 INFO [stdout] (EJB default - 10) period 16:09:47,002 INFO [stdout] (EJB default - 1) period 16:09:48,002 INFO [stdout] (EJB default - 2) period 16:09:49,002 INFO [stdout] (EJB default - 4) period 16:09:50,002 INFO [stdout] (EJB default - 3) period 16:09:51,002 INFO [stdout] (EJB default - 5) period 16:09:52,002 INFO [stdout] (EJB default - 6) period 16:09:53,002 INFO [stdout] (EJB default - 7) period 16:09:54,002 INFO [stdout] (EJB default - 8) period 16:09:55,002 INFO [stdout] (EJB default - 9) period 16:09:56,001 INFO [stdout] (EJB default - 10) period 16:09:57,001 INFO [stdout] (EJB default - 1) period 16:09:58,001 INFO [stdout] (EJB default - 2) period 16:09:59,002 INFO [stdout] (EJB default - 4) period 16:10:00,002 INFO [stdout] (EJB default - 5) period 16:10:00,003 INFO [stdout] (EJB default - 3) schedule 16:10:01,001 INFO [stdout] (EJB default - 6) period 16:10:02,002 INFO [stdout] (EJB default - 7) period 16:10:03,001 INFO [stdout] (EJB default - 8) period 16:10:04,001 INFO [stdout] (EJB default - 9) period 16:10:05,001 INFO [stdout] (EJB default - 10) period 16:10:06,002 INFO [stdout] (EJB default - 1) period 16:10:07,003 INFO [stdout] (EJB default - 2) period
References
Tags: ejb