2.RabbitMQ工作模式

  1. 简单模式HelloWorld
    一个生产者,一个消费者,不需要设置交换机(使用默认交换机)。
  2. 工作队列模式Work queues
    一个生产者,多个消费者(竞争关系),不需要设置交换机(使用默认交换机)。
  3. 发布订阅模式Publish/Subscribe
    需要设置类型为fanout的交换机,并且交换机与队列进行绑定,当发送消息到交换机后,交换机会将消息发送到绑定的队列。
  4. 路由模式Routing
    需要设置类型为direct的交换机,交换机与队列进行绑定,并且指定routing key,当发送消息到交换机后,交换机会根据routing key将消息发送到对应的队列。
  5. 通配符模式Topics
    需要设置类型为topic的交换机,交换机与队列进行绑定,并且指定通配符方式的routing key,当发送消息到交换机后,交换机会根据routing key将消息发送到对应的队列。

1.添加MQ依赖

<dependency>
	<groupId>com.rabbitmq</groupId>
	<artifactId>amqp-client</artifactId>
	<version>5.14.1</version>
</dependency>

2.连接MQ

ConnectionFactory connectionFactory = new ConnectionFactory();
//设置主机信息
connectionFactory.setHost("127.0.0.1");
//5672是RabbitMQ的默认端口
connectionFactory.setPort(5672);
connectionFactory.setUsername("ms");
connectionFactory.setPassword("ms123");
connectionFactory.setVirtualHost("/msbase");
//获取TCP长连接
Connection conn = connectionFactory.newConnection();
//创建通信“通道”,相当于TCP中的虚拟连接
Channel channel = conn.createChannel();

HelloWorld(简单模式)

在这里插入图片描述

生产者

//创建队列,声明并创建队列,如果队列已存在,则使用这个队列
//第一个参数:队列名称ID
//第二个参数:是否持久化,false不持久化数据,MQ停掉数据就会丢失
//第三个参数:是否队列私有化,false则表示所有消费者都可以访问,true则表示只有第一次拥有它的消费者才能一直使用
//第四个参数:是否自动删除,false表示连接停掉后不自动删除掉这个队列
//其他额外的参数,null
channel.queueDeclare("helloworld", false, false, false, null);
String message = "我是消息";
//发布消息
//第一个参数:交换机,暂时用不到,其他模式用到
//第二 个参数:队列名称ID
//第三个参数:额外的设置属性
//第四个参数:需要发送消息的字节数组
channel.basicPublish("", "helloworld", null, message.getBytes());
channel.close();
conn.close();

消费者

//创建队列,声明并创建队列,如果队列已存在,则使用这个队列
//第一个参数:队列名称ID
//第二个参数:是否持久化,false不持久化数据,MQ停掉数据就会丢失
//第三个参数:是否队列私有化,false则表示所有消费者都可以访问,true则表示只有第一次拥有它的消费者才能一直使用
//第四个参数:是否自动删除,false表示连接停掉后不自动删除掉这个队列
//其他额外的参数,null
channel.queueDeclare("helloworld", false, false, false, null);
//消费消息
//第一个参数:队列名称ID
//第二个参数:是否自动确认消息,false表示手动编程确认,这是MQ推荐做法
//第三个参数:传入DefaultConsumer的实现类
channel.basicConsume("helloworld", false, new DefaultConsumer(channel) {
	@Override
	public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
		System.out.println("消费者接收到的消息:" + new String(body));
		//第一个参数:消息的TagId
		//第二个参数:false表示只签收当前的消息,true表示签收该消费者所有未签收的消息
		channel.basicAck(envelope.getDeliveryTag(),false);
	}
});

Work queues(工作队列模式)

在这里插入图片描述

生产者

channel.queueDeclare("helloworld", false, false, false, null);
for (int i = 0; i < 50; i++) {
	String message = "我是消息" + i;
	channel.basicPublish("", "helloworld", null, message.getBytes());
}
channel.close();
conn.close();

消费者1

channel.queueDeclare("helloworld", false, false, false, null);
//如果不写basicQos(1),则MQ会自动将所有请求平均发送给所有消费者
//basicQos:MQ不再对消费者一次发送多个请求,而是消费者处理完一个消息后(签收后),再从队列获取一个新的
channel.basicQos(1);
channel.basicConsume("helloworld", false, new DefaultConsumer(channel) {
    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

        System.out.println("消费者接收到的消息:" + new String(body));
        channel.basicAck(envelope.getDeliveryTag(),false);
    }
});

消费者2

