-
Notifications
You must be signed in to change notification settings - Fork 37.7k
/
JmsTemplate.java
1228 lines (1087 loc) · 41 KB
/
JmsTemplate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jms.core;
import io.micrometer.jakarta9.instrument.jms.JmsInstrumentation;
import io.micrometer.observation.ObservationRegistry;
import jakarta.jms.Connection;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.DeliveryMode;
import jakarta.jms.Destination;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
import jakarta.jms.MessageConsumer;
import jakarta.jms.MessageProducer;
import jakarta.jms.Queue;
import jakarta.jms.QueueBrowser;
import jakarta.jms.Session;
import jakarta.jms.TemporaryQueue;
import org.springframework.jms.JmsException;
import org.springframework.jms.connection.ConnectionFactoryUtils;
import org.springframework.jms.connection.JmsResourceHolder;
import org.springframework.jms.support.JmsUtils;
import org.springframework.jms.support.QosSettings;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.SimpleMessageConverter;
import org.springframework.jms.support.destination.JmsDestinationAccessor;
import org.springframework.lang.Nullable;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* Helper class that simplifies synchronous JMS access code.
*
* <p>If you want to use dynamic destination creation, you must specify
* the type of JMS destination to create, using the "pubSubDomain" property.
* For other operations, this is not necessary. Point-to-Point (Queues) is the default
* domain.
*
* <p>Default settings for JMS Sessions are "not transacted" and "auto-acknowledge".
* As defined by the Jakarta EE specification, the transaction and acknowledgement
* parameters are ignored when a JMS Session is created inside an active
* transaction, no matter if a JTA transaction or a Spring-managed transaction.
* To configure them for native JMS usage, specify appropriate values for
* the "sessionTransacted" and "sessionAcknowledgeMode" bean properties.
*
* <p>This template uses a
* {@link org.springframework.jms.support.destination.DynamicDestinationResolver}
* and a {@link org.springframework.jms.support.converter.SimpleMessageConverter}
* as default strategies for resolving a destination name or converting a message,
* respectively. These defaults can be overridden through the "destinationResolver"
* and "messageConverter" bean properties.
*
* <p><b>NOTE: The {@code ConnectionFactory} used with this template should
* return pooled Connections (or a single shared Connection) as well as pooled
* Sessions and MessageProducers. Otherwise, performance of ad-hoc JMS operations
* is going to suffer.</b> The simplest option is to use the Spring-provided
* {@link org.springframework.jms.connection.SingleConnectionFactory} as a
* decorator for your target {@code ConnectionFactory}, reusing a single
* JMS Connection in a thread-safe fashion; this is often good enough for the
* purpose of sending messages via this template. In a Jakarta EE environment,
* make sure that the {@code ConnectionFactory} is obtained from the
* application's environment naming context via JNDI; application servers
* typically expose pooled, transaction-aware factories there.
*
* @author Mark Pollack
* @author Juergen Hoeller
* @author Stephane Nicoll
* @author Brian Clozel
* @since 1.1
* @see #setConnectionFactory
* @see #setPubSubDomain
* @see #setDestinationResolver
* @see #setMessageConverter
* @see jakarta.jms.MessageProducer
* @see jakarta.jms.MessageConsumer
*/
public class JmsTemplate extends JmsDestinationAccessor implements JmsOperations {
private static final boolean micrometerJakartaPresent = ClassUtils.isPresent(
"io.micrometer.jakarta9.instrument.jms.JmsInstrumentation", JmsTemplate.class.getClassLoader());
/** Internal ResourceFactory adapter for interacting with ConnectionFactoryUtils. */
private final JmsTemplateResourceFactory transactionalResourceFactory = new JmsTemplateResourceFactory();
@Nullable
private Object defaultDestination;
@Nullable
private MessageConverter messageConverter;
private boolean messageIdEnabled = true;
private boolean messageTimestampEnabled = true;
private boolean pubSubNoLocal = false;
private long receiveTimeout = RECEIVE_TIMEOUT_INDEFINITE_WAIT;
private long deliveryDelay = -1;
private boolean explicitQosEnabled = false;
private int deliveryMode = Message.DEFAULT_DELIVERY_MODE;
private int priority = Message.DEFAULT_PRIORITY;
private long timeToLive = Message.DEFAULT_TIME_TO_LIVE;
@Nullable
private ObservationRegistry observationRegistry;
/**
* Create a new JmsTemplate for bean-style usage.
* <p>Note: The ConnectionFactory has to be set before using the instance.
* This constructor can be used to prepare a JmsTemplate via a BeanFactory,
* typically setting the ConnectionFactory via setConnectionFactory.
* @see #setConnectionFactory
*/
public JmsTemplate() {
initDefaultStrategies();
}
/**
* Create a new JmsTemplate, given a ConnectionFactory.
* @param connectionFactory the ConnectionFactory to obtain Connections from
*/
public JmsTemplate(ConnectionFactory connectionFactory) {
this();
setConnectionFactory(connectionFactory);
afterPropertiesSet();
}
/**
* Initialize the default implementations for the template's strategies:
* DynamicDestinationResolver and SimpleMessageConverter.
* @see #setDestinationResolver
* @see #setMessageConverter
* @see org.springframework.jms.support.destination.DynamicDestinationResolver
* @see org.springframework.jms.support.converter.SimpleMessageConverter
*/
protected void initDefaultStrategies() {
setMessageConverter(new SimpleMessageConverter());
}
/**
* Set the destination to be used on send/receive operations that do not
* have a destination parameter.
* <p>Alternatively, specify a "defaultDestinationName", to be
* dynamically resolved via the DestinationResolver.
* @see #send(MessageCreator)
* @see #convertAndSend(Object)
* @see #convertAndSend(Object, MessagePostProcessor)
* @see #setDefaultDestinationName(String)
*/
public void setDefaultDestination(@Nullable Destination destination) {
this.defaultDestination = destination;
}
/**
* Return the destination to be used on send/receive operations that do not
* have a destination parameter.
*/
@Nullable
public Destination getDefaultDestination() {
return (this.defaultDestination instanceof Destination dest ? dest : null);
}
@Nullable
private Queue getDefaultQueue() {
Destination defaultDestination = getDefaultDestination();
if (defaultDestination == null) {
return null;
}
if (!(defaultDestination instanceof Queue queue)) {
throw new IllegalStateException(
"'defaultDestination' does not correspond to a Queue. Check configuration of JmsTemplate.");
}
return queue;
}
/**
* Set the destination name to be used on send/receive operations that
* do not have a destination parameter. The specified name will be
* dynamically resolved via the DestinationResolver.
* <p>Alternatively, specify a JMS Destination object as "defaultDestination".
* @see #send(MessageCreator)
* @see #convertAndSend(Object)
* @see #convertAndSend(Object, MessagePostProcessor)
* @see #setDestinationResolver
* @see #setDefaultDestination(jakarta.jms.Destination)
*/
public void setDefaultDestinationName(@Nullable String destinationName) {
this.defaultDestination = destinationName;
}
/**
* Return the destination name to be used on send/receive operations that
* do not have a destination parameter.
*/
@Nullable
public String getDefaultDestinationName() {
return (this.defaultDestination instanceof String name ? name : null);
}
private String getRequiredDefaultDestinationName() throws IllegalStateException {
String name = getDefaultDestinationName();
if (name == null) {
throw new IllegalStateException(
"No 'defaultDestination' or 'defaultDestinationName' specified. Check configuration of JmsTemplate.");
}
return name;
}
/**
* Set the message converter for this template. Used to resolve
* Object parameters to convertAndSend methods and Object results
* from receiveAndConvert methods.
* <p>The default converter is a SimpleMessageConverter, which is able
* to handle BytesMessages, TextMessages and ObjectMessages.
* @see #convertAndSend
* @see #receiveAndConvert
* @see org.springframework.jms.support.converter.SimpleMessageConverter
*/
public void setMessageConverter(@Nullable MessageConverter messageConverter) {
this.messageConverter = messageConverter;
}
/**
* Return the message converter for this template.
*/
@Nullable
public MessageConverter getMessageConverter() {
return this.messageConverter;
}
private MessageConverter getRequiredMessageConverter() throws IllegalStateException {
MessageConverter converter = getMessageConverter();
if (converter == null) {
throw new IllegalStateException("No 'messageConverter' specified. Check configuration of JmsTemplate.");
}
return converter;
}
/**
* Set whether message IDs are enabled. Default is "true".
* <p>This is only a hint to the JMS producer.
* See the JMS javadocs for details.
* @see jakarta.jms.MessageProducer#setDisableMessageID
*/
public void setMessageIdEnabled(boolean messageIdEnabled) {
this.messageIdEnabled = messageIdEnabled;
}
/**
* Return whether message IDs are enabled.
*/
public boolean isMessageIdEnabled() {
return this.messageIdEnabled;
}
/**
* Set whether message timestamps are enabled. Default is "true".
* <p>This is only a hint to the JMS producer.
* See the JMS javadocs for details.
* @see jakarta.jms.MessageProducer#setDisableMessageTimestamp
*/
public void setMessageTimestampEnabled(boolean messageTimestampEnabled) {
this.messageTimestampEnabled = messageTimestampEnabled;
}
/**
* Return whether message timestamps are enabled.
*/
public boolean isMessageTimestampEnabled() {
return this.messageTimestampEnabled;
}
/**
* Set whether to inhibit the delivery of messages published by its own connection.
* Default is "false".
* @see jakarta.jms.Session#createConsumer(jakarta.jms.Destination, String, boolean)
*/
public void setPubSubNoLocal(boolean pubSubNoLocal) {
this.pubSubNoLocal = pubSubNoLocal;
}
/**
* Return whether to inhibit the delivery of messages published by its own connection.
*/
public boolean isPubSubNoLocal() {
return this.pubSubNoLocal;
}
/**
* Set the timeout to use for receive calls (in milliseconds).
* <p>The default is {@link #RECEIVE_TIMEOUT_INDEFINITE_WAIT}, which indicates
* a blocking receive without timeout.
* <p>Specify {@link #RECEIVE_TIMEOUT_NO_WAIT} (or any other negative value)
* to indicate that a receive operation should check if a message is
* immediately available without blocking.
* @see #receiveFromConsumer(MessageConsumer, long)
* @see jakarta.jms.MessageConsumer#receive(long)
* @see jakarta.jms.MessageConsumer#receiveNoWait()
* @see jakarta.jms.MessageConsumer#receive()
*/
public void setReceiveTimeout(long receiveTimeout) {
this.receiveTimeout = receiveTimeout;
}
/**
* Return the timeout to use for receive calls (in milliseconds).
*/
public long getReceiveTimeout() {
return this.receiveTimeout;
}
/**
* Set the delivery delay to use for send calls (in milliseconds).
* <p>The default is -1 (no delivery delay passed on to the broker).
* Note that this feature requires JMS 2.0.
*/
public void setDeliveryDelay(long deliveryDelay) {
this.deliveryDelay = deliveryDelay;
}
/**
* Return the delivery delay to use for send calls (in milliseconds).
*/
public long getDeliveryDelay() {
return this.deliveryDelay;
}
/**
* Set if the QOS values (deliveryMode, priority, timeToLive)
* should be used for sending a message.
* @see #setDeliveryMode
* @see #setPriority
* @see #setTimeToLive
*/
public void setExplicitQosEnabled(boolean explicitQosEnabled) {
this.explicitQosEnabled = explicitQosEnabled;
}
/**
* If "true", then the values of deliveryMode, priority, and timeToLive
* will be used when sending a message. Otherwise, the default values,
* that may be set administratively, will be used.
* @return true if overriding default values of QOS parameters
* (deliveryMode, priority, and timeToLive)
* @see #setDeliveryMode
* @see #setPriority
* @see #setTimeToLive
*/
public boolean isExplicitQosEnabled() {
return this.explicitQosEnabled;
}
/**
* Set the {@link QosSettings} to use when sending a message.
* @param settings the deliveryMode, priority, and timeToLive settings to use
* @since 5.0
* @see #setExplicitQosEnabled(boolean)
* @see #setDeliveryMode(int)
* @see #setPriority(int)
* @see #setTimeToLive(long)
*/
public void setQosSettings(QosSettings settings) {
Assert.notNull(settings, "Settings must not be null");
setExplicitQosEnabled(true);
setDeliveryMode(settings.getDeliveryMode());
setPriority(settings.getPriority());
setTimeToLive(settings.getTimeToLive());
}
/**
* Set whether message delivery should be persistent or non-persistent,
* specified as boolean value ("true" or "false"). This will set the delivery
* mode accordingly, to either "PERSISTENT" (2) or "NON_PERSISTENT" (1).
* <p>Default is "true" a.k.a. delivery mode "PERSISTENT".
* @see #setDeliveryMode(int)
* @see jakarta.jms.DeliveryMode#PERSISTENT
* @see jakarta.jms.DeliveryMode#NON_PERSISTENT
*/
public void setDeliveryPersistent(boolean deliveryPersistent) {
this.deliveryMode = (deliveryPersistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);
}
/**
* Set the delivery mode to use when sending a message.
* Default is the JMS Message default: "PERSISTENT".
* <p>Since a default value may be defined administratively,
* this is only used when "isExplicitQosEnabled" equals "true".
* @param deliveryMode the delivery mode to use
* @see #isExplicitQosEnabled
* @see jakarta.jms.DeliveryMode#PERSISTENT
* @see jakarta.jms.DeliveryMode#NON_PERSISTENT
* @see jakarta.jms.Message#DEFAULT_DELIVERY_MODE
* @see jakarta.jms.MessageProducer#send(jakarta.jms.Message, int, int, long)
*/
public void setDeliveryMode(int deliveryMode) {
this.deliveryMode = deliveryMode;
}
/**
* Return the delivery mode to use when sending a message.
*/
public int getDeliveryMode() {
return this.deliveryMode;
}
/**
* Set the priority of a message when sending.
* <p>Since a default value may be defined administratively,
* this is only used when "isExplicitQosEnabled" equals "true".
* @see #isExplicitQosEnabled
* @see jakarta.jms.Message#DEFAULT_PRIORITY
* @see jakarta.jms.MessageProducer#send(jakarta.jms.Message, int, int, long)
*/
public void setPriority(int priority) {
this.priority = priority;
}
/**
* Return the priority of a message when sending.
*/
public int getPriority() {
return this.priority;
}
/**
* Set the time-to-live of the message when sending.
* <p>Since a default value may be defined administratively,
* this is only used when "isExplicitQosEnabled" equals "true".
* @param timeToLive the message's lifetime (in milliseconds)
* @see #isExplicitQosEnabled
* @see jakarta.jms.Message#DEFAULT_TIME_TO_LIVE
* @see jakarta.jms.MessageProducer#send(jakarta.jms.Message, int, int, long)
*/
public void setTimeToLive(long timeToLive) {
this.timeToLive = timeToLive;
}
/**
* Return the time-to-live of the message when sending.
*/
public long getTimeToLive() {
return this.timeToLive;
}
/**
* Configure the {@link ObservationRegistry} to use for recording JMS observations.
* @param observationRegistry the observation registry to use.
* @since 6.1
* @see io.micrometer.jakarta9.instrument.jms.JmsInstrumentation
*/
public void setObservationRegistry(ObservationRegistry observationRegistry) {
this.observationRegistry = observationRegistry;
}
//---------------------------------------------------------------------------------------
// JmsOperations execute methods
//---------------------------------------------------------------------------------------
@Override
@Nullable
public <T> T execute(SessionCallback<T> action) throws JmsException {
return execute(action, false);
}
/**
* Execute the action specified by the given action object within a
* JMS Session. Generalized version of {@code execute(SessionCallback)},
* allowing the JMS Connection to be started on the fly.
* <p>Use {@code execute(SessionCallback)} for the general case.
* Starting the JMS Connection is just necessary for receiving messages,
* which is preferably achieved through the {@code receive} methods.
* @param action callback object that exposes the Session
* @param startConnection whether to start the Connection
* @return the result object from working with the Session
* @throws JmsException if there is any problem
* @see #execute(SessionCallback)
* @see #receive
*/
@SuppressWarnings("resource")
@Nullable
public <T> T execute(SessionCallback<T> action, boolean startConnection) throws JmsException {
Assert.notNull(action, "Callback object must not be null");
Connection conToClose = null;
Session sessionToClose = null;
try {
Session sessionToUse = ConnectionFactoryUtils.doGetTransactionalSession(
obtainConnectionFactory(), this.transactionalResourceFactory, startConnection);
if (sessionToUse == null) {
conToClose = createConnection();
sessionToClose = createSession(conToClose);
if (startConnection) {
conToClose.start();
}
sessionToUse = sessionToClose;
}
if (logger.isDebugEnabled()) {
logger.debug("Executing callback on JMS Session: " + sessionToUse);
}
if (micrometerJakartaPresent && this.observationRegistry != null) {
sessionToUse = MicrometerInstrumentation.instrumentSession(sessionToUse, this.observationRegistry);
}
return action.doInJms(sessionToUse);
}
catch (JMSException ex) {
throw convertJmsAccessException(ex);
}
finally {
JmsUtils.closeSession(sessionToClose);
ConnectionFactoryUtils.releaseConnection(conToClose, getConnectionFactory(), startConnection);
}
}
@Override
@Nullable
public <T> T execute(ProducerCallback<T> action) throws JmsException {
String defaultDestinationName = getDefaultDestinationName();
if (defaultDestinationName != null) {
return execute(defaultDestinationName, action);
}
else {
return execute(getDefaultDestination(), action);
}
}
@Override
@Nullable
public <T> T execute(final @Nullable Destination destination, final ProducerCallback<T> action) throws JmsException {
Assert.notNull(action, "Callback object must not be null");
return execute(session -> {
MessageProducer producer = createProducer(session, destination);
try {
return action.doInJms(session, producer);
}
finally {
JmsUtils.closeMessageProducer(producer);
}
}, false);
}
@Override
@Nullable
public <T> T execute(final String destinationName, final ProducerCallback<T> action) throws JmsException {
Assert.notNull(action, "Callback object must not be null");
return execute(session -> {
Destination destination = resolveDestinationName(session, destinationName);
MessageProducer producer = createProducer(session, destination);
try {
return action.doInJms(session, producer);
}
finally {
JmsUtils.closeMessageProducer(producer);
}
}, false);
}
//---------------------------------------------------------------------------------------
// Convenience methods for sending messages
//---------------------------------------------------------------------------------------
@Override
public void send(MessageCreator messageCreator) throws JmsException {
Destination defaultDestination = getDefaultDestination();
if (defaultDestination != null) {
send(defaultDestination, messageCreator);
}
else {
send(getRequiredDefaultDestinationName(), messageCreator);
}
}
@Override
public void send(final Destination destination, final MessageCreator messageCreator) throws JmsException {
execute(session -> {
doSend(session, destination, messageCreator);
return null;
}, false);
}
@Override
public void send(final String destinationName, final MessageCreator messageCreator) throws JmsException {
execute(session -> {
Destination destination = resolveDestinationName(session, destinationName);
doSend(session, destination, messageCreator);
return null;
}, false);
}
/**
* Send the given JMS message.
* @param session the JMS Session to operate on
* @param destination the JMS Destination to send to
* @param messageCreator callback to create a JMS Message
* @throws JMSException if thrown by JMS API methods
*/
protected void doSend(Session session, Destination destination, MessageCreator messageCreator)
throws JMSException {
Assert.notNull(messageCreator, "MessageCreator must not be null");
MessageProducer producer = createProducer(session, destination);
try {
Message message = messageCreator.createMessage(session);
if (logger.isDebugEnabled()) {
logger.debug("Sending created message: " + message);
}
doSend(producer, message);
// Check commit - avoid commit call within a JTA transaction.
if (session.getTransacted() && isSessionLocallyTransacted(session)) {
// Transacted session created by this template -> commit.
JmsUtils.commitIfNecessary(session);
}
}
finally {
JmsUtils.closeMessageProducer(producer);
}
}
/**
* Actually send the given JMS message.
* @param producer the JMS MessageProducer to send with
* @param message the JMS Message to send
* @throws JMSException if thrown by JMS API methods
*/
protected void doSend(MessageProducer producer, Message message) throws JMSException {
if (this.deliveryDelay >= 0) {
producer.setDeliveryDelay(this.deliveryDelay);
}
if (isExplicitQosEnabled()) {
producer.send(message, getDeliveryMode(), getPriority(), getTimeToLive());
}
else {
producer.send(message);
}
}
//---------------------------------------------------------------------------------------
// Convenience methods for sending auto-converted messages
//---------------------------------------------------------------------------------------
@Override
public void convertAndSend(Object message) throws JmsException {
Destination defaultDestination = getDefaultDestination();
if (defaultDestination != null) {
convertAndSend(defaultDestination, message);
}
else {
convertAndSend(getRequiredDefaultDestinationName(), message);
}
}
@Override
public void convertAndSend(Destination destination, final Object message) throws JmsException {
send(destination, session -> getRequiredMessageConverter().toMessage(message, session));
}
@Override
public void convertAndSend(String destinationName, final Object message) throws JmsException {
send(destinationName, session -> getRequiredMessageConverter().toMessage(message, session));
}
@Override
public void convertAndSend(Object message, MessagePostProcessor postProcessor) throws JmsException {
Destination defaultDestination = getDefaultDestination();
if (defaultDestination != null) {
convertAndSend(defaultDestination, message, postProcessor);
}
else {
convertAndSend(getRequiredDefaultDestinationName(), message, postProcessor);
}
}
@Override
public void convertAndSend(
Destination destination, final Object message, final MessagePostProcessor postProcessor)
throws JmsException {
send(destination, session -> {
Message msg = getRequiredMessageConverter().toMessage(message, session);
return postProcessor.postProcessMessage(msg);
});
}
@Override
public void convertAndSend(
String destinationName, final Object message, final MessagePostProcessor postProcessor)
throws JmsException {
send(destinationName, session -> {
Message msg = getRequiredMessageConverter().toMessage(message, session);
return postProcessor.postProcessMessage(msg);
});
}
//---------------------------------------------------------------------------------------
// Convenience methods for receiving messages
//---------------------------------------------------------------------------------------
@Override
@Nullable
public Message receive() throws JmsException {
Destination defaultDestination = getDefaultDestination();
if (defaultDestination != null) {
return receive(defaultDestination);
}
else {
return receive(getRequiredDefaultDestinationName());
}
}
@Override
@Nullable
public Message receive(Destination destination) throws JmsException {
return receiveSelected(destination, null);
}
@Override
@Nullable
public Message receive(String destinationName) throws JmsException {
return receiveSelected(destinationName, null);
}
@Override
@Nullable
public Message receiveSelected(String messageSelector) throws JmsException {
Destination defaultDestination = getDefaultDestination();
if (defaultDestination != null) {
return receiveSelected(defaultDestination, messageSelector);
}
else {
return receiveSelected(getRequiredDefaultDestinationName(), messageSelector);
}
}
@Override
@Nullable
public Message receiveSelected(final Destination destination, @Nullable final String messageSelector) throws JmsException {
return execute(session -> doReceive(session, destination, messageSelector), true);
}
@Override
@Nullable
public Message receiveSelected(final String destinationName, @Nullable final String messageSelector) throws JmsException {
return execute(session -> {
Destination destination = resolveDestinationName(session, destinationName);
return doReceive(session, destination, messageSelector);
}, true);
}
/**
* Receive a JMS message.
* @param session the JMS Session to operate on
* @param destination the JMS Destination to receive from
* @param messageSelector the message selector for this consumer (can be {@code null})
* @return the JMS Message received, or {@code null} if none
* @throws JMSException if thrown by JMS API methods
*/
@Nullable
protected Message doReceive(Session session, Destination destination, @Nullable String messageSelector)
throws JMSException {
return doReceive(session, createConsumer(session, destination, messageSelector));
}
/**
* Actually receive a JMS message.
* @param session the JMS Session to operate on
* @param consumer the JMS MessageConsumer to receive with
* @return the JMS Message received, or {@code null} if none
* @throws JMSException if thrown by JMS API methods
*/
@Nullable
protected Message doReceive(Session session, MessageConsumer consumer) throws JMSException {
try {
// Use transaction timeout (if available).
long timeout = getReceiveTimeout();
ConnectionFactory connectionFactory = getConnectionFactory();
JmsResourceHolder resourceHolder = null;
if (connectionFactory != null) {
resourceHolder = (JmsResourceHolder) TransactionSynchronizationManager.getResource(connectionFactory);
}
if (resourceHolder != null && resourceHolder.hasTimeout()) {
timeout = Math.min(timeout, resourceHolder.getTimeToLiveInMillis());
}
Message message = receiveFromConsumer(consumer, timeout);
if (session.getTransacted()) {
// Commit necessary - but avoid commit call within a JTA transaction.
if (isSessionLocallyTransacted(session)) {
// Transacted session created by this template -> commit.
JmsUtils.commitIfNecessary(session);
}
}
else if (isClientAcknowledge(session)) {
// Manually acknowledge message, if any.
if (message != null) {
message.acknowledge();
}
}
return message;
}
finally {
JmsUtils.closeMessageConsumer(consumer);
}
}
//---------------------------------------------------------------------------------------
// Convenience methods for receiving auto-converted messages
//---------------------------------------------------------------------------------------
@Override
@Nullable
public Object receiveAndConvert() throws JmsException {
return doConvertFromMessage(receive());
}
@Override
@Nullable
public Object receiveAndConvert(Destination destination) throws JmsException {
return doConvertFromMessage(receive(destination));
}
@Override
@Nullable
public Object receiveAndConvert(String destinationName) throws JmsException {
return doConvertFromMessage(receive(destinationName));
}
@Override
@Nullable
public Object receiveSelectedAndConvert(String messageSelector) throws JmsException {
return doConvertFromMessage(receiveSelected(messageSelector));
}
@Override
@Nullable
public Object receiveSelectedAndConvert(Destination destination, String messageSelector) throws JmsException {
return doConvertFromMessage(receiveSelected(destination, messageSelector));
}
@Override
@Nullable
public Object receiveSelectedAndConvert(String destinationName, String messageSelector) throws JmsException {
return doConvertFromMessage(receiveSelected(destinationName, messageSelector));
}
/**
* Extract the content from the given JMS message.
* @param message the JMS Message to convert (can be {@code null})
* @return the content of the message, or {@code null} if none
*/
@Nullable
protected Object doConvertFromMessage(@Nullable Message message) {
if (message != null) {
try {
return getRequiredMessageConverter().fromMessage(message);
}
catch (JMSException ex) {
throw convertJmsAccessException(ex);
}
}
return null;
}
//---------------------------------------------------------------------------------------
// Convenience methods for sending messages to and receiving the reply from a destination
//---------------------------------------------------------------------------------------
@Override
@Nullable
public Message sendAndReceive(MessageCreator messageCreator) throws JmsException {
Destination defaultDestination = getDefaultDestination();
if (defaultDestination != null) {
return sendAndReceive(defaultDestination, messageCreator);
}
else {
return sendAndReceive(getRequiredDefaultDestinationName(), messageCreator);
}
}
@Override
@Nullable
public Message sendAndReceive(final Destination destination, final MessageCreator messageCreator) throws JmsException {
return executeLocal(session -> doSendAndReceive(session, destination, messageCreator), true);
}
@Override
@Nullable
public Message sendAndReceive(final String destinationName, final MessageCreator messageCreator) throws JmsException {
return executeLocal(session -> {
Destination destination = resolveDestinationName(session, destinationName);
return doSendAndReceive(session, destination, messageCreator);
}, true);
}
/**
* Send a request message to the given {@link Destination} and block until
* a reply has been received on a temporary queue created on-the-fly.
* <p>Return the response message or {@code null} if no message has
* @throws JMSException if thrown by JMS API methods
*/
@Nullable
protected Message doSendAndReceive(Session session, Destination destination, MessageCreator messageCreator)
throws JMSException {
Assert.notNull(messageCreator, "MessageCreator must not be null");
TemporaryQueue responseQueue = null;
MessageProducer producer = null;
MessageConsumer consumer = null;
try {
Message requestMessage = messageCreator.createMessage(session);
responseQueue = session.createTemporaryQueue();
producer = session.createProducer(destination);
consumer = session.createConsumer(responseQueue);
requestMessage.setJMSReplyTo(responseQueue);
if (logger.isDebugEnabled()) {
logger.debug("Sending created message: " + requestMessage);
}
doSend(producer, requestMessage);
return receiveFromConsumer(consumer, getReceiveTimeout());
}
finally {
JmsUtils.closeMessageConsumer(consumer);
JmsUtils.closeMessageProducer(producer);
if (responseQueue != null) {
responseQueue.delete();
}
}
}
/**
* A variant of {@link #execute(SessionCallback, boolean)} that explicitly
* creates a non-transactional {@link Session}. The given {@link SessionCallback}
* does not participate in an existing transaction.
*/
@Nullable
private <T> T executeLocal(SessionCallback<T> action, boolean startConnection) throws JmsException {
Assert.notNull(action, "Callback object must not be null");
Connection con = null;
Session session = null;
try {
con = createConnection();
session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
if (startConnection) {
con.start();
}
if (logger.isDebugEnabled()) {
logger.debug("Executing callback on JMS Session: " + session);
}
return action.doInJms(session);
}
catch (JMSException ex) {
throw convertJmsAccessException(ex);
}
finally {
JmsUtils.closeSession(session);
ConnectionFactoryUtils.releaseConnection(con, getConnectionFactory(), startConnection);
}
}
//---------------------------------------------------------------------------------------
// Convenience methods for browsing messages
//---------------------------------------------------------------------------------------
@Override
@Nullable
public <T> T browse(BrowserCallback<T> action) throws JmsException {
Queue defaultQueue = getDefaultQueue();
if (defaultQueue != null) {