`

使用spring注解执行定时任务

 
阅读更多

前言:之前用过quartz框架执行定时调度问题,然后感觉比较麻烦。最近看了下人家的项目,发现都是直接在spring使用注解去执行任务了,所以想记录下来方便以后使用。

 

废话不多说,直接来干货

1.新建fmb-platform-schedule-server.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"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
       default-lazy-init="true" default-autowire="byType">

    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
    <import resource="classpath*:/applicationContext-ds.xml"/>
    
    <import resource="classpath:xxx-platform-finance-client.xml"/>

    <bean id="fmb-properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="location" value="classpath:xxx.properties"/>
    </bean>

    <context:annotation-config/>
    <context:component-scan base-package="cn.xxx"/>
    
    <task:annotation-driven/>
    
</beans>

   如上:要有spring定时执行文件:

 

xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"

   然后要加上task任务扫描注解:

 

  

<task:annotation-driven/>

 

   我们扫描的包是:

 

 

context:component-scan base-package="cn.xxx"/>

 

    注:在配置的时候:

 

default-lazy-init="true" default-autowire="byType"

    autowire的装载方式不能是byName,不然会报装载错误的日志。然后还有这里配置的默认是懒加载,所以在定时器执行时候必须要注解为@Lazyload(false),否则会报错!

 

2.java文件编写:(在扫描的cn.xxx包下面)

 

//定时处理到期订单产品
@Component  
@Lazy(false)
public class DueOrderProductQuartz extends BaseService{
	
	@Autowired
	private OrderProductInfoService orderProductInfoService;
	
	//每天凌晨1点执行
	@Scheduled(cron="0 0 1 * * ?")
//	@Scheduled(cron="0/60 * * * * ?")//测试
	public void queryDueOrderProduct() {
		log.info(Utils.getCurrentDate() + "执行DueOrderProductQuartz-->queryDueOrderProduct");
		try {
			int pageNum = 1;
			int pageSize = 20;
			QueryResult<OrderProductInfoDto> ret = orderProductInfoService.queryDueProductRet(1,1,1);
			if(Utils.isNotNull(ret) && Utils.isNotEmpty(ret.getResults())){
				Integer total = ret.getTotal();
				int size = total / pageSize + 1;
				for (int i = 0; i < size; i++) {
					ret = orderProductInfoService.queryDueProductRet(pageNum + i,pageSize,0);
					if(Utils.isNotNull(ret) && Utils.isNotEmpty(ret.getResults())){
						orderProductInfoService.dealDueOrderProduct(ret.getResults());
						Thread.sleep(5000);//暂停5秒让接口处理
					}
				}
			}
		} catch (Exception e) {
			log.error(Utils.getCurrentDate()+":queryDueOrderProduct"+e.getMessage(),e);
		}
	}
	
}

    上面是@Scheduled是spring定时配置,

    注:@Lazyload(false),不能是懒加载,不然定时器不会执行!

 

 

   然后下面再普及下常用的cron表达式:

  

CRON表达式    含义 
"0 0 12 * * ?"    每天中午十二点触发 
"0 15 10 ? * *"    每天早上10:15触发 
"0 15 10 * * ?"    每天早上10:15触发 
"0 15 10 * * ? *"    每天早上10:15触发 
"0 15 10 * * ? 2005"    2005年的每天早上10:15触发 
"0 * 14 * * ?"    每天从下午2点开始到2点59分每分钟一次触发 
"0 0/5 14 * * ?"    每天从下午2点开始到2:55分结束每5分钟一次触发 
"0 0/5 14,18 * * ?"    每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 
"0 0-5 14 * * ?"    每天14:00至14:05每分钟一次触发 
"0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发 
"0 15 10 ? * MON-FRI"    每个周一、周二、周三、周四、周五的10:15触发 

  4.ok,全局的配置就是这么简单!

 

分享到:
评论

相关推荐

    使用spring @Scheduled注解执行定时任务

    使用spring @Scheduled注解执行定时任务

    spring注解Quartz定时执行功能

    spring注解Quartz定时执行功能

    Spring Boot中的@Scheduled注解:定时任务的原理与实现

    这个注解主要基于Java的内置Timer类以及Quartz等定时任务库,但在Spring Boot中,它提供了更加简洁和易用的方式来实现定时任务。 # 实现原理 @Scheduled注解的实现原理主要依赖于Spring框架的任务调度机制。当...

    Spring @Scheduled定时任务动态修改cron参数

    在不停服务的情况下,动态修改Spring定时任务的执行周期,即动态修改定时任务的cron参数。

    Java课程实验 Spring Boot 任务管理(源代码+实验报告)

    1.在Spring Boot中,你可以使用@Scheduled注解来创建定时任务。将@Scheduled注解与方法一起使用,指定任务执行的时间表达式。 2.使用Spring的TaskScheduler: Spring提供了TaskScheduler接口和相关实现,用于任务...

    详解Spring Boot中使用@Scheduled创建定时任务

    本篇文章中主要介绍了Spring Boot中使用@Scheduled创建定时任务,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

    Spring自动执行方法

    Spring配置每隔X秒执行方法或者定时执行任务 myeclipse完整项目,带Jar包带注释.保证下载后就能运行

    ss-deamon:Spring和qz定时调度任务的整合

    实现对动态数据源的切换功能DataSource注解为拦截标志,用于指定目标数据源-目前作用域为对象级别DynamicMethodInterceptor为拦截执行点,在这里,会根据反射的类,来切换数据源DynamicMongoFactory这里是为了扩展...

    springMvc定时器执行两次

    NULL 博文链接:https://ch-dj.iteye.com/blog/2186072

    spring 定时任务@Scheduled详解

    主要介绍了spring 定时任务@Scheduled的相关资料,文中通过示例代码介绍的很详细,相信对大家的理解和学习具有一定的参考借鉴价值,有需要的朋友们下面来一起看看吧。

    Spring定时任务中@PostConstruct被多次执行异常的分析与解决

    主要给大家介绍了关于Spring定时任务中@PostConstruct被多次执行异常的分析与解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

    Spring_任务调试源码

    这里面有ssh框架集成,有任务调试的源码。有详细的注释。

    Spring 2.0 开发参考手册

    目录 前言 1. 简介 1.1. 概览 1.2. 使用场景 2. Spring 2.0 的新特性 ... 使用JDK 5.0注解 20.3.4. 源代码级的元数据类型 20.3.5. 接口AutodetectCapableMBeanInfoAssembler 20.3.6. 用Java接口定义管理接口 ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring的 Scheduled任务调度.docx

    说明:@Scheduled 注解用于标注这个方法是一个定时任务的方法,有多种配置可选。cron支持cron表达式,指定任务在特定时间执行;fixedRate以特定频率执行任务;fixedRateString以string的形式配置执行频率。

    spring chm文档

    Spring Framework 开发参考手册 Rod Johnson Juergen Hoeller Alef Arendsen Colin Sampaleanu Rob Harrop Thomas Risberg Darren Davison Dmitriy Kopylenko Mark Pollack ...19.2. 使用Spring JMS ...

    mysql安装配置教程

    mysql安装配置【++定时任务+...它介绍了JAVAspring的定时任务的概念、原理和作用,以及如何使用JAVAspring的定时任务来实现任务调度,包括任务的定义、执行、触发、取消、监控等内容,以及一些配置文件和注解的用法。

    Spring boot+Mybatis整合实现增删改查(适合初学者入门必备也可以做脚手架开发)

    5.添加定时任务:不再使用作业自动调度框架Quartz实现作业调度,使用spring框架自带的调度器进行作业调度,简化了配置。@Scheduled是单线程的,每次最多只有一个作业在运行,如果调度时间到了作业还没执行完,就会...

Global site tag (gtag.js) - Google Analytics