1. Spring timed tasks are executed twice
Problem reproducing and analysis
Recently, I used the quartz timing task framework and found that there was no problem with the development environment execution. After deploying it to the server, I found that the task was executed multiple times at the same time. After searching, I found that there was a problem with the tomcat configuration file on the server.
The original configuration file - server.xml is as follows:
<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>
A Host represents a container that can contain several Contexts (applications). The above configuration file means: two containers are configured in tomcat, one name=localhost, the root directory of the application is webapps, and the war package will be automatically decompressed and deployed automatically. If the context is not specified, all web applications in the root directory will be deployed. After the deployment is successful, the external network can be accessed through the server IP + project name; the other name=www.xxx.com, which is different from the first host, is configured with the home page web application and does not need to be accessed with the project name. After the deployment is successful, you can access it through the domain name + project name, and the project where the home page is located can be accessed directly through the root domain name.
At this time, the problem arises. The project containing the timing tasks is deployed in the webapps directory. Two independent containers in tomcat are deployed once, which is equivalent to the project being deployed twice on tomcat on the server. Both sides will run timing tasks at the same time, and the same database is specified.
Problem solving
Therefore, in order not to affect the normal access of other projects as much as possible, I made a compromise and said that the project that needs to perform timing tasks is deployed separately in another folder, such as webroot, and then only use the domain name host. After the configuration file is modified, the following is as follows:
<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>
You can see that projectC is a project containing timing tasks. After successful deployment, except for the project that can only be accessed through the domain name, the access method of the other projects remains unchanged from before. At the same time, the problem is solved, and the timing task is only executed once.
Another saying online
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context docBase="projectA" path="" reloadable="true" /> </Host>
There is only one host. When tomcat is started, all projects in the root directory will be deployed once, and then Context will be deployed once again, which will also cause the timing task to be executed twice.
There are many solutions to this problem:
2. The problem of slow tomcat deployment
The Alibaba Cloud server I used was very slow when deploying tomcat, but the new Alibaba Cloud I bought later did not have this problem. After the project is deployed, it will be
INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /opt/apache-tomcat-8.0.15-server/webapps/ROOT
It takes several minutes to continue here. I used to think it was the server configuration reason, but later I accidentally discovered that it was the jre configuration reason. After referring to several blogs, I found that oracle gave reasons and solutions under WebLogic's documentation.
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.
Meaning:
Modification method:
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.