@Scheduled定时任务重复执行两次的解决方法

本文介绍了如何在Spring MVC中配置DispatcherServlet,通过XML配置文件指定初始化参数并设置映射路径,确保请求能够正确地被DispatcherServlet处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据:https://blog.youkuaiyun.com/pearyangyang/article/details/77248020

添加代码:

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--加上了这个 定时器就不会同时执行两次了-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
 
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>    

那spring boot 怎么处理?

根据:https://blog.youkuaiyun.com/m0_37893932/article/details/79665148

添加类:

package com.wisdom.configuration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;

/**
 * Created by tanly on 2018/7/5 0005.
 */
public class MyWebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) {
        XmlWebApplicationContext appContext = new XmlWebApplicationContext();
        appContext.setConfigLocation("/WEB-INF/classes/application.properties");

        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(appContext));
//        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

### Spring Boot 中 `@Scheduled` 注解的使用方法 #### 启用调度功能 为了使定时任务生效,在应用程序的主要配置类或启动类上需添加 `@EnableScheduling` 注解[^3]。 ```java import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableScheduling public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` #### 定义定时任务 定义一个组件类,其中包含被标记为 `@Scheduled` 的无参方法。该注解支持多种方式来设定执行频率: - **Cron 表达式**: 使用复杂的日程安排模式。 - **Fixed Rate/Interval**: 设置固定的间隔时间触发任务。 ##### Cron 表达式的例子 下面的例子展示了每分钟的第一秒运行一次的任务设置[^2]: ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduledTasks { @Scheduled(cron = "0 * * * * ?") // 每整点的第 0 秒执行 public void reportCurrentTime() { System.out.println("The time is now"); } } ``` ##### Fixed Delay 或者 Fixed Rate 的例子 这里展示了一个每隔两秒钟执行一次的方法实例: ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class SimpleTaskScheduler { private int count = 0; @Scheduled(fixedRate = 2000L) public void performTaskWithFixedRate() { System.out.printf("Executing task with fixed rate %d\n", ++count); } } ``` 当采用 `fixedDelay` 参数时,则是在前次调用完成后等待指定毫秒数再开始下一轮;而 `fixedRate` 则无视上次执行耗时长短,严格按照给定周期重复执行[^1].
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值