`

使用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 框架自带定时任务和Quartz定时任务

    TimerTask是一个抽象类,我们需要创建它的一个子类,并重写run方法来执行定时任务的具体内容。使用这种方式,程序可以按照一定的频率执行,但这种方式是单线程的,所以不适合执行复杂的、高频率的定时任务。 接着是...

    spring注解Quartz定时执行功能

    再使用Quartz的`@DisallowConcurrentExecution`(防止并发执行)和`@PersistJobDataAfterExecution`(持久化任务数据)注解,以及Spring的`@Scheduled`注解来定义定时任务。例如: ```java import org.quartz.Job; ...

    spring的自带定时任务

    在不使用注解的情况下,我们可以使用Spring的XML配置文件来定义定时任务。首先,我们需要配置一个`&lt;task:annotation-driven&gt;`元素来启用基于注解的调度。然后,可以使用`&lt;task:scheduled-tasks&gt;`和`&lt;task:scheduled&gt;...

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

    ### 使用Spring `@Scheduled` 注解执行定时任务 在现代软件开发中,特别是企业级应用领域,定时任务处理是一项常见的需求。例如,自动备份数据库、定时发送报告邮件、定期清理缓存等。Spring 框架自3.0版本起引入了...

    SpringTask动态定时任务的使用案例

    通过上述内容,我们可以看到SpringTask在动态定时任务使用中的灵活性和便捷性。无论是通过XML配置还是注解方式,SpringTask都提供了清晰的步骤和配置选项,使得开发者可以轻松地在项目中集成和使用定时任务。

    Spring 定时任务源码(spring 三种定时任务的实现方式)

    在需要执行定时任务的方法上添加@Scheduled注解,并指定cron表达式或fixedRate/fixedDelay参数,即可实现定时任务。Spring会自动扫描并注册这些任务,无需额外配置。例如: ```java @Component public class ...

    完美解决多应用服务器负载均衡环境下spring quartz同一定时任务重复执行问题

    在多应用服务器负载均衡环境下,Spring Quartz定时任务的重复执行问题是一个常见的挑战。Spring Quartz是一个强大的、开源的作业调度框架,允许开发者定义和执行复杂的定时任务。然而,当多个服务器实例并行运行时,...

    Spring定时任务实现(非Web项目)

    2. **定义定时任务**:在需要执行定时任务的类上,我们可以定义一个或多个`@Scheduled`注解的方法。例如: ```java @Service public class MyTaskService { @Scheduled(fixedRate = 5000) // 每隔5秒执行一次 ...

    Spring定时任务(Web项目)

    3. 为了防止并发执行,可使用`@DisallowConcurrentExecution`注解标记定时任务类。 4. 可以通过调整`ThreadPoolTaskScheduler`的配置,如`poolSize`,来优化任务执行效率和资源利用。 五、总结 Spring的定时任务...

    spring xml 定时任务

    在Spring框架中,XML配置是传统且广泛使用的方式来设置应用的组件和行为,包括实现定时任务。定时任务在软件开发中扮演着重要角色,它允许应用程序在预设的时间执行特定的任务,例如数据清理、日志归档或者发送通知...

    Spring中定时任务

    - `@Scheduled`注解:这是Spring Task中最常用的注解,可以直接在方法上使用,用于标记定时任务。你可以设置`fixedRate`、`fixedDelay`、`cron`等属性来定义任务执行的间隔和时间表达式。 - `TaskScheduler`接口:...

    spring动态配置定时任务

    首先,Spring提供了`@Scheduled`注解来创建定时任务。在类或方法上使用这个注解,可以定义一个定时执行的任务。例如: ```java import org.springframework.scheduling.annotation.Scheduled; import org.spring...

    使用Spring Task开发定时任务的Demo

    Spring Task,也称为Spring的内置任务调度框架,是Spring Framework的一部分,它提供了在Spring应用中定义和执行定时任务的能力。这个功能强大的工具使得开发者无需依赖外部任务调度库(如Quartz或Cron),就能在...

    spring自带定时任务程序

    在本文中,我们将深入探讨Spring的定时任务功能,包括如何配置、创建和执行定时任务,以及如何利用其进行测试。 1. **Spring Task模块**: Spring在其核心模块中包含了Task模块,它提供了异步任务执行和定时任务...

    Spring3.0定时任务简单实例web工程

    总结来说,这个"Spring3.0定时任务简单实例web工程"提供了一个使用Spring 3.0实现定时任务的实例,通过配置`TaskScheduler`,定义任务类,使用`@Scheduled`注解设置执行计划,最后在Web应用启动时启动任务,可以方便...

    spring2.0 Quartz 执行每天定时任务 普通普是执行任务

    标题中的“spring2.0 Quartz 执行每天定时任务 普通普是执行任务”涉及到的是在Spring 2.0框架中使用Quartz库来创建并执行每天定时任务的场景。Quartz是一款强大的开源作业调度框架,它可以用来按计划执行各种任务,...

    spring-boot 定时任务集群

    这将创建一个大小为10的线程池,用于执行定时任务。 **二、定时任务集群** 在分布式环境中,为了保证任务的高可用性和避免任务重复执行,我们需要进行集群部署。在Spring Boot中,可以通过以下策略实现: 1. **...

    springboot定时任务的动态开启关闭

    在Spring Boot中,我们可以使用`@Scheduled`注解来创建一个定时任务。这个注解可以放在方法上,指定任务的执行周期、延迟等参数: ```java @Component public class MyTask { @Scheduled(cron = "0 0/5 * * *...

    spring定时任务关键jar包(齐全)

    3. **@Scheduled注解**: Spring为定时任务提供了`@Scheduled`注解,可以直接在方法上使用,声明该方法为定时任务。可以设置`cron`表达式、固定延迟或固定间隔时间来决定任务的执行周期。 4. **...

Global site tag (gtag.js) - Google Analytics