When the web application is closed, the thread is not recycled, and the web application thread is recycled.

Source: Internet
Author: User

When the web application is closed, the thread is not recycled, and the web application thread is recycled.

Today, I read spring3.0 enterprise application development practices, where the start and end of the scheduler in web applications are as follows:

Static variables are at the ClassLoader level. If the web application stops, these static variables are also cleared from the jvm. However, the thread is JVM-level. If you start a thread in a web application, the lifecycle of the thread will not be synchronized with the web application. That is to say, even if the web application is stopped, this thread is still active.

Therefore, in order to truly verify the above conclusions, the examples in this book have been put into practice.

1. Configure the tomcat Application Management page

1. Start tomcat and access http: // localhost: 8080/through the web. The following page appears.

2. Click the Manager App to open the user name and password pop-up window.

3. Because you do not know the user name and password, you can cancel the operation directly. An error message is displayed, indicating a new page.

4. it can be seen from the new page, that is, if the configuration file is not changed, directly open the tomcat-users.xml file under the conf directory, by adding a role to the user can access the corresponding permissions page

  • Manager-gui: access the html page (that is, the URL path is/manager/html/*) with the permission to access status
  • Manager-script: Access plain text (that is, the URL path is/manager/text/*) and has the permission to access status.
  • Manager-jmx: access to the JMX proxy (that is, the URL path is/manager/jmxproxy/*) and has the permission to access status.
  • Manager-status: view the application status.

5. You can also find related information from tomcat help documentation, http: // localhost: 8080/docs/manager-howto.html,

6. Therefore, you can directly add the manager-gui to the tomcat user role to access the page.

7. Restart tomcat. Log On with the username tomcat and password tomcat to view the application management page.

 

Code 2 Verification

1. Create a web application and the TimerListenerTest class to implement ServletContextListener. The Code is as follows:

1 package com. test. timer; 2 3 import java. util. date; 4 import java. util. timer; 5 import java. util. timerTask; 6 7 import javax. servlet. servletContextEvent; 8 import javax. servlet. servletContextListener; 9 10 public class TimerListenerTest implements ServletContextListener {11 12 private Timer timer; 13 14 @ Override15 public void contextDestroyed (ServletContextEvent arg0) {16 System. out. println ("--- application closed ---"); 17} 18 19 @ Override20 public void contextInitialized (ServletContextEvent arg0) {21 System. out. println ("--- application activation ---"); 22 23 timer = new Timer (); 24 25 TimerTaskTest task = new TimerTaskTest (); 26 27 timer. schedule (task, 1000L, 1000L); 28} 29 30 class TimerTaskTest extends TimerTask {31 32 @ Override33 public void run () {34 System. out. println ("-----" + new Date (); 35} 36} 37 38}

2. Deploy the application and start tomcat. The program runs as follows:

-----Sat Jan 02 22:42:59 CST 2016-----Sat Jan 02 22:43:00 CST 2016-----Sat Jan 02 22:43:01 CST 2016-----Sat Jan 02 22:43:02 CST 2016

3. Stop the application directly on the tomcat web management page.

4. Observe the background running.

 

5. As shown above, the web application does run the contextDestroyed method to destroy the application, but observes the running status of the application,

The thread in the application is still running.

6. Of course, the program can use the timer. cannel () method to collect and close the timer to close the thread.

7. github Code address https://github.com/lixiaojiao2010/WebTest

This can verify the correctness of the conclusion at the beginning of the article.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.