Skip to content

Commit

Permalink
fix(health): Add option to disable health indicator for MQ listeners (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
juancgalvis authored Oct 10, 2024
1 parent 300a8ad commit 39c9a40
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,24 @@ You should create and register many fixed response queues for request reply, in
}
```

### Health Checks

Commons JMS has two health indicators, the first one is the default spring boot jms health indicator, which checks the connection. The second one is the `MQHealthIndicator` which checks the listeners connection to the queue manager.

Both health indicators are enabled by default, but you can disable them by setting the next properties:

- application.properties
```properties
management.health.jms.enabled=false
```
- application.yaml
```yaml
management:
health:
jms:
enabled: false
```
### Usual utilities
- [`MQUtils`](commons-jms-mq/src/main/java/co/com/bancolombia/commons/jms/mq/utils/MQUtils.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
import co.com.bancolombia.commons.jms.api.MQQueueManagerSetter;
import co.com.bancolombia.commons.jms.api.MQQueuesContainer;
import co.com.bancolombia.commons.jms.api.exceptions.MQHealthListener;
import co.com.bancolombia.commons.jms.internal.listener.selector.MQExecutorService;
import co.com.bancolombia.commons.jms.internal.listener.selector.strategy.SelectorBuilder;
import co.com.bancolombia.commons.jms.internal.models.MQListenerConfig;
import co.com.bancolombia.commons.jms.internal.models.RetryableConfig;
import co.com.bancolombia.commons.jms.mq.config.health.MQListenerDisabledHealthIndicator;
import co.com.bancolombia.commons.jms.mq.config.health.MQListenerHealthIndicator;
import co.com.bancolombia.commons.jms.internal.listener.selector.MQExecutorService;
import co.com.bancolombia.commons.jms.mq.utils.MQUtils;
import co.com.bancolombia.commons.jms.utils.MQQueueUtils;
import co.com.bancolombia.commons.jms.utils.MQQueuesContainerImp;
import co.com.bancolombia.commons.jms.utils.ReactiveReplyRouter;
import com.ibm.mq.jakarta.jms.MQQueue;
import jakarta.jms.Message;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationEventPublisher;
Expand Down Expand Up @@ -74,7 +76,11 @@ public MQBrokerUtils defaultMqBrokerUtils() {

@Bean
@ConditionalOnMissingBean(MQHealthListener.class)
public MQHealthListener defaultMqHealthListener(ApplicationEventPublisher publisher) {
public MQHealthListener defaultMqHealthListener(ApplicationEventPublisher publisher,
@Value("${management.health.jms.enabled:true}") boolean enabled) {
if (!enabled) {
return new MQListenerDisabledHealthIndicator();
}
return new MQListenerHealthIndicator(publisher);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package co.com.bancolombia.commons.jms.mq.config.health;

import co.com.bancolombia.commons.jms.api.exceptions.MQHealthListener;
import jakarta.jms.JMSException;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

@Log4j2
@RequiredArgsConstructor
public class MQListenerDisabledHealthIndicator implements MQHealthListener {

@Override
public void onInit(String listener) {
// Do nothing
}

@Override
public void onStarted(String listener) {
// Do nothing
}

@Override
public void onException(String listener, JMSException exception) {
// Do nothing
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import co.com.bancolombia.commons.jms.api.MQQueueManagerSetter;
import co.com.bancolombia.commons.jms.api.MQQueuesContainer;
import co.com.bancolombia.commons.jms.api.exceptions.MQHealthListener;
import co.com.bancolombia.commons.jms.internal.listener.selector.MQExecutorService;
import co.com.bancolombia.commons.jms.internal.listener.selector.strategy.SelectorBuilder;
import co.com.bancolombia.commons.jms.internal.models.RetryableConfig;
import co.com.bancolombia.commons.jms.internal.listener.selector.MQExecutorService;
import co.com.bancolombia.commons.jms.utils.ReactiveReplyRouter;
import com.ibm.mq.jakarta.jms.MQQueue;
import jakarta.jms.JMSContext;
Expand All @@ -21,9 +21,12 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.context.ApplicationEventPublisher;

import static com.ibm.msg.client.jakarta.wmq.common.CommonConstants.WMQ_TARGET_CLIENT;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -92,8 +95,19 @@ void shouldCreateMQQueueManagerSetter() throws JMSException {

@Test
void shouldCreateMQListenerHealthIndicator() {
MQHealthListener bean = configuration.defaultMqHealthListener(publisher);
MQHealthListener bean = configuration.defaultMqHealthListener(publisher, true);
Assertions.assertNotNull(bean);
assertInstanceOf(HealthIndicator.class, bean);
}

@Test
void shouldCreateDisabledMQListenerHealthIndicator() {
MQHealthListener bean = configuration.defaultMqHealthListener(publisher, false);
bean.onInit("listener");
bean.onStarted("listener");
bean.onException("listener", new JMSException("test"));
Assertions.assertNotNull(bean);
assertFalse(bean instanceof HealthIndicator);
}

@Test
Expand Down
2 changes: 2 additions & 0 deletions examples/mq-reactive/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ management:
health:
probes:
enabled: true # To test liveness and readiness probes
jms:
enabled: false
endpoint:
health:
show-details: always
Expand Down

0 comments on commit 39c9a40

Please sign in to comment.