diff --git a/spring-boot-project/spring-boot-autoconfigure/build.gradle b/spring-boot-project/spring-boot-autoconfigure/build.gradle index a4717f29c5..5c19a1adb4 100644 --- a/spring-boot-project/spring-boot-autoconfigure/build.gradle +++ b/spring-boot-project/spring-boot-autoconfigure/build.gradle @@ -142,6 +142,7 @@ dependencies { optional("org.springframework.session:spring-session-hazelcast") optional("org.springframework.session:spring-session-jdbc") optional("org.springframework.amqp:spring-rabbit") + optional("org.springframework.amqp:spring-rabbit-stream") optional("org.springframework.kafka:spring-kafka") optional("org.springframework.ws:spring-ws-core") optional("org.thymeleaf:thymeleaf") diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java index 16d46758a7..e1f68c7f3b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java @@ -88,7 +88,7 @@ import org.springframework.core.io.ResourceLoader; @Configuration(proxyBeanMethods = false) @ConditionalOnClass({ RabbitTemplate.class, Channel.class }) @EnableConfigurationProperties(RabbitProperties.class) -@Import(RabbitAnnotationDrivenConfiguration.class) +@Import({ RabbitAnnotationDrivenConfiguration.class, RabbitStreamConfiguration.class }) public class RabbitAutoConfiguration { @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java index 0a1f3ee57d..5d8b6417a1 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java @@ -51,6 +51,8 @@ public class RabbitProperties { private static final int DEFAULT_PORT_SECURE = 5671; + private static final int DEFAULT_STREAM_PORT = 5552; + /** * RabbitMQ host. Ignored if an address is set. */ @@ -137,6 +139,8 @@ public class RabbitProperties { private final Template template = new Template(); + private final Stream stream = new Stream(); + private List
parsedAddresses; public String getHost() { @@ -361,6 +365,10 @@ public class RabbitProperties { return this.template; } + public Stream getStream() { + return this.stream; + } + public class Ssl { private static final String SUN_X509 = "SunX509"; @@ -629,7 +637,12 @@ public class RabbitProperties { * Container where the listener is invoked directly on the RabbitMQ consumer * thread. */ - DIRECT + DIRECT, + + /** + * Container that uses the RabbitMQ Stream Client. + */ + STREAM } @@ -644,6 +657,8 @@ public class RabbitProperties { private final DirectContainer direct = new DirectContainer(); + private final StreamContainer stream = new StreamContainer(); + public ContainerType getType() { return this.type; } @@ -660,15 +675,31 @@ public class RabbitProperties { return this.direct; } + public StreamContainer getStream() { + return this.stream; + } + } - public abstract static class AmqpContainer { + public abstract static class BaseContainer { /** * Whether to start the container automatically on startup. */ private boolean autoStartup = true; + public boolean isAutoStartup() { + return this.autoStartup; + } + + public void setAutoStartup(boolean autoStartup) { + this.autoStartup = autoStartup; + } + + } + + public abstract static class AmqpContainer extends BaseContainer { + /** * Acknowledge mode of container. */ @@ -701,14 +732,6 @@ public class RabbitProperties { */ private final ListenerRetry retry = new ListenerRetry(); - public boolean isAutoStartup() { - return this.autoStartup; - } - - public void setAutoStartup(boolean autoStartup) { - this.autoStartup = autoStartup; - } - public AcknowledgeMode getAcknowledgeMode() { return this.acknowledgeMode; } @@ -871,6 +894,24 @@ public class RabbitProperties { } + public static class StreamContainer extends BaseContainer { + + /** + * When true, the container factory will create containers that support listeners + * that consume native stream messages instead of spring-amqp {@code Message}s. + */ + boolean nativeListener; + + public boolean isNativeListener() { + return this.nativeListener; + } + + public void setNativeListener(boolean nativeListener) { + this.nativeListener = nativeListener; + } + + } + public static class Template { private final Retry retry = new Retry(); @@ -1128,4 +1169,62 @@ public class RabbitProperties { } + public static final class Stream { + + /** + * Host of a RabbitMQ instance with the Stream Plugin Enabled + */ + private String host = "localhost"; + + /** + * Stream port of a RabbitMQ instance with the Stream Plugin Enabled + */ + private int port = DEFAULT_STREAM_PORT; + + /** + * Login user to authenticate to the broker. If not set + * {@code spring.rabbitmq.username} will be used. + */ + private String username; + + /** + * Login password to authenticate to the broker. If not set + * {@code spring.rabbitmq.password} will be used. + */ + private String password; + + public String getHost() { + return this.host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return this.port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getUsername() { + return this.username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return this.password; + } + + public void setPassword(String password) { + this.password = password; + } + + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitStreamConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitStreamConfiguration.java new file mode 100644 index 0000000000..67923f9914 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitStreamConfiguration.java @@ -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