Solved

schedule a job

Posted on 2006-06-12
11
233 Views
Last Modified: 2011-09-20
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
Comment
Question by:keepworking
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 92

Accepted Solution

by:
objects earned 64 total points
ID: 16890555
quartz handles that type of task well

http://sourceforge.net/projects/quartz
0
 
LVL 5

Expert Comment

by:tbboyett
ID: 16890596
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
 

Author Comment

by:keepworking
ID: 16890604
can I code a method to run a shceduled job, instead of your reply above?

thanks
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:keepworking
ID: 16890608
CRON???
0
 
LVL 5

Assisted Solution

by:tbboyett
tbboyett earned 62 total points
ID: 16890638
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
 
LVL 92

Expert Comment

by:objects
ID: 16890658
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
 

Author Comment

by:keepworking
ID: 16890897
ok, I will try to use a timer and reply soon

thanks
0
 
LVL 13

Assisted Solution

by:kawas
kawas earned 62 total points
ID: 16891670
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
 
LVL 8

Assisted Solution

by:colr__
colr__ earned 62 total points
ID: 16892242
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
micro services spring boot application error 3 199
servlet doXXX methods 3 51
HTTPSessionBindingListernter not configured in deployment descriptor 2 54
ejb on wildfly 5 20
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question