aspose file tools*
The moose likes Servlets and the fly likes Simple schedule Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Simple schedule" Watch "Simple schedule" New topic
Author

Simple schedule

Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
Hi all,

I'm trying to discover the simplest method of calling some code once per day.

I dont really think i need the overhead of a scheduling package like quartz or the others out there, it seems like total overkill.

I was considering a Servlet that loads on startup.. launches my code in a new thread which knows the last day it was run, and if todays date is different then runs some code and sets the lastday to todays date. All in a loop.

But it doesnt at first seem so elegant and i'm not sure how the loop will affect performance of the servlet container etc..

Is there an obvious way to do what I want that i've overlooked, I have a feeling i cant see the wood for the tree's so to speak.


Regards, Dave Brown
SCJP 6 - [url]http://www.dbws.net/[/url] - Check out Grails Forum
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

For me, the simplest way is to use a cron job with the wget program to make a request to a servlet at periodic intervals.
This takes all of 1 line of code.

If I had to do the same with windows, I'd probably write a small command line app in Java that uses the HTTPUrlConnection object to call the servlet, wrap it with a batch script, and call it from the Windows task manager.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
After a bit more research I discoverd using the Timer class works fine.

I've got a servlet which loads on startup and in the init() method I set up the task to run. In fact, i'll post it all here, might be of benefit to others some day...

Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You might want to look at the API for java.util.Timer carefully before using it in a servlet container; especially if you plan on keeping it running for any length of time.

From:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Timer.html

After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can take arbitrarily long to occur. By default, the task execution thread does not run as a daemon thread, so it is capable of keeping an application from terminating. If a caller wants to terminate a timer's task execution thread rapidly, the caller should invoke the timer's cancel method.


You may find, that the timer will keep you from being able to stop or restart your app (or the whole container) gracefully.

I believe Quartz has options for insuring that all of its threads run as daemons.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Just noticed that you did explicitly cancel the thread in your code...

Dave Brown
Ranch Hand

Joined: Mar 08, 2005
Posts: 301
Yeah I noticed that it was still running when I undeployed the app. Glad it was a quick fix ;-)
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

While I'm picking nits...

Do you have a container that is servlet spec 2.3 compliant (or higher)?
If so, you might want to consider taking this code out of the servlet and putting inot a plain old java object that is instanciated with a context listener.

Containers are free to take servlets out of service and to re-initialize them as needed to conserve resources.
A context listener is more stable and, (in my opinion) results in cleaner code.

-Ben
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Originally posted by Ben Souther:

I believe Quartz has options for insuring that all of its threads run as daemons.[/QB]
Timer has this option too. Actually it was my question couple years ago.

Timer(boolean isDaemon)
Creates a new timer whose associated thread may be specified to run as a daemon.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Originally posted by Ben Souther:
While I'm picking nits...

Do you have a container that is servlet spec 2.3 compliant (or higher)?
If so, you might want to consider taking this code out of the servlet and putting inot a plain old java object that is instanciated with a context listener.

Containers are free to take servlets out of service and to re-initialize them as needed to conserve resources.
A context listener is more stable and, (in my opinion) results in cleaner code.

-Ben


Exactly, I used this approach in my address book application.
Download.
Check class RegisterRequestCleaner for details.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple schedule
 
Similar Threads
Date Formatting
WS Vs SOA.
String to Date - JDK 1.4.2
how to get the months
Changing the date format