spring quartz

quartz定时主要分2中:

一、非持久化

xmlns 多加下面的内容、

[html]  view plain copy
  1. xmlns:task="http://www.springframework.org/schema/task"  


然后xsi:schemaLocation多加下面的内容、

[html]  view plain copy
  1. http://www.springframework.org/schema/task  
  2. http://www.springframework.org/schema/task/spring-task-3.1.xsd  

最后是我们的task任务扫描注解

[html]  view plain copy
  1. <task:annotation-driven/>  
代码参考:

package com.exercise.scheduler;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2015/8/24.
 */
@Component
public class TestScheduler {

    @Scheduled(cron="0/5 * * * * ?")
    public void test(){
        System.out.println("非持久化定时任务执行。。。。。");
    }
}
 
就这么easy
 
二、持久化实现(集群需要使用这种方式)
 
 
 
jar spring 3.2 quartz 1.8
 
下载jar   找到源码包的doc的dbtables 根据自己的数据库 选择sql 创建表
 
1、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:aop="http://www.springframework.org/schema/aop"
       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-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">


    <bean id="scheduler" lazy-init="false"
          class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
        <!--  <property name="schedulerName" value="Mscheduler" />   -->
        <property name="configLocation" value="classpath:quartz.properties" />
        <property name="applicationContextSchedulerContextKey"
                  value="applicationContextKey" />
        <property name="autoStartup" value="true" />
    </bean>
    <import resource="classpath:spring/application-mybatis.xml" />
</beans>

quartz.properties

org.quartz.dataSource.ccDS.URL=jdbc\:mysql\://127.0.0.1\:3306/exercise
org.quartz.dataSource.ccDS.driver=com.mysql.jdbc.Driver
org.quartz.dataSource.ccDS.maxConnections=20
org.quartz.dataSource.ccDS.password=123456
org.quartz.dataSource.ccDS.user=root
org.quartz.dataSource.ccDS.validationQuery = select 1
#==============================================================    
#Configure Main Scheduler Properties    
#==============================================================     
org.quartz.scheduler.instanceId=AUTO
org.quartz.scheduler.instanceName=DefaultQuartzScheduler
org.quartz.scheduler.wrapJobExecutionInUserTransaction=false

#==============================================================    
#Configure JobStore    
#==============================================================   
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX   //配置持久化方式
org.quartz.jobStore.clusterCheckinInterval=20000
org.quartz.jobStore.dataSource=ccDS
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.isClustered=true
org.quartz.jobStore.misfireThreshold=60000
org.quartz.jobStore.tablePrefix=CC_
org.quartz.jobStore.useProperties=false

#==============================================================    
#Configure ThreadPool    
#==============================================================   
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool  
org.quartz.threadPool.threadCount = 5  
org.quartz.threadPool.threadPriority = 5  
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread =true 
org.quartz.scheduler.skipUpdateCheck=true

实现自己的job
package com.exercise.scheduler;

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

/**
 * Created by Administrator on 2015/8/24.
 */
public class UserScheduler implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println(" 测试任务开始了;");
    }
}

通过测试用例  往数据库中添加job  trigger 信息

/**
 * Created by Administrator on 2015/8/24.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext.xml")
public class JobTest {

    @Resource
    private StdScheduler schedulerFactoryBean;

    @Test
    public void addJob(){
        //Scheduler schedulerT = schedulerFactoryBean.s();
        JobDetail jobDetail = new JobDetail("job03", "group03", UserScheduler.class);
        try {
            Trigger trigger = new CronTrigger("trigger03", "group03","job03","group03", new Date(new Date().getTime() + 30000), null, "0/5 * * * * ?");
            schedulerFactoryBean.scheduleJob(jobDetail, trigger);
            schedulerFactoryBean.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

启动项目即可完成







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值