spring任务调度器

5 篇文章 0 订阅

当你新建web project ,导入spring的相关包后,还需要导入 spring.jar, quartz-all-1.6.1.jar , commons-collections-3.2.1.jar .

 

(把项目中commons-collections.jar删掉,版本太低)

 

 

一:新建jobDetail类 ,只需要继承QuartzJobBean

 

package com.justtide.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class TestQuartz extends QuartzJobBean{

 @Override
 protected void executeInternal(JobExecutionContext arg0)
   throws JobExecutionException {
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
  Date date = new Date();
  String now = format.format(date);
  System.out.println(now);
 }

}

 

二:在applicationContext.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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <!-- 要调度的对象 -->
 <bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass">
       <value>com.justtide.quartz.TestQuartz</value>
    </property>
 </bean>
 
 <!-- 定义触发的时间 -->
 <bean id="cron" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail">
        <ref bean="jobDetail"/>
    </property>
    <property name="cronExpression">
        <value>0/5 * * * * ?</value>  <!--每隔五秒钟触发一次-->
    </property>
 </bean>
 
 <!-- 定义触发器 -->
 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
       <list>
           <ref bean="cron"/>
       </list>
    </property>
 </bean>
 </beans>

 

三:测试类

 

package com.justtide.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Test {
 
 public static void main(String[] args) {
  
  ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
 }

}

 

可能会报 applicationContext.xml 没找到,参照以前写的博客,这里的话也可以直接将applicationContext.xml拖到src目录下

 

即可。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值