In ActiveMQ, as in every Jakarta Messaging (JMS) provider, a Session is a single-threaded context that creates producers, consumers and messages, and that controls transactions and acknowledgement for the messages it handles. You get it from a Connection, and you use one session per thread.
ActiveMQ Classic 6 uses the jakarta.jms packages, while ActiveMQ 5.x uses javax.jms. The Session API is the same in both; only the import prefix changes.
The objects form a chain: a ConnectionFactory creates a Connection (the network link to the broker), and the connection creates one or more Session objects. The session then creates everything you use to send and receive.
| Object | Created by | Role |
|---|---|---|
Connection | ConnectionFactory | Socket to the broker, thread-safe |
Session | Connection | Single-threaded work context, transactions, acknowledgement |
MessageProducer | Session | Sends to a destination |
MessageConsumer | Session | Receives from a destination |
Message | Session | Text, bytes, map, stream or object payload |
Since JMS 2.0, Connection and Session implement AutoCloseable, so try-with-resources closes them in the right order.
createSession(false, Session.AUTO_ACKNOWLEDGE) means a non-transacted session where the provider acknowledges each message automatically. The older createSession(boolean, int) form is still valid; JMS 2.0 also adds createSession(int sessionMode).
A connection must be started with connection.start() before a consumer gets messages. Producers do not need it. For push delivery, call consumer.setMessageListener(...); the session then runs the listener on its own delivery thread.
A session creates TextMessage, BytesMessage, MapMessage, StreamMessage and ObjectMessage. Prefer text (JSON) or bytes with a schema. ObjectMessage relies on Java serialization, and ActiveMQ restricts it to trusted packages (setTrustedPackages on the connection factory) to prevent deserialization attacks. Avoid it in new code.
The two arguments of createSession decide the delivery guarantees.
| Mode | Behaviour |
|---|---|
Session.AUTO_ACKNOWLEDGE | Acknowledged automatically after a successful receive or listener return |
Session.CLIENT_ACKNOWLEDGE | You call message.acknowledge(), which acknowledges all messages consumed so far in the session |
Session.DUPS_OK_ACKNOWLEDGE | Lazy acknowledgement, duplicates are possible after a failure |
Session.SESSION_TRANSACTED | Used with a transacted session, commit() or rollback() ends the unit of work |
A transacted session groups sends and receives:
A transaction covers one session and one broker. For a database plus broker in one atomic unit, use XA through a Jakarta Transactions manager, or design for idempotent consumers.
A Session is not thread-safe. Do not share it between threads; create one session per thread, or per listener. The Connection is the only object you may share freely.
JMS 2.0 added JMSContext, which combines connection and session, and JMSProducer:
In Spring Boot, JmsTemplate and @JmsListener manage sessions for you, usually with a caching or pooled connection factory so you do not open a new connection per message.
No. A session is single-threaded. Use one session per thread, or let a framework such as Spring JMS manage them.
The connection is the heavy, shared link to the broker. Sessions are lightweight contexts on top of it that hold transaction and acknowledgement state.
The most common cause is a missing connection.start(). Others are a wrong destination name or a selector that filters every message.
When several sends and receives must succeed or fail together, such as consume from one queue and publish to another.
The Session creates producers, consumers and messages, and defines acknowledgement and transaction behaviour. Keep it single-threaded, call start() on the connection before consuming, close resources with try-with-resources, and pick the acknowledgement mode that matches your delivery needs. Use JMSContext or Spring JMS to reduce boilerplate.