parent
d467de50c9
commit
9784838229
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2021-2021 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.autoconfigure.amqp;
|
||||
|
||||
import com.rabbitmq.stream.Environment;
|
||||
import com.rabbitmq.stream.EnvironmentBuilder;
|
||||
|
||||
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
|
||||
import org.springframework.amqp.rabbit.config.ContainerCustomizer;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.rabbit.stream.config.StreamRabbitListenerContainerFactory;
|
||||
import org.springframework.rabbit.stream.listener.ConsumerCustomizer;
|
||||
import org.springframework.rabbit.stream.listener.StreamListenerContainer;
|
||||
|
||||
/**
|
||||
* Configuration for Spring RabbitMQ Stream Plugin support.
|
||||
*
|
||||
* @author Gary Russell
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass(StreamRabbitListenerContainerFactory.class)
|
||||
@ConditionalOnProperty(prefix = "spring.rabbitmq.listener", name = "type", havingValue = "stream")
|
||||
class RabbitStreamConfiguration {
|
||||
|
||||
@Bean(name = "rabbitListenerContainerFactory")
|
||||
@ConditionalOnMissingBean(name = "rabbitListenerContainerFactory")
|
||||
StreamRabbitListenerContainerFactory streamRabbitListenerContainerFactory(Environment rabbitStreamEnvironment,
|
||||
RabbitProperties properties, ObjectProvider<ConsumerCustomizer> consumerCustomizer,
|
||||
ObjectProvider<ContainerCustomizer<StreamListenerContainer>> containerCustomizer) {
|
||||
|
||||
StreamRabbitListenerContainerFactory factory = new StreamRabbitListenerContainerFactory(
|
||||
rabbitStreamEnvironment);
|
||||
factory.setNativeListener(properties.getListener().getStream().isNativeListener());
|
||||
consumerCustomizer.ifUnique(factory::setConsumerCustomizer);
|
||||
containerCustomizer.ifUnique(factory::setContainerCustomizer);
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Bean(name = "rabbitStreamEnvironment")
|
||||
@ConditionalOnMissingBean(name = "rabbitStreamEnvironment")
|
||||
Environment rabbitStreamEnvironment(RabbitProperties properties) {
|
||||
RabbitProperties.Stream stream = properties.getStream();
|
||||
String username = stream.getUsername();
|
||||
if (username == null) {
|
||||
username = properties.getUsername();
|
||||
}
|
||||
String password = stream.getPassword();
|
||||
if (password == null) {
|
||||
password = properties.getPassword();
|
||||
}
|
||||
EnvironmentBuilder builder = Environment.builder().lazyInitialization(true).host(stream.getHost())
|
||||
.port(stream.getPort());
|
||||
if (username != null) {
|
||||
builder.username(username);
|
||||
}
|
||||
if (password != null) {
|
||||
builder.password(password);
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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.autoconfigure.amqp;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.amqp.rabbit.config.ContainerCustomizer;
|
||||
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.rabbit.stream.config.StreamRabbitListenerContainerFactory;
|
||||
import org.springframework.rabbit.stream.listener.ConsumerCustomizer;
|
||||
import org.springframework.rabbit.stream.listener.StreamListenerContainer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link RabbitStreamConfiguration}.
|
||||
*
|
||||
* @author Gary Russell
|
||||
*/
|
||||
class RabbitStreamConfigurationTests {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(RabbitAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
void testContainerType() {
|
||||
this.contextRunner.withUserConfiguration(TestConfiguration.class)
|
||||
.withPropertyValues("spring.rabbitmq.listener.type:stream",
|
||||
"spring.rabbitmq.listener.stream.native-listener:true")
|
||||
.run((context) -> {
|
||||
RabbitListenerEndpointRegistry registry = context.getBean(RabbitListenerEndpointRegistry.class);
|
||||
assertThat(registry.getListenerContainer("test")).isInstanceOf(StreamListenerContainer.class);
|
||||
assertThat(new DirectFieldAccessor(registry.getListenerContainer("test"))
|
||||
.getPropertyValue("consumerCustomizer")).isNotNull();
|
||||
assertThat(new DirectFieldAccessor(context.getBean(StreamRabbitListenerContainerFactory.class))
|
||||
.getPropertyValue("nativeListener")).isEqualTo(Boolean.TRUE);
|
||||
assertThat(context.getBean(TestConfiguration.class).containerCustomizerCalled).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableRabbit
|
||||
static class TestConfiguration {
|
||||
|
||||
boolean containerCustomizerCalled;
|
||||
|
||||
@RabbitListener(id = "test", queues = "stream", autoStartup = "false")
|
||||
void listen(String in) {
|
||||
}
|
||||
|
||||
@Bean
|
||||
ConsumerCustomizer consumerCustomizer() {
|
||||
return (id, consumer) -> {
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
ContainerCustomizer<StreamListenerContainer> containerCustomizer() {
|
||||
return (container) -> this.containerCustomizerCalled = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue