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.
To run the example, simply type mvn verify -Pexample
from this directory
Queue queue = new ActiveMQQueue("exampleQueue");
Map connectionParams = new HashMap();
connectionParams.put(PORT_PROP_NAME, 61617);
TransportConfiguration transportConfiguration = new TransportConfiguration(NettyConnectorFactory.class.getName(),
connectionParams);
ConnectionFactory cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(transportConfiguration);
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(topic);
TextMessage message = session.createTextMessage("This is a text message");
messageProducer.send(message);
MessageConsumer messageConsumer = session.createConsumer(queue);
connection.start();
TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
finally
block.
finally
{
if (connection != null)
{
connection.close();
}
}