diff --git a/spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractMessageChannel.java b/spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractMessageChannel.java index 964433f951..b82ff8b0a0 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractMessageChannel.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractMessageChannel.java @@ -555,8 +555,9 @@ public abstract class AbstractMessageChannel extends IntegrationObjectSupport protected abstract boolean doSend(Message message, long timeout); @Override - public void destroy() throws Exception { + public void destroy() throws Exception { // NOSONAR TODO: remove throws in 5.2 this.meters.forEach(MeterFacade::remove); + this.meters.clear(); } /** diff --git a/spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractPollableChannel.java b/spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractPollableChannel.java index 220cf8a25f..5c3f365dc6 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractPollableChannel.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractPollableChannel.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -233,7 +233,7 @@ public abstract class AbstractPollableChannel extends AbstractMessageChannel protected abstract Message doReceive(long timeout); @Override - public void destroy() throws Exception { + public void destroy() throws Exception { // NOSONAR TODO: remove throws in 5.2 super.destroy(); if (this.receiveCounter != null) { this.receiveCounter.remove(); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/support/management/micrometer/MicrometerMetricsCaptor.java b/spring-integration-core/src/main/java/org/springframework/integration/support/management/micrometer/MicrometerMetricsCaptor.java index 18aed71657..5e87f55901 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/support/management/micrometer/MicrometerMetricsCaptor.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/support/management/micrometer/MicrometerMetricsCaptor.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2018-2019 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. @@ -195,6 +195,19 @@ public class MicrometerMetricsCaptor implements MetricsCaptor { this.timer.record(time, unit); } + @Override + public int hashCode() { + return this.timer.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!MicroTimer.class.equals(obj.getClass())) { + return false; + } + return this.timer.equals(((MicroTimer) obj).timer); + } + } protected static class MicroCounterBuilder implements CounterBuilder { @@ -246,6 +259,19 @@ public class MicrometerMetricsCaptor implements MetricsCaptor { this.counter.increment(); } + @Override + public int hashCode() { + return this.counter.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!MicroCounter.class.equals(obj.getClass())) { + return false; + } + return this.counter.equals(((MicroCounter) obj).counter); + } + } protected static class MicroGaugeBuilder implements GaugeBuilder { @@ -292,6 +318,19 @@ public class MicrometerMetricsCaptor implements MetricsCaptor { return this.gauge; } + @Override + public int hashCode() { + return this.gauge.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!MicroGauge.class.equals(obj.getClass())) { + return false; + } + return this.gauge.equals(((MicroGauge) obj).gauge); + } + } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/support/management/micrometer/MicrometerMetricsTests.java b/spring-integration-core/src/test/java/org/springframework/integration/support/management/micrometer/MicrometerMetricsTests.java index 3749c30e83..be6924e7f7 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/support/management/micrometer/MicrometerMetricsTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/support/management/micrometer/MicrometerMetricsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2018-2019 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. @@ -19,6 +19,8 @@ package org.springframework.integration.support.management.micrometer; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; +import java.util.Set; + import org.junit.Test; import org.junit.runner.RunWith; @@ -40,11 +42,13 @@ import org.springframework.integration.config.EnableIntegrationManagement; import org.springframework.integration.core.MessageSource; import org.springframework.integration.endpoint.AbstractMessageSource; import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; +import org.springframework.integration.test.util.TestUtils; import org.springframework.messaging.Message; import org.springframework.messaging.MessageHandler; import org.springframework.messaging.MessagingException; import org.springframework.messaging.PollableChannel; import org.springframework.messaging.support.GenericMessage; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; import io.micrometer.core.instrument.MeterRegistry; @@ -58,6 +62,7 @@ import io.micrometer.core.instrument.simple.SimpleMeterRegistry; * */ @RunWith(SpringRunner.class) +@DirtiesContext public class MicrometerMetricsTests { @Autowired @@ -84,17 +89,26 @@ public class MicrometerMetricsTests { @Autowired private NullChannel nullChannel; + @SuppressWarnings("unchecked") @Test public void testSend() throws Exception { GenericMessage message = new GenericMessage<>("foo"); this.channel.send(message); try { - this.channel.send(this.source.receive()); + this.channel.send(this.source.receive()); // "bar" fail("Expected exception"); } catch (MessagingException e) { assertThat(e.getCause().getMessage()).isEqualTo("testErrorCount"); } + try { + this.channel.send(new GenericMessage<>("bar")); + fail("Expected exception"); + } + catch (MessagingException e) { + assertThat(e.getCause().getMessage()).isEqualTo("testErrorCount"); + } + assertThat(TestUtils.getPropertyValue(this.channel, "meters", Set.class)).hasSize(2); this.channel2.send(message); this.queue.send(message); this.queue.send(message); @@ -141,12 +155,12 @@ public class MicrometerMetricsTests { assertThat(registry.get("spring.integration.send") .tag("name", "channel") .tag("result", "failure") - .timer().count()).isEqualTo(1); + .timer().count()).isEqualTo(2); assertThat(registry.get("spring.integration.send") .tag("name", "eipMethod.handler") .tag("result", "failure") - .timer().count()).isEqualTo(1); + .timer().count()).isEqualTo(2); assertThat(registry.get("spring.integration.receive") .tag("name", "queue") @@ -202,6 +216,9 @@ public class MicrometerMetricsTests { catch (MeterNotFoundException e) { assertThat(e).hasMessageContaining("No meter with name 'spring.integration.receive' was found"); } + this.channel.destroy(); + assertThat(TestUtils.getPropertyValue(this.channel, "meters", Set.class)).hasSize(0); + } @Configuration