最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring定時(shí)任務(wù)使用及如何使用郵件監(jiān)控服務(wù)器

 更新時(shí)間:2019年07月23日 14:45:10   作者:挑戰(zhàn)者V  
這篇文章主要介紹了Spring定時(shí)任務(wù)使用及如何使用郵件監(jiān)控服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

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è)東西,但是尚未支付,超過訂單時(shí)效期自動(dòng)失效,那么又是怎么樣知道訂單的時(shí)效性過呢?定時(shí)任務(wù),可以每分鐘或者每秒鐘進(jìn)行查詢。

定時(shí)任務(wù)的應(yīng)用是非常廣的,下面應(yīng)用下監(jiān)控服務(wù)器,雖然說現(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)然也存在問題,如果是每分鐘定時(shí)任務(wù)檢測(cè),突然一臺(tái)服務(wù)器掛了,那么將會(huì)源源不斷的發(fā)送郵件,163郵件是有限的,而且頻繁的可能會(huì)被當(dāng)成垃圾郵件,我只需要知道一條信息,某某服務(wù)器宕機(jī)了,一遍就可以,最后給我發(fā)三十遍,如此的話,大量的無效郵件很浪費(fèi)資源,而且浪費(fèi)時(shí)間。

所以說,本文只是一個(gè)服務(wù)器監(jiān)控小示例,實(shí)際開發(fā)中,切勿直接拿來用,最好要有相關(guān)的判斷和邏輯。這樣才能比較高效,達(dá)到預(yù)期期望。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲

    java實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • 簡(jiǎn)單談?wù)凧VM、JRE和JDK的區(qū)別與聯(lián)系

    簡(jiǎn)單談?wù)凧VM、JRE和JDK的區(qū)別與聯(lián)系

    簡(jiǎn)單的說JDK是用于開發(fā)的而JRE是用于運(yùn)行Java程序的。JDK和JRE都包含了JVM,從而使得我們可以運(yùn)行Java程序。JVM是Java編程語言的核心并且具有平臺(tái)獨(dú)立性。
    2016-05-05
  • Java實(shí)現(xiàn)Dijkstra輸出最短路徑的實(shí)例

    Java實(shí)現(xiàn)Dijkstra輸出最短路徑的實(shí)例

    這篇文章主要介紹了Java實(shí)現(xiàn)Dijkstra輸出最短路徑的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • 詳解在Spring MVC或Spring Boot中使用Filter打印請(qǐng)求參數(shù)問題

    詳解在Spring MVC或Spring Boot中使用Filter打印請(qǐng)求參數(shù)問題

    這篇文章主要介紹了詳解在Spring MVC或Spring Boot中使用Filter打印請(qǐng)求參數(shù)問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Spring使用注解存儲(chǔ)Bean對(duì)象的方法詳解

    Spring使用注解存儲(chǔ)Bean對(duì)象的方法詳解

    在使用學(xué)習(xí)使用 Spring過程中,當(dāng)我們要實(shí)現(xiàn)一個(gè)功能的時(shí)候,先應(yīng)該考慮的是有沒有相應(yīng)的注解是實(shí)現(xiàn)對(duì)應(yīng)功能的,Spring 中很多功能的配置都是可以依靠注解實(shí)現(xiàn)的,而本篇中介紹的是使用注解來存儲(chǔ) Bean 對(duì)象
    2023-07-07
  • 重新實(shí)現(xiàn)hashCode()方法

    重新實(shí)現(xiàn)hashCode()方法

    hashCode()是Java中的一個(gè)重要方法,用于計(jì)算對(duì)象的哈希碼。本文介紹了如何重新實(shí)現(xiàn)hashCode()方法,包括使用對(duì)象的屬性計(jì)算哈希碼、使用字符串拼接計(jì)算哈希碼、使用隨機(jī)數(shù)計(jì)算哈希碼等方法。同時(shí),還介紹了如何避免哈希沖突,提高哈希表的效率。
    2023-04-04
  • Java注解Annotation與自定義注解詳解

    Java注解Annotation與自定義注解詳解

    本文全面講述了Java注解Annotation與Java自定義注解及相關(guān)內(nèi)容,大家可以認(rèn)真看看
    2018-03-03
  • 阿里的一道Java并發(fā)面試題詳解

    阿里的一道Java并發(fā)面試題詳解

    這篇文章主要介紹了阿里的一道Java并發(fā)面試題詳解,網(wǎng)絡(luò)、并發(fā)相關(guān)的知識(shí),相對(duì)其他一些編程知識(shí)點(diǎn)更難一些,主要是不好調(diào)試并且涉及內(nèi)容太多 !,需要的朋友可以參考下
    2019-06-06
  • IntelliJ IDEA(2020.2)的下載、安裝步驟詳細(xì)教程

    IntelliJ IDEA(2020.2)的下載、安裝步驟詳細(xì)教程

    這篇文章主要介紹了IntelliJ IDEA(2020.2)的下載、安裝步驟詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Java代理模式之靜態(tài)代理與動(dòng)態(tài)代理的區(qū)別及優(yōu)缺點(diǎn)

    Java代理模式之靜態(tài)代理與動(dòng)態(tài)代理的區(qū)別及優(yōu)缺點(diǎn)

    代理模式是一種常用的設(shè)計(jì)模式,它允許通過引入一個(gè)代理對(duì)象來控制對(duì)目標(biāo)對(duì)象的訪問,在Java中,代理模式被廣泛應(yīng)用,它可以提供額外的功能,如權(quán)限檢查、緩存、日志記錄等,本文將介紹靜態(tài)代理與動(dòng)態(tài)代理的區(qū)別及優(yōu)缺點(diǎn),需要的朋友可以參考下
    2023-06-06

最新評(píng)論

元氏县| 青阳县| 拜城县| 西乡县| 莱阳市| 普兰店市| 陆良县| 义马市| 普格县| 米脂县| 龙泉市| 区。| 黔江区| 新泰市| 榆树市| 裕民县| 攀枝花市| 平塘县| 磐石市| 华亭县| 青铜峡市| 晋中市| 丹棱县| 连南| 陆河县| 都兰县| 蓬莱市| 屏山县| 延安市| 黄石市| 荔浦县| 平乡县| 晋州市| 固镇县| 逊克县| 大荔县| 阿瓦提县| 禹州市| 丰城市| 荔浦县| 涪陵区|