Skip to content

Commit 1d290a3

Browse files
garyrussellartembilan
authored andcommitted
Reduce factory method complexity
- proposed new model for non-null property propagation * Polishing - PR comments; extract to utility and provide chaining. * Javadocs for INSTANCE. * Fix Javadocs (checkstyle) * Configure direct containers the same way. * Returning this is more efficient than INSTANCE.
1 parent 4614f73 commit 1d290a3

File tree

3 files changed

+93
-46
lines changed

3 files changed

+93
-46
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2019 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.amqp.utils;
18+
19+
import java.util.function.Consumer;
20+
21+
/**
22+
* Chained utility methods to simplify some Java repetitive code. Obtain a reference to
23+
* the singleton {@link #INSTANCE} and then chain calls to the utility methods.
24+
*
25+
* @author Gary Russell
26+
* @since 2.1.4
27+
*
28+
*/
29+
public final class JavaUtils {
30+
31+
/**
32+
* The singleton instance of this utility class.
33+
*/
34+
public static final JavaUtils INSTANCE = new JavaUtils();
35+
36+
private JavaUtils() {
37+
super();
38+
}
39+
40+
/**
41+
* Invoke {@link Consumer#accept(Object)} with the value if the condition is true.
42+
* @param condition the condition.
43+
* @param value the value.
44+
* @param consumer the consumer.
45+
* @param <T> the value type.
46+
* @return this.
47+
*/
48+
public <T> JavaUtils acceptIfCondition(boolean condition, T value, Consumer<T> consumer) {
49+
if (condition) {
50+
consumer.accept(value);
51+
}
52+
return this;
53+
}
54+
55+
/**
56+
* Invoke {@link Consumer#accept(Object)} with the value if it is not null.
57+
* @param value the value.
58+
* @param consumer the consumer.
59+
* @param <T> the value type.
60+
* @return this.
61+
*/
62+
public <T> JavaUtils acceptIfNotNull(T value, Consumer<T> consumer) {
63+
if (value != null) {
64+
consumer.accept(value);
65+
}
66+
return this;
67+
}
68+
69+
}

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/config/DirectRabbitListenerContainerFactory.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer;
2020
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpoint;
21+
import org.springframework.amqp.utils.JavaUtils;
2122
import org.springframework.scheduling.TaskScheduler;
2223

