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六种队列模式-主 ...
随机推荐
- 关于pdfbox操作pdf的分享链接手长
http://blog.csdn.net/fighting_no1/article/details/51038966
- FreeSWITCH与FreeSWITCH对接
(主机A ---> 主机B)192.168.100.A主机:修改/usr/local/freeswitch/conf/dialplan/default.xml 10 <ex ...
- 剑指Offer 49. 把字符串转换成整数 (字符串)
题目描述 将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),要求不能使用字符串转换整数的库函数. 数值为0或者字符串不是一 ...
- MySQL常用内置函数
本篇博客源自以下博客地址: http://www.mamicode.com/info-detail-250393.html
- C++常见的概念
1)多态:允许将子类类型的指针赋值给父类类型的指针.赋值以后,父类对象可以根据当前赋值给它的子类对象的特性以不同的方式运作. 2)深拷贝:重新分配内存:浅拷贝:共用同一内存. 3)友元:非成员函数不能 ...
- ES6语法知识
let/const(常用) let,const用于声明变量,用来替代老语法的var关键字,与var不同的是,let/const会创建一个块级作用域(通俗讲就是一个花括号内是一个新的作用域) 这里外部的 ...
- Linux第一节课学习笔记
我的目标是考过RHCE 开源软件有使用.复制.传播.收费.修改及创建衍生品自由,其中后二者只有开源软件才有,前四者开闭源共有.
- Vue组件的介绍与使用
组件系统是将一个大型的界面切分成一个一个更小的可控单元. 组件是可复用的,可维护的. 组件具有强大的封装性,易于使用. 大型应用中,组件与组件之间交互是可以解耦操作的. 全局组件的使用 <!DO ...
- Java StringBuffer和StringBuilder类
Java StringBuffer和StringBuilder类 当对字符串进行修改的时候,需要使用StringBuffer和StringBuilder类. 和String类不同的是,StringBu ...
- (思维导图搞定)Content-Type:application/json,后台如何接收
自己定的规范:只要Content-Type设置为application/json的时候,前台的data要传递字符串 虽然设置为application/json,前台传对象request.getPara ...