简体   繁体   中英

Spring actuator mail HealthIndicators not working

I have enabled Spring actuator in Spring Boot and used following configuration

management.health.mail.enabled=true
management.endpoint.health.show-details=always
spring.mail.host=test@test.cp,
spring.mail.port=587

But whenever I start application, Mail server is not getting checked. in path /actuator/health :

{
  "status": "UP",
  "components": {
    "db": {
      "status": "UP" ...
    },
    "diskSpace": {
      "status": "UP", ...
    },
    "hazelcast": {
      "status": "UP",   
    },
    "ping": {
      "status": "UP"
    }
  }
}

It can surely vary depending on your Spring Boot Version and other dependencies, but one way to analyze this is a test.

Spring Boot MailHealthContributorAutoConfiguration expects couple of conditions, one of them being a valid JavaMailSender Config.

@AutoConfiguration(after = MailSenderAutoConfiguration.class)
@ConditionalOnClass(JavaMailSenderImpl.class)
@ConditionalOnBean(JavaMailSenderImpl.class)
@ConditionalOnEnabledHealthIndicator("mail")
public class MailHealthContributorAutoConfiguration
...

You have not provided a working configuration, but you can test your setup using a similar test class

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMetrics
@ActiveProfiles("test")
class MonitoringTest {

@LocalServerPort
private int port;

@Autowired
private WebApplicationContext context;

@Test
void healthEndpointWithMail() throws Exception {
    MockMvc mvc = MockMvcBuilders.webAppContextSetup(context).build();

    MockHttpServletResponse response =      mvc.perform(get("http://localhost:" + port + "/actuator/health")).andReturn().getResponse();

    assertEquals(HttpStatus.OK.value(), response.getStatus());

    System.out.println(response.getContentAsString());
    JSONObject jo = new JSONObject(response.getContentAsString());

    JSONObject componentsJo = jo.getJSONObject("components");
    JSONObject mailJo = componentsJo.getJSONObject("mail");
    assertEquals("UP", mailJo.getString("status"));

} }

Note the @AutoConfigureMetrics annotation. The config application-test.yml looks like:

management:
  endpoint:
    health:
      show-details: always
  health:
    mail:
      enabled: true
  endpoints:
    web:
      exposure:
        include:
        - health

and the mail config part (with your Mail Server or some Mock like GreenMail ):

spring:
  mail:
    default-encoding: UTF-8
    host: #{smtp.host}
    username: #{user}
    password: #{pw}
    port: #{port}
    properties:
      mail:
        smtp:
          auth: true
          ssl:
            enable: true
            trust: #{smtp.host}
          starttls:
            enable: true
    protocol: smtp

Have a look at the full project .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM