Skip to content

Commit b88cb35

Browse files
committed
Fix JMS health indicator
This commit improves the JMS health indicator to identify a broken broker that uses failover. An attempt to start the connection is a good way to make sure that it is effectively available. Closes gh-6818
1 parent e3ca5e7 commit b88cb35

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/JmsHealthIndicator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@ public JmsHealthIndicator(ConnectionFactory connectionFactory) {
3737
protected void doHealthCheck(Health.Builder builder) throws Exception {
3838
Connection connection = this.connectionFactory.createConnection();
3939
try {
40+
connection.start();
4041
builder.up().withDetail("provider",
4142
connection.getMetaData().getJMSProviderName());
4243
}

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/JmsHealthIndicatorTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import javax.jms.JMSException;
2323

2424
import org.junit.Test;
25+
import org.mockito.Mockito;
2526

2627
import static org.junit.Assert.assertEquals;
2728
import static org.mockito.BDDMockito.given;
@@ -78,4 +79,21 @@ public void jmsBrokerCouldNotRetrieveProviderMetadata() throws JMSException {
7879
verify(connection, times(1)).close();
7980
}
8081

82+
@Test
83+
public void jmsBrokerUsesFailover() throws JMSException {
84+
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
85+
ConnectionMetaData connectionMetaData = mock(ConnectionMetaData.class);
86+
given(connectionMetaData.getJMSProviderName()).willReturn("JMS test provider");
87+
Connection connection = mock(Connection.class);
88+
given(connection.getMetaData()).willReturn(connectionMetaData);
89+
Mockito.doThrow(new JMSException("Could not start", "123"))
90+
.when(connection).start();
91+
given(connectionFactory.createConnection())
92+
.willReturn(connection);
93+
JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory);
94+
Health health = indicator.health();
95+
assertEquals(Status.DOWN, health.getStatus());
96+
assertEquals(null, health.getDetails().get("provider"));
97+
}
98+
8199
}

0 commit comments

Comments
 (0)