Question

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 ?

Was it helpful?

Solution

To call something periodically, see TimerTask

OTHER TIPS

If you need something more robust you can use Quartz

As for crontab is the scheduling tool on Unix machines.

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.

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.

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.

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.

If you already use Spring, you might want to have a look at Spring's task execution framework - using @Scheduled and @Async for annotating methods as tasks and implementing the functionality in a Processor that delegates the actual work to a Worker, as described in:

http://blog.springsource.com/2010/01/05/task-scheduling-simplifications-in-spring-3-0/

The advantage is that you can define timers using a cron-like syntax in your spring context and you don't need anything special to set up tasks, it is also well integrated into Java EE applications and should play well with web servers (which custom Threads tend not to do always).

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top