All Downloads are FREE. Search and download functionalities are using the official Maven repository.

examples.javaee.hajndi.readme.html Maven / Gradle / Ivy

The newest version!

  
    HornetQ Java EE HA-JNDI Example
    
    
    
  
  
     

Java EE HA-JNDI Example

This example demonstrates the use of High Availability JNDI (HA-JNDI) to lookup HornetQ JMS Connection Factories, Queues and Topics.

With normal JNDI you need to configure the client with the specific connection parameters (i.e. host and port) of the JNDI server from which you want to perform look-ups.

This means if that server crashes, or is not available you won't be able to perform lookups.

One solution would be for the client to maintain the connection params of all JNDI servers in the cluster, but this is not practical.

With HA-JNDI the client can be simplify configured with UDP address parameters and can transparently perform JNDI lookups without having to worry about a particular server being unavailable.

HA-JNDI is a service of JBoss Application Server and is not available by default when running against a stand-alone HornetQ instance.

An alternative approach is to avoid JNDI together and directly instantiate JMS Connection Factory, Queue and Topic instances on the client side. HornetQ Connection Factory instances can also be configured to use UDP discovery so the specific details of the available servers are not required on the client side.

For more information on instantiating Connection Factory objects directly please see the user manual and the Instantiate Connection Factory example.

For more information on HA-JNDI, please consult the JBoss Application Server Clustering Documentation.

This example demonstrates a simple symmetric clustering configuration, and failover on JNDI (HAJNDI).

Example configuration

To run the example, you need to download JBoss AS 5.x and create a clustered configuration for HornetQ.

Please refer to HornetQ Quickstart guide to install it in JBoss AS 5

To run this example, we will need two clustered profiles, one for each server instance:

  • Copy the directory $JBOSS_HOME/server/all-with-hornetq to $JBOSS_HOME/server/all-with-hornetq_2
  • Start the first server with ./run.sh -c all-with-hornetq
  • Start the second server with ./run.sh -c all-with-hornetq_2 -Djboss.service.binding.set=ports-01
  • Example step by step

    1. Create a JNDI Context using HAJNDI Properties.

      This JNDI is performing auto-discovery of the servers, by using the default UDP properties.

      You will find more information about these properties at the

    2.            Hashtable jndiParameters = new Hashtable();
                 jndiParameters.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                 jndiParameters.put("java.naming.factory.url.pkgs=", "org.jboss.naming:org.jnp.interfaces");
               
                 initialContext = new InitialContext(jndiParameters);
              
    3. Perform lookups in a loop. As long as you have at least one server alive, these lookups will still work fine
                  for (int i = 0; i < 100; i++)
                  {
                     ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
               
    4. Create and close a JMS Connection, just to show the downloaded Connection Factory is working
                 connection = cf.createConnection();
                 connection.close();
              
    5. As the example sleeps here, use this time to kill one of the servers. You will realise that lookups will still work as long as you have a live server
                 System.out.println("Connection " + i + " was created and closed. If you kill any of the servers now, the lookup operation on Step 2 will still work fine");
                 Thread.sleep(5000);
              
    6. And finally, always remember to close your JMS connections and resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
    7.            finally
                 {
                    if (initialContext != null)
                    {
                      initialContext.close();
                    }
                    if (connection != null)
                    {
                       connection.close();
                    }
                 }
              




    © 2015 - 2025 Weber Informatics LLC | Privacy Policy