当前位置: 首页 > 编程笔记 >

Spring定时任务使用及如何使用邮件监控服务器

申屠锦
2023-03-14
本文向大家介绍Spring定时任务使用及如何使用邮件监控服务器,包括了Spring定时任务使用及如何使用邮件监控服务器的使用技巧和注意事项,需要的朋友参考一下

Spring相关的依赖导入进去,即可使用spring的定时任务!

<!-- 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>

定时任务是开发中常用的,比如订单查询,一位客人订购的某个东西,但是尚未支付,超过订单时效期自动失效,那么又是怎么样知道订单的时效性过呢?定时任务,可以每分钟或者每秒钟进行查询。

定时任务的应用是非常广的,下面应用下监控服务器,虽然说现在开源监控软件挺多的,什么zabbix,nagios或者其他等等。下面我将使用代码监控服务器:

首先准备邮件的依赖:

<!-- 发邮件 -->      
    <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.创建一个程序与邮件服务器会话对象 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

    // 创建验证器
    Authenticator auth = new Authenticator() {
      public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("123@163.com", "123");
      }
    };

    Session session = Session.getInstance(props, auth);

    // 2.创建一个Message,它相当于是邮件内容
    Message message = new MimeMessage(session);

    message.setFrom(new InternetAddress("123@163.com")); // 设置发送者

    message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者

    message.setSubject("邮件告警");

    message.setContent(emailMsg, "text/html;charset=utf-8");

    // 3.创建 Transport用于将邮件发送

    Transport.send(message);
    
  }
  
  
}

监控服务器类:

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服务器端口为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服务器端口为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服务器端口为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服务器端口为1616宕机了");
    } catch (AddressException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (MessagingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

具体定时任务类:

@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("每分钟执行" + System.currentTimeMillis());
  }
  
  @Scheduled(cron = "0 10 0 * * ?")
  public void monitorServerTest() {
    
    System.out.println("每10分钟监控一次2018服务器和1616服务器");
    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分钟监控一次1818测试服务器");
    MonitorUrl.testUrlWithTimeOut1818("http://www.yc520:1818/", 2000);
  }

}

由此就可以达到监控服务器的目的,当然这只是小试牛刀,而且也不够全面,当然也存在问题,如果是每分钟定时任务检测,突然一台服务器挂了,那么将会源源不断的发送邮件,163邮件是有限的,而且频繁的可能会被当成垃圾邮件,我只需要知道一条信息,某某服务器宕机了,一遍就可以,最后给我发三十遍,如此的话,大量的无效邮件很浪费资源,而且浪费时间。

所以说,本文只是一个服务器监控小示例,实际开发中,切勿直接拿来用,最好要有相关的判断和逻辑。这样才能比较高效,达到预期期望。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍Spring Boot Admin监控服务如何使用,包括了Spring Boot Admin监控服务如何使用的使用技巧和注意事项,需要的朋友参考一下 Spring Boot Admin 简介 随着开发周期的推移,项目会不断变大,切分出的服务也会越来越多,这时一个个的微服务构成了错综复杂的系统。 对于各个微服务系统的健康状态、会话数量、并发数、服务资源、延迟等度量信息的收集就成为了一个

  • Prometheus Prometheus是最初在SoundCloud上构建的云原生监视平台。Prometheus提供了多维数据模型,其中包含通过度量标准名称和键/值对标识的时间序列数据。数据收集通过HTTP / HTTPS上的拉模型进行。通过服务发现或静态配置发现要提取数据的目标。 MinIO默认情况下将Prometheus兼容数据作为授权端点导出/minio/prometheus/metric

  • 在我们的项目开发过程中,经常需要定时任务来帮助我们来做一些内容,springboot默认已经帮我们实行了,只需要添加相应的注解就可以实现 1、pom包配置 pom包里面只需要引入springboot starter包即可 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>

  • 【关于Nagios】 Nagios是一款用于监控系统和网络的开源应用软件,它的模式是服务器—客户端,也就是说首先要在在一台服务器上(server)部署相应的主要套件,然后在要监控的服务器上部署客户端程序,这样server会和client通信,从而监控client端的各项资源。Nagios功能十分强大几乎所有的项目都可以监控,大到服务器的存活状态,小到服务器上的某一个服务(web)。这些功能都是通过

  • 本文向大家介绍详解Spring boot Admin 使用eureka监控服务,包括了详解Spring boot Admin 使用eureka监控服务的使用技巧和注意事项,需要的朋友参考一下 前言 最近刚好有空,来学习一下如何搭建spring boot admin环境。其中遇到很多的坑。 网上大多都是使用admin-url的方式直接来监控的,感觉一点也不灵活,这不是我想要的结果,所以本篇介绍借助e

  • 我试图发送电子邮件使用gmail smtp使用javax.mail.下面是我的代码 代码工作正常当我在本地服务器上运行它时,但当我尝试在Elastic beanstek上运行它时(我的服务器在AWS EBS上运行),身份验证失败异常即将到来注意:我已打开从Google A/c设置访问不太安全的应用程序,但我仍然收到此错误 javax.mail.身份验证失败异常:534-5.7.14请通过您的网络浏