Spring定時(shí)任務(wù)使用及如何使用郵件監(jiān)控服務(wù)器
Spring相關(guān)的依賴導(dǎo)入進(jìn)去,即可使用spring的定時(shí)任務(wù)!
<!-- spring核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.13.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.13.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.13.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.13.RELEASE</version> </dependency>
定時(shí)任務(wù)是開發(fā)中常用的,比如訂單查詢,一位客人訂購(gòu)的某個(gè)東西,但是尚未支付,超過(guò)訂單時(shí)效期自動(dòng)失效,那么又是怎么樣知道訂單的時(shí)效性過(guò)呢?定時(shí)任務(wù),可以每分鐘或者每秒鐘進(jìn)行查詢。
定時(shí)任務(wù)的應(yīng)用是非常廣的,下面應(yīng)用下監(jiān)控服務(wù)器,雖然說(shuō)現(xiàn)在開源監(jiān)控軟件挺多的,什么zabbix,nagios或者其他等等。下面我將使用代碼監(jiān)控服務(wù)器:
首先準(zhǔn)備郵件的依賴:
<!-- 發(fā)郵件 --> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.5.2</version> <scope>provided</scope> </dependency>
郵件工具類:
import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; public class MailUtils { public static void sendMail(String email, String emailMsg) throws AddressException, MessagingException { // 1.創(chuàng)建一個(gè)程序與郵件服務(wù)器會(huì)話對(duì)象 Session Properties props = new Properties(); props.setProperty("mail.transport.protocol", "SMTP"); props.setProperty("mail.host", "smtp.163.com"); props.setProperty("mail.smtp.auth", "true");// 指定驗(yàn)證為true // 創(chuàng)建驗(yàn)證器 Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("123@163.com", "123"); } }; Session session = Session.getInstance(props, auth); // 2.創(chuàng)建一個(gè)Message,它相當(dāng)于是郵件內(nèi)容 Message message = new MimeMessage(session); message.setFrom(new InternetAddress("123@163.com")); // 設(shè)置發(fā)送者 message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 設(shè)置發(fā)送方式與接收者 message.setSubject("郵件告警"); message.setContent(emailMsg, "text/html;charset=utf-8"); // 3.創(chuàng)建 Transport用于將郵件發(fā)送 Transport.send(message); } }
監(jiān)控服務(wù)器類:
package cn.pms.monitor; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import javax.mail.MessagingException; import javax.mail.internet.AddressException; import cn.pms.util.MailUtils; public class MonitorUrl { public static void testUrlWithTimeOut2016(String urlString,int timeOutMillSeconds){ long lo = System.currentTimeMillis(); URL url; try { url = new URL(urlString); URLConnection co = url.openConnection(); co.setConnectTimeout(timeOutMillSeconds); co.connect(); System.out.println("連接可用"); } catch (Exception e1) { System.out.println("連接打不開!"); url = null; emailMonitor2016(); } System.out.println(System.currentTimeMillis()-lo); } public static void testUrlWithTimeOut2018(String urlString,int timeOutMillSeconds){ long lo = System.currentTimeMillis(); URL url; try { url = new URL(urlString); URLConnection co = url.openConnection(); co.setConnectTimeout(timeOutMillSeconds); co.connect(); System.out.println("連接可用"); } catch (Exception e1) { System.out.println("連接打不開!"); url = null; emailMonitor2018(); } System.out.println(System.currentTimeMillis()-lo); } public static void testUrlWithTimeOut1818(String urlString,int timeOutMillSeconds){ long lo = System.currentTimeMillis(); URL url; try { url = new URL(urlString); URLConnection co = url.openConnection(); co.setConnectTimeout(timeOutMillSeconds); co.connect(); System.out.println("連接可用"); } catch (Exception e1) { System.out.println("連接打不開!"); url = null; emailMonitor1818();; } System.out.println(System.currentTimeMillis()-lo); } public static void testUrlWithTimeOut1616(String urlString,int timeOutMillSeconds){ long lo = System.currentTimeMillis(); URL url; try { url = new URL(urlString); URLConnection co = url.openConnection(); co.setConnectTimeout(timeOutMillSeconds); co.connect(); System.out.println("連接可用"); } catch (Exception e1) { System.out.println("連接打不開!"); url = null; emailMonitor1616(); } System.out.println(System.currentTimeMillis()-lo); } public static void emailMonitor2016() { try { MailUtils.sendMail("123@qq.com", "tomcat服務(wù)器端口為2016宕機(jī)了"); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void emailMonitor2018() { try { MailUtils.sendMail("123@qq.com", "tomcat服務(wù)器端口為2018宕機(jī)了"); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void emailMonitor1818() { try { MailUtils.sendMail("1236@qq.com", "tomcat服務(wù)器端口為1818宕機(jī)了"); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void emailMonitor1616() { try { MailUtils.sendMail("123@qq.com", "tomcat服務(wù)器端口為1616宕機(jī)了"); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
具體定時(shí)任務(wù)類:
@Component public class QuartzJob { private static Logger logger = Logger.getLogger(QuartzJob.class); @Scheduled(cron = "0 0/1 * * * ? ") public void test() { MonitorUrl.testUrlWithTimeOut2018("http://www.yc520.com:2018/", 2000); MonitorUrl.testUrlWithTimeOut1616("http://www.yc520.com:1616/", 2000); logger.info("每分鐘執(zhí)行" + System.currentTimeMillis()); } @Scheduled(cron = "0 10 0 * * ?") public void monitorServerTest() { System.out.println("每10分鐘監(jiān)控一次2018服務(wù)器和1616服務(wù)器"); MonitorUrl.testUrlWithTimeOut2018("http://www.yc520.com:2018/", 2000); MonitorUrl.testUrlWithTimeOut1616("http://www.yc520.com:1616/", 2000); } @Scheduled(cron="0 30 0 * * ?") public void monitorServer() { System.out.println("每30分鐘監(jiān)控一次1818測(cè)試服務(wù)器"); MonitorUrl.testUrlWithTimeOut1818("http://www.yc520:1818/", 2000); } }
由此就可以達(dá)到監(jiān)控服務(wù)器的目的,當(dāng)然這只是小試牛刀,而且也不夠全面,當(dāng)然也存在問(wèn)題,如果是每分鐘定時(shí)任務(wù)檢測(cè),突然一臺(tái)服務(wù)器掛了,那么將會(huì)源源不斷的發(fā)送郵件,163郵件是有限的,而且頻繁的可能會(huì)被當(dāng)成垃圾郵件,我只需要知道一條信息,某某服務(wù)器宕機(jī)了,一遍就可以,最后給我發(fā)三十遍,如此的話,大量的無(wú)效郵件很浪費(fèi)資源,而且浪費(fèi)時(shí)間。
所以說(shuō),本文只是一個(gè)服務(wù)器監(jiān)控小示例,實(shí)際開發(fā)中,切勿直接拿來(lái)用,最好要有相關(guān)的判斷和邏輯。這樣才能比較高效,達(dá)到預(yù)期期望。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud-Alibaba-Sentinel服務(wù)降級(jí),熱點(diǎn)限流,服務(wù)熔斷
這篇文章主要介紹了SpringCloud-Alibaba-Sentinel服務(wù)降級(jí),熱點(diǎn)限流,服務(wù)熔斷,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Apache Shrio安全框架實(shí)現(xiàn)原理及實(shí)例詳解
這篇文章主要介紹了Apache Shrio安全框架實(shí)現(xiàn)原理及實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04JavaSE static final及abstract修飾符實(shí)例解析
這篇文章主要介紹了JavaSE static final及abstract修飾符實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06SpringBoot整合Drools規(guī)則引擎動(dòng)態(tài)生成業(yè)務(wù)規(guī)則的實(shí)現(xiàn)
本文主要介紹了SpringBoot整合Drools規(guī)則引擎動(dòng)態(tài)生成業(yè)務(wù)規(guī)則的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12詳解Java分布式緩存系統(tǒng)中必須解決的四大問(wèn)題
分布式緩存系統(tǒng)是三高架構(gòu)中不可或缺的部分,極大地提高了整個(gè)項(xiàng)目的并發(fā)量、響應(yīng)速度,但它也帶來(lái)了新的需要解決的問(wèn)題,分別是: 緩存穿透、緩存擊穿、緩存雪崩和緩存一致性問(wèn)題。本文將詳細(xì)講解一下這四大問(wèn)題,需要的可以參考一下2022-04-04java判斷integer是否為空的詳細(xì)過(guò)程
在java編寫過(guò)程中,我們會(huì)使用到各種各樣的表達(dá)式,在使用表達(dá)式的過(guò)程中,有哪些安全問(wèn)題需要我們注意的呢?對(duì)java判斷integer是否為空相關(guān)知識(shí)感興趣的朋友一起來(lái)看看吧2023-02-02SpringBoot項(xiàng)目整合mybatis的方法步驟與實(shí)例
今天小編就為大家分享一篇關(guān)于SpringBoot項(xiàng)目整合mybatis的方法步驟與實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03