我正在使用EJB 3.1并且计时器不起作用。我在Glassfish服务器3.1中运行了部署的代码,但似乎没有执行execute()方法。以下是代码:
@Startup
@Singleton
public class Listener {
javax.annotation.@Resource
private TimerService timerService;
javax.annotation.@PostConstruct
public void init() {
System.out.println("init");
timerService.createTimerInterval(TimeUnit.SECONDS.toMillis(5), TimeUnit.SECONDS.toMillis(5), null);
while(true) {
Thread.sleep(TimeUnit.SECONDS.toMillis(10);
System.out.println("Run");
}
}
javax.ejb.@Timeout
public void execute(javax.ejb.Timer timer) throws Exception {
// check database
System.out.println("Database");
}
}
服务器日志说明如下:
INFO: There are no EJB Timers owned by this server
INFO: <== ... Timers Restored.
我希望每隔一段时间检查一次数据库。
的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>com.screening</groupId>
<artifactId>screening</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>ejb</packaging>
<name>Screening</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<ejbVersion>3.1</ejbVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>screening</finalName>
</build>
</project>
我也尝试了以下内容:
@Startup
@Singleton
public class Listener {
javax.annotation.@PostConstruct
public void init() {
System.out.println("init");
while(true) {
Thread.sleep(TimeUnit.SECONDS.toMillis(10);
System.out.println("Run");
}
}
javax.ejb.@Schedule(second = "5")
public void execute() throws Exception {
// check database
System.out.println("Database");
}
}
但是不执行execute()方法。
我错过了什么吗?有什么想法吗?