// Step 7. Configure the JMS Queue
JMSQueueConfiguration queueConfig = new JMSQueueConfigurationImpl("queue1", null, false, "/queue/queue1");
jmsConfig.getQueueConfigurations().add(queueConfig);
// Step 8. Start the JMS Server using the HornetQ core server and the JMS configuration
JMSServerManager jmsServer = new JMSServerManagerImpl(hornetqServer, jmsConfig);
jmsServer.start();
System.out.println("Started Embedded JMS Server");
// Step 9. Lookup JMS resources defined in the configuration
ConnectionFactory cf = (ConnectionFactory)context.lookup("/cf");
Queue queue = (Queue)context.lookup("/queue/queue1");
// Step 10. Send and receive a message using JMS API
Connection connection = null;
try
{
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
TextMessage message = session.createTextMessage("Hello sent at " + new Date());
System.out.println("Sending message: " + message.getText());
producer.send(message);
MessageConsumer messageConsumer = session.createConsumer(queue);
connection.start();
TextMessage messageReceived = (TextMessage)messageConsumer.receive(1000);
System.out.println("Received message:" + messageReceived.getText());
}
finally
{
if (connection != null)
{
connection.close();
}
// Step 11. Stop the JMS server
jmsServer.stop();
System.out.println("Stopped the JMS Server");
// Step 12. Stop the JNDI server
naming.stop();
jndiServer.stop();