Java timer, executed in the web Project

Source: Internet
Author: User

Import java. util. calendar; import java. util. date; import java. util. timer; import javax. servlet. servletContextEvent; import javax. servlet. servletContextListener; public class SendMessageTimer implements ServletContextListener {private Timer timer = null; @ Override public void contextDestroyed (writable arg0) {// TODO Auto-generated method stub timer. cancel () ;}@ Override public void contextInitialized (ServletContextEvent arg0) {// set the execution time to Calendar calendar = Calendar. getInstance (); int year = calendar. get (Calendar. YEAR); int month = calendar. get (Calendar. MONTH); int day = calendar. get (Calendar. DAY_OF_MONTH); // every day // customizes the daily 10:00:00 execution, calendar. set (year, month, day, 10, 00, 00); Date date = calendar. getTime (); timer = new Timer (true); System. out. println (date); int period = 2*1000; // execute the task at the date point every day and execute the timer again every 2 seconds. schedule (new SendMessageTask (), date, period); // execute the task once every day on the date. // timer. schedule (task, date );}}

Import java. util. TimerTask; public class SendMessageTask extends TimerTask {@ Override public void run () {System. out. println ("executed ...... ");}}

 

Web. xml configuration listening
<listener>           <listener-class>com.tsingsoft.basedata.sendMessage.SendMessageTimer</listener-class>   </listener>  

 

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.