0
# Core JMS Operations
1
2
Central messaging operations using the template pattern for synchronous JMS operations. The JmsTemplate provides a consistent programming model for sending, receiving, and browsing messages with comprehensive configuration options and automatic resource management.
3
4
## Capabilities
5
6
### JmsTemplate
7
8
The central class for synchronous JMS operations, providing template-based access to JMS destinations with automatic resource management and exception translation.
9
10
```java { .api }
11
/**
12
* Central class for synchronous JMS operations with template pattern.
13
* Handles connection and session management automatically.
14
*/
15
public class JmsTemplate implements JmsOperations {
16
17
// Constructors
18
public JmsTemplate();
19
public JmsTemplate(ConnectionFactory connectionFactory);
20
21
// Connection configuration
22
public void setConnectionFactory(ConnectionFactory connectionFactory);
23
public ConnectionFactory getConnectionFactory();
24
25
// Default destination configuration
26
public void setDefaultDestinationName(String defaultDestinationName);
27
public void setDefaultDestination(Destination defaultDestination);
28
29
// Message conversion configuration
30
public void setMessageConverter(MessageConverter messageConverter);
31
public MessageConverter getMessageConverter();
32
33
// Timeout configuration
34
public void setReceiveTimeout(long receiveTimeout);
35
public long getReceiveTimeout();
36
public void setDeliveryDelay(long deliveryDelay);
37
38
// QoS configuration
39
public void setQosSettings(QosSettings qosSettings);
40
public void setDeliveryPersistent(boolean deliveryPersistent);
41
public void setPriority(int priority);
42
public void setTimeToLive(long timeToLive);
43
44
// Pub/Sub configuration
45
public void setPubSubDomain(boolean pubSubDomain);
46
public boolean isPubSubDomain();
47
48
// Session configuration
49
public void setSessionAcknowledgeMode(int sessionAcknowledgeMode);
50
public void setSessionTransacted(boolean sessionTransacted);
51
}
52
```
53
54
### Core Template Operations
55
56
Execute custom operations within JMS Session and MessageProducer contexts with automatic resource management.
57
58
```java { .api }
59
/**
60
* Execute the action specified by the given action object within a JMS Session
61
* @param action callback object that exposes the session
62
* @return the result object from working with the session
63
* @throws JmsException in case of JMS errors
64
*/
65
public <T> T execute(SessionCallback<T> action) throws JmsException;
66
67
/**
68
* Execute the action specified by the given action object within a JMS Session and MessageProducer
69
* @param action callback object that exposes the session and producer
70
* @return the result object from working with the session and producer
71
* @throws JmsException in case of JMS errors
72
*/
73
public <T> T execute(ProducerCallback<T> action) throws JmsException;
74
75
/**
76
* Execute the action with a MessageProducer for the default destination
77
* @param action callback object that exposes the session and producer
78
* @return the result object from working with the session and producer
79
* @throws JmsException in case of JMS errors
80
*/
81
public <T> T execute(String destinationName, ProducerCallback<T> action) throws JmsException;
82
```
83
84
### Message Sending Operations
85
86
Send messages to destinations with various configuration options and message creation patterns.
87
88
```java { .api }
89
/**
90
* Send a message using a MessageCreator callback to create the message
91
* @param destinationName the name of the destination to send to
92
* @param messageCreator callback to create the message
93
* @throws JmsException in case of JMS errors
94
*/
95
public void send(String destinationName, MessageCreator messageCreator) throws JmsException;
96
97
/**
98
* Send a message to the specified destination
99
* @param destination the destination to send to
100
* @param messageCreator callback to create the message
101
* @throws JmsException in case of JMS errors
102
*/
103
public void send(Destination destination, MessageCreator messageCreator) throws JmsException;
104
105
/**
106
* Send a message to the default destination
107
* @param messageCreator callback to create the message
108
* @throws JmsException in case of JMS errors
109
*/
110
public void send(MessageCreator messageCreator) throws JmsException;
111
112
/**
113
* Convert an object to a message and send it to the specified destination
114
* @param destinationName the name of the destination to send to
115
* @param message the object to convert and send
116
* @throws JmsException in case of JMS errors
117
*/
118
public void convertAndSend(String destinationName, Object message) throws JmsException;
119
120
/**
121
* Convert an object to a message and send it to the specified destination with post-processing
122
* @param destinationName the name of the destination to send to
123
* @param message the object to convert and send
124
* @param postProcessor callback to post-process the message
125
* @throws JmsException in case of JMS errors
126
*/
127
public void convertAndSend(String destinationName, Object message, MessagePostProcessor postProcessor)
128
throws JmsException;
129
```
130
131
### Message Receiving Operations
132
133
Receive messages from destinations synchronously with timeout support and automatic message conversion.
134
135
```java { .api }
136
/**
137
* Receive a message from the specified destination
138
* @param destinationName the name of the destination to receive from
139
* @return the received message, or null if timeout occurred
140
* @throws JmsException in case of JMS errors
141
*/
142
public Message receive(String destinationName) throws JmsException;
143
144
/**
145
* Receive a message from the specified destination
146
* @param destination the destination to receive from
147
* @return the received message, or null if timeout occurred
148
* @throws JmsException in case of JMS errors
149
*/
150
public Message receive(Destination destination) throws JmsException;
151
152
/**
153
* Receive a message from the default destination
154
* @return the received message, or null if timeout occurred
155
* @throws JmsException in case of JMS errors
156
*/
157
public Message receive() throws JmsException;
158
159
/**
160
* Receive a message and convert it to an object
161
* @param destinationName the name of the destination to receive from
162
* @return the converted object, or null if timeout occurred
163
* @throws JmsException in case of JMS errors
164
*/
165
public Object receiveAndConvert(String destinationName) throws JmsException;
166
167
/**
168
* Receive a message and convert it to the specified type
169
* @param destinationName the name of the destination to receive from
170
* @param targetClass the target class for conversion
171
* @return the converted object, or null if timeout occurred
172
* @throws JmsException in case of JMS errors
173
*/
174
public <T> T receiveAndConvert(String destinationName, Class<T> targetClass) throws JmsException;
175
```
176
177
### Send-and-Receive Operations
178
179
Send messages and synchronously receive replies, useful for request-reply messaging patterns.
180
181
```java { .api }
182
/**
183
* Send a message and receive the reply from a temporary queue
184
* @param destinationName the name of the destination to send to
185
* @param messageCreator callback to create the message
186
* @return the reply message, or null if timeout occurred
187
* @throws JmsException in case of JMS errors
188
*/
189
public Message sendAndReceive(String destinationName, MessageCreator messageCreator) throws JmsException;
190
191
/**
192
* Send a message and receive the reply from a temporary queue
193
* @param destination the destination to send to
194
* @param messageCreator callback to create the message
195
* @return the reply message, or null if timeout occurred
196
* @throws JmsException in case of JMS errors
197
*/
198
public Message sendAndReceive(Destination destination, MessageCreator messageCreator) throws JmsException;
199
200
/**
201
* Send a message to the default destination and receive the reply
202
* @param messageCreator callback to create the message
203
* @return the reply message, or null if timeout occurred
204
* @throws JmsException in case of JMS errors
205
*/
206
public Message sendAndReceive(MessageCreator messageCreator) throws JmsException;
207
```
208
209
### Message Browsing Operations
210
211
Browse messages in queues without consuming them, useful for monitoring and debugging message queues.
212
213
```java { .api }
214
/**
215
* Browse messages in a queue using a callback
216
* @param destinationName the name of the queue to browse
217
* @param action callback for processing each message
218
* @return the result from the callback
219
* @throws JmsException in case of JMS errors
220
*/
221
public <T> T browse(String destinationName, BrowserCallback<T> action) throws JmsException;
222
223
/**
224
* Browse messages in a queue with a message selector
225
* @param destinationName the name of the queue to browse
226
* @param messageSelector JMS message selector expression
227
* @param action callback for processing each message
228
* @return the result from the callback
229
* @throws JmsException in case of JMS errors
230
*/
231
public <T> T browseSelected(String destinationName, String messageSelector, BrowserCallback<T> action)
232
throws JmsException;
233
```
234
235
### Callback Interfaces
236
237
Callback interfaces for advanced JMS operations providing access to JMS Session and Producer objects.
238
239
```java { .api }
240
/**
241
* Callback interface for creating JMS messages
242
*/
243
public interface MessageCreator {
244
/**
245
* Create a JMS message
246
* @param session the JMS session to use
247
* @return the created message
248
* @throws JMSException in case of JMS errors
249
*/
250
Message createMessage(Session session) throws JMSException;
251
}
252
253
/**
254
* Callback interface for post-processing messages after conversion
255
*/
256
public interface MessagePostProcessor {
257
/**
258
* Post-process the given message
259
* @param message the message to post-process
260
* @return the post-processed message
261
* @throws JMSException in case of JMS errors
262
*/
263
Message postProcessMessage(Message message) throws JMSException;
264
}
265
266
/**
267
* Callback interface for working directly with JMS Session
268
*/
269
public interface SessionCallback<T> {
270
/**
271
* Execute operations within a JMS session
272
* @param session the JMS session
273
* @return the result object
274
* @throws JMSException in case of JMS errors
275
*/
276
T doInJms(Session session) throws JMSException;
277
}
278
279
/**
280
* Callback interface for working with MessageProducer
281
*/
282
public interface ProducerCallback<T> {
283
/**
284
* Execute operations with a MessageProducer
285
* @param session the JMS session
286
* @param producer the MessageProducer
287
* @return the result object
288
* @throws JMSException in case of JMS errors
289
*/
290
T doInJms(Session session, MessageProducer producer) throws JMSException;
291
}
292
293
/**
294
* Callback interface for browsing queue messages
295
*/
296
public interface BrowserCallback<T> {
297
/**
298
* Process messages during queue browsing
299
* @param session the JMS session
300
* @param browser the QueueBrowser
301
* @return the result object
302
* @throws JMSException in case of JMS errors
303
*/
304
T doInJms(Session session, QueueBrowser browser) throws JMSException;
305
}
306
```
307
308
**Usage Examples:**
309
310
```java
311
import org.springframework.jms.core.*;
312
import javax.jms.*;
313
314
// Basic message sending
315
@Service
316
public class OrderService {
317
318
@Autowired
319
private JmsTemplate jmsTemplate;
320
321
public void sendOrder(Order order) {
322
jmsTemplate.convertAndSend("order.queue", order);
323
}
324
325
// Custom message creation
326
public void sendOrderWithHeaders(Order order) {
327
jmsTemplate.send("order.queue", session -> {
328
ObjectMessage message = session.createObjectMessage(order);
329
message.setStringProperty("orderType", order.getType());
330
message.setIntProperty("priority", order.getPriority());
331
return message;
332
});
333
}
334
335
// Receive messages synchronously
336
public Order receiveOrder() {
337
return (Order) jmsTemplate.receiveAndConvert("order.queue");
338
}
339
340
// Browse messages without consuming
341
public List<String> browseOrderIds() {
342
return jmsTemplate.browse("order.queue", (session, browser) -> {
343
List<String> orderIds = new ArrayList<>();
344
Enumeration<?> messages = browser.getEnumeration();
345
while (messages.hasMoreElements()) {
346
Message message = (Message) messages.nextElement();
347
orderIds.add(message.getJMSMessageID());
348
}
349
return orderIds;
350
});
351
}
352
}
353
```