《Spring MVC》 第十一章 单体架构任务调度

本文介绍了如何在SpringMVC项目中集成Springtask来实现定时任务调度,通过配置web.xml和springmvc.xml文件,启用注解驱动和定时任务。示例展示了使用@Scheduled注解定义定时任务的方法,并提供了cron表达式的使用示例和在线生成工具。
摘要由CSDN通过智能技术生成

系列文章目录

第一章 MVC模式
第二章 让程序run起来
第三章 @Controller、@RequestMapping 注解和获取请求参数
第四章 域对象、视图、转发和重定向
第五章 第五章 实现RESTful
第六章 MVC类型转换器、格式化器
第七章 JSON数据交互
第八章 拦截器实现权限验证、异常处理
第九章 文件上传、下载
第十章 使用logback+Slf4j打印日志
第十一章 单体架构任务调度



在这里插入图片描述


前言

在Spring MVC项目基础上使用Spring task,开发定时任务调度功能。
常用于替代循环的线程、ScheduledExecutorService等,Spring task提供Spring Cron格式调度规则,使得任务调度更加灵活,代码更加简洁。

1、引入依赖

在Spring MVC项目基础上使用,首先引用Spring其他组件(context、core等)

<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.3.13</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>5.3.13</version>
</dependency>

2、环境配置

2.1、web.xml配置

配置web,xml文件,加载springMVC.xml文件,spring容器从此文件加载。

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Spring MVC</display-name>

  <!--配置总控-->
  <servlet>
    <servlet-name>springMvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <!--配置 DispatcherServlet 的一个初始化参数:spring mvc 配置文件的位置和名称-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springMVC.xml</param-value>
    </init-param>
  </servlet>
  <!--配置映射路径-->
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

<!--配置字符编码-->
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

2.2、spring mvc 配置文件

配置spring 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: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
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/task
       http://www.springframework.org/schema/task/spring-task-3.1.xsd
    ">
    <!--开启扫描组件-->
    <context:component-scan base-package="com.xxx.springmvc"/>
    <!--注解驱动 -->
    <mvc:annotation-driven/>
    <!-- 注解定时任务 -->
    <task:annotation-driven/>

    <!--thymeleaf视图解析器-->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                        <!-- 视图前缀 -->
                        <property name="prefix" value="/WEB-INF/static/"/>
                        <!-- 视图后缀 -->
                        <property name="suffix" value=".html"/>

                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8"/>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>

</beans>

3、 案例

使用Spring task的案例,包括cron的用法,以及在线生成cron表达式工具

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j  
@Component  
public class Task {
  /**
   * 定时任务方法
   */
  //每隔 1 分钟执行一次:0 */1 * * * ?
  //每天 23 点执行一次:0 0 23 * * ?
  //每天中午12点触发 0 0 12 * * ?
  //每天上午10:15触发 0 15 10 ? * *       0 15 10 * * ?      0 15 10 * * ?
  //每月 1 号凌晨 1 点执行一次:0 0 1 1 * ?
  //每月最后一天 23 点执行一次:0 0 23 L * ?
  //每周星期天凌晨 1 点实行一次:0 0 1 ? * L
  //在 26 分、29 分、33 分执行一次:0 26,29,33 * * * ?
  //2005年的每天上午10:15触发  0 15 10 * * ? 2005
  //在每天下午2点到下午2.59分期间的每1分钟触发   0 * 14 * * ?
  //cron 线上工具 : https://cron.qqe2.com/
  @Scheduled(cron = "0/5 * * * * ?")
  public void send(){
      log.info("start.....");
  }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青花科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值