RabbitMQ深度探索:五种消息模式

  1. RabbitMQ 工作队列:
    1. 默认的传统队列是为均摊消费,存在不公平性;如果每个消费者速度不一样的情况下,均摊消费是不公平的,应该是能者多劳
    2. 采用工作队列模式:
      1. 在通道中只需要设置 baseicQos 的值即可
        1. 表示 MQ 服务器每次只会给消费者推送 n 条消息
        2. 必须手动应答之后才会继续发送
    3. 生产者:
      public class ProducerFanout {
      
          private static final String QUEUE_NAME = "BoyatopMamber";
      
          public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
              // 1.创建新的连接
              Connection connection = RabbitMQConnection.getConnection();
      
              // 2.设置 channel
              Channel channel = connection.createChannel();
      
              for (int i = 0; i < 10; i++) {
                  // 3.发送消息
                  String msg = "Hello my Bro" + i;
                  channel.confirmSelect();
      
                  channel.basicPublish("",QUEUE_NAME,null,msg.getBytes());
                  boolean result = channel.waitForConfirms();
              }
              // 4.关闭资源
              channel.close();
              connection.close();
          }
      }
    4. 消费者1:
      public class Consumer1 {
          //定义队列
          private static final String QUEUE_NAME = "BoyatopMamber";
      
      
          public static void main(String[] args) throws IOException, TimeoutException {
              //创建连接
              Connection connection = RabbitMQConnection.getConnection();
      
              //创建通道
              final Channel channel = connection.createChannel();
              channel.basicQos(1);
      
              DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
                  @Override
                  public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                      String msg = new String(body,"UTF-8");
                      System.out.println("用户1:" + msg);
                      channel.basicAck(envelope.getDeliveryTag(),false);
                  }
              };
      
              //监听消息
              channel.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值