Skip to content

Commit 688fd00

Browse files
committed
Refine nullability for the RabbitMessageOperations
Related to: #3110 * Also, move some delegation methods from the `RabbitMessagingTemplate` to `default` impls in the `RabbitMessageOperations`
1 parent b7f95cf commit 688fd00

File tree

2 files changed

+43
-58
lines changed

2 files changed

+43
-58
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitMessageOperations.java

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
* routing key to use.
3434
*
3535
* @author Stephane Nicoll
36+
* @author Artem Bilan
37+
*
3638
* @since 1.4
39+
*
3740
* @see org.springframework.amqp.rabbit.core.RabbitTemplate
3841
*/
3942
public interface RabbitMessageOperations extends MessageSendingOperations<String>,
@@ -46,7 +49,7 @@ public interface RabbitMessageOperations extends MessageSendingOperations<String
4649
* @param message the message to send
4750
* @throws MessagingException a messaging exception.
4851
*/
49-
void send(String exchange, String routingKey, Message<?> message) throws MessagingException;
52+
void send(@Nullable String exchange, @Nullable String routingKey, Message<?> message) throws MessagingException;
5053

5154
/**
5255
* Convert the given Object to serialized form, possibly using a
@@ -58,7 +61,11 @@ public interface RabbitMessageOperations extends MessageSendingOperations<String
5861
* @param payload the Object to use as payload
5962
* @throws MessagingException a messaging exception.
6063
*/
61-
void convertAndSend(String exchange, String routingKey, Object payload) throws MessagingException;
64+
default void convertAndSend(@Nullable String exchange, @Nullable String routingKey, Object payload)
65+
throws MessagingException {
66+
67+
convertAndSend(exchange, routingKey, payload, null, null);
68+
}
6269

6370
/**
6471
* Convert the given Object to serialized form, possibly using a
@@ -71,8 +78,11 @@ public interface RabbitMessageOperations extends MessageSendingOperations<String
7178
* @param headers headers for the message to send
7279
* @throws MessagingException a messaging exception.
7380
*/
74-
void convertAndSend(String exchange, String routingKey, Object payload, Map<String, Object> headers)
75-
throws MessagingException;
81+
default void convertAndSend(@Nullable String exchange, @Nullable String routingKey, Object payload,
82+
@Nullable Map<String, Object> headers) throws MessagingException {
83+
84+
convertAndSend(exchange, routingKey, payload, headers, null);
85+
}
7686

7787
/**
7888
* Convert the given Object to serialized form, possibly using a
@@ -86,8 +96,11 @@ void convertAndSend(String exchange, String routingKey, Object payload, Map<Stri
8696
* @param postProcessor the post processor to apply to the message
8797
* @throws MessagingException a messaging exception.
8898
*/
89-
void convertAndSend(String exchange, String routingKey, Object payload, MessagePostProcessor postProcessor)
90-
throws MessagingException;
99+
default void convertAndSend(@Nullable String exchange, @Nullable String routingKey, Object payload,
100+
@Nullable MessagePostProcessor postProcessor) throws MessagingException {
101+
102+
convertAndSend(exchange, routingKey, payload, null, postProcessor);
103+
}
91104

92105
/**
93106
* Convert the given Object to serialized form, possibly using a
@@ -102,8 +115,9 @@ void convertAndSend(String exchange, String routingKey, Object payload, MessageP
102115
* @param postProcessor the post processor to apply to the message
103116
* @throws MessagingException a messaging exception.
104117
*/
105-
void convertAndSend(String exchange, String routingKey, Object payload,
106-
@Nullable Map<String, Object> headers, @Nullable MessagePostProcessor postProcessor) throws MessagingException;
118+
void convertAndSend(@Nullable String exchange, @Nullable String routingKey, Object payload,
119+
@Nullable Map<String, Object> headers, @Nullable MessagePostProcessor postProcessor)
120+
throws MessagingException;
107121

108122
/**
109123
* Send a request message to a specific exchange with a specific routing key and
@@ -116,7 +130,8 @@ void convertAndSend(String exchange, String routingKey, Object payload,
116130
* @throws MessagingException a messaging exception.
117131
*/
118132
@Nullable
119-
Message<?> sendAndReceive(String exchange, String routingKey, Message<?> requestMessage) throws MessagingException;
133+
Message<?> sendAndReceive(@Nullable String exchange, @Nullable String routingKey, Message<?> requestMessage)
134+
throws MessagingException;
120135

121136
/**
122137
* Convert the given request Object to serialized form, possibly using a
@@ -132,8 +147,11 @@ void convertAndSend(String exchange, String routingKey, Object payload,
132147
* could not be received, for example due to a timeout
133148
* @throws MessagingException a messaging exception.
134149
*/
135-
<T> @Nullable T convertSendAndReceive(String exchange, String routingKey, Object request, Class<T> targetClass)
136-
throws MessagingException;
150+
default <T> @Nullable T convertSendAndReceive(@Nullable String exchange, @Nullable String routingKey,
151+
Object request, Class<T> targetClass) throws MessagingException {
152+
153+
return convertSendAndReceive(exchange, routingKey, request, null, targetClass);
154+
}
137155