2324
/**
@@ -104,12 +105,10 @@ protected DirectMessageListenerContainer createContainerInstance() {
104105
protected void initializeContainer(DirectMessageListenerContainer instance, RabbitListenerEndpoint endpoint) {
105106
super.initializeContainer(instance, endpoint);
106107

107-
if (this.taskScheduler != null) {
108-
instance.setTaskScheduler(this.taskScheduler);
109-
}
110-
if (this.monitorInterval != null) {
111-
instance.setMonitorInterval(this.monitorInterval);
112-
}
108+
JavaUtils javaUtils = JavaUtils.INSTANCE.acceptIfNotNull(this.taskScheduler, instance::setTaskScheduler)
109+
.acceptIfNotNull(this.monitorInterval, instance::setMonitorInterval)
110+
.acceptIfNotNull(this.messagesPerAck, instance::setMessagesPerAck)
111+
.acceptIfNotNull(this.ackTimeout, instance::setAckTimeout);
113112
if (endpoint != null && endpoint.getConcurrency() != null) {
114113
try {
115114
instance.setConsumersPerQueue(Integer.parseInt(endpoint.getConcurrency()));
@@ -118,14 +117,8 @@ protected void initializeContainer(DirectMessageListenerContainer instance, Rabb
118117
throw new IllegalStateException("Failed to parse concurrency: " + e.getMessage(), e);
119118
}
120119
}
121-
else if (this.consumersPerQueue != null) {
122-
instance.setConsumersPerQueue(this.consumersPerQueue);
123-
}
124-
if (this.messagesPerAck != null) {
125-
instance.setMessagesPerAck(this.messagesPerAck);
126-
}
127-
if (this.ackTimeout != null) {
128-
instance.setAckTimeout(this.ackTimeout);
120+
else {
121+
javaUtils.acceptIfNotNull(this.consumersPerQueue, instance::setConsumersPerQueue);
129122
}
130123
}
131124

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/config/SimpleRabbitListenerContainerFactory.java

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2018 the original author or authors.
2+
* Copyright 2014-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpoint;
2020
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
21+
import org.springframework.amqp.utils.JavaUtils;
2122

2223
/**
2324
* A {@link org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory}
@@ -134,44 +135,28 @@ protected SimpleMessageListenerContainer createContainerInstance() {
134135
return new SimpleMessageListenerContainer();
135136
}
136137

137-
@Override // NOSONAR complexity = mostly null checks
138+
@Override
138139
protected void initializeContainer(SimpleMessageListenerContainer instance, RabbitListenerEndpoint endpoint) {
139140
super.initializeContainer(instance, endpoint);
140141

141-
if (this.txSize != null) {
142-
instance.setTxSize(this.txSize);
143-
}
142+
JavaUtils javaUtils = JavaUtils.INSTANCE
143+
.acceptIfNotNull(this.txSize, instance::setTxSize);
144144
String concurrency = null;
145145
if (endpoint != null) {
146146
concurrency = endpoint.getConcurrency();
147-
if (concurrency != null) {
148-
instance.setConcurrency(concurrency);
149-
}
150-
}
151-
if (concurrency == null && this.concurrentConsumers != null) {
152-
instance.setConcurrentConsumers(this.concurrentConsumers);
153-
}
154-
if ((concurrency == null || !(concurrency.contains("-"))) && this.maxConcurrentConsumers != null) {
155-
instance.setMaxConcurrentConsumers(this.maxConcurrentConsumers);
156-
}
157-
if (this.startConsumerMinInterval != null) {
158-
instance.setStartConsumerMinInterval(this.startConsumerMinInterval);
159-
}
160-
if (this.stopConsumerMinInterval != null) {
161-
instance.setStopConsumerMinInterval(this.stopConsumerMinInterval);
162-
}
163-
if (this.consecutiveActiveTrigger != null) {
164-
instance.setConsecutiveActiveTrigger(this.consecutiveActiveTrigger);
165-
}
166-
if (this.consecutiveIdleTrigger != null) {
167-
instance.setConsecutiveIdleTrigger(this.consecutiveIdleTrigger);
168-
}
169-
if (this.receiveTimeout != null) {
170-
instance.setReceiveTimeout(this.receiveTimeout);
171-
}
172-
if (this.deBatchingEnabled != null) {
173-
instance.setDeBatchingEnabled(this.deBatchingEnabled);
147+
javaUtils.acceptIfNotNull(concurrency, instance::setConcurrency);
174148
}
149+
javaUtils
150+
.acceptIfCondition(concurrency == null && this.concurrentConsumers != null, this.concurrentConsumers,
151+
instance::setConcurrentConsumers)
152+
.acceptIfCondition((concurrency == null || !(concurrency.contains("-"))) && this.maxConcurrentConsumers != null,
153+
this.maxConcurrentConsumers, instance::setMaxConcurrentConsumers)
154+
.acceptIfNotNull(this.startConsumerMinInterval, instance::setStartConsumerMinInterval)
155+
.acceptIfNotNull(this.stopConsumerMinInterval, instance::setStopConsumerMinInterval)
156+
.acceptIfNotNull(this.consecutiveActiveTrigger, instance::setConsecutiveActiveTrigger)
157+
.acceptIfNotNull(this.consecutiveIdleTrigger, instance::setConsecutiveIdleTrigger)
158+
.acceptIfNotNull(this.receiveTimeout, instance::setReceiveTimeout)
159+
.acceptIfNotNull(this.deBatchingEnabled, instance::setDeBatchingEnabled);
175160
}
176161

177162
}

0 commit comments

Comments
 (0)