您好,登錄后才能下訂單哦!
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郵件是有限的,而且頻繁的可能會被當成垃圾郵件,我只需要知道一條信息,某某服務器宕機了,一遍就可以,最后給我發三十遍,如此的話,大量的無效郵件很浪費資源,而且浪費時間。
所以說,本文只是一個服務器監控小示例,實際開發中,切勿直接拿來用,最好要有相關的判斷和邏輯。這樣才能比較高效,達到預期期望。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。