• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Shedule a servlet  RSS feed

 
sripad_devaraju
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,
I have a servelt which i want to run in sertain intervals(say once for every hour)autometically, Is there any way to shedule it in an application server,I am using Resin applicaton server.
Thanks
sripad
 
Chris Victor
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of curiousity, why whould you like to schedule a servlet?
I schedule java-backend processes regularly using the crontab....I can help you with that if you like....
 
Omar IRAQI
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
This question does not make sense at all.
The servlet itselef is not a process that one would want to run or to stop or to schedule.
A copy of the servlet is run in a separate thread by the web container (the actual process) each time a request (that concerns that specific servlet) is received by the web container.
I hope that this would help you realize that you don't know exactly what you want.
Regards
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uh, Omar...

that's being presumptuous. Someone might have implemented a particular 'job' as a servlet, so that it could be invoked by an HTTP request from a browser, but they ALSO want that 'job' to run periodically, like a nightly batch.

ie: I want this to run at LEAST nightly, but I also want it to run whenever I hit this URL: http:///www.foo.com/doMyNightlyJob?param=ButDoItNow

One could argue that the 'job' should be broken out into its own classfile, and thus a simple crontab entry could run it from the command line, or, the servlet could instantiate an instance of the class and call it's "run" method (which would call the 'main' method).
 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you can write another servlet,schedule it to be loaded at starting-up(by configuring your web.xml file),in this servlet,you launch the service servlet periodically.
what's your opinion?
 
srinath karkhani
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets are based on Http Request-Response Pattern i.e there is a response only when there is a request.So one way of solving the above problem is to have a pinging client(eg a java thread) which keeps hitting the server every one hour by sending the request and getting the reponse back.
And the other probable way can be to use the "Thread.sleep" method in the servlet itself make it sleep for 1 hour(which mite not be very appropriate way)
Cheers
Sri
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, if you're running your servlet in an application server, many of them (app servers) do not allow you to call Thread methods.
 
Omar IRAQI
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again,
Assume that the servlet should send data periodically to an HTTP client, in this case I totally agree with srinath karkhani.
Now if the action, that is to be fired periodically, does not consist of replying to a HTTP client, then a good design consists of implementing this action at the level of the middle tier (EJB module, or Corba servant or RMI implementation).
Again, servlets should control requests and reply to them. Nothing else.
 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about creating a simple .bat file that executes the page like so...
START http:///www.foo.com/doMyNightlyJob?param=ButDoItNow
and then adding that Batch file to something like crontab (which I'm not familiar with, so sorry if this is dumb...)
Then, if you come in in the morning and see the reply page, the job ran...
Just a thought...
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well there seems one way.. but I wonder if it is the best to salvage the problem..
Have a console java application ( which you can convert as service with third party tools ) or any other darned application developed in any platform you are good in that has the scheduling logic and the url of the servlet that it has to trigger.
The java application would have the following states..
Sleep ---> Request --> standby ( for response ) --> Sleep
and the cycle continues..
I am sure you wud ve followed the logic..
Wait for the time to trigger the servlet ( depending on yer schedulin logic ), Make a request to the Servlet URL using HTTPconnection object, standby and read response from the servlet, go back to wait for the next scheduling time..
I hope it helps
Regds
Lupo
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is another of those situations where the solution has been stated before the problem was given. Does Sripad REALLY want to run a SERVLET, or it just that that was the percieved solution for some problem?
Servlets are designed to accept a request and supply a response, thus presupposing a client somewhere. If the client is a real web page display, the easiest way to "run the servlet periodically" is to make it a timed-refresh page.
However, it's also quite common to want to run some sort of maintenance within a webapp at intervals or as an asynchronous process. For this, we take advantage of the fact that a servlet has an init() method and we can spawn asynchronous processes within that method. We can do that anywhere within a servlet, actually, but init()'s convenient, since it can be run at webapp startup instead of being delayed until a request comes in.
Then again, as my clever compatriots here have demonstrated, there are other ways to "run a servlet periodically as well".
However, as it is said: "To a small child with a hammer, every problem looks like a nail".
I've got a whole collection of tools, though - is this problem REALLY a nail?
 
gautham kasinath
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"To a small child with a hammer, every problem looks like a nail". - Very true mate, I may have been misled by the problem too
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!