• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

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
0
keepworking
Asked:
keepworking
  • 3
  • 2
  • 2
  • +2
4 Solutions
 
objectsCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
keepworkingAuthor Commented:
CRON???
0
 
tbboyettCommented:
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
 
kawasCommented:
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__Commented:
Another option would be to simplyuse your Operating System schedular to run your 'single execution' java program every now and again.

colr__
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now