JMS--JAVA--Hello World

添加上面的JAR包

 

//JMS
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

 

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.Message;


public class jms implements MessageListener {

 
 public void send()
 {
  
  try {
            // Create a ConnectionFactory
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");

            // Create a Connection
            Connection connection = connectionFactory.createConnection();
            connection.start();

            // Create a Session
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // Create the destination (Topic or Queue)
            Destination destination = session.createQueue("TEST.FOO");

            // Create a MessageProducer from the Session to the Topic or Queue
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            // Create a messages
            String text = "Hello world! From: ";


          // TextMessage message = session.createTextMessage(text);

           ActiveMQTextMessage message=new ActiveMQTextMessage();
           message.setText(text);


           
            //message.set

            // Tell the producer to send the message
            System.out.println("Sent message OK ");
            producer.send(message);

            // Clean up
            session.close();
            connection.close();
        }
        catch (Exception e) {
            System.out.println("Caught: " + e);
            e.printStackTrace();
        }
  
 
  
 }
 
 
 public void receive()
 {
  
   try {

             // Create a ConnectionFactory
             ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");

             // Create a Connection
             Connection connection = connectionFactory.createConnection();
             connection.start();

             //connection.setExceptionListener(this);

             // Create a Session
             Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

             // Create the destination (Topic or Queue)
             Destination destination = session.createQueue("TEST.FOO");
             

             // Create a MessageConsumer from the Session to the Topic or Queue
             MessageConsumer consumer = session.createConsumer(destination);
             

             //监听模式
             consumer.setMessageListener(this);


             //主动接受模式 
//             // Wait for a message
//             Message message = consumer.receive(1000);
//
//             if (message instanceof TextMessage) {
//                 TextMessage textMessage = (TextMessage) message;
//                 String text = textMessage.getText();
//                 System.out.println("Received: " + text);
//             } else {
//                 System.out.println("Received: " + message);
//             }
//
//             consumer.close();
//             session.close();
//             connection.close();
            
            
         } catch (Exception e) {
             System.out.println("Caught: " + e);
             e.printStackTrace();
         }

 
  
 }
 
 
 @Override
 public void onMessage(Message arg0) {
  // TODO Auto-generated method stub
 try{
       Message message = arg0;
 
       if (message instanceof TextMessage) {
           TextMessage textMessage = (TextMessage) message;
           String text = textMessage.getText();
           System.out.println("Received: " + text);
       } else {
           System.out.println("Received: " + message);
       }
  
    } catch (Exception e) {
        System.out.println("Caught: " + e);
        e.printStackTrace();
    }
     
 }

  
 
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  jms j=new jms();
  j.send();
  
  j.receive();
   
 }


}

posted @ 2012-09-17 14:30  旦旦哥  阅读(348)  评论(0编辑  收藏  举报