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

     

  • 相关阅读:
    几种常用的认证机制
    几种任务调度的 Java 实现方法与比较
    vue 安装教程
    JDK版本导致Unsupported major.minor version 52.0 error
    在SpringMVC框架下实现文件的 上传和 下载
    SpringMVC框架下实现JSON(类方法中回传数据到jsp页面,使用jQuery方法回传)
    SpringMVC框架下数据的增删改查,数据类型转换,数据格式化,数据校验,错误输入的消息回显
    SpringMVC框架的基础知识;
    hibernate的二级缓存
    hibernate检索方式(HQL 检索方式,QBC 检索方式,本地 SQL 检索方式)
  • 原文地址:https://www.cnblogs.com/yz-bky/p/13044065.html
Copyright © 2020-2023  润新知