Skip to content

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions module/spring-boot-amqp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies {
optional(project(":module:spring-boot-metrics"))
optional("io.micrometer:micrometer-core")
optional("org.springframework.amqp:spring-rabbit-stream")
optional("org.springframework.amqp:spring-rabbitmq-client")
Copy link
Member

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 that api("org.springframework.amqp:spring-rabbit").
The spring-rabbitmq-client has spring-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 excluding spring-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.

Copy link
Member

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 old spring-rabbit.

But that's my opinion: probably we would need some input from Spring Boot team.

optional("org.testcontainers:rabbitmq")

dockerTestImplementation(project(":test-support:spring-boot-docker-test-support"))
Expand Down
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any comments why no @ConditionalOnMissingBean on that RabbitAutoConfiguration?
We should aim to not auto-configure AMQP 0.9.1, if we got AMQP 1.0.

@ConditionalOnClass({ RabbitAmqpTemplate.class, Connection.class })
@EnableConfigurationProperties(RabbitProperties.class)
public final class RabbitAmqpAutoConfiguration {

private final RabbitProperties properties;

RabbitAmqpAutoConfiguration(RabbitProperties properties) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to look into a separate RabbitAmqpProperties abstraction.
Even if some duplication is possible, a bunch of existing properties are not going to be used for AMQP 1.0 at all.
And might cause confusion.

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
Loading