import java.util.Properties; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.JMSException; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; import org.hornetq.jms.client.HornetQQueue; public class JMSClient { private static final String JNDI_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory"; private static final String JNDI_CONNECTION_URL = "remote://localhost:4447"; private static final String USER_NAME = "guest"; private static final String USER_PWD = "guestguest"; private static final String JNDI_PUBLIC_NAME_FOR_QUEUE = "jms/queue/test"; private static final String JNDI_PUBLIC_NAME_FOR_CONNECTION_FACTORY = "jms/ServletConnectionFactory"; private static final String[] MESSAGES_TO_SEND = new String[] { "This is a text message", "This is another text message" }; public static void main(final String[] args) throws Exception { InitialContext initialContext = null; Connection connection = null; Session session = null; try { final Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_CONTEXT_FACTORY); env.put(Context.PROVIDER_URL, JNDI_CONNECTION_URL); env.put(Context.SECURITY_PRINCIPAL, USER_NAME); env.put(Context.SECURITY_CREDENTIALS, USER_PWD); // Step 1. Create an initial context to perform the JNDI lookup. initialContext = new InitialContext(env); // Step 2. Perform a lookup on the queue Queue queue = (Queue) initialContext.lookup(JNDI_PUBLIC_NAME_FOR_QUEUE); // Step 3. Perform a lookup on the Connection Factory ConnectionFactory cf = (ConnectionFactory) initialContext .lookup(JNDI_PUBLIC_NAME_FOR_CONNECTION_FACTORY); // Step 4.Create a JMS Connection connection = cf.createConnection(env.getProperty(Context.SECURITY_PRINCIPAL), env.getProperty(Context.SECURITY_CREDENTIALS)); System.out.println("connection created: " + connection); // Step 5. Create a JMS Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Step 6. Create a JMS Message Producer MessageProducer producer = session.createProducer(queue); for ( String data : MESSAGES_TO_SEND ) { // Step 7. Create a Text Message TextMessage message = session.createTextMessage(data); // Step 8. Send the Message producer.send(message); System.out.println("Sent message: " + message.getText()); } // Step 8b. Close the Producer producer.close(); // Step 9. Create a JMS Message Consumer MessageConsumer messageConsumer = session.createConsumer(queue); // Step 10. Start the Connection connection.start(); // Step 11. Receive the message(s) TextMessage messageReceived = null; while ( (messageReceived = (TextMessage) messageConsumer .receive(5000)) != null ) { System.out.println("Received message: " + messageReceived.getText()); } // Step 11b. Stop the Connection connection.stop(); // Step 11c. Close the Producer messageConsumer.close(); // Step 11d. Close the Session session.close(); } catch (Exception e) { e.printStackTrace(); } finally { // Step 12. Be sure to close our JMS resources! if (connection != null) { connection.close(); } if (initialContext != null) { initialContext.close(); } } } }