Alan0218

 

quartz

一、引入两个jar包

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

 

二、配置文件:applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
    <!-- ======================== 类方法 ======================== -->
    <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());
  }
}

 

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

 

分类:

技术点:

相关文章:

  • 2021-11-29
  • 2021-09-29
  • 2021-11-29
  • 2021-11-29
  • 2021-11-29
  • 2021-12-09
  • 2021-11-29
  • 2021-11-29
猜你喜欢
  • 2021-11-29
  • 2021-11-29
  • 2021-10-10
  • 2021-11-29
  • 2021-12-06
  • 2021-11-29
  • 2021-04-29
相关资源
相似解决方案