What’s New in Spring 7.0? Exploring the Modern JmsClient API

Spring 7.0 introduces JmsClient, a modern, stream‑oriented API for JMS messaging that mirrors the design of JdbcClient and RestClient, offering fluent creation, flexible destination configuration, rich QoS options, and simplified send/receive operations compared to the traditional JmsTemplate.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
What’s New in Spring 7.0? Exploring the Modern JmsClient API

Overview

JMS (Java Message Service) is the Java API specification for sending messages between distributed applications, supporting point‑to‑point (queues) and publish/subscribe (topics) messaging models. JmsClient is a streaming JMS client that works with Spring’s generic Message abstraction or directly with payload values, and it throws MessagingException instead of the JMS‑specific JmsException.

In fact, JmsClient is a modern replacement for JmsMessagingTemplate, delegating the actual work to JmsTemplate.

Core Features

1. Streaming API Design

JmsClient

follows the same fluent API pattern as JdbcClient and RestClient, making code concise and readable:

// Send a message to a queue
client.destination("myQueue")
    .withTimeToLive(1000)
    .send("myPayload");

// Receive a message from a queue
Optional<String> payload = client.destination("myQueue")
    .withReceiveTimeout(1000)
    .receive(String.class);

2. Multiple Creation Options

JmsClient

provides several static factory methods for creating instances:

// From a ConnectionFactory
JmsClient client = JmsClient.create(connectionFactory);

// From a ConnectionFactory and MessageConverter
JmsClient client = JmsClient.create(connectionFactory, messageConverter);

// From an existing JmsTemplate
JmsClient client = JmsClient.create(jmsTemplate);

// From a JmsTemplate and MessageConverter
JmsClient client = JmsClient.create(jmsTemplate, messageConverter);

3. Flexible Destination Configuration

Supports various ways to specify the JMS destination:

// Using a destination name
client.destination("myQueue");

// Using a Destination object
client.destination(jmsDestination);

Main Operation Types

1. Send Operations

JmsClient

offers multiple ways to send messages:

// Send a simple payload
client.destination("myQueue").send("Hello World");

// Send payload with headers
Map<String, Object> headers = Map.of("priority", 5);
client.destination("myQueue").send("Hello World", headers);

// Send a full Message object
Message<?> message = MessageBuilder.withPayload("Hello World")
    .setHeader("customHeader", "value")
    .build();
client.destination("myQueue").send(message);

2. Receive Operations

Supports several ways to receive messages:

// Receive and convert to a specific type
Optional<String> payload = client.destination("myQueue").receive(String.class);

// Receive the full Message object
Optional<Message<?>> message = client.destination("myQueue").receive();

// Receive with a selector
Optional<String> payload = client.destination("myQueue")
    .receive("priority > 5", String.class);

3. Send‑and‑Receive (Request‑Response)

Supports synchronous request‑response patterns:

// Send a request and receive a response
Optional<String> response = client.destination("requestQueue")
    .sendAndReceive("request payload", String.class);

// With headers
Map<String, Object> headers = Map.of("requestId", "123");
Optional<String> response = client.destination("requestQueue")
    .sendAndReceive("request payload", headers, String.class);

// Send a Message object and receive a response
Message<?> requestMessage = MessageBuilder.withPayload("request").build();
Optional<Message<?>> responseMessage = client.destination("requestQueue")
    .sendAndReceive(requestMessage);

QoS Settings

1. Message Time‑to‑Live

client.destination("myQueue")
    .withTimeToLive(30000) // 30 seconds
    .send("time-sensitive message");

2. Message Priority

client.destination("myQueue")
    .withPriority(9) // high priority
    .send("urgent message");

3. Persistence

client.destination("myQueue")
    .withDeliveryPersistent(true) // persistent message
    .send("important message");

4. Delivery Delay

client.destination("myQueue")
    .withDeliveryDelay(5000) // delay 5 seconds
    .send("delayed message");

5. Receive Timeout

Optional<String> payload = client.destination("myQueue")
    .withReceiveTimeout(10000) // 10 seconds
    .receive(String.class);

Comparison with Traditional Approach

Traditional JmsTemplate

@Service
public class TraditionalJmsService {
    @Autowired
    private JmsTemplate jmsTemplate;

    public void sendMessage(String message) {
        jmsTemplate.setTimeToLive(30000);
        jmsTemplate.setPriority(5);
        jmsTemplate.convertAndSend("myQueue", message);
    }

    public String receiveMessage() {
        jmsTemplate.setReceiveTimeout(5000);
        return (String) jmsTemplate.receiveAndConvert("myQueue");
    }
}

Modern JmsClient

@Service
public class ModernJmsService {
    private final JmsClient jmsClient;

    public void sendMessage(String message) {
        jmsClient.destination("myQueue")
            .withTimeToLive(30000)
            .withPriority(5)
            .send(message);
    }

    public Optional<String> receiveMessage() {
        return jmsClient.destination("myQueue")
            .withReceiveTimeout(5000)
            .receive(String.class);
    }
}

Conclusion

JmsClient

is a key new feature in Spring 7.0 that provides a modern, user‑friendly API for JMS operations. Its fluent design, type safety, and consistent exception handling make JMS programming simpler and more intuitive.

For developers familiar with JdbcClient and RestClient, JmsClient offers a similar experience, reducing the learning curve and offering a solid foundation for new projects to handle JMS messaging in a contemporary way.

Javabackend developmentSpringJMSJmsClient
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.