2

I'm developing a Java App (JSF 2.0) using tomcat 7.0. I need to send an e-mail at a cartain time everyday. I'm going to use JavaMail to send the emails, but How do I make it send the email everyday at a certain time. say noon.?

any and all help greatly apreciated!

Myy
  • 16,507
  • 11
  • 33
  • 55

4 Answers4

2

Best and flexible solution is to use Quartz Scheduler.All you need is to create a Job and a trigger with your specified requirement and you are done.

For details refer to the official documents

Quartz-Scheduler-Tutorials

Umesh Awasthi
  • 22,642
  • 37
  • 122
  • 198
1

As Tomcat is a simple servletcontainer which does not offer builtin scheduling facilities, nor supports the very handy EJB @Schedule annotation, you'd need to manage the scheduling yourself, or to use a 3rd party library such as Quartz, or to just delegate the job to the underlying operating system platform's scheduling facilities like Task Scheduler in Windows based platforms and Cron in Unix based platforms.

When using the standard APIs, you can use a ServletContextListener to initialize the scheduler on startup and you can use ScheduledExecutorService as scheduler.

Here's a kickoff example:

@WebListener
public class Config implements ServletContextListener {

    private ScheduledExecutorService scheduler;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        long secondsUntilNoon = calculateItSomehow();
        long secondsPerDay = 60 * 60 * 24;
        scheduler = Executors.newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(new Mailer(), secondsUntilNoon, secondsPerDay, TimeUnit.SECONDS);
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        scheduler.shutdownNow();
    }

}

Where the Mailer class just look like this:

public class Mailer implements Runnable {

    @Override
    public void run() {
        // Do your mailing job here.
    }

}

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • THanks Balus, I always enjoy your answers because you always seem to give a bit of code to start off. and I hope you don't see this as being lazy, It's just I always prefer having something to start off with. I appreciate the help. Now, I already did this, and it's exactly what I needed. It worked great, so It will work for al long as the app is running on the server correct? – Myy Jan 17 '12 at 23:05
  • You're welcome. That's correct. – BalusC Jan 17 '12 at 23:52
0

You can use the timer task http://www.ibm.com/developerworks/java/library/j-schedule/index.html

Or Quartz scheduler http://www.quartz-scheduler.org/download/

Raveesh Sharma
  • 1,458
  • 5
  • 21
  • 37
0

you can schedule you email your Quartz scheduler as Umesh suggested, you can use the below code to start off:

//set quartz properties in propreties file or map

SchedulerFactory schedFact = new StdSchedulerFactory();

Scheduler sched = schedFact.getScheduler();

//set these parameters
JobDetail jobDetail = new JobDetail( "Email Job" , Scheduler.DEFAULT_GROUP , MyEmailAction.class );

//add data or objects you may require in your scheduled job
JobDataMap dataMap = jobDetail.getJobDataMap();

dataMap.put("mydata", myDataObj);

SimpleTrigger st = new SimpleTrigger();

st.setName("DeployTrigger");            

//set the time when you want to send email

st.setStartTime( java.util.Date );

sched.scheduleJob( jobDetail , st  );

sched.start();
Garry
  • 4,249
  • 3
  • 25
  • 47