-
Notifications
You must be signed in to change notification settings - Fork 41.6k
Spring AMQP: Support Both Container Types #9055
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
...ngframework/boot/autoconfigure/amqp/AbstractRabbitListenerContainerFactoryConfigurer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 2012-2017 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.amqp; | ||
|
||
import org.springframework.amqp.rabbit.config.AbstractRabbitListenerContainerFactory; | ||
import org.springframework.amqp.rabbit.config.RetryInterceptorBuilder; | ||
import org.springframework.amqp.rabbit.connection.ConnectionFactory; | ||
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory; | ||
import org.springframework.amqp.rabbit.retry.MessageRecoverer; | ||
import org.springframework.amqp.rabbit.retry.RejectAndDontRequeueRecoverer; | ||
import org.springframework.amqp.support.converter.MessageConverter; | ||
import org.springframework.boot.autoconfigure.amqp.RabbitProperties.ListenerRetry; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Configure {@link RabbitListenerContainerFactory} with sensible defaults. | ||
* | ||
* @param <T> the container factory type. | ||
* | ||
* @author Gary Russell | ||
* @since 2.0 | ||
* | ||
*/ | ||
public abstract class AbstractRabbitListenerContainerFactoryConfigurer< | ||
T extends AbstractRabbitListenerContainerFactory<?>> { | ||
|
||
private MessageConverter messageConverter; | ||
|
||
private MessageRecoverer messageRecoverer; | ||
|
||
private RabbitProperties rabbitProperties; | ||
|
||
/** | ||
* Set the {@link MessageConverter} to use or {@code null} if the out-of-the-box | ||
* converter should be used. | ||
* @param messageConverter the {@link MessageConverter} | ||
*/ | ||
protected void setMessageConverter(MessageConverter messageConverter) { | ||
this.messageConverter = messageConverter; | ||
} | ||
|
||
/** | ||
* Set the {@link MessageRecoverer} to use or {@code null} to rely on the default. | ||
* @param messageRecoverer the {@link MessageRecoverer} | ||
*/ | ||
protected void setMessageRecoverer(MessageRecoverer messageRecoverer) { | ||
this.messageRecoverer = messageRecoverer; | ||
} | ||
|
||
/** | ||
* Set the {@link RabbitProperties} to use. | ||
* @param rabbitProperties the {@link RabbitProperties} | ||
*/ | ||
protected void setRabbitProperties(RabbitProperties rabbitProperties) { | ||
this.rabbitProperties = rabbitProperties; | ||
} | ||
|
||
/** | ||
* Configure the specified rabbit listener container factory. The factory can be | ||
* further tuned and default settings can be overridden. | ||
* @param factory the {@link AbstractRabbitListenerContainerFactory} instance to | ||
* configure | ||
* @param connectionFactory the {@link ConnectionFactory} to use | ||
*/ | ||
public final void configure(T factory, ConnectionFactory connectionFactory) { | ||
Assert.notNull(factory, "Factory must not be null"); | ||
Assert.notNull(connectionFactory, "ConnectionFactory must not be null"); | ||
factory.setConnectionFactory(connectionFactory); | ||
if (this.messageConverter != null) { | ||
factory.setMessageConverter(this.messageConverter); | ||
} | ||
RabbitProperties.Listener listenerConfig = this.rabbitProperties.getListener(); | ||
factory.setAutoStartup(listenerConfig.isAutoStartup()); | ||
if (listenerConfig.getAcknowledgeMode() != null) { | ||
factory.setAcknowledgeMode(listenerConfig.getAcknowledgeMode()); | ||
} | ||
if (listenerConfig.getPrefetch() != null) { | ||
factory.setPrefetchCount(listenerConfig.getPrefetch()); | ||
} | ||
if (listenerConfig.getDefaultRequeueRejected() != null) { | ||
factory.setDefaultRequeueRejected(listenerConfig.getDefaultRequeueRejected()); | ||
} | ||
if (listenerConfig.getIdleEventInterval() != null) { | ||
factory.setIdleEventInterval(listenerConfig.getIdleEventInterval()); | ||
} | ||
ListenerRetry retryConfig = listenerConfig.getRetry(); | ||
if (retryConfig.isEnabled()) { | ||
RetryInterceptorBuilder<?> builder = (retryConfig.isStateless() | ||
? RetryInterceptorBuilder.stateless() | ||
: RetryInterceptorBuilder.stateful()); | ||
builder.maxAttempts(retryConfig.getMaxAttempts()); | ||
builder.backOffOptions(retryConfig.getInitialInterval(), | ||
retryConfig.getMultiplier(), retryConfig.getMaxInterval()); | ||
MessageRecoverer recoverer = (this.messageRecoverer != null | ||
? this.messageRecoverer : new RejectAndDontRequeueRecoverer()); | ||
builder.recoverer(recoverer); | ||
factory.setAdviceChain(builder.build()); | ||
} | ||
configure(factory, this.rabbitProperties); | ||
} | ||
|
||
/** | ||
* Perform factory-specific configuration. | ||
* | ||
* @param factory the factory. | ||
* @param rabbitProperties the properties. | ||
*/ | ||
protected abstract void configure(T factory, RabbitProperties rabbitProperties); | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...ringframework/boot/autoconfigure/amqp/DirectRabbitListenerContainerFactoryConfigurer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2012-2017 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.amqp; | ||
|
||
import org.springframework.amqp.rabbit.config.DirectRabbitListenerContainerFactory; | ||
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory; | ||
|
||
/** | ||
* Configure {@link RabbitListenerContainerFactory} with sensible defaults. | ||
* | ||
* @author Gary Russell | ||
* @since 2.0 | ||
*/ | ||
public final class DirectRabbitListenerContainerFactoryConfigurer | ||
extends AbstractRabbitListenerContainerFactoryConfigurer<DirectRabbitListenerContainerFactory> { | ||
|
||
@Override | ||
protected void configure(DirectRabbitListenerContainerFactory factory, RabbitProperties rabbitProperties) { | ||
RabbitProperties.Listener listenerConfig = rabbitProperties.getListener(); | ||
if (listenerConfig.getConsumersPerQueue() != null) { | ||
factory.setConsumersPerQueue(listenerConfig.getConsumersPerQueue()); | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't know all of the intimate details, but the description above for
ContainerType.SIMPLE
is that it is the legacy container type. Wouldn't it be better to default to the non-legacy type with the new major version of Spring Boot (and Spring AMQP)?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.
Unfortunately, it's not black and white - each container has certain pros and cons. I feel that changing the container type without the user explicitly choosing to do so could cause unexpected problems for users.
I prefer that the user explicitly opt-in to using the new container. Documentation here.
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.
Thanks @garyrussell - We've created a dedicated issue to figure this out. Can you please move your comment over there? #9173