Java Integration (JNDI)
This page provides steps to configure a Java client to connect with JMS Bridge using Java Naming and Directory Interface (JNDI). It covers setting up the JNDI context, creating producer and consumer clients, verifying message flow with HawtIO, and troubleshooting common issues.
Prerequisites
Section titled “Prerequisites”- JMS Bridge is running and accessible.
- A JNDI context is configured via a
jndi.properties
file with the necessary connection factory and queue definitions.
Setting Up the Java Client
Section titled “Setting Up the Java Client”-
Configure JNDI Properties
Create a
jndi.properties
file in the classpath (e.g.,src/main/resources/jndi.properties
) with the following content:java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactoryconnectionFactory.ConnectionFactory=tcp://<hostname>:61616queue.demoQueue=demo-queue- Specifies the JNDI initial context factory for ActiveMQ Artemis.
- Uses
tcp://
for JNDI lookup, but the connection factory uses the AMQP protocol. - Maps the logical queue name
demoQueue
to the physical queuedemo-queue
.
-
Create JNDI Context and Connection Factory
InitialContext context = new InitialContext();ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("ConnectionFactory");Queue queue = (Queue) context.lookup("demoQueue");- Initializes the JNDI context, which automatically loads
jndi.properties
from the classpath. - Retrieves the connection factory and queue from the JNDI context.
- Initializes the JNDI context, which automatically loads
-
Set Up Producer
try (Connection connection = connectionFactory.createConnection()) {connection.start();Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);MessageProducer producer = session.createProducer(queue);TextMessage message = session.createTextMessage("Hello, JMS Bridge!");producer.send(message);System.out.println("Message sent: " + message.getText());}- Creates a connection and session, sends a text message to the queue, and closes resources.
-
Set Up Consumer
try (Connection connection = connectionFactory.createConnection()) {connection.start();Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);MessageConsumer consumer = session.createConsumer(queue);consumer.setMessageListener(message -> {try {if (message instanceof TextMessage) {System.out.println("Received: " + ((TextMessage) message).getText());}} catch (JMSException e) {e.printStackTrace();}});Thread.sleep(5000); // Keep consumer alive to receive messages}- Creates a consumer that listens for messages on the queue and processes them.
Verifying Message Flow Using HawtIO
Section titled “Verifying Message Flow Using HawtIO”-
Access HawtIO Dashboard
- Ensure HawtIO is running on the designated port (default: 8161).
-
Monitor the Queue
- Navigate to the “Addresses” tab in HawtIO.
- Locate the queue (e.g.,
demo-queue
) used in your application. - After running the producer, check if the “Messages Sent” count increases.
-
Verify Message Consumption
- Run the consumer client.
- In HawtIO, confirm that the “Messages Acknowledged” count increases, indicating successful message receipt.
Troubleshooting
Section titled “Troubleshooting”JNDI Lookup Failure
Section titled “JNDI Lookup Failure”-
Symptom: Application throws
NamingException
during context lookup. -
Possible Causes:
jndi.properties
file is missing or not in the classpath.- Incorrect properties in
jndi.properties
(e.g., wrongjava.naming.factory.initial
orjava.naming.provider.url
). - JMS Bridge is not running or unreachable.
-
Resolution:
- Verify the broker is running:
Terminal window lsof -i :61616 - Ensure
jndi.properties
is in the classpath (e.g.,src/main/resources
). - Check properties for accuracy (e.g.,
tcp://localhost:61616
). - Confirm the connection factory and queue are defined in
jndi.properties
.
- Verify the broker is running:
Queue Does Not Exist
Section titled “Queue Does Not Exist”-
Symptoms:
- Exceptions during producer/consumer creation.
- Messages not delivered or received.
-
Possible Causes:
- Queue name mismatch between
jndi.properties
and broker configuration. - Queue does not exist, and auto-creation is disabled.
- Queue name mismatch between
-
Resolution:
- Ensure the queue name in
jndi.properties
(demoQueue
) maps to the correct physical queue (demo-queue
). - Enable
auto-create-queues
inbroker.xml
or manually create the queue via HawtIO.
- Ensure the queue name in
Protocol Mismatch
Section titled “Protocol Mismatch”-
Symptom: Errors like “AMQP protocol not supported” or connection failures.
-
Possible Causes:
- Incorrect protocol in
connectionFactory.ConnectionFactory
property injndi.properties
.
- Incorrect protocol in
-
Resolution:
- For TCP, ensure the connection factory is configured with
tcp://<hostname>:61616
.
- For TCP, ensure the connection factory is configured with
Message Not Being Received
Section titled “Message Not Being Received”-
Symptom: Consumer does not receive messages, logs “No message received.”
-
Possible Causes:
- Producer failed to send the message.
- Consumer subscribed to the wrong queue.
- Message expired before delivery.
-
Resolution:
- Verify producer sent the message successfully (check HawtIO “Messages Sent”).
- Confirm queue names match in
jndi.properties
and broker configuration. - Check message expiration settings in the producer code.
Missing Dependencies
Section titled “Missing Dependencies”-
Symptom:
ClassNotFoundException
orNoClassDefFoundError
during execution. -
Possible Causes:
- Missing ActiveMQ Artemis client libraries.
-
Resolution:
- Add the required dependencies to your project (e.g., using Maven):
<dependency><groupId>org.apache.activemq</groupId><artifactId>artemis-jms-client</artifactId><version>2.36.0</version></dependency>
- Add the required dependencies to your project (e.g., using Maven):