欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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ù)熔斷

    這篇文章主要介紹了SpringCloud-Alibaba-Sentinel服務(wù)降級,熱點限流,服務(wù)熔斷,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Apache Shrio安全框架實現(xiàn)原理及實例詳解

    Apache Shrio安全框架實現(xiàn)原理及實例詳解

    這篇文章主要介紹了Apache Shrio安全框架實現(xiàn)原理及實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • JavaSE static final及abstract修飾符實例解析

    JavaSE static final及abstract修飾符實例解析

    這篇文章主要介紹了JavaSE static final及abstract修飾符實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • Java遍歷json字符串取值的實例

    Java遍歷json字符串取值的實例

    下面小編就為大家分享一篇Java遍歷json字符串取值的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Spring Boot Web應(yīng)用程序配置詳解

    Spring Boot Web應(yīng)用程序配置詳解

    這篇文章主要介紹了Spring Boot Web應(yīng)用程序配置詳解,本文中將介紹一些Web應(yīng)用程序最常用的配置,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • SpringBoot整合Drools規(guī)則引擎動態(tài)生成業(yè)務(wù)規(guī)則的實現(xiàn)

    SpringBoot整合Drools規(guī)則引擎動態(tài)生成業(yè)務(wù)規(guī)則的實現(xiàn)

    本文主要介紹了SpringBoot整合Drools規(guī)則引擎動態(tài)生成業(yè)務(wù)規(guī)則的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • 詳解Java分布式緩存系統(tǒng)中必須解決的四大問題

    詳解Java分布式緩存系統(tǒng)中必須解決的四大問題

    分布式緩存系統(tǒng)是三高架構(gòu)中不可或缺的部分,極大地提高了整個項目的并發(fā)量、響應(yīng)速度,但它也帶來了新的需要解決的問題,分別是: 緩存穿透、緩存擊穿、緩存雪崩和緩存一致性問題。本文將詳細講解一下這四大問題,需要的可以參考一下
    2022-04-04
  • 分布式事務(wù)CAP兩階段提交及三階段提交詳解

    分布式事務(wù)CAP兩階段提交及三階段提交詳解

    這篇文章主要為大家介紹了分布式事務(wù)CAP、兩階段提交及三階段提交的內(nèi)容詳解有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-01-01
  • java判斷integer是否為空的詳細過程

    java判斷integer是否為空的詳細過程

    在java編寫過程中,我們會使用到各種各樣的表達式,在使用表達式的過程中,有哪些安全問題需要我們注意的呢?對java判斷integer是否為空相關(guān)知識感興趣的朋友一起來看看吧
    2023-02-02
  • SpringBoot項目整合mybatis的方法步驟與實例

    SpringBoot項目整合mybatis的方法步驟與實例

    今天小編就為大家分享一篇關(guān)于SpringBoot項目整合mybatis的方法步驟與實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03

最新評論