quartz 集群

来源:互联网 发布:淘宝运营工作职责 编辑:程序博客网 时间:2024/05/20 17:07

public class MyDetailQuartzJobBean extends QuartzJobBean {

                protected final Log logger = LogFactory.getLog(getClass());

 

                private String targetObject;

                private String targetMethod;

                private ApplicationContext ctx;

 

                protected void executeInternal(JobExecutionContext context)

                                                throws JobExecutionException {

                                try {

 

                                                logger.info("execute [" + targetObject + "] at once>>>>>>");

                                                Object otargetObject = ctx.getBean(targetObject);

                                                Method m = null;

                                                try {

                                                                m = otargetObject.getClass().getMethod(targetMethod,

                                                                                                new Class[] {});

 

                                                                m.invoke(otargetObject, new Object[] {});

                                                } catch (SecurityException e) {

                                                                logger.error(e);

                                                } catch (NoSuchMethodException e) {

                                                                logger.error(e);

                                                }

 

                                } catch (Exception e) {

                                                throw new JobExecutionException(e);

                                }

 

                }

 

                public void setApplicationContext(ApplicationContext applicationContext){

                                this.ctx=applicationContext;

                }

 

                public void setTargetObject(String targetObject) {

                                this.targetObject = targetObject;

                }

 

                public void setTargetMethod(String targetMethod) {

                                this.targetMethod = targetMethod;

                }

 

}

再来看完整的 quartz.xml (注意红色加粗部分尤为重要):

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd ">

<beans>

                <bean id="mapScheduler" lazy-init="false" autowire="no"

                                class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

                                <property name="configLocation" value="classpath:quartz.properties" />

                                <property name="triggers">

                                                <list>

                                                                <ref bean="cronTrigger" />

                                                </list>

                                </property>

                                <property name=" applicationContextSchedulerContextKey " value=" applicationContext " />

                               

                </bean>

               

 

 

                <bean id="quartzJob" class="com.testcompany.framework.quartz.QuartzJob">

                </bean>

 

                <bean id="jobTask" class="org.springframework.scheduling.quartz.JobDetailBean">

                                <property name="jobClass">

                                                <value>com.testcompany.framework.quartz. MyDetailQuartzJobBean </value>

                                </property>

                                <property name="jobDataAsMap">

                                                <map>

                                                                <entry key="quartzJob" value="quartzJob" />

                                                                <entry key="targetMethod" value="execute" />

                                                </map>

                                </property> 

                </bean>

 

                <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

                                <property name="jobDetail">

                                                <ref bean="jobTask" />

                                </property>

                                <property name="cronExpression">

                                                <value>0/5 * * * * ?</value>

                                </property>

                </bean>

</beans>

4.       下载最新的 quartz1.8 版,把 quartz-all-1.8.4.jar, quartz-oracle-1.8.4.jar,quartz-weblogic-1.8.4.jar 这三个包放到 web-inf/lib 目录下,布署。

原创粉丝点击