一、什么是定时任务?

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

二、怎么实现?

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】486. Predict the Winner 解题报告(Python)

    [LeetCode]486. Predict the Winner 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  2. MLP-Mixer: An all-MLP Architecture for Vision

    目录 概 主要内容 代码 Tolstlkhin I., Houlsby N., Kolesnikov A., Beyer L., Zhai X., Unterthiner T., Yung J., S ...

  3. Linux_at任务调度

    基本介绍 一次性定时计划任务,由守护进程atd以后台模式执行,检查作业队列来进行 默认情况下,atd每60s检查一次作业队列 在使用at命令时,要确保atd进程的启动,用指令来查看 ps -ef | ...

  4. 【MySQL作业】MySQL函数——美和易思系统信息函数和加密函数应用习题

    点击打开所使用到的数据库>>> 1.显示当前 MySQL 服务器的版本信息和登录信息. MySQL 系统信息函数 version() 用于返回当前 MySQL 的版本号," ...

  5. 1.spring系列之优雅的实现接口统一返回

    好处 现在公司开发基本上都是以前后分离模式为主,所以要有个统一的数据格式,这样有什么好处呢? 能够提高前后端对接的效率(特别重要) 代码更加优雅和简洁 对于前端和后端维护更方便容易 实现(直接上代码) ...

  6. pod存在,但是deployment和statefulset不存在

    pod存在,但是deployment和statefulset不存在 这样的话,可以看一下是不是ReplicaSet, kubectl get ReplicaSet  -n iot

  7. Eclipse启动SpringCloud微服务集群的方法

    1.说明 下面这篇文章介绍了Eureka Server集群的启动方法, SpringCloud创建Eureka模块集群 是通过jar包启动时指定配置文件的方式实现的. 现在只有Eureka Serve ...

  8. JSP请求响应流程入门介绍

    一个完整的jsp请求响应流程可以简单的使用下图表示: 过滤器:直观的了解,就是对请求做一个过滤作用,比如身份验证,验证不通过的不让他继续往下走 Servlet:请求处理中心,这个也是我们写业务逻辑的地 ...

  9. C# - 集合差集计算

    使用  Except 方法做差集, 结果赋值给 IEnumerable 类 ,这是一个枚举集合类 ,泛型使用对应的类型即可,没办法之间使用count 或 lenght 方法获取,只能循环计算

  10. spring boot 打包war后 部署到外部 tomcat 的具体正确操作【包括修改端口 与 去除请求路径的工程名】

    1.前言 工程做好了,总不能放在idea运行吧?不然怎么把项目放到云服务器呢?[这一篇随笔不讲解发布的云服务器的操作,在其他随笔有详细记载.] 解决的方案是把springboot 工程 打包成war文 ...