Quartz集群实际应用 (二)

一个实现类,简单的测试,打印出当前调度实例的名称和下次触发时间:

package com.christ.test.action;

import java.io.Serializable;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
* @author
*/
public class HelloJob extends QuartzJobBean implements Serializable {

/**
*
*/
static Log log = LogFactory.getLog(HelloJob.class);

/**
*
*/
public void helloJob() {
log.info("每分钟第10秒非集群测试" + (new Date().toString()));
}

@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
try {
log.info(context.getScheduler().getSchedulerName() + "集群测试下一次运行时间应该为: "+ context.getTrigger().getNextFireTime());
// Scheduler sc = context.getScheduler();
// sc.shutdown();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}



然后就是配置好quartz.xml,这里包括非集群和集群的方法,非集群也可以用插件实现,不过要多个配置文件,我嫌麻烦就直接写这里了:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">

<!-- 集群任务 -->
<bean id="job" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.christ.test.action.HelloJob" />
</bean>

<!-- 集群作业触发器 -->
<bean id="clusterJobTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="job" />
<!-- cron表达式 -->
<property name="cronExpression" value="30 * * * * ?" />
</bean>

<!-- 集群调度 -->
<bean name="clusterQuartzScheduler" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!--启动时更新数据库中己存在的Job-->
<property name="overwriteExistingJobs" value="true"/>
<!-- 应用启动后启动QuartzScheduler -->
<property name="startupDelay" value="30"/>
<property name="triggers">
<list>
<ref bean="clusterJobTime" />
</list>
</property>
<property name="configLocation" value="classpath:quartz.properties"/>
</bean>

<!--非集群作业-->
<bean id="helloJob" class="com.christ.test.action.HelloJob"></bean>

<bean id="jobtest"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject" ref="helloJob"/>
<!-- 调用类中的方法 -->
<property name="targetMethod" value="helloJob"/>
</bean>

<!-- 非集群作业触发器-->
<bean id="JobTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobtest" />
<!-- cron表达式 -->
<property name="cronExpression" value="10 * * * * ?" />
</bean>

<!-- 非集群调度 -->
<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 触发器 -->
<property name="triggers">
<list>
<ref bean="JobTime" />
</list>
</property>
</bean>

</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值