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定时任务使用和如何使用邮件监控服务器的更多相关文章
- Cron和Spring定时任务
1.Java Spring spring定时任务cronExpression的值(配置定时时间)格式说明: 一个cronExpression表达式有至少6个(也可能是7个)由空格分隔的时间元素.从左至 ...
- Springboot 系列(十七)迅速使用 Spring Boot Admin 监控你的 Spring Boot 程序,支持异常邮件通知
1. Spring Boot Admin 是什么 Spring Boot Admin 是由 codecentric 组织开发的开源项目,使用 Spring Boot Admin 可以管理和监控你的 S ...
- 摆脱Spring 定时任务的@Scheduled cron表达式的困扰
一.背景 最近因为需要,需要适用Spring的task定时任务进行跑定时任务,以前也接触过,但是因为懒没有好好地理解@Scheduled的cron表达式,这次便对它做了一个全方位的了解和任务,记录下来 ...
- spring 定时任务配置
1.(易)如何在spring中配置定时任务? spring的定时任务配置分为三个步骤: 1.定义任务 2.任务执行策略配置 3.启动任务 (程序中一般我们都是到过写的,直观些) 1.定义任务 < ...
- Spring 定时任务2
转载自http://www.cnblogs.com/nick-huang/p/4864737.html > 版本说明 <dependencies> <dependency> ...
- 关于Spring定时任务(定时器)用法
Spring定时任务的几种实现 Spring定时任务的几种实现 一.分类 从实现的技术上来分类,目前主要有三种技术(或者说有三种产品): 从作业类的继承方式来讲,可以分为两类: 从任务调度的触发时机来 ...
- spring 定时任务的 执行时间设置规则(转)
spring 定时任务的 执行时间设置规则 单纯针对时间的设置规则org.springframework.scheduling.quartz.CronTriggerBean允许你更精确地控制任务的运 ...
- (3)Spring定时任务的几种实现
Spring定时任务的几种实现 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几种实现方式,由于项目采用spring框架,所以我都将 ...
- Spring定时任务,Spring4整合quartz2.2,quartz-scheduler定时任务
Spring4整合quartz2.2,quartz-scheduler定时任务,Spring定时任务 >>>>>>>>>>>>& ...
随机推荐
- 一道生成不重复随机数字的C#笔试编程题
当时写在纸上的程序没有验证输入,出面试公司没多久就突然想起来这点了,囧啊! 不过当时笔试的时候想到写异常处理了. 回来上机整理了一下程序,才发现原来还会用到递归的. 当时面试官边说边出的题,问他数字是 ...
- 【Tomcat】详解tomcat的连接数与线程池
前言 在使用tomcat时,经常会遇到连接数.线程数之类的配置问题,要真正理解这些概念,必须先了解Tomcat的连接器(Connector). Connector的主要功能,是接收连接请求,创建Req ...
- Java集合之Vector源码分析
概述 Vector与ArrayLIst类似, 内部同样维护一个数组, Vector是线程安全的. 方法与ArrayList大体一致, 只是加上 synchronized 关键字, 保证线程安全, 下面 ...
- Linux常用基本命令:三剑客命令之-awk内置变量与自定义变量
AWK中,变量分为两种:内置变量与自定义变量. 常见的内置变量有: FS:输入字段分隔符, 默认为空白字符 OFS:输出字段分隔符, 默认为空白字符 RS:输入记录分隔符(输入换行符), 指定输入时的 ...
- JS 中的 __proto__ 、prototype、constructor
首先 先解释这三个属性: (1) prototype : 它是函数独有的,从一个函数指向一个对象(函数的原型),含义是函数的原型对象,也就是这个函数所创建的实例的原型对象.(普通函数的该属性没有作用 ...
- Berlekamp-Massey算法学习笔记
Berlekamp-Massey算法 很久之前就听说过这个算法,当时六校联考的时候Day1T1是一道很有意思的递推,神仙zzx不会做于是就拿BM算法艹出了递推式Orzzzzzzzzzzx 推荐一篇讲的 ...
- Python中元组相关知识
下面给大家介绍以下元组的相关知识: ·元组可以看成是一个不可更改的list 1.元组的创建 # 创建空元祖 t = () print(type(t)) # 创建只有一个值的元组 # 观察可知元组中如果 ...
- 理解jQuery中$.get、$.post、$.getJSON和$.ajax的用法
ajax的4种方法:$.get.$.post.$getJSON.$ajax. 1.$.get $.get()方法使用GET方式来进行异步请求,它的语法结构为: $.get( url [, data] ...
- 《ASP.NET MVC企业实战》(一) MVC开发前奏
一.工具和方法 学到了一些没用过的工具和方法: a)删除多余的using指令并排序:一个类头部的using一般会有很多用不到的,在完成类的编写后,可以右键选择”组织using”来删除没用的using并 ...
- 爬虫 scrapy 笔记
scrapy 基础 1. 创建一个spider项目 a) Scrapy startproject project_name [project_dir] b) Cd p ...