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

Spring相關(guān)的依賴導(dǎo)入進(jìn)去,即可使用spring的定時(shí)任務(wù)!

站在用戶的角度思考問題,與客戶深入溝通,找到江城網(wǎng)站設(shè)計(jì)與江城網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、主機(jī)域名、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋江城地區(qū)。

<!-- 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ù)期期望。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)頁名稱:Spring定時(shí)任務(wù)使用及如何使用郵件監(jiān)控服務(wù)器
URL分享:http://www.muchs.cn/article22/ghgejc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、商城網(wǎng)站、動(dòng)態(tài)網(wǎng)站服務(wù)器托管、外貿(mào)建站手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化