-
Notifications
You must be signed in to change notification settings - Fork 41.4k
Add support for RabbitMQ AMQP 1.0 #46608
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2012-present 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 | ||
* | ||
* https://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.amqp.autoconfigure; | ||
|
||
import com.rabbitmq.client.amqp.Environment; | ||
import com.rabbitmq.client.amqp.impl.AmqpEnvironmentBuilder; | ||
|
||
/** | ||
* Callback interface that can be implemented by beans wishing to customize the | ||
* auto-configured {@link Environment} that is created by an | ||
* {@link AmqpEnvironmentBuilder}. | ||
* | ||
* @author Eddú Meléndez | ||
* @since 4.0.0 | ||
*/ | ||
@FunctionalInterface | ||
public interface AmqpEnvironmentBuilderCustomizer { | ||
|
||
/** | ||
* Customize the {@code AmqpEnvironmentBuilder}. | ||
* @param builder the builder to customize | ||
*/ | ||
void customize(AmqpEnvironmentBuilder builder); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Copyright 2012-present 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 | ||
* | ||
* https://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.amqp.autoconfigure; | ||
|
||
import com.rabbitmq.client.amqp.Connection; | ||
import com.rabbitmq.client.amqp.CredentialsProvider; | ||
import com.rabbitmq.client.amqp.Environment; | ||
import com.rabbitmq.client.amqp.impl.AmqpEnvironmentBuilder; | ||
import com.rabbitmq.client.amqp.impl.AmqpEnvironmentBuilder.EnvironmentConnectionSettings; | ||
|
||
import org.springframework.amqp.rabbit.config.ContainerCustomizer; | ||
import org.springframework.amqp.rabbit.retry.MessageRecoverer; | ||
import org.springframework.amqp.rabbitmq.client.AmqpConnectionFactory; | ||
import org.springframework.amqp.rabbitmq.client.RabbitAmqpAdmin; | ||
import org.springframework.amqp.rabbitmq.client.RabbitAmqpTemplate; | ||
import org.springframework.amqp.rabbitmq.client.SingleAmqpConnectionFactory; | ||
import org.springframework.amqp.rabbitmq.client.config.RabbitAmqpListenerContainerFactory; | ||
import org.springframework.amqp.rabbitmq.client.listener.RabbitAmqpListenerContainer; | ||
import org.springframework.amqp.support.converter.MessageConverter; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.boot.amqp.autoconfigure.RabbitConnectionDetails.Address; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.boot.context.properties.PropertyMapper; | ||
import org.springframework.boot.ssl.SslBundles; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for {@link RabbitAmqpTemplate}. | ||
* | ||
* @author Eddú Meléndez | ||
* @since 4.0.0 | ||
*/ | ||
@AutoConfiguration(before = RabbitAutoConfiguration.class) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any comments why no |
||
@ConditionalOnClass({ RabbitAmqpTemplate.class, Connection.class }) | ||
@EnableConfigurationProperties(RabbitProperties.class) | ||
public final class RabbitAmqpAutoConfiguration { | ||
|
||
private final RabbitProperties properties; | ||
|
||
RabbitAmqpAutoConfiguration(RabbitProperties properties) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to look into a separate |
||
this.properties = properties; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
RabbitConnectionDetails rabbitConnectionDetails(ObjectProvider<SslBundles> sslBundles) { | ||
return new PropertiesRabbitConnectionDetails(this.properties, sslBundles.getIfAvailable()); | ||
} | ||
|
||
@Bean(name = "rabbitAmqpListenerContainerFactory") | ||
@ConditionalOnMissingBean(name = "rabbitAmqpListenerContainerFactory") | ||
RabbitAmqpListenerContainerFactory rabbitAmqpListenerContainerFactory(AmqpConnectionFactory connectionFactory, | ||
ObjectProvider<ContainerCustomizer<RabbitAmqpListenerContainer>> amqpContainerCustomizer, | ||
ObjectProvider<RabbitRetryTemplateCustomizer> retryTemplateCustomizers, | ||
ObjectProvider<MessageRecoverer> messageRecoverer) { | ||
RabbitAmqpListenerContainerFactory factory = new RabbitAmqpListenerContainerFactory(connectionFactory); | ||
amqpContainerCustomizer.ifUnique(factory::setContainerCustomizer); | ||
|
||
RabbitProperties.AmqpContainer configuration = this.properties.getListener().getSimple(); | ||
factory.setObservationEnabled(configuration.isObservationEnabled()); | ||
return factory; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
Environment rabbitAmqpEnvironment(RabbitConnectionDetails connectionDetails, | ||
ObjectProvider<AmqpEnvironmentBuilderCustomizer> customizers, | ||
ObjectProvider<CredentialsProvider> credentialsProvider) { | ||
PropertyMapper map = PropertyMapper.get(); | ||
EnvironmentConnectionSettings environmentConnectionSettings = new AmqpEnvironmentBuilder().connectionSettings(); | ||
Address address = connectionDetails.getFirstAddress(); | ||
map.from(address::host).whenNonNull().to(environmentConnectionSettings::host); | ||
map.from(address::port).to(environmentConnectionSettings::port); | ||
map.from(connectionDetails::getUsername).whenNonNull().to(environmentConnectionSettings::username); | ||
map.from(connectionDetails::getPassword).whenNonNull().to(environmentConnectionSettings::password); | ||
map.from(connectionDetails::getVirtualHost).whenNonNull().to(environmentConnectionSettings::virtualHost); | ||
map.from(credentialsProvider::getIfAvailable) | ||
.whenNonNull() | ||
.to(environmentConnectionSettings::credentialsProvider); | ||
|
||
AmqpEnvironmentBuilder builder = environmentConnectionSettings.environmentBuilder(); | ||
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder)); | ||
return builder.build(); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
AmqpConnectionFactory amqpConnectionFactory(Environment environment) { | ||
return new SingleAmqpConnectionFactory(environment); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
RabbitAmqpTemplate rabbitAmqpTemplate(AmqpConnectionFactory connectionFactory, | ||
ObjectProvider<RabbitAmqpTemplateCustomizer> customizers, | ||
ObjectProvider<MessageConverter> messageConverter) { | ||
RabbitAmqpTemplate rabbitAmqpTemplate = new RabbitAmqpTemplate(connectionFactory); | ||
if (messageConverter.getIfAvailable() != null) { | ||
rabbitAmqpTemplate.setMessageConverter(messageConverter.getIfAvailable()); | ||
} | ||
RabbitProperties.Template templateProperties = this.properties.getTemplate(); | ||
|
||
PropertyMapper map = PropertyMapper.get(); | ||
map.from(templateProperties::getDefaultReceiveQueue).whenNonNull().to(rabbitAmqpTemplate::setReceiveQueue); | ||
map.from(templateProperties::getExchange).whenNonNull().to(rabbitAmqpTemplate::setExchange); | ||
map.from(templateProperties::getRoutingKey).to(rabbitAmqpTemplate::setRoutingKey); | ||
|
||
customizers.orderedStream().forEach((customizer) -> customizer.customize(rabbitAmqpTemplate)); | ||
return rabbitAmqpTemplate; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
RabbitAmqpAdmin rabbitAmqpAdmin(AmqpConnectionFactory connectionFactory) { | ||
return new RabbitAmqpAdmin(connectionFactory); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2012-present 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 | ||
* | ||
* https://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.amqp.autoconfigure; | ||
|
||
import org.springframework.amqp.rabbitmq.client.RabbitAmqpTemplate; | ||
|
||
/** | ||
* Callback interface that can be used to customize a {@link RabbitAmqpTemplate}. | ||
* | ||
* @author Eddú Meléndez | ||
* @since 4.0.0 | ||
*/ | ||
@FunctionalInterface | ||
public interface RabbitAmqpTemplateCustomizer { | ||
|
||
/** | ||
* Callback to customize a {@link RabbitAmqpTemplate} instance. | ||
* @param rabbitAmqpTemplate the rabbitAmqpTemplate to customize | ||
*/ | ||
void customize(RabbitAmqpTemplate rabbitAmqpTemplate); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
org.springframework.boot.amqp.autoconfigure.RabbitAmqpAutoConfiguration | ||
org.springframework.boot.amqp.autoconfigure.RabbitAutoConfiguration | ||
org.springframework.boot.amqp.autoconfigure.health.RabbitHealthContributorAutoConfiguration | ||
org.springframework.boot.amqp.autoconfigure.metrics.RabbitMetricsAutoConfiguration |
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 think it is better to have an AMQP 1.0 as default one.
Therefore I suggest to do
api("org.springframework.amqp:spring-rabbitmq-client")
instead of thatapi("org.springframework.amqp:spring-rabbit")
.The
spring-rabbitmq-client
hasspring-rabbit
as dependency.So, both AMQP 1.0 and 0.9.1 are on classpath.
but that should be end-user choice to opt-in to
0.9.1
by excludingspring-rabbitmq-client
auto-configuration or so.I mean this is Spring Boot 4.0 and we probably can bite a bullet and make AMQP 1.0 auto-configured by default.
Just my opinion: we can continue discussion.
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.
Any comments why this was not addressed?
I think
spring-boot-starter-amqp
as it is right now, but definitely aimed for AMQP 1.0.For previous behavior for AMQP 0.9.1, we would need to introduce a new starter -
spring-boot-starter-rabbitmq
.There we would exclude this new
spring-rabbitmq-client
and declare that oldspring-rabbit
.But that's my opinion: probably we would need some input from Spring Boot team.