Add mail health check

Define an additional health indicator for each JavaMailSenderImpl
instance in the context.

Closes gh-2017 and gh-2617
This commit is contained in:
Johannes Stelzer
2015-03-08 20:21:13 +01:00
committed by Stephane Nicoll
parent 7c6ee8713a
commit cd8c3d7327
7 changed files with 316 additions and 5 deletions

View File

@@ -32,6 +32,7 @@ import org.springframework.boot.actuate.health.DiskSpaceHealthIndicator;
import org.springframework.boot.actuate.health.DiskSpaceHealthIndicatorProperties;
import org.springframework.boot.actuate.health.HealthAggregator;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.MailHealthIndicator;
import org.springframework.boot.actuate.health.MongoHealthIndicator;
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
import org.springframework.boot.actuate.health.RabbitHealthIndicator;
@@ -48,6 +49,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadata;
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProvider;
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadataProviders;
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration;
@@ -57,6 +59,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.mail.javamail.JavaMailSenderImpl;
/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link HealthIndicator}s.
@@ -70,7 +73,8 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
@AutoConfigureBefore({ EndpointAutoConfiguration.class })
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class, RedisAutoConfiguration.class,
RabbitAutoConfiguration.class, SolrAutoConfiguration.class })
RabbitAutoConfiguration.class, SolrAutoConfiguration.class,
MailSenderAutoConfiguration.class })
@EnableConfigurationProperties({ HealthIndicatorAutoConfigurationProperties.class })
public class HealthIndicatorAutoConfiguration {
@@ -276,4 +280,40 @@ public class HealthIndicatorAutoConfiguration {
}
@Configuration
@ConditionalOnBean(JavaMailSenderImpl.class)
@ConditionalOnProperty(prefix = "management.health.mail", name = "enabled", matchIfMissing = true)
public static class MailHealthIndicatorConfiguration {
@Autowired
private HealthAggregator healthAggregator;
@Autowired(required = false)
private Map<String, JavaMailSenderImpl> mailSenders;
@Bean
@ConditionalOnMissingBean(name = "mailSenderHealthIndicator")
public HealthIndicator mailHealthIndicator() {
if (this.mailSenders.size() == 1) {
JavaMailSenderImpl mailSender = this.mailSenders.values().iterator()
.next();
return createMailHealthIndicator(mailSender);
}
CompositeHealthIndicator composite = new CompositeHealthIndicator(
this.healthAggregator);
for (Map.Entry<String, JavaMailSenderImpl> entry : this.mailSenders
.entrySet()) {
String name = entry.getKey();
JavaMailSenderImpl mailSender = entry.getValue();
composite.addHealthIndicator(name, createMailHealthIndicator(mailSender));
}
return composite;
}
private MailHealthIndicator createMailHealthIndicator(
JavaMailSenderImpl mailSender) {
return new MailHealthIndicator(mailSender);
}
}
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.health;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import org.springframework.boot.actuate.health.Health.Builder;
import org.springframework.mail.javamail.JavaMailSenderImpl;
/**
* {@link HealthIndicator} for configured smtp server(s).
*
* @author Johannes Stelzer
* @since 1.3.0
*/
public class MailHealthIndicator extends AbstractHealthIndicator {
private final JavaMailSenderImpl mailSender;
public MailHealthIndicator(JavaMailSenderImpl mailSender) {
this.mailSender = mailSender;
}
@Override
protected void doHealthCheck(Builder builder) throws Exception {
String location = String.format("%s:%d", this.mailSender.getHost(), this.mailSender.getPort());
builder.withDetail("location", location);
Transport transport = null;
try {
transport = connectTransport();
builder.up();
}
finally {
if (transport != null) {
transport.close();
}
}
}
// Copy-paste from JavaMailSenderImpl - see SPR-12799
private Transport connectTransport() throws MessagingException {
String username = this.mailSender.getUsername();
String password = this.mailSender.getPassword();
if ("".equals(username)) {
username = null;
if ("".equals(password)) {
password = null;
}
}
Transport transport = getTransport(this.mailSender.getSession());
transport.connect(this.mailSender.getHost(), this.mailSender.getPort(), username,
password);
return transport;
}
private Transport getTransport(Session session) throws NoSuchProviderException {
String protocol = this.mailSender.getProtocol();
if (protocol == null) {
protocol = session.getProperty("mail.transport.protocol");
if (protocol == null) {
protocol = JavaMailSenderImpl.DEFAULT_PROTOCOL;
}
}
return session.getTransport(protocol);
}
}

View File

@@ -58,6 +58,12 @@
"description": "Enable Solr health check.",
"defaultValue": true
},
{
"name": "management.health.mail.enabled",
"type": "java.lang.Boolean",
"description": "Enable Mail health check.",
"defaultValue": true
},
{
"name": "spring.git.properties",
"type": "java.lang.String",