aspose file tools*
The moose likes Servlets and the fly likes To run task once in a every month using servletcontextlistener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "To run task once in a every month using servletcontextlistener" Watch "To run task once in a every month using servletcontextlistener" New topic
Author

To run task once in a every month using servletcontextlistener

jayashri hosur
Greenhorn

Joined: Jun 05, 2008
Posts: 8
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
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
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 ]

Ping & DNS - updated with new look and Ping home screen widget
jayashri hosur
Greenhorn

Joined: Jun 05, 2008
Posts: 8
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 ]

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To run task once in a every month using servletcontextlistener
 
Similar Threads
Timer Schedule
Thread Scheduler
Rescheduling a Timer Task
WSAD 5.0 Schedular with UserCalender
Quartz Cron Expression -- need help