Using EJB Timer Service

后端 未结 2 1865
迷失自我
迷失自我 2021-01-02 22:08

I have a small bit of code that I\'m trying to execute with the timer service.

I\'m having trouble finding a good example or tutorial online. Oracle\'s tutorial cov

相关标签:
2条回答
  • 2021-01-02 22:35

    If you want your timer to run every hour, containerise the hour using day of the week. Here is an example

    @Schedule(dayOfWeek = "*", hour = "*/1", persistent = false)
    public void run() {
        // Do your job here.
    }
    
    0 讨论(0)
  • 2021-01-02 22:45

    That's the simplest to achieve with a @Singleton @Schedule and an additional @PostConstruct to invoke the method directly after construction:

    package com.example;
    
    import javax.annotation.PostConstruct;
    import javax.ejb.Schedule;
    import javax.ejb.Singleton;
    
    @Singleton
    public class SomeBackgroundJob {
    
        @PostConstruct
        @Schedule(hour="*/1", minute="0", second="0", persistent=false)
        public void run() {
            // Do your job here.
        }
    
    }
    

    The only difference is that it doesn't run every hour after startup, but only on every whole hour after startup. That shouldn't really matter, I think?

    0 讨论(0)
提交回复
热议问题