zoukankan      html  css  js  c++  java
  • RabbitMQ第一种模型(直连)

    在上图的模型中,有以下概念:

    p:生产者,也就是要发消息的程序

    c:消费者,消息的接受者,会一直等待消息到来

    queue:消息列队,图中红色部分。类似一个邮箱,可以缓存消息;生产者向其中投递消息,消费者从中取出消息。  

    加入依赖

      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
        </dependency>
    
        <dependency>
          <groupId>com.rabbitmq</groupId>
          <artifactId>amqp-client</artifactId>
          <version>5.7.2</version>
        </dependency>
      </dependencies>

    加入工具类

    public class rabbitMQUtils {
        //总量级的
        private static ConnectionFactory connectionFactory;
        static{
            //静态代码块在类加载时加载  并且只加载一次
            //设置连接rabbitmq主机 ip地址
            connectionFactory.setHost("192.168.1.5");
            //设置端口号
            connectionFactory.setPort(5672);
            //设置连接那个虚拟主机
            connectionFactory.setVirtualHost("/ems");
            //设置访问虚拟主机的用户名和密码
            connectionFactory.setUsername("ems");
            connectionFactory.setPassword("123");
            
        }
        public static Connection getConnection()
        {
            try
            {
                //获取连接对象
                return connectionFactory.newConnection();
            }catch (Exception e){
                e.printStackTrace();
            }
            return null;
        };
    
        public static void connectionAndchannelClose(Connection connection, Channel channel)
        {
            try{
                if(channel!=null){channel.close();};
                if(connection!=null){
                    connection.close();
                };
    
            }catch (Exception e)
            {
                e.printStackTrace();
            }
    
        };
    }

    生产者

    public class Provider {
        @Test
        public void testProviderMessage() throws IOException, TimeoutException {
            Connection connection = rabbitMQUtils.getConnection();
            //获取连接中通道
            Channel channel = connection.createChannel();
            //通道绑定对应消息队列
            //参数1:队列名称,如果队列不存在自动创建
            //参数2:用来定义队列特性是否要持久化 如果为true时重启服务的话队列也不会消失  他会存在硬盘中 启动之后会回复
            //参数3:是否独占队列  多个连接可以对应一个列队
            //参数4:是否在消费完成之后自动删除队列
            //参数5,额外附加参数
            channel.queueDeclare("hello",false,false,false,null);
            //发布消息
            //参数1:交换机名称 参数2:队列名称 参数3:传递消息额外设置MessageProperties.PERSISTENT_TEXT_PLAIN消息变成持久化   参数4:消息的具体内容
            channel.basicPublish("","hello", MessageProperties.PERSISTENT_TEXT_PLAIN,"hello rabbitmq".getBytes());
            rabbitMQUtils.connectionAndchannelClose(connection,channel);
        };
    
    }

    消费者

    public class Customer {
        public static void main(String[] args) throws IOException, TimeoutException {
    
            Connection connection = rabbitMQUtils.getConnection();
            Channel channel = connection.createChannel();
            channel.queueDeclare("hello",false,false,false,null);
            //消费消息
            /*
            * 参数1:消费那个队列的消息 队列名称
            * 参数2:开始消息的自动确认机制
            * 参数3:消费时的回调接口
            * 消费者属性值必须与生产者一样
            */
            channel.basicConsume("hello", true, new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    super.handleDelivery(consumerTag, envelope, properties, body);
                    System.out.println(new String(body));
                }
            });
        }
    }        

     

  • 相关阅读:
    Python入门-函数进阶
    Python入门-初始函数
    Leetcode300. Longest Increasing Subsequence最长上升子序列
    Leetcode139. Word Break单词拆分
    Leetcode279. Perfect Squares完全平方数
    Leetcode319. Bulb Switcher灯泡开关
    Leetcode322. Coin Change零钱兑换
    二叉树三种遍历两种方法(递归和迭代)
    Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历
    Leetcode515. Find Largest Value in Each Tree Row在每个树行中找最大值
  • 原文地址:https://www.cnblogs.com/yz-bky/p/13044065.html
Copyright © 2011-2022 走看看