Servlet Listener and Timer timer for Java Web application simple automatic job

Source: Internet
Author: User
Tags java web

In Web applications, sometimes customers need timed programs that do not require the customer to operate on their own, but are triggered by the application to perform certain actions on their own. This time, the use of monitoring and timer can basically achieve this requirement.

1. Create a listening Servelet, this class inherits Javax.servlet.http.HttpServlet and implements Javax.servlet.ServletContextListener.

[HTML]View PlainCopy
  1. Package com.bndz.outMail;
  2. Import Java.util.Timer;
  3. Import javax.servlet.ServletContextEvent;
  4. Import Javax.servlet.ServletContextListener;
  5. Import Javax.servlet.http.HttpServlet;
  6. /**
  7. *
  8. * Files sent by the listener need to be configured in Web-xml
  9. */
  10. public class Mailsendlistener extends HttpServlet implements
  11. Servletcontextlistener {
  12. /**
  13. * Monitoring begins to destroy
  14. */
  15. public void contextdestroyed (Servletcontextevent sce) {
  16. }
  17. /**
  18. * Monitoring starts execution
  19. */
  20. public void contextinitialized (Servletcontextevent sce) {
  21. When the listener starts executing, set a time
  22. Timer timer = new timer ();
  23. System.out.println ("-------MailSend timer begins execution--------------");
  24. Mailsendtask msendtask = new Mailsendtask ();
  25. Timer.schedule (Msendtask, 60 * 1000, 10 * 1000);
  26. System.out.println ("-------MailSend timer is already executing--------------");
  27. }
  28. }



2. Configure the Web. xml file for configuration.

[HTML]View PlainCopy
    1. <listener>
    2. <listener-class>com.bndz.outmail.mailsendlistener</listener-class>
    3. </Listener>


3. Create a mailsendtask, inherit the Java.util.TimerTask, and execute the program within the Run method.

[HTML]View PlainCopy
  1. Package com.bndz.outMail;
  2. Import Java.util.Date;
  3. Import Java.util.TimerTask;
  4. /**
  5. * JAVA a timed task
  6. *
  7. * @author guox2011-05-10
  8. *
  9. */
  10. public class Mailsendtask extends TimerTask {
  11. Public Mailsendtask () {
  12. }
  13. /**
  14. * The Run method is put into a schedule when executing a timed program to perform the import
  15. */
  16. public void Run () {
  17. System.out.println (New Date () + "---mailsendtask begin to RUN------------");
  18. Mailsendmanager Mainsendmanager = new Mailsendmanager ();
  19. Mainsendmanager.waitingmailremind ();
  20. }
  21. }

4. In the Contextinitialized method of listening for Servelet, write a timer, and let it schedule method to perform the Mailsendtask task described above.

[HTML]View PlainCopy
      1. When the listener starts executing, set a time
      2. Timer timer = new timer ();
      3. System.out.println ("-------MailSend timer begins execution--------------");
      4. Mailsendtask msendtask = new Mailsendtask ();
      5. Timer.schedule (Msendtask, 60 * 1000, 10 * 1000);
      6. System.out.println ("-------MailSend timer is already executing--------------");

Servlet Listener and Timer timer for Java Web application simple automatic job

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.