33
33
import org .springframework .beans .factory .support .StaticListableBeanFactory ;
34
34
import org .springframework .jms .StubTextMessage ;
35
35
import org .springframework .jms .support .JmsHeaders ;
36
+ import org .springframework .jms .support .converter .MappingJackson2MessageConverter ;
36
37
import org .springframework .jms .support .converter .MessageConverter ;
38
+ import org .springframework .jms .support .converter .MessageType ;
37
39
import org .springframework .messaging .Message ;
38
40
import org .springframework .messaging .converter .MessageConversionException ;
39
41
import org .springframework .messaging .handler .annotation .support .DefaultMessageHandlerMethodFactory ;
@@ -137,9 +139,7 @@ public void replyUsesMessageConverterForPayload() throws JMSException {
137
139
MessageConverter messageConverter = mock (MessageConverter .class );
138
140
given (messageConverter .toMessage ("Response" , session )).willReturn (new StubTextMessage ("Response" ));
139
141
140
- Message <String > result = MessageBuilder .withPayload ("Response" )
141
- .build ();
142
-
142
+ Message <String > result = MessageBuilder .withPayload ("Response" ).build ();
143
143
MessagingMessageListenerAdapter listener = getSimpleInstance ("echo" , Message .class );
144
144
listener .setMessageConverter (messageConverter );
145
145
javax .jms .Message replyMessage = listener .buildMessage (session , result );
@@ -152,12 +152,10 @@ public void replyUsesMessageConverterForPayload() throws JMSException {
152
152
@ Test
153
153
public void replyPayloadToQueue () throws JMSException {
154
154
Message <String > request = MessageBuilder .withPayload ("Response" ).build ();
155
-
156
155
Session session = mock (Session .class );
157
156
Queue replyDestination = mock (Queue .class );
158
157
given (session .createQueue ("queueOut" )).willReturn (replyDestination );
159
158
160
-
161
159
MessageProducer messageProducer = mock (MessageProducer .class );
162
160
TextMessage responseMessage = mock (TextMessage .class );
163
161
given (session .createTextMessage ("Response" )).willReturn (responseMessage );
@@ -166,7 +164,6 @@ public void replyPayloadToQueue() throws JMSException {
166
164
MessagingMessageListenerAdapter listener = getPayloadInstance (request , "replyPayloadToQueue" , Message .class );
167
165
listener .onMessage (mock (javax .jms .Message .class ), session );
168
166
169
-
170
167
verify (session ).createQueue ("queueOut" );
171
168
verify (session ).createTextMessage ("Response" );
172
169
verify (messageProducer ).send (responseMessage );
@@ -176,12 +173,10 @@ public void replyPayloadToQueue() throws JMSException {
176
173
@ Test
177
174
public void replyPayloadToTopic () throws JMSException {
178
175
Message <String > request = MessageBuilder .withPayload ("Response" ).build ();
179
-
180
176
Session session = mock (Session .class );
181
177
Topic replyDestination = mock (Topic .class );
182
178
given (session .createTopic ("topicOut" )).willReturn (replyDestination );
183
179
184
-
185
180
MessageProducer messageProducer = mock (MessageProducer .class );
186
181
TextMessage responseMessage = mock (TextMessage .class );
187
182
given (session .createTextMessage ("Response" )).willReturn (responseMessage );
@@ -190,7 +185,6 @@ public void replyPayloadToTopic() throws JMSException {
190
185
MessagingMessageListenerAdapter listener = getPayloadInstance (request , "replyPayloadToTopic" , Message .class );
191
186
listener .onMessage (mock (javax .jms .Message .class ), session );
192
187
193
-
194
188
verify (session ).createTopic ("topicOut" );
195
189
verify (session ).createTextMessage ("Response" );
196
190
verify (messageProducer ).send (responseMessage );
@@ -240,6 +234,38 @@ public void replyPayloadNoDestination() throws JMSException {
240
234
verify (messageProducer ).close ();
241
235
}
242
236
237
+ @ Test
238
+ public void replyJackson () throws JMSException {
239
+ TextMessage reply = testReplyWithJackson ("replyJackson" ,
240
+ "{\" counter\" :42,\" name\" :\" Response\" ,\" description\" :\" lengthy description\" }" );
241
+ verify (reply ).setObjectProperty ("foo" , "bar" );
242
+ }
243
+
244
+ private TextMessage testReplyWithJackson (String methodName , String replyContent ) throws JMSException {
245
+ Queue replyDestination = mock (Queue .class );
246
+ Message <String > request = MessageBuilder .withPayload ("Response" ).build ();
247
+
248
+ Session session = mock (Session .class );
249
+ MessageProducer messageProducer = mock (MessageProducer .class );
250
+ TextMessage responseMessage = mock (TextMessage .class );
251
+ given (session .createTextMessage (replyContent )).willReturn (responseMessage );
252
+ given (session .createProducer (replyDestination )).willReturn (messageProducer );
253
+
254
+ MessagingMessageListenerAdapter listener = getPayloadInstance (request , methodName , Message .class );
255
+ MappingJackson2MessageConverter messageConverter = new MappingJackson2MessageConverter ();
256
+ messageConverter .setTargetType (MessageType .TEXT );
257
+ listener .setMessageConverter (messageConverter );
258
+ listener .setDefaultResponseDestination (replyDestination );
259
+ listener .onMessage (mock (javax .jms .Message .class ), session );
260
+
261
+ verify (session , times (0 )).createQueue (anyString ());
262
+ verify (session ).createTextMessage (replyContent );
263
+ verify (messageProducer ).send (responseMessage );
264
+ verify (messageProducer ).close ();
265
+ return responseMessage ;
266
+ }
267
+
268
+
243
269
protected MessagingMessageListenerAdapter getSimpleInstance (String methodName , Class ... parameterTypes ) {
244
270
Method m = ReflectionUtils .findMethod (SampleBean .class , methodName , parameterTypes );
245
271
return createInstance (m );
@@ -253,14 +279,15 @@ protected MessagingMessageListenerAdapter createInstance(Method m) {
253
279
254
280
protected MessagingMessageListenerAdapter getPayloadInstance (final Object payload ,
255
281
String methodName , Class ... parameterTypes ) {
256
- Method m = ReflectionUtils .findMethod (SampleBean .class , methodName , parameterTypes );
282
+
283
+ Method method = ReflectionUtils .findMethod (SampleBean .class , methodName , parameterTypes );
257
284
MessagingMessageListenerAdapter adapter = new MessagingMessageListenerAdapter () {
258
285
@ Override
259
286
protected Object extractMessage (javax .jms .Message message ) {
260
287
return payload ;
261
288
}
262
289
};
263
- adapter .setHandlerMethod (factory .createInvocableHandlerMethod (sample , m ));
290
+ adapter .setHandlerMethod (factory .createInvocableHandlerMethod (sample , method ));
264
291
return adapter ;
265
292
}
266
293
@@ -302,6 +329,15 @@ public JmsResponse<String> replyPayloadNoDestination(Message<String> input) {
302
329
return new JmsResponse <>(input .getPayload (), null );
303
330
}
304
331
332
+ public Message <SampleResponse > replyJackson (Message <String > input ) {
333
+ return MessageBuilder .withPayload (createSampleResponse (input .getPayload ()))
334
+ .setHeader ("foo" , "bar" ).build ();
335
+ }
336
+
337
+ private SampleResponse createSampleResponse (String name ) {
338
+ return new SampleResponse (name , "lengthy description" );
339
+ }
340
+
305
341
public void fail (String input ) {
306
342
throw new IllegalArgumentException ("Expected test exception" );
307
343
}
@@ -311,4 +347,43 @@ public void wrongParam(Integer i) {
311
347
}
312
348
}
313
349
350
+
351
+ private static class SampleResponse {
352
+
353
+ private int counter = 42 ;
354
+
355
+ private String name ;
356
+
357
+ private String description ;
358
+
359
+ public SampleResponse (String name , String description ) {
360
+ this .name = name ;
361
+ this .description = description ;
362
+ }
363
+
364
+ public int getCounter () {
365
+ return counter ;
366
+ }
367
+
368
+ public void setCounter (int counter ) {
369
+ this .counter = counter ;
370
+ }
371
+
372
+ public String getName () {
373
+ return name ;
374
+ }
375
+
376
+ public void setName (String name ) {
377
+ this .name = name ;
378
+ }
379
+
380
+ public String getDescription () {
381
+ return description ;
382
+ }
383
+
384
+ public void setDescription (String description ) {
385
+ this .description = description ;
386
+ }
387
+ }
388
+
314
389
}
0 commit comments