Solved

A timer Function

Posted on 2004-03-22
10
422 Views
Last Modified: 2010-04-01
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?
0
Comment
Question by:Havalchy
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 1

Author Comment

by:Havalchy
ID: 10649485
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);
    }
}
0
 
LVL 27

Expert Comment

by:rrz
ID: 10651514
0
 
LVL 1

Assisted Solution

by:gagaliya
gagaliya earned 50 total points
ID: 10653007
just run this on the server, it does what you asked for. it will keep running until you kill the processs.

import java.util.*;

public class ScheduleTask
{

    Timer timer;

    public ScheduleTask()
    {

        timer = new Timer();
        timer.schedule(new RemindTask(),
                     0,        //initial delay
                     600*1000);  //interval,  1000=1sec, so it runs every hour
    }

    class RemindTask extends TimerTask
    {

        public void run()
        {
               Calendar cal = Calendar.getInstance(TimeZone.getDefault());
                String DATE_FORMAT = "HH";
               java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
               sdf.setTimeZone(TimeZone.getDefault());   //get timezone
              System.out.println("Current hour: " + sdf.format(cal.getTime())); //prints out current hour(0-24) for debug purpose
              int currenthour = Integer.parseInt(sdf.format(cal.getTime())); //get the current hour(0-24)
              if (currenthour >10 && currenthour<23  ) //runs between 10am and 11pm
             {
                            //this is where you call the javabean or whatever program that will run every x sec/min/hour interval
                 System.out.println("program ran");
            }

           }//end of run()
    }//end of class RemindTask

    public static void main(String args[])
    {
        new ScheduleTask();
    }
}



0
 
LVL 1

Expert Comment

by:gagaliya
ID: 10653028
just to clarify, put all this code in 1 file -  ScheduleTask.java
0
 
LVL 92

Expert Comment

by:objects
ID: 10674274
Use a servlet that uses the code you posted above to run your task. And have that servlet load on startup.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:Havalchy
ID: 10675762
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 10675822
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.
0
 
LVL 1

Author Comment

by:Havalchy
ID: 10676008
how would I do that, if possible could provide code skeleton.
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 10676045

class MyTask implements TimerTask
{
   private Timer Control;
   private Vector Times;

   public void start()
   {
      scheduleNextTask();
   }

   private void scheduleNextTask()
   {
      // get next time from vector
      // schedule this task to be executed at that time
   }

   public void run()
   {
       // perform task

      scheduleNextTask();
   }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 10736359
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
html form inside div 3 91
Stuck in Bootstrap WysiHtml Editor 3 37
runtime exception and error differences 4 72
oracle fusion vs MQ 2 109
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With the rapid rise in mobile usage, mobile devices are here to stay and have become an integral part of doing business. Here are 9 great apps for your BYOD environment.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

26 Experts available now in Live!

Get 1:1 Help Now