You can use Quartz (http://www.opensymphony.com/quartz/) for scheduling.

To be notified with the servlet loads, add a listener in your web.xml
file:

  <listener>
    <listener-class>com.foo.ApplicationListener</listener-class>
  </listener>

Also, you probably want your application to startup on load in web.xml:

  <servlet>
    <servlet-name>YourName</servlet-name>
 
<servlet-class>org.apache.tapestry.ApplicationServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

In com.foo.ApplicationListener.java, add things like (and I'm
copying/pasting here, so expect parts will be left out and won't
compile, but it should be enough to get you going):

package com.foo;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

public class ApplicationListener implements ServletContextListener
{
   // The Quartz standard scheduler for use in this application.
  private static final SchedulerFactory schedulerFactory = new
StdSchedulerFactory();

  // This method called when servlet starts up
  public void contextInitialized(ServletContextEvent event)
  {
    try
    {
      JobDetail job       = new JobDetail("jobName", "jobGroup",
NotificationJob.class);
      Scheduler scheduler = schedulerFactory.getScheduler();

      // Create hourly trigger, MON-FRI, from 8am until 11pm
      Trigger trigger = new CronTrigger("triggerName", "groupName", "0 0
8-23 ? * MON-FRI");

      // Schedule the job with the trigger
      scheduler.scheduleJob(job, trigger);

      // Scheduler will not execute jobs until it has been started
      //(though they can be scheduled before start())
      scheduler.start();
    }
    catch (ParseException exception)
    {
      exception.printStackTrace();
    }
    catch (SchedulerException exception)
    {
      exception.printStackTrace();
    }
  }

  // This method called when servlet shutdown
  public void contextDestroyed(ServletContextEvent event)
  {
    try
    {
      schedulerFactory.getScheduler().shutdown();
    }
    catch (SchedulerException exception)
    {
      exception.printStackTrace();
    }
  }
}


Quartz has several trigger types (hourly, etc).  You'll also want a
1-shot that fires on load, it sounds like (check the docs for how to do
this -- fairly simple, but I'd have to look it up, too).  Your
NotificationJob.java will look something like:

package com.foo;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class NotificationJob implements Job
{
  public void execute(JobExecutionContext context) throws
JobExecutionException
  {
    // Do your stuff here
  }
}


/dev/mrg


-----Original Message-----
From: Frank [mailto:[EMAIL PROTECTED] 
Sent: Monday, January 30, 2006 9:45 AMimport 
To: [email protected]
Subject: Schedule a task when servlet loads on server.


Hi,

How do I make a scheduled task, that starts when the servlet loads and
run
ex. every hour ?

Cheers

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to