Java Web監(jiān)聽器如何實(shí)現(xiàn)定時(shí)發(fā)送郵件
首先介紹java定時(shí)器(java.util.Timer)有定時(shí)執(zhí)行計(jì)劃任務(wù)的功能,通過設(shè)定定時(shí)器的間隔時(shí)間,會自動在此間隔時(shí)間后執(zhí)行預(yù)先安排好的任務(wù)(java.util. TimerTask)
由于我們希望當(dāng)Web工程啟動時(shí),定時(shí)器能自動開始計(jì)時(shí),這樣在整個(gè)Web工程的生命期里,就會定時(shí)的執(zhí)行任務(wù),因此啟動定時(shí)器的類不能是一般的類,此處用Servlet的監(jiān)聽器類來啟動定時(shí)器,通過在配置文件里配置此監(jiān)聽器, 讓其在工程啟動時(shí)自動加載運(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啟動的時(shí)候監(jiān)聽器啟動,可以在這里實(shí)現(xiàn)定時(shí)器功能
timer = new Timer(true);
event.getServletContext().log("定時(shí)器已啟動");//添加日志,可在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就會發(fā)送一條郵件,當(dāng)然這里是模擬發(fā)送的郵件。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java基礎(chǔ)之打印萬年歷的簡單實(shí)現(xiàn)(案例)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)之打印萬年歷的簡單實(shí)現(xiàn)(案例)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07
Springboot2.x+ShardingSphere實(shí)現(xiàn)分庫分表的示例代碼
這篇文章主要介紹了Springboot2.x+ShardingSphere實(shí)現(xiàn)分庫分表的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
java中hashmap的底層數(shù)據(jù)結(jié)構(gòu)與實(shí)現(xiàn)原理
Hashmap是java面試中經(jīng)常遇到的面試題,大部分都會問其底層原理與實(shí)現(xiàn),本人也是被這道題問慘了,為了能夠溫故而知新,特地寫了這篇文章,以便時(shí)時(shí)學(xué)習(xí)2021-08-08
Spring session實(shí)現(xiàn)共享單點(diǎn)登錄案例過程解析
這篇文章主要介紹了Spring session實(shí)現(xiàn)共享單點(diǎn)登錄案例過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Java自學(xué)書籍推薦 程序員到架構(gòu)師必看的書
這篇文章主要為大家推薦了Java程序員到架構(gòu)師自學(xué)書籍,幫助大家不斷提高自己的專業(yè)水平,感興趣的小伙伴們可以參考一下2016-09-09
Hystrix?Dashboard斷路監(jiān)控儀表盤的實(shí)現(xiàn)詳細(xì)介紹
這篇文章主要介紹了Hystrix?Dashboard斷路監(jiān)控儀表盤的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09

