Spring+quartz实现任务调度的小例子

Spring+quartz实现任务调度的小例子

实现任务调度分为三大模块:1.任务调度器Scheduler;
2.触发器cronTrigger;
3.自定义任务JobtTask;


*自定义任务JobtTask*

package com.zjt.quartz;



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

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * 自定义任务;
 */
public class MyJob implements Job {
    // 自定义任务Task;
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("当前任务开始执行: " +
         new SimpleDateFormat("yyyy-MM-dd HH-mm-ss")
         .format(new Date()));
    }
}
*测试类*

package com.zjt.quartz;

import java.text.ParseException;

import org.quartz.JobExecutionException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 *  测试类
 */
public class Test {
    @SuppressWarnings("resource")
    public static void main(String[] args) throws ParseException, JobExecutionException {
        System.out.println("--------------------------------");
        new ClassPathXmlApplicationContext("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" 
    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/context  
    http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    default-autowire="byName">

    <!-- 调度器 -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <!-- 触发器列表 -->
                <ref bean="cronTrigger" />
            </list>
        </property>
        <!-- 加载配置文件, 如果不配置, 将会使用quartz默认的配置 -->
        <property name="configLocation" value="classpath:quartz.properties" />
    </bean>

    <!-- 触发器 -->
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!-- 指向我们的任务 -->
        <property name="jobDetail" ref="myJobtTask" />
        <!-- 每天11点1分到59分,每分钟运行一次 -->
        <property name="cronExpression" value="0 0/1 9 * * ?" />
    </bean>

    <!-- 处理类 -->
    <bean name="myJobtTask" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.zjt.quartz.MyJob" />
    </bean>
</beans>
*quartz.properties*

#============================================================================
# Configure Main Scheduler Properties  
#============================================================================
org.quartz.scheduler.instanceName = DefaultQuartzScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false

#============================================================================
# Configure ThreadPool  
#============================================================================
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5

#============================================================================
# Configure JobStore  
#============================================================================
org.quartz.jobStore.misfireThreshold = 60000

下载链接:http://download.csdn.net/my

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值