Build a HornetQ-backed Java Message-Driven Bean in NetBeans
Creating a Java Message-Driven Bean with HornetQ in NetBeans separates transport handling from business logic. HornetQ is the message broker, the application server activates the MDB, and Java code receives messages through the JMS listener contract.
The NetBeans IDE handles the enterprise project, EJB module and deployment, while GlassFish or a compatible Java EE server supplies the EJB container and JMS resources. Java EE uses javax.ejb; Jakarta EE uses jakarta.ejb.
How the message-driven architecture fits
An MDB suits work that continues after the caller releases control, such as order events, notifications and reconciliation. A queue gives each message to one consumer, while a topic fans messages out to subscribers; durable subscriptions let an absent subscriber catch up.
| Pattern | Trigger | Main strength | Main constraint |
|---|---|---|---|
| MDB with HornetQ | JMS delivery | Container-managed consumption and acknowledgement | Broker and resource-adapter setup |
| JMS client | Explicit application call | Direct control over producer and consumer | Polling and client lifecycle are application code |
| Synchronous session bean | Caller request | Immediate response within the caller flow | Couples the caller to remote work |
| JSR 352 batch job | Timer or schedule | Efficient bulk processing | Runs on a schedule rather than message delivery |
For scheduled bulk work, a JSR 352 batch walkthrough offers a contrast: batch jobs run on a timer, while an MDB reacts to each broker message and uses JMS-aware delivery and failure handling.
Create the enterprise project in NetBeans
Install NetBeans with Java EE support and a compatible server, typically GlassFish for embedded HornetQ. Newer bundles may use ActiveMQ Artemis, so check the provider before copying old JNDI names.
Create an Enterprise Application, add an EJB Module, and use the wizard to create a Message-Driven Bean. Enter its class and package, choose the server and destination, and let NetBeans generate the annotated listener class. Keep messaging code in its own module.
For local development, start the server before the application and use its built-in JMS provider. Check the provider in the administration console: activation properties reference resources but do not create them.
Implement the JMS listener contract
@MessageDriven binds the class to a destination, while activationConfig names the JMS destination and acknowledgement behaviour. For Java EE, import javax.ejb and javax.jms; use the jakarta equivalents for a Jakarta EE target.
@MessageDriven(
name = "orderEventBean",
activationConfig = {
@ActivationConfigProperty(
propertyName = "destinationType",
propertyValue = "jms/queue/Orders"),
@ActivationConfigProperty(
propertyName = "destinationName",
propertyValue = "Orders"),
@ActivationConfigProperty(
propertyName = "acknowledgeMode",
propertyValue = "Auto")
})
public class OrderEventBean implements MessageListener {
@Override
public void onMessage(Message message) {
// Process the validated payload.
}
}
With container-managed transactions, return normally to commit and acknowledge the message. On failure, let the exception escape or mark the invocation for rollback; make the handler idempotent because redelivery is expected. @TransactionAttribute(TransactionAttributeType.REQUIRED) explicitly joins an existing transaction.
Read a TextMessage with getText, check its type before casting, and validate fields before changing persistent state. Propagate a correlation ID into logs so a producer’s request can be traced through the queue, and version payloads as consumers evolve.
Connect HornetQ and deploy the resource adapter
In GlassFish, create a JMS destination named Orders, select the embedded HornetQ provider and map it to a physical queue. Create a connection factory for producers and check that the MDB resource adapter is deployed. GlassFish activation names commonly resemble jms/queue/Orders for the type and Orders for the destination.
For an external HornetQ server, deploy its resource adapter, configure the connector and connection-factory JNDI entry, and point the destination at the remote broker. Keep connection details in server configuration. A lightweight colour-coded netstat viewer can show whether the expected port is listening and which connection states are active; broker logs and JNDI lookups establish the actual cause.
Package the EJB module in the enterprise archive and deploy it to the server profile that owns the JMS resources. If the destination cannot be resolved, verify the resource adapter, then the queue name and queue-versus-topic setting.
Test delivery and diagnose failures
Run the application from NetBeans, send a small text message to Orders with a tiny producer, and inspect destination statistics in the server console. The MDB should appear as an active consumer, and NetBeans can debug onMessage. Check server logs as well as the IDE console when the server runs separately.
Common failures include a missing destination, a destinationType pointing at a topic when the resource is a queue, an undeployed resource adapter, invalid JNDI credentials or a selector that matches no message. Restarting the broker may clear a transient connection fault but not a configuration mismatch. Preserve safe test payloads and record each reproduction sequence.
If messages arrive but throughput drops, measure the application instead of guessing. NetBeans CPU profiling helps identify an expensive parsing path or accidental loop, while queue depth and redelivery counters show whether the MDB is actually the bottleneck.
Production practices for Australian teams
Australian teams often span Sydney, Melbourne, Brisbane and Perth, where daylight-saving changes shift test windows. Store timestamps in UTC and show local times in runbooks. Hosting choices often favour Sydney or Melbourne cloud regions, while local suppliers may invoice in AUD with GST and expect support during Australian business hours.
Use Australian English such as “queue” and “organisation” in documentation. A friendly “give it a go” can ease collaboration, but escalation steps must still identify ownership, broker failover actions and response times clearly.
Deployment is easier to audit when two concise checklists sit beside the runbook. Use this release checklist:
- Pin the NetBeans, JDK and application-server versions.
- Record the destination, connection factory and resource adapter.
- Exercise success, rollback and redelivery paths.
- Set broker, connection and transaction timeouts deliberately.
- Confirm queue-depth alerts and access to operational logs.
After deployment, use this runbook checklist:
- Name primary and backup operators for each support window.
- Test failover during daylight-saving transitions.
- Document broker version, endpoint and credential rotation.
- Record dead-lettered messages and their review owner.
- Track consumer lag against service targets shown in local time.