Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get notification about EJB deployment (to set up a timer)?

Tags:

java

ejb-3.0

ejb

I'm deploying an EJB which should set a timer and be triggered by it every 24 hours. But where should I set the timer? @PostConstruct does not help -- this is a session bean, so post-construct method will be invoked when the actual instance is created (this never happens as the sole purpose of this bean is to track timer).

Is there any other way to get notification about the bean deployment (not instantiation) in order to set up timer there?

Thanks

like image 650
Sergey Mikhanov Avatar asked Oct 15 '09 12:10

Sergey Mikhanov


People also ask

How does EJB timer work?

The EJB timer service provides a reliable and transactional notification service for timed events. Timer notifications may be scheduled to occur at a specific time, after a specific elapsed duration, or at specific recurring intervals. You can define callback methods on your EJB to receive these time-based events.

Which method will be executed by container after time out through timer service?

EJB Container calls the method, which is annotated by @Timeout. EJB Timer Service is a service provided by EJB container, which helps to create timer and to schedule callback when timer expires.

Which of the following are valid ways to create non persistent timers?

Nonpersistent programmatic timers are created by calling TimerConfig. setPersistent(false) and passing the TimerConfig object to one of the timer-creation methods.


3 Answers

EJB 3.1 introduces the Singleton bean. It will be created at deplyoment of the EJB.

@Singleton
@Startup
public class TimerSessionBean {

    @Resource
    TimerService timerService;

    @PostConstruct
    public void startTimer() {
        Logger.getLogger(getClass().getName()).log(Level.INFO,
                timerService.getTimers().size() + " timers running");

        Logger.getLogger(getClass().getName()).log(Level.INFO, "create a timer");

        timerService.createTimer(10000, 10000, "a timer");
    }

    @Timeout
    void doSomething(Timer timer) {
        System.out.println("something");
    }
}

Another new feature in EJB 3.1 which can be used to run a task periodically is the Schedule annotation.

like image 84
mjn Avatar answered Nov 03 '22 10:11

mjn


I think that the easiest and most portable solution is to add a web-application to your enterprise-application with a context listener (contextInitialized event) that initializes the ejb.

By the way, that's more or less what the Quartz Scheduler does as well (class QuartzInitializerListener)

like image 39
fvu Avatar answered Nov 03 '22 09:11

fvu


The solution I found is ugly, but just as ugly as any other legal solution to this problem (e.g. solution from @fvu). Applying @WebService annotation to the bean make JBoss instantiate it immediately after deployment (because it needs the way to construct the bean's WSDL), so @PostConstruct-marked method will be invoked. I was able to set a timer from there.

like image 29
Sergey Mikhanov Avatar answered Nov 03 '22 09:11

Sergey Mikhanov