# 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 HornetQ 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 `./build.sh` (or `build.bat` on windows) 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.

         Queue queue = new HornetQQueue("exampleQueue");

2. 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 5446.

         Map connectionParams = new HashMap();

         connectionParams.put(PORT_PROP_NAME, 5446);

         TransportConfiguration transportConfiguration = new TransportConfiguration(NettyConnectorFactory.class.getName(),
                                                                                    connectionParams);

3. Directly instantiate the JMS ConnectionFactory object using that TransportConfiguration.

         ConnectionFactory cf = HornetQJMSClient.createConnectionFactoryWithoutHA(transportConfiguration);

4. We create a JMS connection

               connection = cf.createConnection();

5. We create a JMS session. The session is created as non transacted and will auto acknowledge messages.

               Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

6. We create a JMS message producer on the session. This will be used to send the messages.

              MessageProducer messageProducer = session.createProducer(topic);

7. We create a JMS text message that we are going to send.

               TextMessage message = session.createTextMessage("This is a text message");

8. We send message to the queue

               messageProducer.send(message);

9. We create a JMS Message Consumer to receive the message.

               MessageConsumer messageConsumer = session.createConsumer(queue);

10. We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started

               connection.start();

11. The message arrives at the consumer. In this case we use a timeout of 5000 milliseconds but we could use a blocking 'receive()'

               TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);

12. And finally, always remember to close your resources after use, in a `finally` block.

               finally
               {
                  if (connection != null)
                  {
                     connection.close();
                  }
               }