* 匹配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) 主题模式的更多相关文章

  1. RabbitMQ消息队列(八)-通过Topic主题模式分发消息(.Net Core版)

    前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过routingkey来匹配消息的模式已经有一定了解那fanout也很好 ...

  2. RabbitMQ (七) 订阅者模式之主题模式 ( topic )

    主题模式和路由模式很像 路由模式是精确匹配 主题模式是模糊匹配 依然先通过管理后台添加一个交换机. 生产者 public class Producer { private const string E ...

  3. (八)RabbitMQ消息队列-通过Topic主题模式分发消息

    原文:(八)RabbitMQ消息队列-通过Topic主题模式分发消息 前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过 ...

  4. RabbitMQ六种队列模式-主题模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主题模式 [ ...

  5. 队列模式&主题模式

    # RabbitMQ 消息中间件 **Advanced Message Queuing Protocol (高级消息队列协议** The Advanced Message Queuing Protoc ...

  6. 【RabbitMQ】工作模式介绍

    一.前言 之前,笔者写过< CentOS 7.2 安装 RabbitMQ> 这篇文章,今天整理一下 RabbitMQ 相关的笔记便于以后复习. 二.模式介绍 在 RabbitMQ 官网上提 ...

  7. RabbitMQ六种队列模式-简单队列模式

    前言 RabbitMQ六种队列模式-简单队列 [本文]RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...

  8. RabbitMQ六种队列模式-工作队列模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列 [本文]RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...

  9. RabbitMQ六种队列模式-发布订阅模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅 [本文]RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主 ...

随机推荐

  1. python 如何生成好看的报告,在unittest的框架下

    怎么生成报告:需要导入BeautifulReport import BeautifulReport as bf import unittest class Test_login(unittest.Te ...

  2. 马凯军201771010116《面向对象程序设计(java)》第二周学习总结

    第一部分:理论知识学习部分 (1)基本知识:简单应用程序的结构:Java环境里的注释方式: (2)数据类型(4种整型.2种浮点型.1种字符型‘char’.真值型‘Boolean’. (3)变量,每个变 ...

  3. Spring Cloud 请求重试机制核心代码分析

    场景 发布微服务的操作一般都是打完新代码的包,kill掉在跑的应用,替换新的包,启动. spring cloud 中使用eureka为注册中心,它是允许服务列表数据的延迟性的,就是说即使应用已经不在服 ...

  4. Tomcat7.0/8.0 详细安装配置图解,以及UTF-8编码配置

    Tomcat7.0/8.0 详细安装配置图解,以及UTF-8编码配置 2017年01月24日 10:01:48 阅读数:51265 标签: tomcattomcat安装tomcat配置tomcat编码 ...

  5. Python学习之路基础篇--01Python的基本常识

    1 计算机基础 首先认识什么是CPU(Central Processing Unit),即中央处理器,相当于人类的大脑.内存,临时储存数据,断电即消失.硬盘,可以长久的储存数据,有固态硬盘,机械硬盘之 ...

  6. js post下载相当于 location.href

    /** *参数说明: url:下载地址,val:需要提交的参数值,具体类型和个数自行扩展 * 参数可以用obj = {url:""",val1:"111&quo ...

  7. c# ef

    找出不同项 ).ToList(); resultMsg = string.Join(",", query.select(p=>p.key).ToList())

  8. 启动Kernel提示Bad Data CRC

    如上图,我明明将uImage正确写入到里nandflash里面,但启动但时候就是提示bad CRC. 后来我手动执行nand read kernel想看看是不是环境变量里面的命令执行有问题,意外但被我 ...

  9. 策略模式(Strategy )

    为实现一个目的采用不同的方式都可实现,具体看要采取哪种方式. //接口 public interface Strategy {    public void algorithmInterface(); ...

  10. 彻底放弃没落的MFC,对新人的忠告!--吃瓜群众围观撕逼

    http://bbs.csdn.net/topics/391817496 完全没想到10多年后还有人纠结要不要学MFC,我花点时间给新人们一个总结. 第1种观点 学习完MFC,你会更理解编程的思想,再 ...