2/08/2010
8:00:00 PM 0

Send JMS Message Using ActiveMQ

library : activemq-core-5.3.0.jar/commons-logging-1.1.jar/j2ee library
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


public class Sender 
{    
    public static void main(String[] args) 
    {                                                   
        Connection connection = null;
        
        try 
        {
            String url = "tcp://localhost:9876";
            String subject = "test_mq";
            
            ConnectionFactory factory = new ActiveMQConnectionFactory(url);
            connection = factory.createConnection();  
            connection.start();
                                                                                                                                        
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            Destination destination = session.createQueue(subject);
            MessageProducer producer = session.createProducer(destination);                    
            producer.setDeliveryMode(javax.jms.DeliveryMode.NON_PERSISTENT);
            producer.setTimeToLive(1000 * 10);
            
            TextMessage message = session.createTextMessage();
            message.setJMSCorrelationID(String.valueOf(System.currentTimeMillis()));
            message.setJMSReplyTo(destination);
            message.setText("Test");            
            producer.send(message);
            
            producer.close();
            session.close();
            connection.stop();
        } 
        catch (JMSException e) 
        {                                                             
            e.printStackTrace();                                                                                                            
        } 
        finally 
        {                                                                            
            if (connection != null) 
            {                                                          
                try { connection.close(); }                                                    
                catch (JMSException e) {}                                                      
            }                                                                                  
        }                                                                                      
    }                                                                                          
}

0 comments:

Post a Comment