When Tomcat is started, a specific method is executed. When tomcat is started

Source: Internet
Author: User

When Tomcat is started, a specific method is executed. When tomcat is started

import java.util.Timer;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class TestListener implements ServletContextListener {  private Timer timer = null;  public void contextInitialized(ServletContextEvent event) {    timer = new Timer(true);    timer.schedule(new TestTimer(), 0, 86400000);//milliseconds  }  public void contextDestroyed(ServletContextEvent event) {    timer.cancel();  }}------------------------------------------import java.util.TimerTask;public class TestTimer extends TimerTask {  public void run() {    // do something ...  }}------------------------------------------web.xml:<listener><listener-class>test.TestListener</listener-class></listener>

When tomcat is started in a java program, the methods in a class are executed.

Java has a Listener! Call your own method when tomcat starts! It can also be called cyclically! Web. xml <listener>
<Listener-class> com. function. SysContextListener </listener-class>
</Listener> SysContextListener. javapackage com. function; import java. util. Timer; // Timer class
Import javax. servlet. ServletContextEvent;
Import javax. servlet. ServletContextListener;
Public class SysContextListener implements ServletContextListener
{
Private Timer timer = null;
Public void contextInitialized (ServletContextEvent event)
{// Initialize the listener here. The Listener starts when tomcat is started. The timer function can be implemented here.
Timer = new Timer (true );
System. out. println ("started successfully ");
Event. getServletContext (). log ("timer started --------------------------------------------------"); // Add logs, which can be viewed in tomcat logs
// Timer. schedule (new exportHistoryBean (event. getServletContext (), DealDate. getTime (), 1000*1000); // call exportHistoryBean. 0 indicates that the task has no latency, 5*1000 indicates that the task is executed every 5 seconds, and 60*60 * indicates an hour.
Event. getServletContext (). log ("added task --------------------------------------------------");
}
Public void contextDestroyed (ServletContextEvent event)
{// Disable the listener here, So destroy the timer here.
Timer. cancel ();
Event. getServletContext (). log ("timer destruction --------------------------------------------------");
} ExportHistoryBean. javapackage com. function; import java. util. Calendar;
Import java. util. TimerTask;
Import javax. servlet. ServletContext;
Public class exportHistoryBe... the remaining full text>

How to execute a program at tomcat startup

Write a serlvet and start this thread in the Init method.
Set start up to 1 s.

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.