spring定时任务执行两次及tomcat部署缓慢问题的解决方法
作者:Fururur 时间:2022-12-27 09:53:25
一、spring定时任务执行两次
问题重现和解析
最近使用quartz定时任务框架,结果发现开发环境执行无任何问题,部署到服务器上后,发现同一时间任务执行了多次。经过搜索发现是服务器上tomcat的配置文件出现了问题。
原来的配置文件——server.xml如下:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
<Host name="www.xxx.com" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/usr/local/tomcat/apache-tomcat-8.5.9/webapps/xxxindex" reloadable="true"></Context>
</Host>
一个Host表示一个容器,里面可以包含若干个Context(应用)。上面这段配置文件意思就是:在tomcat中配置了两个容器,一个name=localhost,应用的根目录为webapps,并且会自动解压war包和自动部署。没有指定context,会把根目录下的所有web应用都部署,部署成功后,外网可以通过服务器IP+项目名来访问;另一个name=www.xxx.com,和第一个host不同在于,配置了主页web应用,且不需要跟项目名就可以访问。部署成功后可以通过域名+项目名访问,主页所在项目可以直接通过根域名访问。
这个时候问题就来了,包含定时任务的项目部署在webapps目录下,tomcat中两个独立的容器都部署了一遍,相当于项目在服务器上的tomcat上部署了两次,两边同时会运行定时任务,指定的是同一个数据库。
问题解决
因此,为了尽可能不影响其他项目的正常访问,我做了折中,讲需要执行定时任务的项目单独部署在另一个文件夹中,例如webroot ,然后只使用域名那个host,配置文件修改后如下:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
<Host name="www.xxx.com" appBase="" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/usr/local/tomcat/apache-tomcat-8.5.9/webapps/xxxindex" reloadable="true"></Context>
<Context path="/projectA" docBase="/usr/local/tomcat/apache-tomcat-8.5.9/webapps/projectA" reloadable="true"></Context>
<Context path="/projectB" docBase="/usr/local/tomcat/apache-tomcat-8.5.9/webapps/projectB" reloadable="true"></Context>
<Context path="/projectC" docBase="/usr/local/tomcat/apache-tomcat-8.5.9/webroot/projectC" reloadable="true"></Context>
</Host>
可以看到projectC是包含定时任务的项目。这样部署成功后,除了该项目只能通过域名访问之外,其余项目的访问方式和之前保持不变。同时问题解决,定时任务只执行一次。
网上的另一种说法
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Context docBase="projectA" path="" reloadable="true" />
</Host>
只有一个host,tomcat在启动时,会部署一次根目录下的所有项目,然后Context又会单独部署一次,所以也会导致定时任务执行2次。
对于这种问题,解决的方案也有多种:
将huost的appBase设为空,将Context的Context 指向项目部署位置的绝对路径。
删除Context节点。
二、tomcat部署缓慢的问题
用的阿里云服务器,部署tomcat时速度非常慢,但是后来买的新阿里云又没有这个问题。部署项目后一直会在
INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /opt/apache-tomcat-8.0.15-server/webapps/ROOT
这里卡好几分钟才会继续下去。之前一直以为是服务器配置原因,后来无意中发现是jre的配置原因。参考了几篇博客,发现oracle在WebLogic的文档下Avoiding JVM Delays Caused by Random Number Generation给了原因和解决方案。
The library used for random number generation in Sun's JVM relies on /dev/random by default for UNIX platforms. This can potentially block the WebLogic SIP Server process because on some operating systems /dev/random waits for a certain amount of "noise" to be generated on the host machine before returning a result. Although /dev/random is more secure, BEA recommends using /dev/urandom if the default JVM configuration delays WebLogic SIP Server startup.
意思就是:
JVM上产生随机数的策略有两种:/dev/random 和/dev/urandom。
tomcat或者WebLogic等web服务器在部署时需要等待若一段随机数产生的时间。unix平台下JVM默认采用的是安全性更好的/dev/random,但是潜在的会阻塞服务进程。
推荐使用/dev/urandom,产生随机数速度快,/dev/random需要时间间隔生成随机数,部署时间长。
修改方式:
打开$JAVA_HOME/jre/lib/security/java.security文件。
将securerandom.source=file:/dev/random 修改为securerandom.source=file:/dev/urandom
重启tomcat,三十秒部署成功,solve it
来源:https://www.cnblogs.com/Sinte-Beuve/p/8360321.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java IO之File 类详解
![](https://img.aspxhome.com/file/2023/2/57802_0s.png)
简单解析java方法在调用在内存中的执行过程
![](https://img.aspxhome.com/file/2023/6/59756_0s.png)
Android自定义View之组合控件实现类似电商app顶部栏
![](https://img.aspxhome.com/file/2023/3/84713_0s.png)
java文件操作输入输出结构详解
C#实现简单的JSON序列化功能代码实例
SpringBoot中@Import注解如何正确使用
![](https://img.aspxhome.com/file/2023/7/57707_0s.png)
java编写ftp下载工具
Spring深入刨析声明式事务注解的源码
![](https://img.aspxhome.com/file/2023/6/61026_0s.png)
Java 实现常见的非对称加密算法
hibernate 命名查询如何实现
JavaMail入门教程之解析邮件(5)
![](https://img.aspxhome.com/file/2023/0/60030_0s.jpg)
Java单例模式下的MongoDB数据库操作工具类
java设计模式之工厂模式实例详解
![](https://img.aspxhome.com/file/2023/2/60142_0s.png)
Java多线程工具CompletableFuture的使用教程
![](https://img.aspxhome.com/file/2023/1/57711_0s.jpg)
Java调用.dll文件的方法
Java Filter 过滤器详细介绍及实例代码
Android使用xUtils3.0实现文件上传
java对ArrayList排序代码示例
Java算法实战之排一亿个随机数
![](https://img.aspxhome.com/file/2023/4/60694_0s.png)