|
| 1 | +/* |
| 2 | + * Copyright 2020-2020 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.cloud.task.batch.autoconfigure.rabbit; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import org.springframework.amqp.core.AmqpTemplate; |
| 22 | +import org.springframework.amqp.core.Queue; |
| 23 | +import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; |
| 24 | +import org.springframework.amqp.support.converter.MessageConverter; |
| 25 | +import org.springframework.batch.item.amqp.AmqpItemReader; |
| 26 | +import org.springframework.batch.item.amqp.builder.AmqpItemReaderBuilder; |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
| 29 | +import org.springframework.boot.autoconfigure.amqp.RabbitProperties; |
| 30 | +import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration; |
| 31 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| 32 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 33 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 34 | +import org.springframework.context.annotation.Bean; |
| 35 | +import org.springframework.context.annotation.Configuration; |
| 36 | +import org.springframework.util.StringUtils; |
| 37 | + |
| 38 | +/** |
| 39 | + * Autconfiguration for a {@code AmqpItemReader}. |
| 40 | + * |
| 41 | + * @author Glenn Renfro |
| 42 | + * @since 2.3 |
| 43 | + */ |
| 44 | +@Configuration |
| 45 | +@EnableConfigurationProperties(AmqpItemReaderProperties.class) |
| 46 | +@AutoConfigureAfter(BatchAutoConfiguration.class) |
| 47 | +@ConditionalOnProperty(name = "spring.batch.job.amqpitemreader.enabled", |
| 48 | + havingValue = "true", matchIfMissing = false) |
| 49 | +public class AmqpItemReaderAutoConfiguration { |
| 50 | + |
| 51 | + @Autowired(required = false) |
| 52 | + private RabbitProperties rabbitProperties; |
| 53 | + |
| 54 | + @Bean |
| 55 | + public AmqpItemReaderProperties amqpItemReaderProperties() { |
| 56 | + return new AmqpItemReaderProperties(); |
| 57 | + } |
| 58 | + |
| 59 | + @ConditionalOnBean(RabbitProperties.class) |
| 60 | + @Bean |
| 61 | + public Queue defaultQueue() { |
| 62 | + if (!StringUtils |
| 63 | + .hasText(this.rabbitProperties.getTemplate().getDefaultReceiveQueue())) { |
| 64 | + throw new IllegalArgumentException( |
| 65 | + "DefaultReceiveQueue must not be empty nor null"); |
| 66 | + } |
| 67 | + return new Queue(this.rabbitProperties.getTemplate().getDefaultReceiveQueue(), |
| 68 | + true); |
| 69 | + } |
| 70 | + |
| 71 | + @Bean |
| 72 | + public AmqpItemReader<Map<Object, Object>> amqpItemReader(AmqpTemplate amqpTemplate, |
| 73 | + @Autowired(required = false) Class itemType) { |
| 74 | + AmqpItemReaderBuilder<Map<Object, Object>> builder = new AmqpItemReaderBuilder<Map<Object, Object>>() |
| 75 | + .amqpTemplate(amqpTemplate); |
| 76 | + if (itemType != null) { |
| 77 | + builder.itemType(itemType); |
| 78 | + } |
| 79 | + return builder.build(); |
| 80 | + } |
| 81 | + |
| 82 | + @ConditionalOnProperty(name = "spring.batch.job.amqpitemreader.jsonConverterEnabled", |
| 83 | + havingValue = "true", matchIfMissing = true) |
| 84 | + @Bean |
| 85 | + public MessageConverter messageConverter() { |
| 86 | + return new Jackson2JsonMessageConverter(); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments