spring2.5与Quartz的小例子

applicationContext-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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="false">

<!-- task -->
<!-- 定义了一个任务 -->
<bean id="quartzClock" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>mrd.shcreating.task.TaskServer</value><!-- 类的名字 有这个类完成具体的 任务 -->
</property>
</bean>

<!-- 第二步 调度定时任务 -->
<!--这种配置与第一种方法效果一样 -->

<bean id="quartzClockTask" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="quartzClock"/>
</property>
<property name="startDelay"><!-- 这里是服务启动后延时多少时间,开始计时任务,单位ms -->
<value>6000</value>


</property>
<property name="repeatInterval"><!-- 这里是每隔多长时间就进行一次计时任务,单位ms -->
<value>2000</value>


</property>
</bean>

<!-- 这种配置可以精确几点执行定时任务 -->
<!-- 定义了任务的执行方式 -->
<bean id="cronQuartzClock" class="org.springframework.scheduling.quartz.CronTriggerBean" >
<property name="jobDetail">
<ref bean="quartzClock"></ref>
</property>
<property name="cronExpression">
<value>3 * * * * ?</value>
</property>
</bean>

<!--第三步 启动定时任务,注意这里的ref bean -->
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronQuartzClock"></ref>
</list>
</property>
</bean>

</beans>

//@Component
public class TaskServer extends QuartzJobBean{

// public TaskServer() {
// super();
// System.out.println("每次都创建一个新对象 任务开始");
// }

@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
// System.out.println("执行定时任务..!"+String.format("%1$tF %1$tT" ,new Date()));
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值