JMS Instantiate Connection Factory Example

Usually, JMS Objects such as ConnectionFactories, Queue and Topic instances are looked up from JNDI before being used by the client code. This objects are called "administered objects" in JMS specification terminology.

However, in some cases a JNDI server may not be available or desired. To come to the rescue ActiveMQ also supports the direct instantiation of these administered objects on the client side.

This allows the full set of JMS functionality to be available without requiring a JNDI server!

This example is very simple and based on the simple Queue example, however in this example we instantiate the JMS Queue and ConnectionFactory objects directly.

A wide variety of methods are available for instantiating ConnectionFactory objects. In this example we use a simple method which just takes the server connection details so it knows where to make the connection to.

Other methods are available so all the connection factory parameters can be specified including specifying UDP discovery so the client does not need hard-wired knowledge of where the servers are that it wishes to connect to, or for specifying live-backup pairs of servers for failover.

For more information on instantiating ConnectionFactories directly please consult the user manual and javadoc.

Example step-by-step

To run the example, simply type mvn verify -Pexample from this directory

  1. Instead of looking it up from JNDI we directly instantiate the JMS Queue object. We pass in the name of the JMS Queue in the constructor. The actual JMS Queue must already be deployed on the server.
  2.            
         Queue queue = new ActiveMQQueue("exampleQueue");
            
  3. Instantiate the TransportConfiguration object. The TransportConfiguration instance encapsulates the connection details of the server we're connecting to. In this case we're using Netty as a transport, and we're specifying to connect on port 61617.
  4.            
         Map connectionParams = new HashMap();
    
         connectionParams.put(PORT_PROP_NAME, 61617);
    
         TransportConfiguration transportConfiguration = new TransportConfiguration(NettyConnectorFactory.class.getName(),
                                                                                    connectionParams);
               
               
            
  5. Directly instantiate the JMS ConnectionFactory object using that TransportConfiguration.
  6.            
         ConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(transportConfiguration);
               
            
  7. We create a JMS connection
  8.            connection = cf.createConnection();
            
  9. We create a JMS session. The session is created as non transacted and will auto acknowledge messages.
  10.            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
  11. We create a JMS message producer on the session. This will be used to send the messages.
  12.           MessageProducer messageProducer = session.createProducer(topic);
           
  13. We create a JMS text message that we are going to send.
  14.            TextMessage message = session.createTextMessage("This is a text message");
            
  15. We send message to the queue
  16.            messageProducer.send(message);
            
  17. We create a JMS Message Consumer to receive the message.
  18.            MessageConsumer messageConsumer = session.createConsumer(queue);
            
  19. We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started
  20.            connection.start();
            
  21. The message arrives at the consumer. In this case we use a timeout of 5000 milliseconds but we could use a blocking 'receive()'
  22.            TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
            
  23. And finally, always remember to close your resources after use, in a finally block.
  24.            finally
               {
                  if (connection != null)
                  {
                     connection.close();
                  }
               }