The @MessageDriven annotation declares an enterprise bean as a message-driven bean (MDB), a component that the application server invokes whenever a message arrives on a JMS queue or topic. It is how Jakarta EE (formerly Java EE) applications consume messages asynchronously, without any client calling the bean directly.
An MDB has no client-visible interface and no state tied to a caller. The container manages a pool of instances, handles transactions and acknowledgement, and calls onMessage for each message. In Jakarta EE 9 and later the annotation lives in jakarta.ejb; in Java EE 8 and earlier it is javax.ejb.MessageDriven.
A message-driven bean is an EJB that implements a message listener interface (usually jakarta.jms.MessageListener) and is annotated with @MessageDriven. Unlike session beans, it is never looked up or injected into callers. A producer sends a message to a destination, and the container selects a pooled instance and delivers it.
This decouples sender and receiver in time and in deployment: the producer does not wait, and consumers can be scaled or restarted independently. Because the container pools instances, several messages can be processed in parallel.
Modern code configures the destination with activationConfig, which is portable across servers, rather than the server-specific mappedName.
Common activation properties are destinationLookup, destinationType (queue or topic), messageSelector, acknowledgeMode, and for topics subscriptionDurability and clientId. Additional keys depend on the JMS provider or resource adapter, for example ActiveMQ Artemis or IBM MQ.
| Attribute | Purpose |
|---|---|
name | Name of the bean (defaults to the class name) |
activationConfig | Array of @ActivationConfigProperty key and value pairs describing the destination and behaviour |
messageListenerInterface | The listener interface, needed if the class implements more than one or none |
mappedName | Vendor-specific destination or resource name; prefer destinationLookup |
description | Free-text description |
By default, an MDB method runs in a container-managed transaction with the attribute REQUIRED. The message acknowledgement is tied to that transaction: if onMessage throws a system exception or you call MessageDrivenContext.setRollbackOnly(), the transaction rolls back and the message is redelivered according to the provider's retry policy, often ending in a dead-letter queue.
Design for this:
You can switch to bean-managed transactions with @TransactionManagement(TransactionManagementType.BEAN), but the default is simpler and safer.
An MDB can inject session beans, EntityManager and @Resource instances like any other EJB, so a typical MDB parses the message and delegates to a service.
An MDB can also send replies using a JMSContext, injected with @Inject.
MDBs remain a solid choice inside Jakarta EE servers such as WildFly, Payara and Open Liberty, especially when you already rely on container-managed transactions. In Spring applications the equivalent is @JmsListener, and in Quarkus or MicroProfile you would use reactive messaging. MDBs need an application server or EJB container; they do not run in plain Java SE.
A session bean is invoked by a client through a reference. An MDB is invoked only by the container when a message arrives and has no client interface.
Yes. It is part of Jakarta Enterprise Beans, but note that the package changed from javax.ejb to jakarta.ejb in Jakarta EE 9.
Yes. Set destinationType to jakarta.jms.Topic and optionally configure a durable subscription so messages are kept while the server is down.
The container keeps a pool of instances and can process messages concurrently. Pool size is configured on the server, not through the annotation.
@MessageDriven turns a class into a container-managed JMS consumer. Configure the destination through activationConfig, rely on container-managed transactions for reliable delivery, make handlers idempotent, and use the jakarta.* packages on current Jakarta EE servers.