zoukankan      html  css  js  c++  java
  • activemq 学习系列(四) 消息提供和消费

    消息提供和消费

    package com.test;
    
    import javax.jms.Connection;
    import javax.jms.DeliveryMode;
    import javax.jms.Destination;
    import javax.jms.Message;
    import javax.jms.MessageConsumer;
    import javax.jms.MessageProducer;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    
    import org.apache.activemq.ActiveMQConnectionFactory;
    
    public class ActiveMqTest {
    
        public static void main (String[] args) {
            producer();
            // consumer();
        }
        
        public static void producer () {
            try {
                String brokerURL = "tcp://localhost:61616";
                String user = "admin";
                String password = "admin";
                ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(user, password, brokerURL);
                
                Connection connection = factory.createConnection();
                connection.start();
                
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                Destination queue = session.createQueue("demoQueue");
                
                MessageProducer messageProducer = session.createProducer(queue);
                messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
                
                String text = "hello activemq";
                
                Message textMessage = session.createTextMessage(text);
                messageProducer.send(textMessage);
                
                session.close();
                connection.close();
            } catch (Exception e) {
                System.out.println(e);
                e.printStackTrace();
            }
        }
        
        public static void consumer () {
            try {
                String brokerURL = "tcp://localhost:61616";
                String user = "admin";
                String password = "admin";
                ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(user, password, brokerURL);
                
                Connection connection = factory.createConnection();
                connection.start();
                
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                Destination queue = session.createQueue("demoQueue");
                
                MessageConsumer consumer = session.createConsumer(queue);
                Message message = consumer.receive(1000);
                
                if (message instanceof TextMessage) {
                    TextMessage textMessage = (TextMessage) message;
                    String text = textMessage.getText();
                    
                    System.out.println( text );
                }
                consumer.close();
                session.close();
                connection.close();
            } catch (Exception e) {
                System.out.println(e);
                e.printStackTrace();
            }
        }
    
    }

    1、执行 producer 方法后在管理界面可以看到相关信息

    2、执行 consumer 方法,消费消息

  • 相关阅读:
    H5调用相机和相册更换头像
    二分查找的递归解法以及希尔排序
    递归----经典问题:汉诺塔游戏
    递归----基础训练
    位运算-实现加减乘除
    位运算-出现k次与出现一次
    位运算-将整数的二进制进行奇偶位互换
    位运算-二进制中1的个数(三种解法)
    位运算-查找数组中唯一成对的数
    小小的总结一下2018年
  • 原文地址:https://www.cnblogs.com/bmw320li/p/10006018.html
Copyright © 2011-2022 走看看