views:

423

answers:

8

we need run one function periodically in Java web application . How to call function of some class periodically ? Is there any way that call function when some event occured like high load in server and so on . what is crontab ? Is that work periodically ?

+6  A: 

To call something periodically, see TimerTask

Brian Agnew
+4  A: 

If you need something more robust you can use Quartz

As for crontab is the scheduling tool on Unix machines.

OscarRyz
More robust than TimerTask? It's just more flexible.
BalusC
@BalusC: Robust would be, with Quartz, you can schedule different kinds of objects, you can use JTA transactions, clustering and many other Java EE features.
OscarRyz
I think you mean it has more features. I don't think robustness is the issue here
Brian Agnew
@Brian. Well yes, it depends of your concept of robustness then. One of the extra features Quartz has is persistence between system restarts. That is a future alone, yes, but it is a future that add robustness to the library anyway, at least in my point of view.
OscarRyz
@Oscar - yes. That particular feature does sound like a robustness feature
Brian Agnew
+2  A: 

If you want to run something periodically, don't do it in the webserver. That would be a very wrong approach IMO. It's better to use cron instead, if you are on a Unix-like operating system. Windows servers offer similar functionality.

ammoQ
Java Web App might well run in a JEE App Server. If the periodic action you want in to invoke Business Logic implemented in JEE then using JEE Timer failcities is entirely reasonable and fully supported.
djna
Perhaps you could elaborate why it's a bad approach to put it in the webserver versus using something like cron?
chrisbunney
djna: Agreeed. More information is necessary.
ammoQ
chrisbunney: Assuming it's possible to do that e.g. in Jetty or Tomcat, it might cause unexpected side effects.
ammoQ
+4  A: 

For calling methods when server has high load, you have at least two possible approaches. Your App Server might have management hooks that would you allow to monitor its behaviour and take progrommatic action. Alterntaively you have some system monitoring capability (Eg. Tivoli or OpenView) and it generates "events", it should not be too hard to deliver such events as (for example) JMS messages and have your server pick them up.

However, you might want to explain a bit more about what you want to achieve. Adaptive application beahviour might be quite tricky to get right.

djna
+1  A: 

How to call function of some class periodically?

There are several solutions: a Timer, a Java cron implementation like cron4j, Quartz, or even the EJB Timer API. Choosing one or the other highly depends on the context: the type of application, the technologies used, the number of jobs, etc.

Is there any way that call function when some event occurred like high load in server and so on

You could maybe use JMX in your jobs to access and monitor informations and trigger an action under some specific condition. But this is more a pull mode, not event based.

Pascal Thivent
+2  A: 

we need run one function periodically in Java web application

(1) So look in your deployment descriptor (web.xml) define a listener to startup at startup time.

How to call function of some class periodically ?

(2) Create a Timer in the listener.

Is there any way that call function when some event occured like high load in server and so on

(3) and run some Threads to check for system conditions that are accesible with Java, even run system progs (uptime, etc) and parse the output.

crontab could be a way but the execution of Java will start another JVM and that is really the hot thing in servlet containers: all runs in the same JVM.

PeterMmm
+2  A: 

Don't forget about java.util.concurrent - it has a lot of classes for scheduling, e.g. ScheduledThreadPoolExecutor, if you need more than a simple Timer.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/package-summary.html

There is also a backport of it to Java 1.4: http://backport-jsr166.sourceforge.net.

Brian Deterling