Skip to content

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.

  • JMS Bridge is running and accessible.
  • A JNDI context is configured via a jndi.properties file with the necessary connection factory and queue definitions.

  1. 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.ActiveMQInitialContextFactory
    connectionFactory.ConnectionFactory=tcp://<hostname>:61616
    queue.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 queue demo-queue.
  2. 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.
  3. 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.
  4. 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.

  1. Access HawtIO Dashboard

    • Ensure HawtIO is running on the designated port (default: 8161).
  2. 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.
  3. Verify Message Consumption

    • Run the consumer client.
    • In HawtIO, confirm that the “Messages Acknowledged” count increases, indicating successful message receipt.

  • 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., wrong java.naming.factory.initial or java.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.
  • 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.
  • Resolution:

    • Ensure the queue name in jndi.properties (demoQueue) maps to the correct physical queue (demo-queue).
    • Enable auto-create-queues in broker.xml or manually create the queue via HawtIO.
  • Symptom: Errors like “AMQP protocol not supported” or connection failures.

  • Possible Causes:

    • Incorrect protocol in connectionFactory.ConnectionFactory property in jndi.properties.
  • Resolution:

    • For TCP, ensure the connection factory is configured with tcp://<hostname>:61616.
  • 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.
  • Symptom: ClassNotFoundException or NoClassDefFoundError 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>