Quartz is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application - from the smallest stand-alone application to the largest e-commerce system. Quartz is freely usable, licensed under the Apache 2.0 license.
So to configure Quartz with Sring you should first create plain POJO class that will do the job that you want to schedule. Next in spring configuration (applicationContext.xml) you should add your bean:
<bean id="onTimeServiceImpl" class="com.company.DatabaseSetupServiceImpl">
OnTimeService just uses some other service (company) and some xml folder path, but you can defined your own servoces or remove this lines.
then you should add quartz job method invoking and specify target obj and target method so OnTimeServiceImpl must have that method "fullReload".
<bean id="reloadDatabaseJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
after that define quartz triger for example we used cron triger
<bean id="reloadDatabaseTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="cronExpression" value="0 0 6 * * ?"/>
and finally schedule your trigered
<bean id="schQuartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<list >
and that is that.
Problem that you can also face is that you need to run the same functionality that you schedule from your web application but in separate tread. We used and for this purpose quartz.
Define in your action (struts 2) scheduler factory bean
/**
* Handle for scheduler.
*/
private SchedulerFactoryBean schQuartz;
inject it with spirng with this spring cconfig
<bean id="adminAction" class="com.company.AdminAction" scope="prototype">
and in action
/**
* Constructor.
*
* @param service
* Handle to the index service (injected via spring).
* @param schQuartz
* Handle to the scheduler for (re-)populating the db.service.
*/
public AdminAction (SchedulerFactoryBean schQuartz) {
/**
* Trigger a load in DB from xmls.
*
* @return The action forward - back to the admin page
*/
public String rebuildDatabase() {
try {
log.error(msg, se);
throw new RuntimeException? (msg, se);
return execute();
And that is all.
No comments:
Post a Comment