138156
/**
139157
* Convert the given request Object to serialized form, possibly using a
@@ -151,8 +169,11 @@ void convertAndSend(String exchange, String routingKey, Object payload,
151169
* could not be received, for example due to a timeout
152170
* @throws MessagingException a messaging exception.
153171
*/
154-
<T> @Nullable T convertSendAndReceive(String exchange, String routingKey, Object request,
155-
@Nullable Map<String, Object> headers, Class<T> targetClass) throws MessagingException;
172+
default <T> @Nullable T convertSendAndReceive(@Nullable String exchange, @Nullable String routingKey,
173+
Object request, @Nullable Map<String, Object> headers, Class<T> targetClass) throws MessagingException {
174+
175+
return convertSendAndReceive(exchange, routingKey, request, headers, targetClass, null);
176+
}
156177

157178
/**
158179
* Convert the given request Object to serialized form, possibly using a
@@ -170,8 +191,12 @@ void convertAndSend(String exchange, String routingKey, Object payload,
170191
* could not be received, for example due to a timeout
171192
* @throws MessagingException a messaging exception.
172193
*/
173-
<T> @Nullable T convertSendAndReceive(String exchange, String routingKey, Object request, Class<T> targetClass,
174-
@Nullable MessagePostProcessor requestPostProcessor) throws MessagingException;
194+
default <T> @Nullable T convertSendAndReceive(@Nullable String exchange, @Nullable String routingKey,
195+
Object request, Class<T> targetClass, @Nullable MessagePostProcessor requestPostProcessor)
196+
throws MessagingException {
197+
198+
return convertSendAndReceive(exchange, routingKey, request, null, targetClass, requestPostProcessor);
199+
}
175200

176201
/**
177202
* Convert the given request Object to serialized form, possibly using a
@@ -191,7 +216,7 @@ void convertAndSend(String exchange, String routingKey, Object payload,
191216
* could not be received, for example due to a timeout
192217
* @throws MessagingException a messaging exception.
193218
*/
194-
<T> @Nullable T convertSendAndReceive(String exchange, String routingKey, Object request,
219+
<T> @Nullable T convertSendAndReceive(@Nullable String exchange, @Nullable String routingKey, Object request,
195220
@Nullable Map<String, Object> headers, Class<T> targetClass,
196221
@Nullable MessagePostProcessor requestPostProcessor) throws MessagingException;
197222

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitMessagingTemplate.java

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
*
3939
* @author Stephane Nicoll
4040
* @author Gary Russell
41+
* @author Artem Bilan
42+
*
4143
* @since 1.4
4244
*/
4345
public class RabbitMessagingTemplate extends AbstractMessagingTemplate<String>
@@ -139,27 +141,6 @@ public void send(@Nullable String exchange, @Nullable String routingKey, Message
139141
doSend(exchange, routingKey, message);
140142
}
141143

142-
@Override
143-
public void convertAndSend(@Nullable String exchange, @Nullable String routingKey, Object payload)
144-
throws MessagingException {
145-
146-
convertAndSend(exchange, routingKey, payload, (Map<String, Object>) null);
147-
}
148-
149-
@Override
150-
public void convertAndSend(@Nullable String exchange, @Nullable String routingKey, Object payload,
151-
@Nullable Map<String, Object> headers) throws MessagingException {
152-
153-
convertAndSend(exchange, routingKey, payload, headers, null);
154-
}
155-
156-
@Override
157-
public void convertAndSend(@Nullable String exchange, @Nullable String routingKey, Object payload,
158-
@Nullable MessagePostProcessor postProcessor) throws MessagingException {
159-
160-
convertAndSend(exchange, routingKey, payload, null, postProcessor);
161-
}
162-
163144
@Override
164145
public void convertAndSend(@Nullable String exchange, @Nullable String routingKey, Object payload,
165146
@Nullable Map<String, Object> headers, @Nullable MessagePostProcessor postProcessor)
@@ -176,27 +157,6 @@ public void convertAndSend(@Nullable String exchange, @Nullable String routingKe
176157
return doSendAndReceive(exchange, routingKey, requestMessage);
177158
}
178159

179-
@Override
180-
public <T> @Nullable T convertSendAndReceive(@Nullable String exchange, @Nullable String routingKey, Object request,
181-
Class<T> targetClass) throws MessagingException {
182-
183-
return convertSendAndReceive(exchange, routingKey, request, null, targetClass);
184-
}
185-
186-
@Override
187-
public <T> @Nullable T convertSendAndReceive(@Nullable String exchange, @Nullable String routingKey, Object request,
188-
@Nullable Map<String, Object> headers, Class<T> targetClass) throws MessagingException {
189-
190-
return convertSendAndReceive(exchange, routingKey, request, headers, targetClass, null);
191-
}
192-
193-
@Override
194-
public <T> @Nullable T convertSendAndReceive(@Nullable String exchange, @Nullable String routingKey, Object request,
195-
Class<T> targetClass, @Nullable MessagePostProcessor requestPostProcessor) throws MessagingException {
196-
197-
return convertSendAndReceive(exchange, routingKey, request, null, targetClass, requestPostProcessor);
198-
}
199-
200160
@SuppressWarnings("unchecked")
201161
@Override
202162
public <T> @Nullable T convertSendAndReceive(@Nullable String exchange, @Nullable String routingKey, Object request,

0 commit comments

Comments
 (0)