Use virtual threads in JmsHealthIndicator if enabled
Closes gh-36694
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.boot.actuate.jms;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -28,11 +29,15 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.core.log.LogMessage;
|
||||
import org.springframework.core.task.AsyncTaskExecutor;
|
||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||
|
||||
/**
|
||||
* {@link HealthIndicator} for a JMS {@link ConnectionFactory}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Moritz Halbritter
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class JmsHealthIndicator extends AbstractHealthIndicator {
|
||||
@@ -41,9 +46,33 @@ public class JmsHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
private final ConnectionFactory connectionFactory;
|
||||
|
||||
private final AsyncTaskExecutor taskExecutor;
|
||||
|
||||
private final Duration timeout;
|
||||
|
||||
/**
|
||||
* Creates a new {@link JmsHealthIndicator}, using a {@link SimpleAsyncTaskExecutor}
|
||||
* and a timeout of 5 seconds.
|
||||
* @param connectionFactory the connection factory
|
||||
* @deprecated since 3.2.0 for removal in 3.4.0 in favor of
|
||||
* {@link #JmsHealthIndicator(ConnectionFactory, AsyncTaskExecutor, Duration)}
|
||||
*/
|
||||
@Deprecated(since = "3.2.0", forRemoval = true)
|
||||
public JmsHealthIndicator(ConnectionFactory connectionFactory) {
|
||||
this(connectionFactory, new SimpleAsyncTaskExecutor("jms-health-indicator"), Duration.ofSeconds(5));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link JmsHealthIndicator}.
|
||||
* @param connectionFactory the connection factory
|
||||
* @param taskExecutor the task executor used to run timeout checks
|
||||
* @param timeout the connection timeout
|
||||
*/
|
||||
public JmsHealthIndicator(ConnectionFactory connectionFactory, AsyncTaskExecutor taskExecutor, Duration timeout) {
|
||||
super("JMS health check failed");
|
||||
this.connectionFactory = connectionFactory;
|
||||
this.taskExecutor = taskExecutor;
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -65,18 +94,19 @@ public class JmsHealthIndicator extends AbstractHealthIndicator {
|
||||
}
|
||||
|
||||
void start() throws JMSException {
|
||||
new Thread(() -> {
|
||||
JmsHealthIndicator.this.taskExecutor.execute(() -> {
|
||||
try {
|
||||
if (!this.latch.await(5, TimeUnit.SECONDS)) {
|
||||
if (!this.latch.await(JmsHealthIndicator.this.timeout.toMillis(), TimeUnit.MILLISECONDS)) {
|
||||
JmsHealthIndicator.this.logger
|
||||
.warn("Connection failed to start within 5 seconds and will be closed.");
|
||||
.warn(LogMessage.format("Connection failed to start within %s and will be closed.",
|
||||
JmsHealthIndicator.this.timeout));
|
||||
closeConnection();
|
||||
}
|
||||
}
|
||||
catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}, "jms-health-indicator").start();
|
||||
});
|
||||
this.connection.start();
|
||||
this.latch.countDown();
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.boot.actuate.jms;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import jakarta.jms.Connection;
|
||||
import jakarta.jms.ConnectionFactory;
|
||||
import jakarta.jms.ConnectionMetaData;
|
||||
@@ -26,6 +28,8 @@ import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.Status;
|
||||
import org.springframework.core.task.AsyncTaskExecutor;
|
||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
@@ -41,6 +45,10 @@ import static org.mockito.Mockito.mock;
|
||||
*/
|
||||
class JmsHealthIndicatorTests {
|
||||
|
||||
private static final Duration TIMEOUT = Duration.ofMillis(100);
|
||||
|
||||
private final AsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
|
||||
|
||||
@Test
|
||||
void jmsBrokerIsUp() throws JMSException {
|
||||
ConnectionMetaData connectionMetaData = mock(ConnectionMetaData.class);
|
||||
@@ -49,7 +57,7 @@ class JmsHealthIndicatorTests {
|
||||
given(connection.getMetaData()).willReturn(connectionMetaData);
|
||||
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
|
||||
given(connectionFactory.createConnection()).willReturn(connection);
|
||||
JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory);
|
||||
JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory, this.taskExecutor, TIMEOUT);
|
||||
Health health = indicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.UP);
|
||||
assertThat(health.getDetails()).containsEntry("provider", "JMS test provider");
|
||||
@@ -60,7 +68,7 @@ class JmsHealthIndicatorTests {
|
||||
void jmsBrokerIsDown() throws JMSException {
|
||||
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
|
||||
given(connectionFactory.createConnection()).willThrow(new JMSException("test", "123"));
|
||||
JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory);
|
||||
JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory, this.taskExecutor, TIMEOUT);
|
||||
Health health = indicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(health.getDetails()).doesNotContainKey("provider");
|
||||
@@ -74,7 +82,7 @@ class JmsHealthIndicatorTests {
|
||||
given(connection.getMetaData()).willReturn(connectionMetaData);
|
||||
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
|
||||
given(connectionFactory.createConnection()).willReturn(connection);
|
||||
JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory);
|
||||
JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory, this.taskExecutor, TIMEOUT);
|
||||
Health health = indicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(health.getDetails()).doesNotContainKey("provider");
|
||||
@@ -90,7 +98,7 @@ class JmsHealthIndicatorTests {
|
||||
given(connection.getMetaData()).willReturn(connectionMetaData);
|
||||
willThrow(new JMSException("Could not start", "123")).given(connection).start();
|
||||
given(connectionFactory.createConnection()).willReturn(connection);
|
||||
JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory);
|
||||
JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory, this.taskExecutor, TIMEOUT);
|
||||
Health health = indicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat(health.getDetails()).doesNotContainKey("provider");
|
||||
@@ -109,7 +117,7 @@ class JmsHealthIndicatorTests {
|
||||
}).given(connection).close();
|
||||
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
|
||||
given(connectionFactory.createConnection()).willReturn(connection);
|
||||
JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory);
|
||||
JmsHealthIndicator indicator = new JmsHealthIndicator(connectionFactory, this.taskExecutor, TIMEOUT);
|
||||
Health health = indicator.health();
|
||||
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
||||
assertThat((String) health.getDetails().get("error")).contains("Connection closed");
|
||||
|
||||
Reference in New Issue
Block a user