Skip to content

Commit 5fa4a20

Browse files
cppwfsmminella
authored andcommitted
Add AMQPItemReader
This adds autoconfiguration for an AMQPItemReader to the single step batch job starter. resolves TASK-680A
1 parent 7223f60 commit 5fa4a20

File tree

5 files changed

+452
-1
lines changed

5 files changed

+452
-1
lines changed

spring-cloud-starter-single-step-batch-job/pom.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
<artifactId>spring-cloud-starter-single-step-batch-job</artifactId>
1111

12+
<properties>
13+
<test.containers.version>1.14.3</test.containers.version>
14+
<test.rabbit.containers.version>1.14.3</test.rabbit.containers.version>
15+
<test.ducttape.version>1.0.8</test.ducttape.version>
16+
</properties>
17+
1218
<dependencies>
1319
<dependency>
1420
<groupId>org.springframework.boot</groupId>
@@ -41,6 +47,40 @@
4147
<artifactId>h2</artifactId>
4248
<scope>test</scope>
4349
</dependency>
50+
<dependency>
51+
<groupId>org.springframework.amqp</groupId>
52+
<artifactId>spring-amqp</artifactId>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.springframework.amqp</groupId>
56+
<artifactId>spring-rabbit</artifactId>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.testcontainers</groupId>
60+
<artifactId>testcontainers</artifactId>
61+
<version>${test.containers.version}</version>
62+
<scope>test</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.testcontainers</groupId>
66+
<artifactId>rabbitmq</artifactId>
67+
<version>${test.rabbit.containers.version}</version>
68+
<scope>test</scope>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.rnorth.duct-tape</groupId>
72+
<artifactId>duct-tape</artifactId>
73+
<version>${test.ducttape.version}</version>
74+
<scope>test</scope>
75+
</dependency>
76+
<dependency>
77+
<groupId>com.fasterxml.jackson.core</groupId>
78+
<artifactId>jackson-core</artifactId>
79+
</dependency>
80+
<dependency>
81+
<groupId>com.fasterxml.jackson.core</groupId>
82+
<artifactId>jackson-annotations</artifactId>
83+
</dependency>
4484
</dependencies>
4585

4686
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
20+
import org.springframework.boot.context.properties.ConfigurationProperties;
21+
22+
/**
23+
* Properties to configure a {@code AmqpItemReader}.
24+
*
25+
* @author Glenn Renfro
26+
* @since 2.3
27+
*/
28+
@ConfigurationProperties(prefix = "spring.batch.job.amqpitemreader")
29+
public class AmqpItemReaderProperties {
30+
31+
private boolean enabled;
32+
33+
private boolean jsonConverterEnabled = true;
34+
35+
/**
36+
* The state of the enabled flag.
37+
* @return true if AmqpItemReader is enabled. Otherwise false.
38+
*/
39+
public boolean isEnabled() {
40+
return enabled;
41+
}
42+
43+
/**
44+
* Enables or disables the AmqpItemReader.
45+
* @param enabled if true then AmqpItemReader will be enabled. Defaults to false.
46+
*/
47+
public void setEnabled(boolean enabled) {
48+
this.enabled = enabled;
49+
}
50+
51+
/**
52+
* States whether the {@link Jackson2JsonMessageConverter} is used as a message
53+
* converter.
54+
* @return true if enabled else false.
55+
*/
56+
public boolean isJsonConverterEnabled() {
57+
return jsonConverterEnabled;
58+
}
59+
60+
/**
61+
* Establishes whether the {@link Jackson2JsonMessageConverter} is to be used as a
62+
* message converter.
63+
* @param jsonConverterEnabled true if it is to be enabled else false. Defaults to
64+
* true.
65+
*/
66+
public void setJsonConverterEnabled(boolean jsonConverterEnabled) {
67+
this.jsonConverterEnabled = jsonConverterEnabled;
68+
}
69+
70+
}

spring-cloud-starter-single-step-batch-job/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframewo
33
org.springframework.cloud.task.batch.autoconfigure.SingleStepJobAutoConfiguration,\
44
org.springframework.cloud.task.batch.autoconfigure.flatfile.FlatFileItemWriterAutoConfiguration, \
55
org.springframework.cloud.task.batch.autoconfigure.jdbc.JdbcItemWriterAutoConfiguration, \
6-
org.springframework.cloud.task.batch.autoconfigure.jdbc.JdbcCursorItemReaderAutoConfiguration
6+
org.springframework.cloud.task.batch.autoconfigure.jdbc.JdbcCursorItemReaderAutoConfiguration, \
7+
org.springframework.cloud.task.batch.autoconfigure.rabbit.AmqpItemReaderAutoConfiguration

0 commit comments

Comments
 (0)