瀏覽代碼

扩展源码 ,发送邮件可配置

chenweida 7 年之前
父節點
當前提交
2adeda8b31

+ 8 - 0
server/svr-admin-server/src/main/java/com.yihu.admin/AdminServer.java

@ -1,9 +1,15 @@
package com.yihu.admin;
import com.yihu.admin.email.MyMailNotifier;
import de.codecentric.boot.admin.config.EnableAdminServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.MailSender;
/**
 * Created by chenweida on 2017/8/15.
@ -11,7 +17,9 @@ import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
@SpringBootApplication
public class AdminServer {
    public static void main(String[] args) {
        SpringApplication.run(AdminServer.class, args);
    }

+ 33 - 0
server/svr-admin-server/src/main/java/com.yihu.admin/config/NotifierConfig.java

@ -0,0 +1,33 @@
package com.yihu.admin.config;
import com.yihu.admin.email.JKZLMailNotifier;
import de.codecentric.boot.admin.config.NotifierConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.MailSender;
/**
 * Created by chenweida on 2018/5/10 0010.
 */
@Configuration
@AutoConfigureAfter({MailSenderAutoConfiguration.class})
@AutoConfigureBefore({NotifierConfiguration.MailNotifierConfiguration.class})
public class NotifierConfig {
    @Autowired
    private MailSender mailSender;
    public NotifierConfig() {
        System.out.println("初始化数据");
    }
    @Bean("mailNotifier")
    @ConfigurationProperties("spring.boot.admin.notify.mail")
    public JKZLMailNotifier mailNotifier() {
        return new JKZLMailNotifier(mailSender);
    }
}

+ 123 - 0
server/svr-admin-server/src/main/java/com.yihu.admin/email/JKZLMailNotifier.java

@ -0,0 +1,123 @@
package com.yihu.admin.email;
import de.codecentric.boot.admin.event.ClientApplicationEvent;
import de.codecentric.boot.admin.notify.MailNotifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;
import java.util.Arrays;
/**
 * Created by chenweida on 2018/5/8 0008.
 * de.codecentric.boot.admin.config.NotifierConfiguration
 */
public class JKZLMailNotifier extends MailNotifier {
    private static final String DEFAULT_SUBJECT = "#{application.name} (#{application.id}) is #{to.status}";
    private static final String DEFAULT_TEXT = "#{application.name} (#{application.id})\nstatus changed from #{from.status} to #{to.status}\n\n#{application.healthUrl}";
    private final SpelExpressionParser parser = new SpelExpressionParser();
    private Logger logger = LoggerFactory.getLogger(JKZLMailNotifier.class);
    private final MailSender sender;
    /**
     * recipients of the mail
     */
    private String to[] = {"root@localhost"};
    /**
     * cc-recipients of the mail
     */
    private String cc[];
    /**
     * sender of the change
     */
    private String from = null;
    /**
     * Mail Text. SpEL template using event as root;
     */
    private Expression text;
    /**
     * Mail Subject. SpEL template using event as root;
     */
    private Expression subject;
    public JKZLMailNotifier(MailSender sender) {
        super(sender);
        logger.info("init email");
        this.sender = sender;
        this.subject = parser.parseExpression(DEFAULT_SUBJECT, ParserContext.TEMPLATE_EXPRESSION);
        this.text = parser.parseExpression(DEFAULT_TEXT, ParserContext.TEMPLATE_EXPRESSION);
    }
    @Override
    protected void doNotify(ClientApplicationEvent event) {
        logger.info("send email");
        EvaluationContext context = new StandardEvaluationContext(event);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setFrom(from);
        message.setSubject(subject.getValue(context, String.class));
        message.setText(text.getValue(context, String.class));
        message.setCc(cc);
        sender.send(message);
    }
    public void setTo(String[] to) {
        this.to = Arrays.copyOf(to, to.length);
    }
    public String[] getTo() {
        return Arrays.copyOf(to, to.length);
    }
    public void setCc(String[] cc) {
        this.cc = Arrays.copyOf(cc, cc.length);
    }
    public String[] getCc() {
        return Arrays.copyOf(cc, cc.length);
    }
    public void setFrom(String from) {
        this.from = from;
    }
    public String getFrom() {
        return from;
    }
    public void setSubject(String subject) {
        this.subject = parser.parseExpression(subject, ParserContext.TEMPLATE_EXPRESSION);
    }
    public String getSubject() {
        return subject.getExpressionString();
    }
    public void setText(String text) {
        this.text = parser.parseExpression(text, ParserContext.TEMPLATE_EXPRESSION);
    }
    public String getText() {
        return text.getExpressionString();
    }
}

