|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.rabbit.stream.producer; |
| 18 | + |
| 19 | +import java.util.concurrent.CompletableFuture; |
| 20 | + |
| 21 | +import org.springframework.amqp.core.Message; |
| 22 | +import org.springframework.amqp.core.MessagePostProcessor; |
| 23 | +import org.springframework.amqp.support.converter.MessageConverter; |
| 24 | +import org.springframework.amqp.support.converter.SimpleMessageConverter; |
| 25 | +import org.springframework.beans.factory.BeanNameAware; |
| 26 | +import org.springframework.core.log.LogAccessor; |
| 27 | +import org.springframework.lang.Nullable; |
| 28 | +import org.springframework.rabbit.stream.support.StreamMessageProperties; |
| 29 | +import org.springframework.rabbit.stream.support.converter.DefaultStreamMessageConverter; |
| 30 | +import org.springframework.rabbit.stream.support.converter.StreamMessageConverter; |
| 31 | +import org.springframework.util.Assert; |
| 32 | + |
| 33 | +import com.rabbitmq.stream.ConfirmationHandler; |
| 34 | +import com.rabbitmq.stream.Constants; |
| 35 | +import com.rabbitmq.stream.Environment; |
| 36 | +import com.rabbitmq.stream.MessageBuilder; |
| 37 | +import com.rabbitmq.stream.Producer; |
| 38 | +import com.rabbitmq.stream.ProducerBuilder; |
| 39 | + |
| 40 | +/** |
| 41 | + * Default implementation of {@link RabbitStreamOperations}. |
| 42 | + * |
| 43 | + * @author Gary Russell |
| 44 | + * @since 2.4 |
| 45 | + * |
| 46 | + */ |
| 47 | +public class RabbitStreamTemplate2 implements RabbitStreamOperations2, BeanNameAware { |
| 48 | + |
| 49 | + protected final LogAccessor logger = new LogAccessor(getClass()); // NOSONAR |
| 50 | + |
| 51 | + private final Environment environment; |
| 52 | + |
| 53 | + private final String streamName; |
| 54 | + |
| 55 | + private MessageConverter messageConverter = new SimpleMessageConverter(); |
| 56 | + |
| 57 | + private StreamMessageConverter streamConverter = new DefaultStreamMessageConverter(); |
| 58 | + |
| 59 | + private boolean streamConverterSet; |
| 60 | + |
| 61 | + private Producer producer; |
| 62 | + |
| 63 | + private String beanName; |
| 64 | + |
| 65 | + private ProducerCustomizer producerCustomizer = (name, builder) -> { }; |
| 66 | + |
| 67 | + /** |
| 68 | + * Construct an instance with the provided {@link Environment}. |
| 69 | + * @param environment the environment. |
| 70 | + * @param streamName the stream name. |
| 71 | + */ |
| 72 | + public RabbitStreamTemplate2(Environment environment, String streamName) { |
| 73 | + Assert.notNull(environment, "'environment' cannot be null"); |
| 74 | + Assert.notNull(streamName, "'streamName' cannot be null"); |
| 75 | + this.environment = environment; |
| 76 | + this.streamName = streamName; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + private synchronized Producer createOrGetProducer() { |
| 81 | + if (this.producer == null) { |
| 82 | + ProducerBuilder builder = this.environment.producerBuilder(); |
| 83 | + builder.stream(this.streamName); |
| 84 | + this.producerCustomizer.accept(this.beanName, builder); |
| 85 | + this.producer = builder.build(); |
| 86 | + if (!this.streamConverterSet) { |
| 87 | + ((DefaultStreamMessageConverter) this.streamConverter).setBuilderSupplier( |
| 88 | + () -> this.producer.messageBuilder()); |
| 89 | + } |
| 90 | + } |
| 91 | + return this.producer; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public synchronized void setBeanName(String name) { |
| 96 | + this.beanName = name; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Set a converter for {@link #convertAndSend(Object)} operations. |
| 101 | + * @param messageConverter the converter. |
| 102 | + */ |
| 103 | + public void setMessageConverter(MessageConverter messageConverter) { |
| 104 | + Assert.notNull(messageConverter, "'messageConverter' cannot be null"); |
| 105 | + this.messageConverter = messageConverter; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Set a converter to convert from {@link Message} to {@link com.rabbitmq.stream.Message} |
| 110 | + * for {@link #send(Message)} and {@link #convertAndSend(Object)} methods. |
| 111 | + * @param streamConverter the converter. |
| 112 | + */ |
| 113 | + public synchronized void setStreamConverter(StreamMessageConverter streamConverter) { |
| 114 | + Assert.notNull(streamConverter, "'streamConverter' cannot be null"); |
| 115 | + this.streamConverter = streamConverter; |
| 116 | + this.streamConverterSet = true; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Used to customize the {@link ProducerBuilder} before the {@link Producer} is built. |
| 121 | + * @param producerCustomizer the customizer; |
| 122 | + */ |
| 123 | + public synchronized void setProducerCustomizer(ProducerCustomizer producerCustomizer) { |
| 124 | + Assert.notNull(producerCustomizer, "'producerCustomizer' cannot be null"); |
| 125 | + this.producerCustomizer = producerCustomizer; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public MessageConverter messageConverter() { |
| 130 | + return this.messageConverter; |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + @Override |
| 135 | + public StreamMessageConverter streamMessageConverter() { |
| 136 | + return this.streamConverter; |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + @Override |
| 141 | + public CompletableFuture<Boolean> send(Message message) { |
| 142 | + CompletableFuture<Boolean> future = new CompletableFuture<>(); |
| 143 | + createOrGetProducer().send(this.streamConverter.fromMessage(message), handleConfirm(future)); |
| 144 | + return future; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public CompletableFuture<Boolean> convertAndSend(Object message) { |
| 149 | + return convertAndSend(message, null); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public CompletableFuture<Boolean> convertAndSend(Object message, @Nullable MessagePostProcessor mpp) { |
| 154 | + Message message2 = this.messageConverter.toMessage(message, new StreamMessageProperties()); |
| 155 | + Assert.notNull(message2, "The message converter returned null"); |
| 156 | + if (mpp != null) { |
| 157 | + message2 = mpp.postProcessMessage(message2); |
| 158 | + if (message2 == null) { |
| 159 | + this.logger.debug("Message Post Processor returned null, message not sent"); |
| 160 | + CompletableFuture<Boolean> future = new CompletableFuture<>(); |
| 161 | + future.complete(false); |
| 162 | + return future; |
| 163 | + } |
| 164 | + } |
| 165 | + return send(message2); |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + @Override |
| 170 | + public CompletableFuture<Boolean> send(com.rabbitmq.stream.Message message) { |
| 171 | + CompletableFuture<Boolean> future = new CompletableFuture<>(); |
| 172 | + createOrGetProducer().send(message, handleConfirm(future)); |
| 173 | + return future; |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public MessageBuilder messageBuilder() { |
| 178 | + return createOrGetProducer().messageBuilder(); |
| 179 | + } |
| 180 | + |
| 181 | + private ConfirmationHandler handleConfirm(CompletableFuture<Boolean> future) { |
| 182 | + return confStatus -> { |
| 183 | + if (confStatus.isConfirmed()) { |
| 184 | + future.complete(true); |
| 185 | + } |
| 186 | + else { |
| 187 | + int code = confStatus.getCode(); |
| 188 | + String errorMessage; |
| 189 | + switch (code) { |
| 190 | + case Constants.CODE_MESSAGE_ENQUEUEING_FAILED: |
| 191 | + errorMessage = "Message Enqueueing Failed"; |
| 192 | + break; |
| 193 | + case Constants.CODE_PRODUCER_CLOSED: |
| 194 | + errorMessage = "Producer Closed"; |
| 195 | + break; |
| 196 | + case Constants.CODE_PRODUCER_NOT_AVAILABLE: |
| 197 | + errorMessage = "Producer Not Available"; |
| 198 | + break; |
| 199 | + case Constants.CODE_PUBLISH_CONFIRM_TIMEOUT: |
| 200 | + errorMessage = "Publish Confirm Timeout"; |
| 201 | + break; |
| 202 | + default: |
| 203 | + errorMessage = "Unknown code: " + code; |
| 204 | + break; |
| 205 | + } |
| 206 | + future.completeExceptionally(new StreamSendException(errorMessage, code)); |
| 207 | + } |
| 208 | + }; |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * {@inheritDoc} |
| 213 | + * <p> |
| 214 | + * <b>Close the underlying producer; a new producer will be created on the next |
| 215 | + * operation that requires one.</b> |
| 216 | + */ |
| 217 | + @Override |
| 218 | + public synchronized void close() { |
| 219 | + if (this.producer != null) { |
| 220 | + this.producer.close(); |
| 221 | + this.producer = null; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | +} |
0 commit comments