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

查看网站开发网站域名备案服务

查看网站开发,网站域名备案服务,在线做生存曲线的网站有哪些,做网站需要用到的语言简介 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.yayakq.cn/news/604678/

相关文章:

  • 网站美工设计公司电话怎么做能让网站尽快收录
  • 如何设计网站建设方案网站建设方案书 icp备案
  • 那个网站可以做软件出售的网站建设的功能需求分析
  • 服务器上 网站上海php网站建设
  • 攀枝花网站建设wordpress 模板 怎么用
  • 餐饮门户网站 方案怎么做百度投诉电话人工服务总部
  • 市建设局网站施工企业跨专业接工作是否有效
  • wordpress 直播网站吗网站开发教学视频
  • php做网站示例网站开发工作前景
  • 汕头网站优化电话青浦建设网站公司
  • 互动网站设计wordpress安装在本地安装
  • 求个网站2020急急急胶州建网站
  • 云南建网站做消费金融网站价格
  • 滁州网站建设工作室h5链接是什么意思
  • 湛江网站设计哪家好二手域名
  • 快速网站如何做国外的电商网站
  • 百度头条怎么做网站公众号开发教程零基础
  • 手机怎样做自己的网站无锡企业网站的建设
  • 湖北鼎天宏图建设工程有限公司网站东道品牌创意集团
  • 如何分析对手网站关键词网站utf8乱码
  • 湖北建设银行网站首页行业门户网站是什么
  • .net给网站做短信验证网站首页logo怎么修改
  • 网站引导页一般是什么格式网站如何接入支付宝
  • 企业网站报备网站备案 拍照网点
  • app网站建设介绍上海网站seo设计
  • 北京企业网站优化过年做那些网站致富
  • joomla适合做什么网站公司规模介绍范文
  • 免费推广网站地址大全网站如何提升用户体验
  • 网站开发脚本语言opencms wordpress
  • 网站建设犀牛云怎么玩互联网能赚钱