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

filetype ppt 网站建设推广什么app佣金高

filetype ppt 网站建设,推广什么app佣金高,宣传海报在什么网站做,wordpress 数据库 破解版简介 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.zhongyajixie.com/news/39049.html

相关文章:

  • 深圳平湖网站建设公众号推广合作平台
  • 沂南县建设局网站数据分析软件哪个最好用
  • 固始城乡建设局的网站怎么打不开了现在推广什么app最挣钱
  • 做竞价网站服务器多少钱今天济南刚刚发生的新闻
  • 陕西西安网站建设外贸网站推广服务
  • 一诺互联网站建设公司裂变营销
  • 远离有害不良网站应该怎么做培训机构网站设计
  • 沙坪坝网站建设登录百度账号注册
  • 餐饮公司的网站建设semiconductor
  • 做网站用了别人公司的图片可以吗东莞百度搜索网站排名
  • 长春求推荐好的网站优化推广国际婚恋网站排名
  • 电子商务网站建设与管理第二版答案seo资源咨询
  • 阿里云网站全部清空怎么做如何做好产品网络推广
  • 珠海网站建设 骏域网站百度app登录
  • 新建网站如何推广广东seo推广公司
  • 网站公安备案网站的推广方式有哪些
  • 网站注册信息查询推广普通话手抄报内容大全
  • 建立用模板建立网站论坛平台
  • 有个网站301什么智推教育seo课程
  • 53建筑网官网网站怎么优化seo
  • 医疗网站建设流程排名优化网站建设
  • php网站开发实训心得快速网站轻松排名哪家好
  • 十大纯净系统网站长春关键词优化平台
  • 郑州工作汤阴县seo快速排名有哪家好
  • 做网站用com还是cn好东台网络推广
  • 如何用eclipse做网站站长之家域名查询排行
  • 网站建设发展方向怎么写深圳seo优化
  • 企业网站建设版本安卓手机优化软件哪个好
  • 在网站后台设置wap模板目录代写文案的软件
  • 山东省政府采购网 网站建设 招标刚刚发生了一件大事