手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux 端Tomcat监控程序(JAVA版)

[日期:2012-11-08] 来源:Linux社区  作者:dcwyiqpl [字体:大 中 小]

3.Email发送工具类

  1. package com.util;
  2. import Java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Properties;
  5. import javax.mail.Authenticator;
  6. import javax.mail.Message;
  7. import javax.mail.PasswordAuthentication;
  8. import javax.mail.Session;
  9. import javax.mail.Transport;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeMessage;
  12. import javax.mail.internet.MimeUtility;
  13. import javax.mail.internet.MimeMessage.RecipientType;
  14. /**
  15. * MailUtil
  16. * @author Administrator
  17. *
  18. */
  19. publicclass MailUtils {
  20. private String MAIL_NAME = "****";
  21. private String MAIL_PASSWORD = "******";
  22. private String mailHost = "smtp.163.com";
  23. private String MAIL_FROM = "******@163.com";
  24. private String MAIL_FROM_NAME = "****";
  25. public MailUtils(){
  26. }
  27. public MailUtils(String username,String password,String mailHost,String mailFrom,String mailFromName ){
  28. this.MAIL_NAME = username;
  29. this.MAIL_PASSWORD = password;
  30. this.mailHost = mailHost;
  31. this.MAIL_FROM = mailFrom;
  32. this.MAIL_FROM_NAME = mailFromName;
  33. }
  34. /**
  35. * 邮箱认证
  36. * @param userAuth
  37. * @param mailType
  38. * @return
  39. * @throws Exception
  40. */
  41. public Session mailAuth() throws Exception{
  42. Properties props = new Properties();
  43. props.setProperty("mail.smtp.auth", "true");
  44. props.setProperty("mail.transport.protocol", "true");
  45. props.setProperty("mail.host", mailHost);
  46. Session session = Session.getDefaultInstance(props,new Authenticator(){
  47. @Override
  48. protected PasswordAuthentication getPasswordAuthentication() {
  49. returnnew PasswordAuthentication(MAIL_NAME,MAIL_PASSWORD);
  50. }
  51. });
  52. return session;
  53. }
  54. /**
  55. * @param subject
  56. * @param content
  57. * @param mailTo
  58. * @throws Exception
  59. */
  60. publicvoid sendSimpleMail(String subject,String content,String mailTo) throws Exception{
  61. Session session = mailAuth();
  62. session.setDebug(true);
  63. Message msg = new MimeMessage(session);
  64. msg.setFrom(new InternetAddress(MimeUtility.encodeText(MAIL_FROM_NAME)+"<"+MAIL_FROM+">" ));
  65. msg.setSubject(subject);
  66. msg.setText(content);
  67. msg.addRecipient( RecipientType.TO, new InternetAddress(mailTo));
  68. Transport.send(msg);
  69. }
  70. publicvoid sendMutilHtmlMail(String subject,String content,List<String> mailto) throws Exception{
  71. if(mailto != null){
  72. for(int i = 0 ; i < mailto.size() ; i++){
  73. sendHtmlMail(subject, content,mailto.get(i));
  74. }
  75. }
  76. }
  77. }

4.生成日志工具类

  1. package com.util;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStreamWriter;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. publicclass LogUtil {
  10. privatestatic String FILE_PATH = "/usr/local/tomcatLook/";
  11. publicstaticvoid writerINF(String info){
  12. File file = null;
  13. File filedir = null;
  14. BufferedWriter writer = null;
  15. Date date = new Date();
  16. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  17. SimpleDateFormat sdf2 = new SimpleDateFormat("MM-dd_HH:mm:ss");
  18. try {
  19. filedir = new File(FILE_PATH);
  20. if(!filedir.exists()){
  21. filedir.mkdirs();
  22. }
  23. file = new File(FILE_PATH+"domain."+sdf.format(date)+".log");
  24. if(file.exists()){
  25. file.createNewFile();
  26. }
  27. writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
  28. writer.write("["+sdf2.format(date)+"]-[INFO]:"+info+"\n");
  29. writer.flush();
  30. } catch (IOException e) {
  31. } finally{
  32. if(writer != null){
  33. try {
  34. writer.close();
  35. } catch (IOException e) {
  36. }
  37. }
  38. writer = null;
  39. file = null;
  40. filedir = null;
  41. date = null;
  42. sdf = null;
  43. sdf2 = null;
  44. }
  45. }
  46. publicstaticvoid writerE(Exception e){
  47. StringBuffer info = new StringBuffer();
  48. File file = null;
  49. File filedir = null;
  50. BufferedWriter writer = null;
  51. Date date = new Date();
  52. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  53. SimpleDateFormat sdf2 = new SimpleDateFormat("MM-dd_HH:mm:ss");
  54. try {
  55. info.append("[").append(sdf2.format(date)).append("]-[Exception]:").append(e.getClass().getName()).append("\n\t").append(getExceptionString(e)).append("\n");
  56. filedir = new File(FILE_PATH);
  57. if(!filedir.exists()){
  58. filedir.mkdirs();
  59. }
  60. file = new File(FILE_PATH+"domain."+sdf.format(date)+".log");
  61. if(file.exists()){
  62. file.createNewFile();
  63. }
  64. writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
  65. writer.write(info.toString());
  66. writer.flush();
  67. } catch (IOException ioe) {
  68. } finally{
  69. if(writer != null){
  70. try {
  71. writer.close();
  72. } catch (IOException ioe2) {
  73. }
  74. }
  75. writer = null;
  76. file = null;
  77. filedir = null;
  78. date = null;
  79. sdf = null;
  80. }
  81. }
  82. /**
  83. * 返回处理后的异常信息
  84. * @param e 异常对象
  85. * @return 处理后的异常信息
  86. */
  87. privatestatic String getExceptionString(Exception e) {
  88. String errorString = e.toString() + "\r\n";
  89. StackTraceElement[] stackArr = e.getStackTrace();
  90. for (int i = 0; i < stackArr.length; i++) {
  91. errorString += "\tSTACK-->: " + stackArr[i].toString();
  92. if((i+1) != stackArr.length) errorString += "\r\n";
  93. }
  94. return errorString;
  95. }
  96. }
linux
相关资讯       Java  Tomcat  Tomcat监控程序 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款