|
| 1 | +/* |
| 2 | + * Copyright 2012-2017 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 | + * http://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.boot.autoconfigure.amqp; |
| 18 | + |
| 19 | +import org.springframework.amqp.rabbit.config.AbstractRabbitListenerContainerFactory; |
| 20 | +import org.springframework.amqp.rabbit.config.RetryInterceptorBuilder; |
| 21 | +import org.springframework.amqp.rabbit.connection.ConnectionFactory; |
| 22 | +import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory; |
| 23 | +import org.springframework.amqp.rabbit.retry.MessageRecoverer; |
| 24 | +import org.springframework.amqp.rabbit.retry.RejectAndDontRequeueRecoverer; |
| 25 | +import org.springframework.amqp.support.converter.MessageConverter; |
| 26 | +import org.springframework.boot.autoconfigure.amqp.RabbitProperties.ListenerRetry; |
| 27 | +import org.springframework.util.Assert; |
| 28 | + |
| 29 | +/** |
| 30 | + * Configure {@link RabbitListenerContainerFactory} with sensible defaults. |
| 31 | + * |
| 32 | + * @param <T> the container factory type. |
| 33 | + * @author Gary Russell |
| 34 | + * @author Stephane Nicoll |
| 35 | + * @since 2.0.0 |
| 36 | + */ |
| 37 | +public abstract class AbstractRabbitListenerContainerFactoryConfigurer< |
| 38 | + T extends AbstractRabbitListenerContainerFactory<?>> { |
| 39 | + |
| 40 | + private MessageConverter messageConverter; |
| 41 | + |
| 42 | + private MessageRecoverer messageRecoverer; |
| 43 | + |
| 44 | + private RabbitProperties rabbitProperties; |
| 45 | + |
| 46 | + /** |
| 47 | + * Set the {@link MessageConverter} to use or {@code null} if the out-of-the-box |
| 48 | + * converter should be used. |
| 49 | + * @param messageConverter the {@link MessageConverter} |
| 50 | + */ |
| 51 | + protected void setMessageConverter(MessageConverter messageConverter) { |
| 52 | + this.messageConverter = messageConverter; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Set the {@link MessageRecoverer} to use or {@code null} to rely on the default. |
| 57 | + * @param messageRecoverer the {@link MessageRecoverer} |
| 58 | + */ |
| 59 | + protected void setMessageRecoverer(MessageRecoverer messageRecoverer) { |
| 60 | + this.messageRecoverer = messageRecoverer; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Set the {@link RabbitProperties} to use. |
| 65 | + * @param rabbitProperties the {@link RabbitProperties} |
| 66 | + */ |
| 67 | + protected void setRabbitProperties(RabbitProperties rabbitProperties) { |
| 68 | + this.rabbitProperties = rabbitProperties; |
| 69 | + } |
| 70 | + |
| 71 | + protected final RabbitProperties getRabbitProperties() { |
| 72 | + return this.rabbitProperties; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Configure the specified rabbit listener container factory. The factory can be |
| 77 | + * further tuned and default settings can be overridden. |
| 78 | + * @param factory the {@link AbstractRabbitListenerContainerFactory} instance to |
| 79 | + * configure |
| 80 | + * @param connectionFactory the {@link ConnectionFactory} to use |
| 81 | + */ |
| 82 | + public abstract void configure(T factory, ConnectionFactory connectionFactory); |
| 83 | + |
| 84 | + |
| 85 | + protected void configure(T factory, ConnectionFactory connectionFactory, |
| 86 | + RabbitProperties.AmqpContainer configuration) { |
| 87 | + Assert.notNull(factory, "Factory must not be null"); |
| 88 | + Assert.notNull(connectionFactory, "ConnectionFactory must not be null"); |
| 89 | + Assert.notNull(configuration, "Configuration must not be null"); |
| 90 | + factory.setConnectionFactory(connectionFactory); |
| 91 | + if (this.messageConverter != null) { |
| 92 | + factory.setMessageConverter(this.messageConverter); |
| 93 | + } |
| 94 | + factory.setAutoStartup(configuration.isAutoStartup()); |
| 95 | + if (configuration.getAcknowledgeMode() != null) { |
| 96 | + factory.setAcknowledgeMode(configuration.getAcknowledgeMode()); |
| 97 | + } |
| 98 | + if (configuration.getPrefetch() != null) { |
| 99 | + factory.setPrefetchCount(configuration.getPrefetch()); |
| 100 | + } |
| 101 | + if (configuration.getDefaultRequeueRejected() != null) { |
| 102 | + factory.setDefaultRequeueRejected(configuration.getDefaultRequeueRejected()); |
| 103 | + } |
| 104 | + if (configuration.getIdleEventInterval() != null) { |
| 105 | + factory.setIdleEventInterval(configuration.getIdleEventInterval()); |
| 106 | + } |
| 107 | + ListenerRetry retryConfig = configuration.getRetry(); |
| 108 | + if (retryConfig.isEnabled()) { |
| 109 | + RetryInterceptorBuilder<?> builder = (retryConfig.isStateless() |
| 110 | + ? RetryInterceptorBuilder.stateless() |
| 111 | + : RetryInterceptorBuilder.stateful()); |
| 112 | + builder.maxAttempts(retryConfig.getMaxAttempts()); |
| 113 | + builder.backOffOptions(retryConfig.getInitialInterval(), |
| 114 | + retryConfig.getMultiplier(), retryConfig.getMaxInterval()); |
| 115 | + MessageRecoverer recoverer = (this.messageRecoverer != null |
| 116 | + ? this.messageRecoverer : new RejectAndDontRequeueRecoverer()); |
| 117 | + builder.recoverer(recoverer); |
| 118 | + factory.setAdviceChain(builder.build()); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments