1

I need to try to create and send emails within a JSF application, but if an exception is encountered, store the email in a database table and periodically check this table (say every 10 mins) to try and send the email again.

I have the email piece working, but want to understand the basic techniques I should try to create the polling service. I was thinking perhaps an @ApplicationScoped bean, but am not sure of how to create a process which runs in the bean every 10 mins or so.

Any advice appreciated.

Magnilex
  • 11,584
  • 9
  • 62
  • 84
smackenzie
  • 2,880
  • 7
  • 46
  • 99
  • possible duplicate of [ApplicationScope bean that uses a TimerTask (sheduler), good or bad?](http://stackoverflow.com/questions/7499534/applicationscope-bean-that-uses-a-timertask-sheduler-good-or-bad) – BalusC Jun 25 '13 at 01:07
  • You can create `java.util.Timer` in init method annotated with `@PostConstruct` – GKlimov Jun 25 '13 at 14:50
  • 2
    @GKlimov: no, no, NO! Read the duplicate link for the reasons why not. – BalusC Jun 26 '13 at 12:12

1 Answers1

0

You don't specify exactly what environment you are working in, expect JSF. What you want, however, sounds like some kind of scheduled timer. There are several ways to achieve this:

Java EE Timer Service is probably the easiest way to go if you have access to the full Java EE stack. A simple annotation, like @Schedule(minute="*/10", hour="*") on a method in a @Stateless bean would create a timer that executes every tenth minute.

Another very frequently used library for this is Quartz, which can do the same thing. If Spring is being used, I would recommend using Quartz through Spring. See the official documentation for help on how to set it up.

Magnilex
  • 11,584
  • 9
  • 62
  • 84
  • Hi, thanks. Sorry I am not using a JEE container, but just plain old Tomcat. I read somewhere to avoid using Timer due to its threading model i.e. if an exception gets thrown it kills the Timer. – smackenzie Jun 24 '13 at 22:18
  • Well, some kind of timer is needed here. And the statement that the timer would get killed if an exception is thrown, I don't think that is true at all. For EJB Java EE timers I'm pretty sure that a rollback would occur. Since you are using Tomcat I would recommend using Quartz, it is really more or less the standard library to use for timers. I'm also pretty sure it can handle exceptions in an appropriate way. – Magnilex Jun 25 '13 at 06:34
  • Do I, or should I be using any Spring config with quartz? – smackenzie Jun 25 '13 at 15:19
  • Yeah. Typically Spring has support for anything you want. :) I updated my answer with a link to the Spring documentation. You should have specified in your question that you are using Spring. In your case I would use Quartz and Spring together, as described in the link. – Magnilex Jun 26 '13 at 06:32