Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using EJB Timer Service

Tags:

jakarta-ee

ejb

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 covered a bit too much too quickly for me to grasp the basic utility that I need. I just want it to execute immediately when the program starts and then every hour after that.

What would a sample timer look like?

like image 546
Randnum Avatar asked Dec 13 '11 00:12

Randnum


1 Answers

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?

like image 153
BalusC Avatar answered Sep 21 '22 22:09

BalusC