当前位置: 首页 > news >正文

filetype ppt 网站建设宁波超值关键词优化

filetype ppt 网站建设,宁波超值关键词优化,青岛网站建设设计,网络设计是干什么的呢简介 Spring 内部有一个 task 是 Spring 自带的一个设定时间自动任务调度,提供了两种方式进行配置,一种是注解的方式,而另外一种就是 XML 配置方式了;注解方式比较简洁,XML 配置方式相对而言有些繁琐,但是应用场景的不…

简介

Spring 内部有一个 task 是 Spring 自带的一个设定时间自动任务调度,提供了两种方式进行配置,一种是注解的方式,而另外一种就是 XML 配置方式了;注解方式比较简洁,XML 配置方式相对而言有些繁琐,但是应用场景的不同,两者又各有优点,所以具体使用还是根据需求来划分;因为任务调度这样的需求,通常改动都是比较多的,如果用注解的方式改动就变得麻烦了,必须去重新编译;所以更多的时候推荐使用XML配置的方式

使用 XML 配置

  1. 定义一个 Task 类,添加一个测试方法
public class Task {public void cron() {System.out.println(new Date() + ", 定时任务执行中 ...");}
}
  1. 在 spring.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:p="http://www.springframework.org/schema/p"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"><!-- 定时任务 --><bean id="task" class="com.review.spring.model.Task"/><!-- 创建任务池 -->   <task:scheduler id="taskScheduler" pool-size="100" /><!-- 定义定时任务 -->   	<task:scheduled-tasks scheduler="taskScheduler"><!-- 表示周一到周五的每分钟的第五秒执行--><task:scheduled ref="task" method="cron" cron="5 * * * * MON-FRI"/></task:scheduled-tasks>
</beans>
  1. 在测试类中初始化容器,定时任务就会自动执行
public class TestTask {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring.xml");}
}

使用注解配置

  1. 定义 Task 类,添加如下代码

public class Task {(cron="0/2 * * * * *") // 每隔 2 秒运行一次public void cron() {System.out.println(new Date() + ", 定时任务执行中 ...");}
}
  1. 在 spring.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:p="http://www.springframework.org/schema/p"xmlns:task="http://www.springframework.org/schema/task"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:component-scan base-package="com.review.spring"/><task:annotation-driven/> <!-- 如果是代码式配置, 需要在配置类添加注解  -->
</beans>

cronExpression 的配置说明
cron 表达式是一个字符串,字符串以 5 或 6 个空格隔开,分为 6 或 7 个域,每一个域代表一个含义,cron 有如下两种语法格式:

(1) Seconds Minutes Hours DayofMonth Month DayofWeek Year

(2) Seconds Minutes Hours DayofMonth Month DayofWeek(常用)
在这里插入图片描述
在这里插入图片描述
注意:Spring 中的任务调度以及其 ThreadPoolTaskScheduler 中的 cron 表达式是不支持 L W 字符的;所以最后一天和最近的工作日就无法定义在 cron 表达式中

扩展:如何实现每月最后一天执行任务?
方式一:每个月的最后一天,因为不同的月份最后一天是不一样的,但是好在有个范围:28 – 31;那么我们可以结合 cron 表达式 + 代码实现控制每月的最后一天执行任务;代码如下

(cron = "0 0 0 28-31 * ?")
public void execute() {final Calendar c = Calendar.getInstance();// 判断是不是最后一天if (c.get(Calendar.DATE) == c.getActualMaximum(Calendar.DATE)) {// 你的业务逻辑代码}
}

方式二:在 cron 表达式中,L 表示最后一个的意思,所以可定义为

(cron = "* * * L * ?")

前提是定义的 cron 表达式支持 L W 指定,例如:Quartz 等框架,如果是 Spring 自带定时器则不支持

http://www.hrbkazy.com/news/44030.html

相关文章:

  • 233小游戏网页入口优化网站seo方案
  • 建立网站的第一步线上推广营销
  • wordpress文章位置免费网站做seo
  • 企业网站建设原则惠州关键词排名优化
  • 政府网站建设 领导重视企业微信会话存档
  • 怎样做好公司网站百度网盘网页版
  • 北仑网站建设网站seo关键词优化软件app
  • 短视频营销的优势独立站优化
  • 学做美食交流网站友链查询站长工具
  • 优秀的网站建设公司排名网络销售技巧和话术
  • 做h5网站做网页怎么做
  • 应聘网站建设工程师百度快速优化排名软件
  • 备案用什么网站名称好免费个人网站源码
  • 网站开发平面设计师岗位要求如何自己开个网站平台
  • 网站做百度推广有没有效果自己怎么开电商平台
  • 医疗器械招商网站大全网络营销是什么课程
  • 潮州网站制作长沙h5网站建设
  • 注册帐号网站seo系统
  • seo推广网站有哪四川seo快速排名
  • 珠海哪里做网站的国内推广平台有哪些
  • 做seo比较好的网站高明搜索seo
  • 做搜狗手机网站优化网站关键词优化多少钱
  • 投资公司网站源码经典广告语
  • 美团网站做疏通广告十大免费网站推广平台有哪些
  • wordpress柒比贰主题优化公司结构
  • 网站开发背景图模板互动网站建设
  • 营销型网站四大元素电商seo优化是什么
  • 便宜网站建设seo网站优化外包
  • wordpress系统邮件seo优化平台
  • 网站公司简介模板引流软件