/*
* Copyright 2009 Red Hat, Inc.
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.hornetq.javaee.example;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
/**
* @author <a href="mailto:andy.taylor@jboss.org">Andy Taylor</a>
*/
public class MDBMessageSendTxClientExample
{
public static void main(String[] args) throws Exception
{
Connection connection = null;
InitialContext initialContext = null;
try
{
//Step 1. Create an initial context to perform the JNDI lookup.
initialContext = new InitialContext();
//Step 2. Perfom a lookup on the queue
Queue queue = (Queue) initialContext.lookup("/queue/testQueue");
//Step 3. Perform a lookup on the Connection Factory
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
//Step 4.Create a JMS Connection
connection = cf.createConnection();
//Step 5. Create a JMS Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//Step 6. Create a JMS Message Producer
MessageProducer producer = session.createProducer(queue);
//Step 7. Create a Text Message
TextMessage message = session.createTextMessage("This is a text message");
System.out.println("Sent message: " + message.getText());
//Step 8. Send the Message
producer.send(message);
//Step 15. We lookup the reply queue
queue = (Queue) initialContext.lookup("/queue/replyQueue");
//Step 16. We create a JMS message consumer
MessageConsumer messageConsumer = session.createConsumer(queue);
//Step 17. We start the connedction so we can receive messages
connection.start();
//Step 18. We receive the message and print it out
message = (TextMessage) messageConsumer.receive(5000);
System.out.println("message.getText() = " + message.getText());
}
finally
{
//Step 19. Be sure to close our JMS resources!
if (initialContext != null)
{
initialContext.close();
}
if(connection != null)
{
connection.close();
}
}
}
}