定时器(quartz和线程的使用)

 

quartz

一、引入两个jar包

spring-context-support-3.0.2.RELEASE.jar、quartz.jar

 

二、配置文件:applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
    <!-- ======================== 类方法 ======================== -->
    <bean id="vehChangeStatusQueueJob" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="testwebapp.com.wangzuojia.schedule.ScheduleTest"/>
        <!-- <property name="jobDataAsMap">
            <map>
                <entry key="autoCommonBsh2014" value-ref="autoCommonBsh2014"/>
                <entry key="changeStatusQueueBsh" value-ref="changeStatusQueueBsh"/>
            </map>
        </property> -->
    </bean>
    <!-- ======================== 触发器 ======================== -->
    <bean id="vehChangeStatusQueueCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="vehChangeStatusQueueJob" />
        <property name="cronExpression" value="2/3 * * * * ?" />
    </bean>
    <!-- ======================== 调度工厂 ======================== -->  
    <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="triggers">  
            <list>  
                <ref bean="vehChangeStatusQueueCronTrigger"/>  
            </list>  
        </property>  
    </bean>    
 
</beans>
 
三、测试类ScheduleTest 
package testwebapp.com.wangzuojia.schedule;
 
import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
 
public class ScheduleTest extends QuartzJobBean {
    @Override
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
        System.out.println("Schedule start at:"+ new Date());
    }
}

 



 
线程
一、配置文件(applicationContext.xml)中扫描定时器

  <!-- 扫描定时器 -->
  <context:component-scan base-package="com.util.thread" />

二、定期器的类

package com.util.thread;

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

import com.util.UtilDate;

@Service("jobServiceTwo")
public class AttentionTime {
  // <!-- 早9点到晚9点每10分钟运行一次 -->
  @Scheduled(cron = "0 0/10 9-21 * * ?")
  public void job1() {
    System.out.println("----定时任务:10分钟执行一次----" + UtilDate.getDateFormatter());
  }
}

 

===============一般采用线程的方法===============

 

posted on 2018-04-26 10:38  荆棘Study  阅读(884)  评论(0编辑  收藏  举报