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:
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".
after that define quartz triger for example we used cron triger
and finally schedule your trigered
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() {
And that is all.
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:
1 2 3 4 | <bean id="onTimeServiceImpl" class="com.company.DatabaseSetupServiceImpl"> <constructor-arg ref="companyService" /> <property name="xmlFolderPath" value="${xml.target}" /> </bean> |
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".
1 2 3 4 | <bean id="reloadDatabaseJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property ref="onTimeServiceImpl" name="targetObject" /> <property name="targetMethod" value="fullReload" /> </bean> |
after that define quartz triger for example we used cron triger
1 2 3 4 | <bean id="reloadDatabaseTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="reloadDatabaseJob" /> <property name="cronExpression" value="0 0 6 * * ?"/> </bean> |
and finally schedule your trigered
1 2 3 4 5 6 | <bean id="schQuartz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers" > <list > <ref bean="reloadDatabaseTrigger" /> </list> </bean> |
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">
<constructor-arg ref="schQuartz" />
</bean> 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) {
this.schQuartz = schQuartz;
}/**
* Trigger a load in DB from xmls.
*
* @return The action forward - back to the admin page
*/
public String rebuildDatabase() {
StdScheduler std = (StdScheduler) schQuartz.getObject();
try {
try {
std.triggerJob("reloadDatabaseJob", "DEFAULT");
} catch (SchedulerException se) {
String msg = "Error triggering database reload";
log.error(msg, se);
throw new RuntimeException? (msg, se);
log.error(msg, se);
throw new RuntimeException? (msg, se);
}
return execute();
} return execute();
And that is all.
Comments