Java Web監(jiān)聽器如何實(shí)現(xiàn)定時(shí)發(fā)送郵件
首先介紹java定時(shí)器(java.util.Timer)有定時(shí)執(zhí)行計(jì)劃任務(wù)的功能,通過設(shè)定定時(shí)器的間隔時(shí)間,會(huì)自動(dòng)在此間隔時(shí)間后執(zhí)行預(yù)先安排好的任務(wù)(java.util. TimerTask)
由于我們希望當(dāng)Web工程啟動(dòng)時(shí),定時(shí)器能自動(dòng)開始計(jì)時(shí),這樣在整個(gè)Web工程的生命期里,就會(huì)定時(shí)的執(zhí)行任務(wù),因此啟動(dòng)定時(shí)器的類不能是一般的類,此處用Servlet的監(jiān)聽器類來啟動(dòng)定時(shí)器,通過在配置文件里配置此監(jiān)聽器, 讓其在工程啟動(dòng)時(shí)自動(dòng)加載運(yùn)行,存活期為整個(gè)Web工程生命期.
首先要去實(shí)現(xiàn)一個(gè)監(jiān)聽任務(wù):
package com.sun.action; import java.util.Timer; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; /** * @author szy * @version 創(chuàng)建時(shí)間:2018-4-5 上午10:46:11 * */ public class MyTimerTask implements ServletContextListener { private Timer timer = null; @Override public void contextDestroyed(ServletContextEvent event) { // TODO Auto-generated method stub timer.cancel(); event.getServletContext().log("定時(shí)器銷毀"); } @Override public void contextInitialized(ServletContextEvent event) { // TODO Auto-generated method stub //在這里初始化監(jiān)聽器,在tomcat啟動(dòng)的時(shí)候監(jiān)聽器啟動(dòng),可以在這里實(shí)現(xiàn)定時(shí)器功能 timer = new Timer(true); event.getServletContext().log("定時(shí)器已啟動(dòng)");//添加日志,可在tomcat日志中查看到 //調(diào)用exportHistoryBean,0表示任務(wù)無延遲,5*1000表示每隔5秒執(zhí)行任務(wù),60*60*1000表示一個(gè)小時(shí); //timer.schedule(new SendEmail(event.getServletContext()),0,24*60*60*1000); timer.schedule(new SendEmail(event.getServletContext()),0,5*1000); } }
然后實(shí)現(xiàn)監(jiān)聽的方法類:
package com.sun.action; import java.util.TimerTask; import javax.servlet.ServletContext; /** * @author szy * @version 創(chuàng)建時(shí)間:2018-4-5 上午10:50:00 * */ public class SendEmail extends TimerTask { private ServletContext context = null; public SendEmail(ServletContext context) { this.context = context; } @Override public void run() { // TODO Auto-generated method stub System.out.println("您的郵件已發(fā)送,清注意查收"); } }
完成后,配置監(jiān)聽到web.xml里面去。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>TimerWeb</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>com.sun.action.MyTimerTask</listener-class> </listener> </web-app>
OK,通過Tomcat運(yùn)行項(xiàng)目即可,可看到隔5s就會(huì)發(fā)送一條郵件,當(dāng)然這里是模擬發(fā)送的郵件。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)多線程文件的斷點(diǎn)續(xù)傳
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)多線程文件的斷點(diǎn)續(xù)傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06java web中 HttpClient模擬瀏覽器登錄后發(fā)起請(qǐng)求
這篇文章主要介紹了java web中 HttpClient模擬瀏覽器登錄后發(fā)起請(qǐng)求的相關(guān)資料,需要的朋友可以參考下2017-05-055種必會(huì)的Java異步調(diào)用轉(zhuǎn)同步的方法你會(huì)幾種
這篇文章主要介紹了5種必會(huì)的Java異步調(diào)用轉(zhuǎn)同步的方法你會(huì)幾種,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12SpringBoot項(xiàng)目中Date類型數(shù)據(jù)在接口返回的時(shí)間不正確的問題解決
如果接口返回的Date類型時(shí)間與數(shù)據(jù)庫(kù)中datetime不一致,可能是由于沒有正確配置時(shí)區(qū)導(dǎo)致的,解決方法是在yaml配置文件中指定正確的日期格式和時(shí)區(qū)配置,修改配置并重啟項(xiàng)目后,可以獲得正確的時(shí)間,下面就來介紹一下2024-09-09淺談JAVA實(shí)現(xiàn)選擇排序,插入排序,冒泡排序,以及兩個(gè)有序數(shù)組的合并
這篇文章主要介紹了JAVA實(shí)現(xiàn)選擇排序,插入排序,冒泡排序,以及兩個(gè)有序數(shù)組的合并,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03springboot支持https請(qǐng)求的實(shí)現(xiàn)
本文主要介紹了springboot支持https請(qǐng)求的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01