English 中文(简体)
Call function periodically in Java
原标题:

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 ?

最佳回答

To call something periodically, see TimerTask

问题回答

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签