spring Scheduled多次执行问题 线上Case已解决

2024-04-02 15:38

本文主要是介绍spring Scheduled多次执行问题 线上Case已解决,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

spring Scheduled多次执行问题 线上Case已解决

需求:对VIP即将到期的用户发送邮件通知,每天的9点半,17点半,22点执行一次
项目:springboot + tomcat

这里面涉及两个问题

第一:tomcat配置问题。
有很多公司可能会有多个项目部署在一个tomcat下面,例如两个域名service.xxx.com和manage.xxx.com都部署在tomcat下面。
我原来的tomcat/config/server.conf host部分配置文件是这样的,

<Host name="manage.XXX.cn"  appBase=""unpackWARs="true" autoDeploy="true"><!-- SingleSignOn valve, share authentication between web applicationsDocumentation at: /docs/config/valve.html --><!--<Valve className="org.apache.catalina.authenticator.SingleSignOn" />--><!-- Access log processes all example.Documentation at: /docs/config/valve.htmlNote: The pattern used is equivalent to using pattern="common" --><Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"prefix="localhost_access_log" suffix=".txt"pattern="%h %l %u %t &quot;%r&quot; %s %b" /><Context path="" docBase="D:\apache-tomcat-9.0.0.M10\webapps\manage.XXX.cn" reloadable="true"></Context></Host><Host name="service.XXX.cn"  appBase=""unpackWARs="true" autoDeploy="true"><!-- SingleSignOn valve, share authentication between web applicationsDocumentation at: /docs/config/valve.html --><!--<Valve className="org.apache.catalina.authenticator.SingleSignOn" />--><!-- Access log processes all example.Documentation at: /docs/config/valve.htmlNote: The pattern used is equivalent to using pattern="common" --><Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"prefix="localhost_access_log" suffix=".txt"pattern="%h %l %u %t &quot;%r&quot; %s %b" /><Context path="" docBase="D:\apache-tomcat-9.0.0.M10\webapps\service.XXX.cn" reloadable="true"></Context></Host>

原配置会导致项目被加载两次,由于本人技术有限,还不清楚tomcat的加载机制,以后会去深入学习。

但是查看了tomcat的官方文档:

path: Even when statically defining a Context in server.xml, this attribute must not be set unless either the docBase is not located
under the Host’s appBase or both deployOnStartup and autoDeploy are false. If this rule is not followed, double deployment is likely to result.

假如docBase的目录已经在appBase配置的目录下,或者deployOnStartup与autoDeploy都为false,就不需要配置path属性。如果配置了,就极有可能导致双重部署的结果。

修改后:

<Host name="manage.XXX.cn"  appBase=""unpackWARs="true" autoDeploy="false" deployOnStartup="false"><!-- SingleSignOn valve, share authentication between web applicationsDocumentation at: /docs/config/valve.html --><!--<Valve className="org.apache.catalina.authenticator.SingleSignOn" />--><!-- Access log processes all example.Documentation at: /docs/config/valve.htmlNote: The pattern used is equivalent to using pattern="common" --><Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"prefix="localhost_access_log" suffix=".txt"pattern="%h %l %u %t &quot;%r&quot; %s %b" /><Context path="" docBase="D:\apache-tomcat-9.0.0.M10\webapps\manage.XXX.cn" reloadable="true"></Context></Host><Host name="service.XXX.cn"  appBase=""unpackWARs="true" autoDeploy="false" deployOnStartup="false"><!-- SingleSignOn valve, share authentication between web applicationsDocumentation at: /docs/config/valve.html --><!--<Valve className="org.apache.catalina.authenticator.SingleSignOn" />--><!-- Access log processes all example.Documentation at: /docs/config/valve.htmlNote: The pattern used is equivalent to using pattern="common" --><Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"prefix="localhost_access_log" suffix=".txt"pattern="%h %l %u %t &quot;%r&quot; %s %b" /><Context path="" docBase="D:\apache-tomcat-9.0.0.M10\webapps\service.XXX.cn" reloadable="true"></Context></Host>

第二:Scheduled cron表达式语法不熟悉,入坑了
我原来cron表达式的写法:@Scheduled(cron = “0 30,30,0 9,17,22 * * ?”)
我本以为这样会是每天9点半,17点半,22点执行一次,但实际上是9点,9点半,17点,17点半,22点,22点半各执行了一次。这里就是因为对语法不够了解,入了坑。

我的解决方法可能有点笨,就是写了三个Scheduled
@Scheduled(cron = “0 30 9 * * ?”)
@Scheduled(cron = “0 30 17 * * ?”)
@Scheduled(cron = “0 0 22 * * ?”)

推荐一个在线Cron表达式解析网址:http://cron.qqe2.com/

JAVA学无止境

这篇关于spring Scheduled多次执行问题 线上Case已解决的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/870380

相关文章

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

Java中Integer128陷阱

《Java中Integer128陷阱》本文主要介绍了Java中Integer与int的区别及装箱拆箱机制,重点指出-128至127范围内的Integer值会复用缓存对象,导致==比较结果为true,下... 目录一、Integer和int的联系1.1 Integer和int的区别1.2 Integer和in

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

nginx 负载均衡配置及如何解决重复登录问题

《nginx负载均衡配置及如何解决重复登录问题》文章详解Nginx源码安装与Docker部署,介绍四层/七层代理区别及负载均衡策略,通过ip_hash解决重复登录问题,对nginx负载均衡配置及如何... 目录一:源码安装:1.配置编译参数2.编译3.编译安装 二,四层代理和七层代理区别1.二者混合使用举例

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA