Hi all,
As the title suggestions, I want something to happen when my
EJB application is started.
To give further context, I would like my bean to run, doing something every x minutes (e.g. checking some system property then sending out an email).
I can use the TimerService and schedule a reoccuring task - that seems straightforward. But, the question is, where can I put this Timer creation code?
Looking around, EJB3.1 gets over this with @schedule, but I'm trying to stick with EJB3.0 for the time being as it is this exam that I'm working towards and I don't want to confuse myself with what's in what!
Another solution is to use a ServletContextListener, but hijacking the web container in this way just feels like a bodge. As Timer's are persistent across Container restarts, you also have to be careful to check whether the appropriate Timer has already been created, or you could create multiples!
I'm wondering whether I can just use the bean's no-arg constructor. As long as I call super(), and keep it no-arg, perhaps I could sneak my Timer creation in there. The sacrifice is that I would still need to do the above described check (as we also could also have mutliple bean creations as well).
This also brings up an interesting question about bean instance numbers. How can I limit my bean to a single instance? I'm sure I read somewhere about a singleton
pattern, so should I use that? Or, can I limit the pool to a single instance (ie. a max of one).
So many questions. I imagine though that this is a relatively simple/common thing, so would appreciate suggestions on any aspect of the above.
Thanks in advance,
MG