• JMSJAVAHello 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();
       
     }


    }

  • 相关阅读:
    结构本身和结构成员在内存中储存的形式
    C语言字符,字符串,字节操作常用函数
    可变参数列表
    用数组代替指针实现静态链表
    cout对象一些常用方法的总结
    cin对象的一些常用方法使用总结
    数据结构基本概念和术语总结
    GCH文件
    Ubuntu16 搭建Git 服务器
    Navicat 连接VMware中Ubuntu 下的mysql5.7遇到的坑
  • 原文地址:https://www.cnblogs.com/hedan/p/2688695.html
Copyright © 2020-2023  润新知