Solved

A timer Function

Posted on 2004-03-22
10
423 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

An article on effective troubleshooting
Knowing where your website is hosted is as important as the features you receive, the monthly fee, and the support you receive. Due diligence should be done when choosing your next hosting provider.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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