Spring定時任務(wù)使用及如何使用郵件監(jiān)控服務(wù)器
Spring相關(guān)的依賴導(dǎo)入進去,即可使用spring的定時任務(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>
定時任務(wù)是開發(fā)中常用的,比如訂單查詢,一位客人訂購的某個東西,但是尚未支付,超過訂單時效期自動失效,那么又是怎么樣知道訂單的時效性過呢?定時任務(wù),可以每分鐘或者每秒鐘進行查詢。
定時任務(wù)的應(yīng)用是非常廣的,下面應(yīng)用下監(jiān)控服務(wù)器,雖然說現(xiàn)在開源監(jiān)控軟件挺多的,什么zabbix,nagios或者其他等等。下面我將使用代碼監(jiān)控服務(wù)器:
首先準備郵件的依賴:
<!-- 發(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)建一個程序與郵件服務(wù)器會話對象 Session Properties props = new Properties(); props.setProperty("mail.transport.protocol", "SMTP"); props.setProperty("mail.host", "smtp.163.com"); props.setProperty("mail.smtp.auth", "true");// 指定驗證為true // 創(chuàng)建驗證器 Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("123@163.com", "123"); } }; Session session = Session.getInstance(props, auth); // 2.創(chuàng)建一個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宕機了"); } 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宕機了"); } 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宕機了"); } 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宕機了"); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
具體定時任務(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測試服務(wù)器"); MonitorUrl.testUrlWithTimeOut1818("http://www.yc520:1818/", 2000); } }
由此就可以達到監(jiān)控服務(wù)器的目的,當(dāng)然這只是小試牛刀,而且也不夠全面,當(dāng)然也存在問題,如果是每分鐘定時任務(wù)檢測,突然一臺服務(wù)器掛了,那么將會源源不斷的發(fā)送郵件,163郵件是有限的,而且頻繁的可能會被當(dāng)成垃圾郵件,我只需要知道一條信息,某某服務(wù)器宕機了,一遍就可以,最后給我發(fā)三十遍,如此的話,大量的無效郵件很浪費資源,而且浪費時間。
所以說,本文只是一個服務(wù)器監(jiān)控小示例,實際開發(fā)中,切勿直接拿來用,最好要有相關(guān)的判斷和邏輯。這樣才能比較高效,達到預(yù)期期望。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud-Alibaba-Sentinel服務(wù)降級,熱點限流,服務(wù)熔斷
這篇文章主要介紹了SpringCloud-Alibaba-Sentinel服務(wù)降級,熱點限流,服務(wù)熔斷,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12Apache Shrio安全框架實現(xiàn)原理及實例詳解
這篇文章主要介紹了Apache Shrio安全框架實現(xiàn)原理及實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04JavaSE static final及abstract修飾符實例解析
這篇文章主要介紹了JavaSE static final及abstract修飾符實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06SpringBoot整合Drools規(guī)則引擎動態(tài)生成業(yè)務(wù)規(guī)則的實現(xiàn)
本文主要介紹了SpringBoot整合Drools規(guī)則引擎動態(tài)生成業(yè)務(wù)規(guī)則的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12