• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

To run task once in a every month using servletcontextlistener

 
jayashri hosur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
my requirement is to run some task once in every month.i am getting this information from xml file exa first saturday of every month so i am setting this using gregorian calender. but problem is it is running the task only when i deploy my application.so anybody help me.

thanks in advance.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused. Obviously, an application can schedule something only if it is running. Are you asking how a web app that is not running can do the same?

Generally you can use the java.util.Timer and TimerTask classes for this.
[ January 02, 2009: Message edited by: Ulf Dittmer ]
 
jayashri hosur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
thanks for reply.
i am using the below code. for this requirement. Adding TimerShecdulerContextListener class as a listener in web.xml
BackupTimer is my task. the below code is working fine only at the time of deployment.But my requirement is if the server is running(with required war in deploy folder of server) the task has to execute for the given time without doing redeployment.

public class TimerShecdulerContextListener implements ServletContextListener {
private TimerTask backupTimer = null;
Timer timer = new Timer();

public void contextInitialized(ServletContextEvent servletContextEvent) {
backupTimer = new BackupTimer();
futureCalender= new GregorianCalendar();

futureCalender.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
futureCalender.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);

timer.schedule(backupTimer, futureCalender.getTime());
}}
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at java.util.Timer and TimerTask class as suggested by Ulf.

You need to put the JOB, which is your

method in Timer class and start it in ContextListener class.
[ January 03, 2009: Message edited by: Sagar Rohankar ]
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The schedule call you use only schedules the task to run a single time, not repeatedly; that's what some of the other Timer.schedule(atFixedRate) methods are for.

The difficulty here is that the interval between executions is not fixed, since the number of days between first Saturdays varies. You can either schedule the next run at the end of the current run, or schedule the task to run every day, and then check within the task whether it is the first Saturday of a month (and otherwise do nothing).

(Added later: Just saw Sagar's post, but I'm not sure what he's getting at.)
[ January 03, 2009: Message edited by: Ulf Dittmer ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic