Link to home
Start Free TrialLog in
Avatar of Havalchy
HavalchyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

A timer Function

Dear Experts

I have a function that I have written which will do some checks on data stored in a database.
This function is effectivly a database data synchronisation, it implements rules on the data, and updates the database accordingly.

This is called from a jsp page as a javabean and everything is working fine.

What i want to do is to have this function run at set intervals and fixed time each day.
So function wakes up at 10:00 GMT and runs,
then every 1 hour repeat,

I want this function to exist and continue to work throught the web application so unless I undeploy the webapplication from tomcat.

I am using NetBeans, and Tomcat 4.

How do I go about implementing this?
Avatar of Havalchy
Havalchy
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

effectivly I want a function that does this:

import java.util.Timer;
import java.util.TimerTask;

/**
 * Simple demo that uses java.util.Timer to schedule a task to execute
 * once 5 seconds have passed.
 */
public class autoTimer {
    public static void main(String[] args) throws Exception {
        autoTimer t = new autoTimer();
        t.execute();
    } private void execute() throws Exception {
        System.out.println(" Tasks scheduled");
        int initialDelay = 1000; // start after 30 seconds
        int period = 2000; // repeat every 5 seconds
        Timer timer = new Timer();
        System.out.println(" Timer set");
       
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                System.out.println("Task Run");
            }
        } , initialDelay, period);
    }
}
Avatar of rrz
SOLUTION
Avatar of gagaliya
gagaliya

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gagaliya
gagaliya

just to clarify, put all this code in 1 file -  ScheduleTask.java
Use a servlet that uses the code you posted above to run your task. And have that servlet load on startup.
Experts

I have implemented the load on start up and i found out that I was not using the full class path, so that is fixed.

Say I want this timer to wake up based on a list of times I give it, so for example.

I have a Vector of objects passes, with startTimes which are number of secs since epoch.
how would I go about creating a function that will wake up to preform a task, based on a list of objects.
Use the Timer task to schedule the first time to perform task from your Vector, when this task is executed and completed then schedule the next time from Vector for task to be completed.
Repeat this until done.
how would I do that, if possible could provide code skeleton.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial