一、什么是定时任务?

我们在项目中遇到的需求: 需要定时送异步请求。

二、怎么实现?

2.1  mvc中启用定时任务。

 <?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:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <!--spring mvc的配置文件-->
<!--开启mvc的注解-->
<mvc:annotation-driven conversion-service="conversionService" >
<!--配置转换器 转换日期的格式。-->
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd"/>
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven> <!--配置日期转换器-->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.aaa.controller.DateConverter"/>
</set>
</property>
</bean> <mvc:default-servlet-handler/> <!--扫描器:扫描控制器的注解-->
<context:component-scan base-package="com.aaa.controller"/> <!--4.静态资源注解-->
<mvc:default-servlet-handler/>
<!--<mvc:resource mapping="/static/**" location="/static/"/>--> <!--3.视图解析器:进行视图解析
prefix+ 视图名字+suffix
-->
<!--5.文件上传的解析器 可以设置相关的属性。-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<!--文件上传的大小:单位:字节-->
<property name="maxUploadSize" value="#{10*1024*1024}"/>
</bean> <!--&lt;!&ndash; 异常处理 1. 配置解析器 &ndash;&gt;-->
<!--<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">-->
<!--&lt;!&ndash;1.1默认的错误视图 发生异常时, 跳转到的页面&ndash;&gt;-->
<!--<property name="defaultErrorView" value="error"/>-->
<!--&lt;!&ndash;1.2 异常的属性 捕获到的错误信息。&ndash;&gt;-->
<!--<property name="exceptionAttribute" value="ex"/>--> <!--&lt;!&ndash;1.3exceptionMappings &ndash;&gt;-->
<!--<property name="exceptionMappings">-->
<!--<props>-->
<!--<prop key="异常类型1">-->
<!--error1-->
<!--</prop>-->
<!--<prop key="异常类型2">-->
<!--error2-->
<!--</prop>-->
<!--</props>-->
<!--</property>-->
<!--</bean>--> <!--6. 配置一个拦截器 -->
<mvc:interceptors>
<!--<mvc:interceptor>-->
<!--&lt;!&ndash; 拦截的路径 &ndash;&gt;-->
<!--<mvc:mapping path="/**"/>-->
<!--&lt;!&ndash; 配置拦截器的bean &ndash;&gt;-->
<!--&lt;!&ndash; 放行路径 &ndash;&gt;-->
<!--<mvc:exclude-mapping path="/user/login"/>-->
<!--<bean class="com.aaa.interceptors.MyIntercept"/>-->
<!--</mvc:interceptor>-->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.aaa.interceptors.Demo02"/>
</mvc:interceptor> </mvc:interceptors> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/> <!--视图前缀-->
<property name="suffix" value=".jsp"/> <!--视图后缀-->
</bean> <!--授权 -->
<aop:config ></aop:config> <!--启用定时任务。 导包 spring frame word. org/schema/task-->
<task:annotation-driven></task:annotation-driven> </beans>

2.2  控制层中创建SchController

package com.aaa.controller;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller; import java.text.SimpleDateFormat;
import java.util.Date; /*
* 定时任务? 需求:
1.定时发送异步请求
2.使用java计时器,自启动的servlet中,线程(Thread,Thread sleep) 使用 Schedule组件:
1.配置注解。 1.1 mvc中 启用定时任务。
1.2 导包 【spring frame word. org/schema/task】
1.3 创建控制器 加入 schedule的注解。
1.4 秒分时日月年。 / 代表 每的意思 “0/5 * * * * *” 就是每五秒执行一次。
*
* */
@Controller
public class SchController {
@Scheduled(cron = "0/3 * * * * ?")
public void task1(){
//日期格式的转换。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = new Date();
String nowTime = sdf.format(date);
System.out.println("每三秒执行一次, 你好世界!"+nowTime);
}
}

2.3 属性说明。

2.4 字符含义

2.5 演示

spring 定时任务?的更多相关文章

  1. 摆脱Spring 定时任务的@Scheduled cron表达式的困扰

    一.背景 最近因为需要,需要适用Spring的task定时任务进行跑定时任务,以前也接触过,但是因为懒没有好好地理解@Scheduled的cron表达式,这次便对它做了一个全方位的了解和任务,记录下来 ...

  2. spring定时任务注解@Scheduled的记录

    spring 定时任务@Scheduled 转自https://www.cnblogs.com/0201zcr/p/5995779.html 1.配置文件 <?xml version=" ...

  3. spring定时任务(@Scheduled注解)

    (一)在xml里加入task的命名空间 xmlns:task="http://www.springframework.org/schema/task" http://www.spr ...

  4. Spring3.0.6定时任务task:scheduled

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  5. Spring 定时任务之 @Scheduled cron表达式

    一个基于Spring boot的一个demo: Java配置中开户对Scheduled的支持 import org.springframework.context.annotation.Configu ...

  6. spring定时任务(@Scheduled注解)cron表达式详解

    cron表达式详解: 一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素. 按顺序依次为 秒(~) 分钟(~) 小时(~) 天(~) 月(~) 星期(~ =SUN 或 SUN,MON,TU ...

  7. java中实现定时任务 task 或quartz

    转载大神的 https://www.cnblogs.com/hafiz/p/6159106.html https://www.cnblogs.com/luchangyou/p/6856725.html ...

  8. spring定时任务详解(@Scheduled注解)( 转 李秀才的博客 )

    在springMVC里使用spring的定时任务非常的简单,如下: (一)在xml里加入task的命名空间 xmlns:task="http://www.springframework.or ...

  9. spring定时任务轮询(spring Task)

    定时任务轮询比如任务自服务器启动就开始运行,并且每隔5秒执行一次. 以下用spring注解配置定时任务.1.添加相应的schema xmlns:task=" xsi:schemaLocati ...

  10. spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务

    spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务 >>>>>>>>>>>> ...

随机推荐

  1. 【LeetCode】621. Task Scheduler 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 公式法 日期 题目地址:https://leetco ...

  2. 【LeetCode】456. 132 Pattern 解题报告(Python)

    [LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  3. codeforce 597C-Subsequences(dp+树状数组)

    题目和南阳那道题一样链接http://www.cnblogs.com/zzuli2sjy/p/4943774.html 代码: 1 #include<stdio.h> 2 #include ...

  4. 第十五个知识点:RSA-OAEP和ECIES的密钥生成,加密和解密

    第十五个知识点:RSA-OAEP和ECIES的密钥生成,加密和解密 1.RSA-OAEP RSA-OAEP是RSA加密方案和OAEP填充方案的同时使用.现实世界中它们同时使用.(这里介绍的只是&quo ...

  5. 利用 jQuery 操作页面元素的方法,实现电商网站购物车页面商品数量的增加和减少操作,要求单项价格和总价随着数量的改变而改变

    查看本章节 查看作业目录 需求说明: 利用 jQuery 操作页面元素的方法,实现电商网站购物车页面商品数量的增加和减少操作,要求单项价格和总价随着数量的改变而改变 当用户单击"+" ...

  6. 三角网格上的寻路算法Part.2—A*算法

    背景 继上一篇三角网格Dijkstra寻路算法之后,本篇将继续介绍一种更加智能,更具效率的寻路算法-A*算法,本文将首先介绍该算法的思想原理,再通过对比来说明二者之间的相同与不同之处,然后采用类似Di ...

  7. Drupal 8 环境搭建部署

    运行环境安装 系统:Ubuntu Server 16.04  (ubuntu-16.04.4-server-amd64.iso) Web服务器:Apache / 2.4.18 数据库:Mysql / ...

  8. PL/SQL连接时,报无法解析指定的字符串

    前言: 工作原因,需要安装PL/SQL连接数据,oracle和PL/SQL都装好了,环境变量也配好了,启动PL/SQL进行连接数据库,结果报"无法解析指定的字符串",连接失败了. ...

  9. Linux中ssh登陆慢的两种原因

    useDNS配置导致登陆慢 如果ssh server的配置文件(通常是 /etc/ssh/sshd_config )中设置 useDNS yes ,可能会导致 ssh 登陆卡住几十秒.将该配置项设为 ...

  10. python pathlib模块(面向对象的文件系统路径)

    该模块提供表示文件系统路径的类,其语义适用于不同的操作系统 导入Path类: 获取当前目录的绝对路径: 返回当前目录的路径对象 路径拼接 os与PurePath/Path函数映射表 来自为知笔记(Wi ...