+ 0 - 124
server/svr-admin-server/src/main/java/com.yihu.admin/email/MyMailNotifier.java

@ -1,124 +0,0 @@
//package com.yihu.admin.email;
//
//import de.codecentric.boot.admin.event.ClientApplicationEvent;
//import de.codecentric.boot.admin.notify.MailNotifier;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.expression.EvaluationContext;
//import org.springframework.expression.Expression;
//import org.springframework.expression.ParserContext;
//import org.springframework.expression.spel.standard.SpelExpressionParser;
//import org.springframework.expression.spel.support.StandardEvaluationContext;
//import org.springframework.mail.MailSender;
//import org.springframework.mail.SimpleMailMessage;
//import org.springframework.stereotype.Component;
//
//import java.util.Arrays;
//
///**
// * Created by chenweida on 2018/5/8 0008.
// * de.codecentric.boot.admin.config.NotifierConfiguration */
//@Component
//@ConfigurationProperties("spring.boot.admin.notify.mail")
//public class MyMailNotifier extends MailNotifier {
//
//
//    private static final String DEFAULT_SUBJECT = "#{application.name} (#{application.id}) is #{to.status}";
//    private static final String DEFAULT_TEXT = "#{application.name} (#{application.id})\nstatus changed from #{from.status} to #{to.status}\n\n#{application.healthUrl}";
//
//    private final SpelExpressionParser parser = new SpelExpressionParser();
//
//    private Logger logger = LoggerFactory.getLogger(MyMailNotifier.class);
//
//    private final MailSender sender;
//
//    /**
//     * recipients of the mail
//     */
//    private String to[] = {"root@localhost"};
//
//    /**
//     * cc-recipients of the mail
//     */
//    private String cc[];
//
//    /**
//     * sender of the change
//     */
//    private String from = null;
//
//    /**
//     * Mail Text. SpEL template using event as root;
//     */
//    private Expression text;
//
//    /**
//     * Mail Subject. SpEL template using event as root;
//     */
//    private Expression subject;
//
//    public MyMailNotifier(MailSender sender) {
//        super(sender);
//        logger.info("init email");
//        this.sender = sender;
//        this.subject = parser.parseExpression(DEFAULT_SUBJECT, ParserContext.TEMPLATE_EXPRESSION);
//        this.text = parser.parseExpression(DEFAULT_TEXT, ParserContext.TEMPLATE_EXPRESSION);
//
//    }
//
//    @Override
//    protected void doNotify(ClientApplicationEvent event) {
//        logger.info("send email");
//        EvaluationContext context = new StandardEvaluationContext(event);
//
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setTo(to);
//        message.setFrom(from);
//        message.setSubject(subject.getValue(context, String.class));
//        message.setText(text.getValue(context, String.class));
//        message.setCc(cc);
//
//        sender.send(message);
//    }
//
//    public void setTo(String[] to) {
//        this.to = Arrays.copyOf(to, to.length);
//    }
//
//    public String[] getTo() {
//        return Arrays.copyOf(to, to.length);
//    }
//
//    public void setCc(String[] cc) {
//        this.cc = Arrays.copyOf(cc, cc.length);
//    }
//
//    public String[] getCc() {
//        return Arrays.copyOf(cc, cc.length);
//    }
//
//    public void setFrom(String from) {
//        this.from = from;
//    }
//
//    public String getFrom() {
//        return from;
//    }
//
//    public void setSubject(String subject) {
//        this.subject = parser.parseExpression(subject, ParserContext.TEMPLATE_EXPRESSION);
//    }
//
//    public String getSubject() {
//        return subject.getExpressionString();
//    }
//
//    public void setText(String text) {
//        this.text = parser.parseExpression(text, ParserContext.TEMPLATE_EXPRESSION);
//    }
//
//    public String getText() {
//        return text.getExpressionString();
//    }
//}