-
Notifications
You must be signed in to change notification settings - Fork 646
GH-3031: Defer SMLC shutdown for pending replies #3147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
368fe54
1a48e51
7a732db
cc2e48c
1dd2d72
00487ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -692,6 +692,24 @@ protected void shutdownAndWaitOrCallback(@Nullable Runnable callback) { | |
| Runnable awaitShutdown = () -> { | ||
| logger.info("Waiting for workers to finish."); | ||
| try { | ||
| ActiveObjectCounter<Object> replyCounter = null; | ||
| Object listener = getMessageListener(); | ||
| if (listener instanceof ListenerContainerAware) { | ||
| replyCounter = ((ListenerContainerAware) listener).getPendingReplyCounter(); | ||
| } | ||
|
|
||
| if (replyCounter != null) { | ||
|
||
| if (logger.isInfoEnabled()) { | ||
| logger.info("Waiting for pending replies: " + replyCounter.getCount()); | ||
| } | ||
| if (!replyCounter.await(getShutdownTimeout(), TimeUnit.MILLISECONDS)) { | ||
| if (logger.isWarnEnabled()) { | ||
| logger.warn("Shutdown timeout expired, but " + replyCounter.getCount() | ||
| + " pending replies still remain."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| boolean finished = this.cancellationLock.await(getShutdownTimeout(), TimeUnit.MILLISECONDS); | ||
| if (finished) { | ||
| logger.info("Successfully waited for workers to finish."); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,4 +39,13 @@ public interface ListenerContainerAware { | |
| @Nullable | ||
| Collection<String> expectedQueueNames(); | ||
|
|
||
| /** | ||
| * Return a counter for pending replies, if any. | ||
| * @return the counter, or null. | ||
| * @since 4.0 | ||
| */ | ||
| @Nullable | ||
| default ActiveObjectCounter<Object> getPendingReplyCounter() { | ||
|
||
| return null; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,7 +62,9 @@ | |
| import org.springframework.amqp.rabbit.connection.Connection; | ||
| import org.springframework.amqp.rabbit.connection.ConnectionFactory; | ||
| import org.springframework.amqp.rabbit.connection.SingleConnectionFactory; | ||
| import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
| import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; | ||
| import org.springframework.amqp.rabbit.support.ActiveObjectCounter; | ||
| import org.springframework.amqp.utils.test.TestUtils; | ||
| import org.springframework.aop.support.AopUtils; | ||
| import org.springframework.beans.DirectFieldAccessor; | ||
|
|
@@ -716,6 +718,45 @@ void testWithConsumerStartWhenNotActive() { | |
| assertThat(start.getCount()).isEqualTo(0L); | ||
| } | ||
|
|
||
| @Test | ||
| @SuppressWarnings("unchecked") | ||
| void testShutdownWithPendingReplies() { | ||
| ConnectionFactory connectionFactory = mock(ConnectionFactory.class); | ||
| Connection connection = mock(Connection.class); | ||
| Channel channel = mock(Channel.class); | ||
| given(connectionFactory.createConnection()).willReturn(connection); | ||
| given(connection.createChannel(false)).willReturn(channel); | ||
| given(channel.isOpen()).willReturn(true); | ||
|
|
||
| RabbitTemplate template = new RabbitTemplate(connectionFactory); | ||
| SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); | ||
| container.setQueueNames("shutdown.test.queue"); | ||
| container.setMessageListener(template); | ||
|
|
||
| template.setReplyAddress(container.getQueueNames()[0]); | ||
|
|
||
| long shutdownTimeout = 2000L; | ||
|
||
| container.setShutdownTimeout(shutdownTimeout); | ||
|
|
||
| ActiveObjectCounter<Object> replyCounter = | ||
| (ActiveObjectCounter<Object>) ReflectionTestUtils.getField(template, "pendingRepliesCounter"); | ||
|
||
| assertThat(replyCounter).isNotNull(); | ||
|
|
||
| Object pending = new Object(); | ||
| replyCounter.add(pending); | ||
| assertThat(replyCounter.getCount()).isEqualTo(1); | ||
|
|
||
| container.start(); | ||
|
|
||
| long startTime = System.currentTimeMillis(); | ||
| container.stop(); | ||
| long stopDuration = System.currentTimeMillis() - startTime; | ||
|
||
|
|
||
| replyCounter.release(pending); | ||
|
||
|
|
||
| assertThat(stopDuration).isGreaterThanOrEqualTo(shutdownTimeout - 500); | ||
| } | ||
|
|
||
| private Answer<Object> messageToConsumer(final Channel mockChannel, final SimpleMessageListenerContainer container, | ||
| final boolean cancel, final CountDownLatch latch) { | ||
| return invocation -> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can just do this
if (getMessageListener() instanceof ListenerContainerAware listenerContainerAware) {Then use that pattern matching variable in the block.