Skip to content

Commit 2431118

Browse files
committed
Apply Nullability to JMS module
related to: #10083 Signed-off-by: Jiandong Ma <[email protected]>
1 parent c1efa8c commit 2431118

22 files changed

+175
-140
lines changed

spring-integration-jms/src/main/java/org/springframework/integration/jms/ChannelPublishingJmsMessageListener.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import jakarta.jms.JMSException;
2727
import jakarta.jms.MessageProducer;
2828
import jakarta.jms.Session;
29+
import org.jspecify.annotations.Nullable;
2930

3031
import org.springframework.beans.BeansException;
3132
import org.springframework.beans.factory.BeanFactory;
@@ -56,7 +57,6 @@
5657
import org.springframework.jms.support.converter.SimpleMessageConverter;
5758
import org.springframework.jms.support.destination.DestinationResolver;
5859
import org.springframework.jms.support.destination.DynamicDestinationResolver;
59-
import org.springframework.lang.Nullable;
6060
import org.springframework.messaging.Message;
6161
import org.springframework.messaging.MessageChannel;
6262
import org.springframework.messaging.MessagingException;
@@ -96,9 +96,9 @@ public class ChannelPublishingJmsMessageListener
9696

9797
private boolean extractReplyPayload = true;
9898

99-
private Object defaultReplyDestination;
99+
private @Nullable Object defaultReplyDestination;
100100

101-
private String correlationKey;
101+
private @Nullable String correlationKey;
102102

103103
private long replyTimeToLive = jakarta.jms.Message.DEFAULT_TIME_TO_LIVE;
104104

@@ -110,14 +110,15 @@ public class ChannelPublishingJmsMessageListener
110110

111111
private DestinationResolver destinationResolver = new DynamicDestinationResolver();
112112

113-
private Expression replyToExpression;
113+
private @Nullable Expression replyToExpression;
114114

115115
private JmsHeaderMapper headerMapper = new DefaultJmsHeaderMapper();
116116

117-
private BeanFactory beanFactory;
117+
private @Nullable BeanFactory beanFactory;
118118

119119
private MessageBuilderFactory messageBuilderFactory = new DefaultMessageBuilderFactory();
120120

121+
@SuppressWarnings("NullAway.Init")
121122
private StandardEvaluationContext evaluationContext;
122123

