Solved

A timer Function

Posted on 2004-03-22
10
421 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

10 Experts available now in Live!

Get 1:1 Help Now