Spring MVC 定时调度时间整理

配置文件里面配置:

<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"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/task http://www.springframework.org/schema/task/spring-task-3.0.xsd 
       ">

<task:annotation-driven/> 

 
/**
 * 任务定时调度
 * 
 * @author liuyd
 * 
 */
@Component
public class Test001
{
	@Autowired
	@Qualifier("NewService")
	private NewService newService;

	@Scheduled(fixedDelay = 5000)
	void doSomethingWithDelay()
	{
		NewsEntity news = new NewsEntity();
		news.setNewMsgOne("testddddddddddddddddddddd");

		newService.save(news);
	}

	@Scheduled(fixedRate = 5000)
	void doSomethingWithRate()
	{
		System.out.println("I'm doing with rate now!");
	}

	@Scheduled(cron = "0/5 * * * * *")
	void doSomethingWith()
	{
		System.out.println("I'm doing with cron now!");
	}
}




"0 0 12 * * ?" 每天中午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期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发

"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring中,有多种方式可以实现定时任务调度。其中一种方式是使用Spring Task提供的定时任务功能。另外还有其他的定时任务框架,如Quartz和xxl-job。Spring Task是一种功能简单的定时任务框架,可以通过在配置文件中添加约束文件来启用它。在spring-mvc.xml配置文件中,可以添加以下约束文件来启用Spring Task的定时任务功能: ```xml xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd ``` 接下来,需要配置定时任务的具体执行方法。可以通过在配置文件中添加以下代码来配置定时任务的执行方法: ```xml <bean id="adminJob" class="com.xxx.xxx.AdminJob"/> <!--此处id值为需要执行的定时任务方法名--> <bean id="job1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="adminJob"/> <property name="targetMethod" value="job1"/> </bean> ``` 以上代码中,`adminJob`是定时任务的执行类,`job1`是需要执行的具体方法。 最后,需要配置定时任务的触发器。可以通过在配置文件中添加以下代码来配置定时任务的触发器: ```xml <bean id="job1Trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail"> <ref bean="job1"/> </property> <property name="cronExpression"> <value>0 15 0 16 * ?</value> </property> </bean> ``` 以上代码中,`job1Trigger`是定时任务的触发器,`cronExpression`指定了定时任务的执行时间表达式。 这样,就完成了Spring定时任务的调试配置。可以根据具体的需求和业务逻辑来配置定时任务的执行方法和触发器。 #### 引用[.reference_title] - *1* [22-09-29 西安 谷粒商城(07)分布式定时任务xxl-job、Cron表达式、springTask定时任务、订单业务](https://blog.csdn.net/m0_56799642/article/details/127109242)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [spring配置定时任务的几种方式](https://blog.csdn.net/kaicen/article/details/114886381)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值