How to run a method every hour

Hi

  I have a servlet running all the time. How could i
run a method say every hour from that servlet ?
(need to create some zip files)

Thanx alot
b3cfAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ykaganovCommented:
class MyTimer extends Thread {
  static final long HOUR = 3600000L; // 60*60*1000
  ZipCreator _creator;

  public MyTimer(ZipCreator creator) {
    super();
    creator = _creator;
  }

  public void run() {
    while(true) {
      try {
        sleep(HOUR);
        _creator.createZipFiles();
      } catch (InterruptedException ex) {
        ex.printStackTrace();  // shouldn't happen
      }
    }
  }
}

Make sure that createZipFiles() is threadsafe (or you may just implement it inside MyTimer), and think through where you create MyTimer objects: by default, a new instance of Servlet is created for each web request, so if you create new MyTimer inside a servlet instance, you will have one MyTimer running for each servlet request...

Hope this helps,

- Eugene
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
b3cfAuthor Commented:
Thanx alot Eugene.

I have a quick question. So the thread will sleep only
after doProcess() is done ?
Since its doing zipping alot of files, it might take a
few minutes. (may be even 25 minutes)

Thanx again.

 public void run() {
          while(true) {

               try {
                   doProcess();
                   sleep(this.DELAY);
               }
               catch (InterruptedException ex) {
                      ex.printStackTrace();  // shouldn't happen
               }
          }
   }
0
objectsCommented:
Just use a Timer:

import java.util.Timer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public final class TimerServlet extends HttpServlet
{
   private static final HOUR = 60 * 60 * 1000;

   private Timer timer = new Timer(true);

   public void init()
   {
      timer.scheduleAtFixedRate(new MyTimerTask(), HOUR, HOUR);
   }

   public void destroy()
   {
      timer.cancel();
   }

   public void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException
   {
   }
}


And create the task to run as a subclass of TimerTask:

import java.util.TimerTask;

public class MyTimerTask extends TimerTask
{
  public void run()
  {
     // do your task here
  }
}
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

ykaganovCommented:
b3cf,

Yes, it will sleep only after doProcess() is done.

If you want to have (processTime + sleepTime) == DELAY instead, you could do something like this:

  long startTime = System.currentTimeMillis();
  doProcess();
  long endTime   = System.currentTimeMillis();

  long processTime = endTime - startTime;
  long sleepTime = this.DELAY - processTime;

  if(sleepTime > 0) {
    sleep(sleepTime);
  }
0
objectsCommented:
Why reinvent the wheel?
0
ykaganovCommented:
hmm, because it's easy? :)

Yeah, objects is right (as always), java.util.Timer will do exactly what you need. The only caveat is that it requires jdk1.3 and up.

But my way you get to learn about threads a little ;)
0
functionpointerCommented:
do beware. java.util.Timer just sucks. It;s single thread blocks for the entire run() method of every task it holds. It's queue has somewhat questionable task sorting. It is impossible to predict exactly what time anything will happen, since it runs late tasks ASAP instead of the time you may have thought.

You can use it safely for a very few quick and simple tasks, but be very careful trusting it to be a faithful workhorse without alot of babysitting.
0
objectsCommented:
> java.util.Timer just sucks

do u know of a better alternative?

> It;s single thread blocks for the entire run() method of every task it holds.

If this is an issue then just use a TimerTask that kicks off a new thread to run the 'real' thread.

> since it runs late tasks ASAP instead of the time you may have thought.

It is documented that tasks should complete quickly.
If you know a task takes a while and may impact on the scheduling on other tasks, then use method suggected above.

> You can use it safely for a very few quick and simple tasks

I disagree, as it is designed to schedule *many* quick tasks.

Of course if you use it for a job it is not designed then of course you will run into problems.



0
functionpointerCommented:
> do u know of a better alternative?
One better alternative is to write your own timer that doesn't block. That way you dont have to kick off a new thread. The idea of Timer was to simplify this. Creating a new Thread yourself everytime you schedule a task is not 'simplifying' things.

> If you know a task takes a while and may impact on the scheduling on other tasks, then use method suggected above.
And when do you have just too many Timer objects, just because your tasks are lengthy? Worrying about the tasks is enough, no need to worry about what Timer it is in.

> I disagree, as it is designed to schedule *many* quick tasks
Rightly said, depending on your definition of *many* and *quick*. Not so many you kill yourself on it's blocks that synchronize the whole queue object.

I respect your defense of the Language, objects! You truly are a great evangelist for our cause.
I just don't like java.util.Timer. ;)

If b3cf needs this task to run at the top of every hour, Timer may let him down unless he has his Task spin off a new Thread.
0
b3cfAuthor Commented:
Thanx alot guys. Appreciate your help a lot.
0
objectsCommented:
functionpointer,

It really comes down to using the right class for the right job. If Timer class meets your needs then your better off using it rather having to write something yourself. But it is not suitable for all situations, and if it isn't then you should look to an alternative that matches your requirements.

:)
0
functionpointerCommented:
my sentiments exactly.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.