Solved

schedule a job

Posted on 2006-06-12
11
231 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
 

Author Comment

by:keepworking
ID: 16890608
CRON???
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now