Jakarta Messaging 3.1 on Valkey and Redis: A Walkthrough
In the world of enterprise Java applications, Jakarta Messaging 3.1 is a standard nearly all developers know and use. For a broader look at how Redisson implements JMS on Valkey and Redis, see our JMS messaging overview. Also known as the Java Message Service (JMS) API, Jakarta Messaging 3.1 is often used with heavyweight message brokers such as ActiveMQ or IBM MQ. While these enterprise-grade message brokers work well for their intended purpose, they add significant weight to your tech stack in the form of standalone servers and the need for a dedicated team to maintain them.
Redisson PRO, the Valkey and Redis client for Java developers, offers a much lighter, yet equally powerful, JMS API alternative. With its fully TCK-passing JMS 3.1 implementation on top of your existing Valkey or Redis service, Redisson PRO lets you use the fast, reliable caching infrastructure you already have for enterprise-grade distributed Java applications. In this way, Redisson PRO shrinks your tech stack, cutting hardware costs and keeping your app architecture clean and simple. Here's how it works.
Prerequisites and Dependencies for Jakarta Messaging 3.1 on Valkey/Redis
You only need a few basic tools to get started with Jakarta Messaging 3.1 and Redisson PRO. First, you need Java 11 or higher and Jakarta EE 10. Of course, you'll also need a Valkey or Redis instance up and running.
After that, setting up your Java project is very straightforward. In fact, you only need to add one Maven dependency to your build file. Just include the pro.redisson:redisson-jms-31 artifact for the JMS 3.1 specification. Full API details are available in the JMS messaging documentation.
Redisson PRO also supports older enterprise stacks. If you maintain legacy systems, you can use the -jms-20 or -jms-30 artifacts instead.
Creating a ConnectionFactory in Your Java Code
The RedissonConnectionFactory implements the standard jakarta.jms.ConnectionFactory, along with the QueueConnectionFactory and TopicConnectionFactory interfaces.
Setup is very simple, as you first configure a standard Redisson client, then pass that client to the ConnectionFactory:
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.jms.RedissonConnectionFactory;
Config config = new Config();
config.useSingleServer()
.setAddress("redis://127.0.0.1:6379");
RedissonClient redisson = Redisson.create(config);
ConnectionFactory cf = new RedissonConnectionFactory(redisson);
Point-to-Point Sending and Receiving
Redisson PRO's JMS API supports both point-to-point message sending and receiving, as well as pub/sub topics. First, here's how you use a JMSContext object for point-to-point:
try (JMSContext ctx = cf.createContext()) {
Queue queue = ctx.createQueue("orders");
ctx.createProducer()
.setDeliveryDelay(1000)
.setPriority(7)
.send(queue, "order-456");
String body = ctx.createConsumer(queue)
.receiveBody(String.class, 5000);
}
This code creates an orders queue and builds a producer. Next, it sets a brief delivery delay and a high-priority flag (7 out of a possible 10). Then, it sends the payload. Finally, a consumer waits up to 5 seconds to receive the message (noted as 5,000 milliseconds in the code).
Under the hood, the Redisson PRO RReliableQueue structure gives you powerful enterprise features like visibility timeouts, delivery limits and dead letter queues. You can configure these behaviors per destination to suit your application's needs.
Pub/Sub Topics and Durable Subscriptions
When message data must reach many places at once, you need pub/sub topics.
Redisson PRO handles this equally well as point-to-point messaging. Take a look at this code sample:
try (JMSContext ctx = cf.createContext()) {
Topic topic = ctx.createTopic("events");
JMSConsumer subscriber = ctx.createDurableConsumer(topic, "my-durable-sub");
ctx.createProducer().send(topic, "event-1");
String event = subscriber.receiveBody(String.class, 5000);
}
The consumer in this code sample is a durable consumer, which means it survives client restarts. If your server crashes and goes offline, the broker safely holds the messages. The consumer receives them when it reconnects. With non-durable subscriptions, messages are lost the moment a consumer disconnects, leading to potential data loss during downtime or network congestion.
Redisson PRO's RReliablePubSubTopic, the engine behind Reliable PubSub, gives you precise control over how long to hang on to data with its retention modes. You can also use settings such as SUBSCRIPTION_REQUIRED_DELETE_PROCESSED to automatically purge messages once all subscribers have finished their work.
Using Global Defaults or Per-Destination Configs
Enterprise applications often manage dozens of queues and topics. Configuring each one individually can waste significant development time, so Redisson PRO lets you set global defaults for all queues or topics.
Simply call cf.setQueueConfig(defaultConfig) to apply your default configuration. This allows you to build your rules once and handle the exceptions on a per-destination basis. If you want to override the defaults for a specific queue (the "orders" queue in this case), call cf.setQueueConfig("orders", overrideConfig). Similarly, to override the defaults for a specific topic (the "events" topic in this example), you can call cf.setTopicConfig("events", overrideConfig).
The global default applies unless you call a per-destination config. If you don't define a config at all, the built-in Redisson defaults apply. These precedence rules also apply to topics and their individual subscriptions.
Jakarta Messaging 3.1 Without a Heavy Tech Stack
For years, Java developers have accepted the idea that their messaging infrastructure must be built on industry titans like ActiveMQ or IBM MQ. Jakarta Messaging 3.1 provides the glue that holds this heavy tech stack together. Now, Redisson PRO offers a lighter yet powerful option that retains the familiarity of the JMS API.
If you're currently using RabbitMQ and evaluating alternatives, see how Reliable PubSub compares to RabbitMQ across delivery guarantees, DLQ handling, and consumer models.
You can take advantage of the speed and flexibility of your Valkey or Redis instance and simplify your tech stack with Redisson PRO. Read about Redisson's Reliable Queue to learn more.