123124
/**
@@ -174,7 +175,7 @@ public void setShouldTrack(boolean shouldTrack) {
174175
}
175176

176177
@Override
177-
public String getComponentName() {
178+
public @Nullable String getComponentName() {
178179
return this.gatewayDelegate.getComponentName();
179180
}
180181

@@ -563,7 +564,7 @@ else if (replyToValue instanceof String destinationName) {
563564
* @see #setDefaultReplyTopicName
564565
* @see #setDestinationResolver
565566
*/
566-
private Destination resolveDefaultReplyDestination(Session session) throws JMSException {
567+
private @Nullable Destination resolveDefaultReplyDestination(Session session) throws JMSException {
567568
if (this.defaultReplyDestination instanceof Destination destination) {
568569
return destination;
569570
}
@@ -600,7 +601,7 @@ private record DestinationNameHolder(String name, boolean isTopic) {
600601

601602
private class GatewayDelegate extends MessagingGatewaySupport {
602603

603-
private static final ThreadLocal<AttributeAccessor> ATTRIBUTES_HOLDER = new ThreadLocal<>();
604+
private static final ThreadLocal<@Nullable AttributeAccessor> ATTRIBUTES_HOLDER = new ThreadLocal<>();
604605

605606
@Nullable
606607
private RetryOperations retryTemplate;
@@ -621,7 +622,9 @@ private void send(jakarta.jms.Message jmsMessage, Message<?> requestMessage) {
621622
else {
622623
this.retryTemplate.execute(
623624
context -> {
624-
StaticMessageHeaderAccessor.getDeliveryAttempt(requestMessage).incrementAndGet();
625+
var deliveryAttempt = StaticMessageHeaderAccessor.getDeliveryAttempt(requestMessage);
626+
Assert.notNull(deliveryAttempt, "deliveryAttempt must not be null");
627+
deliveryAttempt.incrementAndGet();
625628
setAttributesIfNecessary(jmsMessage, requestMessage);
626629
send(requestMessage);
627630
return null;
@@ -635,7 +638,7 @@ private void send(jakarta.jms.Message jmsMessage, Message<?> requestMessage) {
635638
}
636639
}
637640

638-
private Message<?> sendAndReceiveMessage(jakarta.jms.Message jmsMessage, Message<?> requestMessage) {
641+
private @Nullable Message<?> sendAndReceiveMessage(jakarta.jms.Message jmsMessage, Message<?> requestMessage) {
639642
try {
640643
if (this.retryTemplate == null) {
641644
setAttributesIfNecessary(jmsMessage, requestMessage);
@@ -644,7 +647,9 @@ private Message<?> sendAndReceiveMessage(jakarta.jms.Message jmsMessage, Message
644647
else {
645648
return this.retryTemplate.execute(
646649
context -> {
647-
StaticMessageHeaderAccessor.getDeliveryAttempt(requestMessage).incrementAndGet();
650+
var deliveryAttempt = StaticMessageHeaderAccessor.getDeliveryAttempt(requestMessage);
651+
Assert.notNull(deliveryAttempt, "deliveryAttempt must not be null");
652+
deliveryAttempt.incrementAndGet();
648653
setAttributesIfNecessary(jmsMessage, requestMessage);
649654
return sendAndReceiveMessage(requestMessage);
650655
}, this.recoveryCallback);

spring-integration-jms/src/main/java/org/springframework/integration/jms/DynamicJmsTemplate.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.jms;
1818

1919
import jakarta.jms.ConnectionFactory;
20+
import org.jspecify.annotations.Nullable;
2021

2122
import org.springframework.jms.connection.CachingConnectionFactory;
2223
import org.springframework.jms.core.JmsTemplate;
@@ -51,7 +52,7 @@ public void setReceiveTimeout(long receiveTimeout) {
5152
}
5253

5354
@Override
54-
public void setConnectionFactory(ConnectionFactory connectionFactory) {
55+
public void setConnectionFactory(@Nullable ConnectionFactory connectionFactory) {
5556
super.setConnectionFactory(connectionFactory);
5657
if (!this.receiveTimeoutExplicitlySet) {
5758
if (connectionFactory instanceof CachingConnectionFactory cachingConnectionFactory &&

spring-integration-jms/src/main/java/org/springframework/integration/jms/DynamicJmsTemplateProperties.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.integration.jms;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
/**
2022
* @author Mark Fisher
2123
* @author Artem Bilan
@@ -24,30 +26,30 @@
2426
*/
2527
abstract class DynamicJmsTemplateProperties {
2628

27-
private static final ThreadLocal<Integer> PRIORITY_HOLDER = new ThreadLocal<>();
29+
private static final ThreadLocal<@Nullable Integer> PRIORITY_HOLDER = new ThreadLocal<>();
2830

29-
private static final ThreadLocal<Long> RECEIVE_TIMEOUT_HOLDER = new ThreadLocal<>();
31+
private static final ThreadLocal<@Nullable Long> RECEIVE_TIMEOUT_HOLDER = new ThreadLocal<>();
3032

31-
private static final ThreadLocal<Integer> DELIVER_MODE_HOLDER = new ThreadLocal<>();
33+
private static final ThreadLocal<@Nullable Integer> DELIVER_MODE_HOLDER = new ThreadLocal<>();
3234

33-
private static final ThreadLocal<Long> TIME_TO_LIVE_HOLDER = new ThreadLocal<>();
35+
private static final ThreadLocal<@Nullable Long> TIME_TO_LIVE_HOLDER = new ThreadLocal<>();
3436

3537
private DynamicJmsTemplateProperties() {
3638
}
3739

38-
public static Integer getPriority() {
40+
public static @Nullable Integer getPriority() {
3941
return PRIORITY_HOLDER.get();
4042
}
4143

42-
public static void setPriority(Integer priority) {
44+
public static void setPriority(@Nullable Integer priority) {
4345
PRIORITY_HOLDER.set(priority);
4446
}
4547

4648
public static void clearPriority() {
4749
PRIORITY_HOLDER.remove();
4850
}
4951

50-
public static Long getReceiveTimeout() {
52+
public static @Nullable Long getReceiveTimeout() {
5153
return RECEIVE_TIMEOUT_HOLDER.get();
5254
}
5355

@@ -59,7 +61,7 @@ public static void clearReceiveTimeout() {
5961
RECEIVE_TIMEOUT_HOLDER.remove();
6062
}
6163

62-
public static Integer getDeliveryMode() {
64+
public static @Nullable Integer getDeliveryMode() {
6365
return DELIVER_MODE_HOLDER.get();
6466
}
6567

@@ -71,7 +73,7 @@ public static void clearDeliveryMode() {
7173
DELIVER_MODE_HOLDER.remove();
7274
}
7375

74-
public static Long getTimeToLive() {
76+
public static @Nullable Long getTimeToLive() {
7577
return TIME_TO_LIVE_HOLDER.get();
7678
}
7779

spring-integration-jms/src/main/java/org/springframework/integration/jms/JmsDestinationPollingSource.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Map;
2020

2121
import jakarta.jms.Destination;
22+
import org.jspecify.annotations.Nullable;
2223

2324
import org.springframework.integration.endpoint.AbstractMessageSource;
2425
import org.springframework.integration.jms.util.JmsAdapterUtils;
@@ -43,15 +44,15 @@ public class JmsDestinationPollingSource extends AbstractMessageSource<Object> {
4344

4445
private final JmsTemplate jmsTemplate;
4546

46-
private volatile Destination destination;
47+
private volatile @Nullable Destination destination;
4748

48-
private volatile String destinationName;
49+
private volatile @Nullable String destinationName;
4950

50-
private volatile String messageSelector;
51+
private volatile @Nullable String messageSelector;
5152

5253
private volatile JmsHeaderMapper headerMapper = new DefaultJmsHeaderMapper();
5354

54-
private volatile String sessionAcknowledgeMode;
55+
private volatile @Nullable String sessionAcknowledgeMode;
5556

5657
private volatile boolean extractPayload = true;
5758

@@ -121,7 +122,7 @@ protected void onInit() {
121122
* {@link JmsHeaderMapper} instance to map JMS properties to the MessageHeaders.
122123
*/
123124
@Override
124-
protected Object doReceive() {
125+
protected @Nullable Object doReceive() {
125126
jakarta.jms.Message jmsMessage = doReceiveJmsMessage();
126127
if (jmsMessage == null) {
127128
return null;
@@ -147,7 +148,7 @@ protected Object doReceive() {
147148
}
148149
}
149150

150-
private jakarta.jms.Message doReceiveJmsMessage() {
151+
private jakarta.jms.@Nullable Message doReceiveJmsMessage() {
151152
jakarta.jms.Message jmsMessage;
152153
if (this.destination != null) {
153154
jmsMessage = this.jmsTemplate.receiveSelected(this.destination, this.messageSelector);

spring-integration-jms/src/main/java/org/springframework/integration/jms/JmsInboundGateway.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.jms;
1818

1919
import io.micrometer.observation.ObservationRegistry;
20+
import org.jspecify.annotations.Nullable;
2021

2122
import org.springframework.beans.BeansException;
2223
import org.springframework.beans.factory.BeanFactory;
@@ -132,7 +133,7 @@ public void registerObservationRegistry(ObservationRegistry observationRegistry)
132133
}
133134

134135
@Override
135-
public void setObservationConvention(MessageRequestReplyReceiverObservationConvention observationConvention) {
136+
public void setObservationConvention(@Nullable MessageRequestReplyReceiverObservationConvention observationConvention) {
136137
super.setObservationConvention(observationConvention);
137138
this.endpoint.getListener().setRequestReplyObservationConvention(observationConvention);
138139
}
@@ -156,7 +157,8 @@ public void setBeanFactory(BeanFactory beanFactory) {
156157

157158
@Override
158159
protected void onInit() {
159-
this.endpoint.setComponentName(getComponentName());
160+
String componentName = getComponentName();
161+
this.endpoint.setComponentName(componentName == null ? "jms-inbound-gateway" : componentName);
160162
this.endpoint.afterPropertiesSet();
161163
}
162164

spring-integration-jms/src/main/java/org/springframework/integration/jms/JmsMessageDrivenEndpoint.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.jms;
1818

1919
import io.micrometer.observation.ObservationRegistry;
20+
import org.jspecify.annotations.Nullable;
2021

2122
import org.springframework.beans.BeansException;
2223
import org.springframework.context.ApplicationContext;
@@ -48,7 +49,7 @@ public class JmsMessageDrivenEndpoint extends MessageProducerSupport implements
4849

4950
private final ChannelPublishingJmsMessageListener listener;
5051

51-
private String sessionAcknowledgeMode;
52+
private @Nullable String sessionAcknowledgeMode;
5253

5354
private boolean shutdownContainerOnStop = true;
5455

@@ -175,7 +176,7 @@ public void registerObservationRegistry(ObservationRegistry observationRegistry)
175176
}
176177

177178
@Override
178-
public void setObservationConvention(MessageReceiverObservationConvention observationConvention) {
179+
public void setObservationConvention(@Nullable MessageReceiverObservationConvention observationConvention) {
179180
super.setObservationConvention(observationConvention);
180181
this.listener.setReceiverObservationConvention(observationConvention);
181182
}
@@ -218,7 +219,8 @@ protected void onInit() {
218219
this.listenerContainer.setSessionAcknowledgeMode(acknowledgeMode);
219220
}
220221
}
221-
this.listener.setComponentName(getComponentName());
222+
String componentName = getComponentName();
223+
this.listener.setComponentName(componentName == null ? "jms-message-listener" : componentName);
222224
}
223225

224226
@Override

0 commit comments

Comments
 (0)