File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes Tomcat and timer services Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Tomcat and timer services" Watch "Tomcat and timer services" New topic
Author

Tomcat and timer services

Adrien Lyon
Greenhorn

Joined: Apr 03, 2007
Posts: 11
Hi,

I'm using Tomcat and I would like to know how if its possible to create a web application that runs at scheduled/periodic intervals or can this functionality only be provided by a full J2EE server like JBoss?

Thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There is no scheduler in Tomcat.

Look at java.util.Timer or, for more robust functionality, look at a third party product like Quartz.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Adrien Lyon
Greenhorn

Joined: Apr 03, 2007
Posts: 11
Hi,

I didn't know I was asking a Tomcat specific question.

So, this means there is no way of implementing a scheduled service in a limited server "like" Tomcat?

Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
So, this means there is no way of implementing a scheduled service in a limited server "like" Tomcat?


Yes, there is. Either of the two ways Ben suggested will work. What he meant was that scheduling is not a functionality provided by a servlet container like Tomcat, but you can use any scheduling facility available in Java.


Ping & DNS - my free Android networking tools app
Rohit Passi
Greenhorn

Joined: Apr 05, 2007
Posts: 2
you can write a filter that redirects to some page when request comes in at specific time.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12756
    
    5
web application that runs at scheduled/periodic intervals


I don't think that phrase makes sense. A web application responds to requests - it is important to keep the nomenclature clear.

Perhaps you mean an application running in a web server environment which is visible to the web applications - as the earlier suggestions noted, all of Java is available to you for this.

Be sure you provide for the cases in which a web application needs data to handle a request but the scheduled app is in the middle of revising the data.

Bill
Adrien Lyon
Greenhorn

Joined: Apr 03, 2007
Posts: 11
Originally posted by William Brogden:

Perhaps you mean an application running in a web server environment which is visible to the web applications - as the earlier suggestions noted, all of Java is available to you for this.


Well, what I'm actually after is the following. I need to build an application that runs at certain time intervals (say 12:00am every day) to do certain tasks (like updating a database etc.). I want to be able to distribute this application as a single package, like say in a WAR file. What I definitally not what is this application to be made up out of different parts like, a seperate server that does the updating and that has to be run outside of the Java container (in my case Tomcat).

Is it possible to create such an application and host it in a Tomcat (or tomcat like environment) or do I really need to upgrade to a full J2EE server like JBoss to get functionallity like this?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Is that all this application is going to do; run a certain intervals to perform a task? It's never going to have to respond to requests from the web?

If it's not going to have to respond to web requests, you don't need a container at all; JBoss, Tomcat or otherwise. This could be done from a simple command line app with java.util.Timer or with something like Quartz.
Adrien Lyon
Greenhorn

Joined: Apr 03, 2007
Posts: 11
Originally posted by Ben Souther:
Is that all this application is going to do; run a certain intervals to perform a task? It's never going to have to respond to requests from the web?


The application is going to be a full web app with a user interface and the whole kit and caboodle. The periodic update stuff is just an element of the application and not a big part actually but a necessary one. This is the reason I do not like to separate it out of the webapp but keep it included.

What I was basically wondering was; how to create a timed service using servlets and JSP's because these only act on when there are explictly called, and this timed service always runs in the background. Is it possible to create a periodic service like this in a server like Tomcat or do I really need to upgrade to a full J2EE server that implements the full stack of J2EE services?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
What I was basically wondering was; how to create a timed service using servlets and JSP's because these only act on when there are explictly called, and this timed service always runs in the background. Is it possible to create a periodic service like this in a server like Tomcat or do I really need to upgrade to a full J2EE server that implements the full stack of J2EE services?[/QB]


And that's exactly what Bens first post answered: yes, it's possible, use the Timer/TimerTask classes, or Quartz if you need something more sophisticated.

The place to do set this up in your code would be a ServletContextListener that runs at startup time.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I think the part that's leading to confusion here is:
how to create a timed service using servlets and JSP's

Servlets and JSP, by their very nature, would never do something like this.
They are designed to respond to HTTP requests with HTTP responses.

To give you a straight and simple answer to your question: Yes, you can build a scheduled job in a container like Tomcat. You do not need to upgrade to a full J2EE server to do this.

To repeat what's been said before:
Tomcat itself does not provide a scheduler.
It doesn't need to because Java provides one with the java.util.Timer class.
http://java.sun.com/j2se/1.5.0/docs/api/index.html
There are also more elaborate 3rd party products which are open source and thus free to use. The one that I know of is called Quartz.
http://javaboutique.internet.com/reviews/quartz/

With older versions of the Servlet spec this type of thing was kicked off in the init method of a servlet. To insure that this servlet was deployed as soon as the app started, developers would set the "load-on-startup' attribute for this servlet to 1 or 0. There are several problems with this approach; not the least of which is that it uses a servlet for something other than what it was originally designed to do (respond to HTTP requests).

With current versions of the servlet spec, this can be done by building your component in a plain old Java object (POJO) and kicking it off using a context listener. This can be done (and is often done) in applications running under Tomcat.
No need for a full J2EE app server.
[ April 06, 2007: Message edited by: Ben Souther ]
Adrien Lyon
Greenhorn

Joined: Apr 03, 2007
Posts: 11
Thanks guys!
Em Walsh
Greenhorn

Joined: Jul 18, 2006
Posts: 3
Thanks from me too, very helpful.

I still have a question, however. What's the best way to ensure a Timer keeps going? I don't quite understand from the Java API whether you need to keep a reference to the Timer object - but it maks sense that you'd have to. So, in a web application, where's a good place to keep this reference? I'm using JSF and I'm thinking of setting the timer up from a backing bean, but obviously it would lose the reference when the request ends.

TIA
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
You don't need to keep references to Timer or TimerTask around ... unless you foresee a need to access them later, e.g. for rescheduling or cancelling.
utsav gupta
Ranch Hand

Joined: Mar 19, 2009
Posts: 57
Thanks everyone guys!

It solved my purpose.

Regards
Dhaval J. Patel
Ranch Hand

Joined: Mar 10, 2011
Posts: 90

I love JavaRanch Thank you all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tomcat and timer services
 
Similar Threads
rg.apache.jasper.JasperException: Unable to get connection, DataSource invalid: "java.sql.SQLExcepti
OpenSource SunOne
tomcat debug mode
The connection was refused when attempting to contact localhost:8080
Container StandardContext[/SpringTest] has not been started Exception