This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes Scheduling a Task With Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Scheduling a Task With Servlet" Watch "Scheduling a Task With Servlet" New topic
Author

Scheduling a Task With Servlet

Rajesh Rajaram
Ranch Hand

Joined: Aug 20, 2002
Posts: 63
Hello All,


Can we implement java util Timer and TimerTask in a servlet?

I have requirement to generate a report daily by End of the Day (EOD).

Appreciate your time.


Thanks

Rajesh Rajaram


Rajesh Rajaram
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

A servlet's purpose is to respond to requests.

If you're going to implement a timer, you're better off creating a plain old Java object with your timer code and instanciate it with a contextListener.
There are timer libraries out there you can use for this.
Google "Quartz" or "java.util.Timer" for more information on them.

Another (easier) approach is to use the operating system's timing capability to call a servlet.
I was able to do this with one line of code on a Linux box using cron and wget. Windows has scheduling capabilities as well. If you can't find an HTTP client, it is easy to write a small one in Java using java.net.URLConnection or the libraries at http://jakarta.apache.org/commons/httpclient.
[ November 16, 2006: Message edited by: Ben Souther ]

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

Joined: Aug 20, 2002
Posts: 63
Thank a lot Ben, i download the HttpClient and tried running it.

getting this error, all jars are in place.

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/DecoderException
at org.apache.commons.httpclient.HttpMethodBase.<init>(HttpMethodBase.java:217)
at org.apache.commons.httpclient.methods.GetMethod.<init>(GetMethod.java:88)
at HttpClientTutorial.main(HttpClientTutorial.java:16)


anything missing!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

The HttpClient documentation will list the other jars that must be available.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Put apaches commons-codec.jar in the run path.


Rahul Bhattacharjee
LinkedIn - Blog
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Sorry for multiple posts.
There might be some other dependent jars required to make http-client work.Check the documentation.
Kartik Patel
Ranch Hand

Joined: Sep 12, 2005
Posts: 73
The easiest way you can achieve timer is to have one startup servlet which will initiate one class. This class inturn implements java.util.Timer.

So you will have timer running from the start of your server. and its clean approach also.


Chop your own wood, and it will warm you twice. - Henry Ford
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Kartik Patel:

So you will have timer running from the start of your server. and its clean approach also.


Putting the same in servletcontext listner is even more better.You can put the cleanup code in contextDedtroyed method.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

Originally posted by Kartik Patel:
is to have one startup servlet ...


Startup servlet? That's so 2003. A context listener, as Rahul mentioned, is the proper means to perform "startup" code.
Rajesh Rajaram
Ranch Hand

Joined: Aug 20, 2002
Posts: 63
Thanks Guys! thanks for your time and different approach on this.

Put apaches commons-codec.jar in the run path. from which application i can find this JAR.

I am not able to see any documentation on Jakarta HttpClient. I just downloaded their package and set the jar in the classpath.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

Originally posted by Rajesh Rajaram:
Put apaches commons-codec.jar in the run path. from which application i can find this JAR.


I believe that you can type "commons codec" into google just as easily as any of us can.
Rajesh Rajaram
Ranch Hand

Joined: Aug 20, 2002
Posts: 63
Thanks Bear Bibeault, It works fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scheduling a Task With Servlet