Rabbitmq(6) 主题模式
* 匹配1个
# 匹配所有
发送者:
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,"topic");
//在手动确认机制之前
//一次只发送一条消息,给不同的消费者
channel.basicQos(1); String message = "hello ps";
String routingKey ="goods.delete";
channel.basicPublish(Exchange_NAME,routingKey,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,"goods.add");
// 一次只处理一个消息
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,"goods.#");
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);
}
}
Rabbitmq(6) 主题模式的更多相关文章
- RabbitMQ消息队列(八)-通过Topic主题模式分发消息(.Net Core版)
前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过routingkey来匹配消息的模式已经有一定了解那fanout也很好 ...
- RabbitMQ (七) 订阅者模式之主题模式 ( topic )
主题模式和路由模式很像 路由模式是精确匹配 主题模式是模糊匹配 依然先通过管理后台添加一个交换机. 生产者 public class Producer { private const string E ...
- (八)RabbitMQ消息队列-通过Topic主题模式分发消息
原文:(八)RabbitMQ消息队列-通过Topic主题模式分发消息 前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过 ...
- RabbitMQ六种队列模式-主题模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主题模式 [ ...
- 队列模式&主题模式
# RabbitMQ 消息中间件 **Advanced Message Queuing Protocol (高级消息队列协议** The Advanced Message Queuing Protoc ...
- 【RabbitMQ】工作模式介绍
一.前言 之前,笔者写过< CentOS 7.2 安装 RabbitMQ> 这篇文章,今天整理一下 RabbitMQ 相关的笔记便于以后复习. 二.模式介绍 在 RabbitMQ 官网上提 ...
- RabbitMQ六种队列模式-简单队列模式
前言 RabbitMQ六种队列模式-简单队列 [本文]RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...
- RabbitMQ六种队列模式-工作队列模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列 [本文]RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...
- RabbitMQ六种队列模式-发布订阅模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅 [本文]RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...
随机推荐
- 周强 201771010141《面向对象程序设计(java)》第四周学习总结
实验目的与要求 (1) 理解用户自定义类的定义: (2) 掌握对象的声明: (3) 学会使用构造函数初始化对象: (4) 使用类属性与方法的使用掌握使用: (5) 掌握package和import语句 ...
- cocos2dx开发之util类&方法——取范围随机数
#include <random> int random(int start, int end) { //return start+rand()%(end-start+1); static ...
- vue2.0 父子组件通信 兄弟组件通信
父组件是通过props属性给子组件通信的来看下代码: 父组件: <parent> <child :child-com="content"></chil ...
- hdu 5776 抽屉定理
sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submiss ...
- c# ef
找出不同项 ).ToList(); resultMsg = string.Join(",", query.select(p=>p.key).ToList())
- error: invalid use of void expression
void*类型定义的指针变量只可以接收对象的地址,而没有对象类型这个概念.所以void*指针变量是不能直接用“*指针变量”去访问,需要强制类型转换后才能“间接”访问: *(type*)指针变量,必须给 ...
- java 获取键盘输入常用的两种方法
java 获取键盘输入常用的两种方法 方法1: 通过 Scanner Scanner input = new Scanner(System.in); String s = input.nextLine ...
- IndentationError:expected an indented block错误解决
Python语言是一款对缩进非常敏感的语言,给很多初学者带来了困惑,即便是很有经验的Python程序员,也可能陷入陷阱当中.最常见的情况是tab和空格的混用会导致错误,或者缩进不对,而这是用肉眼无法分 ...
- VS2010+WPF+LINQ for MySQL
学习wpf,连接数据库和linq for mysql 1.参考以前博文,恢复在 Vs2010+linQ for Mysql的环境. 2.建立 wpf工程,参照1,生成 datacontext.cs , ...
- Linux之cd、pwd、mkdir、rmdir
cd.pwd.mkdir.rmdir 命令功能: 切换到指定的目录,可用绝对路径和相对路径 命令格式: cd directory 命令参数: 无 命令实例: 1.切换到/bin目录 vbird@Ubu ...