channel.queueDeclare("helloworld", false, false, false, null);
channel.basicQos(1);
channel.basicConsume("helloworld", false, new DefaultConsumer(channel) {
    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
        System.out.println("消费者接收到的消息:" + new String(body));
        channel.basicAck(envelope.getDeliveryTag(),false);
    }
});

Publish/Subscribe(发布订阅模式)

在这里插入图片描述

生产者

String message = "我是消息";
//第一个参数:交换机名字
channel.basicPublish("weather", "", null, message.getBytes());
channel.close();
conn.close();

消费者1

channel.queueDeclare("sina", false, false, false, null);
//queueBind用于将队列与交换机绑定
//第一个参数:队列名
//第二个参数:交换机名
//第三个参数:路由key(暂时用不到)
channel.queueBind("sina","weather","");
channel.basicQos(1);
channel.basicConsume("sina", false, new DefaultConsumer(channel) {
   @Override
   public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
       System.out.println("消费者接收到的消息:" + new String(body));
       channel.basicAck(envelope.getDeliveryTag(), false);
   }
});

消费者2

channel.queueDeclare("baidu", false, false, false, null);
//queueBind用于将队列与交换机绑定
//第一个参数:队列名
//第二个参数:交换机名
//第三个参数:路由key(暂时用不到)
channel.queueBind("baidu","weather","");
channel.basicQos(1);
channel.basicConsume("baidu", false, new DefaultConsumer(channel) {
   @Override
   public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
       System.out.println("消费者接收到的消息:" + new String(body));
       channel.basicAck(envelope.getDeliveryTag(), false);
   }
});

Routing(路由模式)

在这里插入图片描述

生产者

Map<String,String> map = new Hashtable<>();
map.put("message.info","这是普通信息");
map.put("message.error","这是错误信息");
map.put("message.warning","这是警告信息");
for (Map.Entry<String, String> entry : map.entrySet()){
    //第一个参数:交换机名字
    //第二个参数:消息的routing key
    channel.basicPublish("weather_routing", entry.getKey(), null, entry.getValue().getBytes());
}
channel.close();
conn.close();

消费者1

channel.queueDeclare("sina", false, false, false, null);
//指定队列与交换机以及routing key之间的关系
channel.queueBind("sina","weather_routing","message.info");
channel.queueBind("sina","weather_routing","message.warning");
channel.basicQos(1);
channel.basicConsume("sina", false, new DefaultConsumer(channel) {
   @Override
   public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
       System.out.println("消费者接收到的消息:" + new String(body));
       channel.basicAck(envelope.getDeliveryTag(), false);
   }
});

消费者2

channel.queueDeclare("baidu", false, false, false, null);
//指定队列与交换机以及routing key之间的关系
channel.queueBind("baidu","weather_routing","message.error");
channel.basicQos(1);
channel.basicConsume("baidu", false, new DefaultConsumer(channel) {
  @Override
   public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
       System.out.println("消费者接收到的消息:" + new String(body));
       channel.basicAck(envelope.getDeliveryTag(), false);
   }
});

Topics(通配符模式)

在这里插入图片描述
#匹配一个或多个词,*匹配一个词;例如ms.#而已匹配ms.a或者ms.a.b;ms.*只能匹配ms.a

生产者

Map<String,String> map = new Hashtable<>();
map.put("message.info","这是普通信息");
map.put("message.error","这是错误信息");
map.put("message.warning","这是警告信息");
for (Map.Entry<String, String> entry : map.entrySet()){
    //第一个参数:交换机名字
    //第二个参数:消息的routing key
    channel.basicPublish("weather_topic", entry.getKey(), null, entry.getValue().getBytes());
}
channel.close();
conn.close();

消费者1

channel.queueDeclare("sina", false, false, false, null);
//指定队列与交换机以及routing key之间的关系
channel.queueBind("sina","weather_topic","message.*");
channel.basicQos(1);
channel.basicConsume("sina", false, new DefaultConsumer(channel) {
   @Override
   public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
       System.out.println("消费者接收到的消息:" + new String(body));
       channel.basicAck(envelope.getDeliveryTag(), false);
   }
});

消费者2

channel.queueDeclare("baidu", false, false, false, null);
//指定队列与交换机以及routing key之间的关系
channel.queueBind("baidu","weather_topic","message.#");
channel.basicQos(1);
channel.basicConsume("baidu", false, new DefaultConsumer(channel) {
    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
        System.out.println("消费者接收到的消息:" + new String(body));
        channel.basicAck(envelope.getDeliveryTag(), false);
    }
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迷图羊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值