schedule a job

in a web application, I need call a method in a certain period, for example, I need run a method every 10 mins, how to do it?

thanks
keepworkingAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
quartz handles that type of task well

http://sourceforge.net/projects/quartz
0
 
tbboyettCommented:
You could create a timer similar to the one found on this page:
http://www.javakb.com/Uwe/Forum.aspx/java-programmer/4638/Java-Timer
0
 
keepworkingAuthor Commented:
can I code a method to run a shceduled job, instead of your reply above?

thanks
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
keepworkingAuthor Commented:
CRON???
0
 
tbboyettConnect With a Mentor Commented:
I believe you can use a thread, something like this:

public class TimerThread extends Thread{

    public void run() {
        while (true) {
            try {
                // call some method
            }
            catch (Exception e) {
                // Do something with the exception
            }

            try {
                sleep(5000); // I forget but I think it's like 1000 per second if so 10mins would be like 600000
            } catch (InterruptedException ex) {
               // Do something with the exception
            }
        }
    }
}


// Start it like this
 TimerThread timer = new TimerThread();
 timer.start();
0
 
objectsCommented:
for a simple implementation you could use a Timer

http://javaalmanac.com/egs/java.util/ScheduleRepeat.html

Have a servlet start the timer on startup
0
 
keepworkingAuthor Commented:
ok, I will try to use a timer and reply soon

thanks
0
 
kawasConnect With a Mentor Commented:
I have a task that runs every hour,

this is my inner class in a servlet


private class Schedular extends TimerTask {

            public void run() {
                  // do your stuff here
                  
            }

      }

then on initialization, the servlet does the following:

Timer timer = new Timer();
timer.scheduleAtFixedRate(new Schedular(), new java.util.Date(), 1000 * 60 * 60); // run every 60 minutes starting now

// from api:
scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
          Schedules the specified task for repeated fixed-rate execution, beginning at the specified time
0
 
colr__Connect With a Mentor Commented:
Another option would be to simplyuse your Operating System schedular to run your 'single execution' java program every now and again.

colr__
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.