Rabbitmq(4) 订阅模式

 

p;发送者

x: 交换机

 消息队列

c: 接收者

------------------------------------------------------------------------

发送者

package com.aynu.bootamqp.service;

import com.aynu.bootamqp.commons.utils.Amqp;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;


import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Send {

    private final static String Exchange_NAME ="hello";
    public static void main(String[] args) throws IOException, TimeoutException {
            Connection connection = Amqp.getConnection();
            Channel channel = connection.createChannel();
            //声明交换机
            channel.exchangeDeclare(Exchange_NAME,"fanout");
            //在手动确认机制之前
            //一次只发送一条消息,给不同的消费者
            channel.basicQos(1);

            String message = "hello ps";
            channel.basicPublish(Exchange_NAME,"",null,message.getBytes("utf-8"));
            System.out.println(message);
            channel.close();
            connection.close();
    }
}

消费者1

package com.aynu.bootamqp.service;

import com.aynu.bootamqp.commons.utils.Amqp;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

@SuppressWarnings("all")
public class Receive {

    private final static String QUEUE_NAME ="hello";
    private final static String Exchange_NAME ="hello";
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = Amqp.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        //绑定队列
        channel.queueBind(QUEUE_NAME,Exchange_NAME,"");
       // 一次只处理一个消息
        channel.basicQos(1);
        DefaultConsumer consumer = new DefaultConsumer(channel) {

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                super.handleDelivery(consumerTag, envelope, properties, body);
                String msg = new String(body,"utf-8");
                System.out.println("receive"+msg);
                try {
                    Thread.sleep(1000*2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    // 手动发送消息确认机制
                    channel.basicAck(envelope.getDeliveryTag(),false);
                }
            }
        };
        // 自动应答
        boolean autoAck = false;
        channel.basicConsume(QUEUE_NAME,autoAck,consumer);
    }
}

接受者2

package com.aynu.bootamqp.service;

import com.aynu.bootamqp.commons.utils.Amqp;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;
@SuppressWarnings("all")
public class Receive2 {

    private final static String QUEUE_NAME ="hello1";
    private final static String Exchange_NAME ="hello";
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = Amqp.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        channel.queueBind(QUEUE_NAME,Exchange_NAME,"");
        channel.basicQos(1);
        DefaultConsumer consumer = new DefaultConsumer(channel) {

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                super.handleDelivery(consumerTag, envelope, properties, body);
                String msg = new String(body,"utf-8");
                System.out.println("receive2222"+msg);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    // 手动发送消息确认机制
                    channel.basicAck(envelope.getDeliveryTag(),false);
                }
            }
        };
        boolean autoAck = false;
        channel.basicConsume(QUEUE_NAME,autoAck,consumer);
    }
}

 

转载于:https://www.cnblogs.com/mm163/p/10703199.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值