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


Previous
springboot+mybatis多数据源最简解决方案 springboot+mybatis多数据源最简解决方案
在配置多数据源之前先理解@Configuration注解、@Bean注解以及配置自动扫描、bean作用域,参考文章如下:@Configuration注解、@Bean注解以及配置自动扫描、bean作用域1、原理:定义多个数据源,根据baseP
2018-12-04 Pursue
Next
Shiro的学习Helloworld Shiro的学习Helloworld
Shiro的学习Helloworld8月 18, 2017 框架相关, 权限管理 Apache Shiro是Java的一个安全框架1.1. Shiro可以帮助我们完成:认证、授权、加密、会话管理、与Web集成、缓存等 Shiro基本功
2018-12-04 Pursue