Solved

A timer Function

Posted on 2004-03-22
10
426 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Enabling the Skype for Business Meeting Scheduler in Hybrid OWA
Adults who share images on social media aren’t the only ones who need to worry about their privacy. Our culture’s tendency to share every move and celebration affects the privacy of our children, too.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

735 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