Solved

A timer Function

Posted on 2004-03-22
10
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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!

Question has a verified solution.

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

This article is in regards to the Cisco QSFP-4SFP10G-CU1M cables, which are designed to uplink/downlink 40GB ports to 10GB SFP ports. I recently experienced this and found very little configuration documentation on how these are supposed to be confi…
The recent Petya-like ransomware attack served a big blow to hundreds of banks, corporations and government offices The Acronis blog takes a closer look at this damaging worm to see what’s behind it – and offers up tips on how you can safeguard your…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

628 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