Java EE 7 Automatic Timer (EJB Timer) not working

2019-04-08 01:24发布

问题:

I am following the Using the Timer Service tutorial to build a simple scheduled execution. Trying the automatic approach and using WildFly 8.1.0 Final for it.

Session Bean

@Singleton
@Startup
public class HelloJob {

    private static final Logger logger = Logger.getLogger(HelloJob.class);

    public HelloJob() {
        logger.error(">>> Hello Job Created.");
    }

    @Schedule(second="*")
    public void sayHello() {
        logger.error(">>> Server Hello!");
    }

}

On deploy the class is properly instantiated printing the >>> Hello Job Created. message, but the method sayHello() is never called.

According to the tutorial the @Schedule(second="*") means that it should execute every second.

Setting an attribute to an asterisk symbol (*) represents all allowable values for the attribute.

Also only stateful session beans are not allowed for timers, and I am using a singleton, which is also used in the example.

The timer service of the enterprise bean container enables you to schedule timed notifications for all types of enterprise beans except for stateful session beans.

回答1:

Use @Schedule(second="*", minute="*", hour="*").

the default values for hour and minute are "0" which can be quite irritating and effectively forces you to set these.