• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Tomcat and timer services

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no scheduler in Tomcat.

Look at java.util.Timer or, for more robust functionality, look at a third party product like Quartz.
 
Adrien Lyon
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can write a filter that redirects to some page when request comes in at specific time.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone guys!

It solved my purpose.

Regards
 
Ranch Hand
Posts: 90
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I love JavaRanch Thank you all
 
reply
    Bookmark Topic Watch Topic
  • New Topic