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));
                }
            });
        }
    }        

     

  • 相关阅读:
    雪花算法解决的问题
    ServiceStack6000次限制破解
    电商 详情页面 nginx 配置 优先请求静态页,若没有请求动态页,同时生成静态页
    docker 使用汇总
    .net 5 新特性 -- EFCoreDBFirst 介绍 和 .NET5 AOP 5个Filter
    模板方法设计模式:定义抽象类-模板:定义业务流程,执行步骤--》各业务类继承抽象类,实现各自不同 具体的执行步骤
    通过代理模式(包一层),实现对业务增加功能如日志,异常处理,缓存结果,到达不破坏原有的业务代码,扩展了功能
    设计模式
    mongodb 基本操作(增删改查),事务,数据失效机制
    log4net.Config
  • 原文地址:https://www.cnblogs.com/yz-bky/p/13044065.html
Copyright © 2011-2022 走看看