Skip to content

Commit 200934c

Browse files
committed
Add reference documentation for JmsClient next to JmsTemplate
See gh-32501
1 parent a3e9f0c commit 200934c

File tree

3 files changed

+63
-18
lines changed

3 files changed

+63
-18
lines changed

framework-docs/modules/ROOT/pages/integration/jms/receiving.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ This describes how to receive messages with JMS in Spring.
77
[[jms-receiving-sync]]
88
== Synchronous Receipt
99

10-
While JMS is typically associated with asynchronous processing, you can
11-
consume messages synchronously. The overloaded `receive(..)` methods provide this
10+
While JMS is typically associated with asynchronous processing, you can consume messages
11+
synchronously. The `receive(..)` methods on `JmsTemplate` and `JmsClient` provide this
1212
functionality. During a synchronous receive, the calling thread blocks until a message
1313
becomes available. This can be a dangerous operation, since the calling thread can
1414
potentially be blocked indefinitely. The `receiveTimeout` property specifies how long

framework-docs/modules/ROOT/pages/integration/jms/sending.adoc

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ If you created the `JmsTemplate` and specified a default destination, the
5959
`send(MessageCreator c)` sends a message to that destination.
6060

6161

62-
[[jms-msg-conversion]]
63-
== Using Message Converters
62+
[[jms-sending-conversion]]
63+
== Using JMS Message Converters
6464

6565
To facilitate the sending of domain model objects, the `JmsTemplate` has
6666
various send methods that take a Java object as an argument for a message's data
@@ -87,7 +87,7 @@ following example shows how to modify a message header and a property after a
8787
[source,java,indent=0,subs="verbatim,quotes"]
8888
----
8989
public void sendWithConversion() {
90-
Map map = new HashMap();
90+
Map<String, String> map = new HashMap<>();
9191
map.put("Name", "Mark");
9292
map.put("Age", new Integer(47));
9393
jmsTemplate.convertAndSend("testQueue", map, new MessagePostProcessor() {
@@ -120,15 +120,43 @@ MapMessage={
120120
}
121121
----
122122

123+
NOTE: This JMS-specific `org.springframework.jms.support.converter.MessageConverter`
124+
arrangement operates on JMS message types and is responsible for immediate conversion
125+
to `jakarta.jms.TextMessage`, `jakarta.jms.BytesMessage`, etc. For a contract supporting
126+
generic message payloads, use `org.springframework.messaging.converter.MessageConverter`
127+
with `JmsMessagingTemplate` or preferably `JmsClient` as your central delegate instead.
123128

124-
[[jms-callbacks]]
125-
== Using `SessionCallback` and `ProducerCallback`
129+
130+
[[jms-sending-jmsclient]]
131+
== Sending a Message with `JmsClient`
132+
133+
[source,java,indent=0,subs="verbatim,quotes"]
134+
----
135+
// Reusable handle, typically created through JmsClient.create(ConnectionFactory)
136+
// For custom conversion, use JmsClient.create(ConnectionFactory, MessageConverter)
137+
private JmsClient jmsClient;
138+
139+
public void sendWithConversion() {
140+
this.jmsClient.destination("myQueue")
141+
.withTimeToLive(1000)
142+
.send("myPayload"); // optionally with a headers Map next to the payload
143+
}
144+
145+
public void sendCustomMessage() {
146+
Message<?> message =
147+
MessageBuilder.withPayload("myPayload").build(); // optionally with headers
148+
this.jmsClient.destination("myQueue")
149+
.withTimeToLive(1000)
150+
.send(message);
151+
}
152+
----
153+
154+
155+
[[jms-sending-callbacks]]
156+
== Using `SessionCallback` and `ProducerCallback` on `JmsTemplate`
126157

127158
While the send operations cover many common usage scenarios, you might sometimes
128159
want to perform multiple operations on a JMS `Session` or `MessageProducer`. The
129160
`SessionCallback` and `ProducerCallback` expose the JMS `Session` and `Session` /
130161
`MessageProducer` pair, respectively. The `execute()` methods on `JmsTemplate` run
131162
these callback methods.
132-
133-
134-

framework-docs/modules/ROOT/pages/integration/jms/using.adoc

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@
44
This section describes how to use Spring's JMS components.
55

66

7-
[[jms-jmstemplate]]
8-
== Using `JmsTemplate`
7+
[[jms-jmstemplate-jmsclient]]
8+
== `JmsTemplate` and `JmsClient`
99

1010
The `JmsTemplate` class is the central class in the JMS core package. It simplifies the
1111
use of JMS, since it handles the creation and release of resources when sending or
1212
synchronously receiving messages.
1313

14+
`JmsClient` is a new API variant in Spring Framework 7.0, following the design of
15+
`JdbcClient` and co. `JmsClient` builds on `JmsTemplate` for straightforward send
16+
and receive operations with customization options per operation.
17+
18+
[[jms-jmstemplate]]
19+
=== Using `JmsTemplate`
20+
1421
Code that uses the `JmsTemplate` needs only to implement callback interfaces that give them
1522
a clearly defined high-level contract. The `MessageCreator` callback interface creates a
1623
message when given a `Session` provided by the calling code in `JmsTemplate`. To
@@ -43,10 +50,23 @@ and then safely inject this shared reference into multiple collaborators. To be
4350
clear, the `JmsTemplate` is stateful, in that it maintains a reference to a
4451
`ConnectionFactory`, but this state is not conversational state.
4552

53+
[[jms-jmsclient]]
54+
=== Using `JmsClient`
55+
4656
As of Spring Framework 4.1, `JmsMessagingTemplate` is built on top of `JmsTemplate`
47-
and provides an integration with the messaging abstraction -- that is,
48-
`org.springframework.messaging.Message`. This lets you create the message to
49-
send in a generic manner.
57+
and provides an integration with the Spring's common messaging abstraction -- that is,
58+
handling `org.springframework.messaging.Message` for sending and receiving,
59+
throwing `org.springframework.messaging.MessagingException` and with payload conversion
60+
going through `org.springframework.messaging.converter.MessageConverter` (with many
61+
common converter implementations available).
62+
63+
As of Spring Framework 7.0, a fluent API called `JmsClient` is available. This provides
64+
customizable operations around `org.springframework.messaging.Message` and throwing
65+
`org.springframework.messaging.MessagingException`, similar to `JmsMessagingTemplate`,
66+
as well as integration with `org.springframework.messaging.converter.MessageConverter`.
67+
A `JmsClient can either be created for a given `ConnectionFactory` or for a given
68+
`JmsTemplate`, in the latter case reusing its settings by default. See
69+
{spring-framework-api}/jms/core/JmsClient.html[`JmsClient`] for usage examples.
5070

5171

5272
[[jms-connections]]
@@ -293,6 +313,3 @@ in an unmanaged environment, you can specify these values through the use of the
293313
properties `sessionTransacted` and `sessionAcknowledgeMode`. When you use a
294314
`PlatformTransactionManager` with `JmsTemplate`, the template is always given a
295315
transactional JMS `Session`.
296-
297-
298-

0 commit comments

Comments
 (0)