Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scheduling a Task With Servlet

 
Rajesh Rajaram
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rajesh Rajaram
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64183
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The HttpClient documentation will list the other jars that must be available.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put apaches commons-codec.jar in the run path.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for multiple posts.
There might be some other dependent jars required to make http-client work.Check the documentation.
 
Kartik Patel
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64183
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64183
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear Bibeault, It works fine.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic