1.exchange类型,rabbitmq交换机类型

exchange类型
fanout 扇形交换机,它会把所有发送到该交换机的消息路由到所有与该交换机绑定的队列中。
direct 直连交换机,它会把消息路由到那些BindingKey和RoutingKey完全匹配的队列中。
topic 主题交换机,与direct类似,但它可以通过通配符进行模糊匹配,topic模糊匹配 井号代表全部 星号代表一个字符。
headers 头交换机,不依赖于路由键的匹配规则来路由消息,而是根据发送的消息内容中的headers属性进行匹配,headers类型的交换机性能很差,而且也不实用。

2.

EndPoint.java 定义连接,channel,队列,绑定交换机和路由键
Producer.java 生产者 发送消息,基于交换机,路由键来发送
QueueConsumer.java 消费者 基于队列来接收消息
MqTest.java MQ测试类

package com.redis.demo.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory; import java.io.IOException; public abstract class EndPoint { protected Channel channel;
protected Connection connection;
protected String endPointName; public EndPoint(String endpointName) throws Exception {
this.endPointName = endpointName;
//Create a connection factory
ConnectionFactory factory = new ConnectionFactory(); //hostname of your rabbitmq server
factory.setHost("localhost");
factory.setPort(5672);
factory.setUsername("guest");
factory.setPassword("guest"); //getting a connection
connection = factory.newConnection(); //creating a channel
channel = connection.createChannel(); //declaring a queue for this channel. If queue does not exist,
//it will be created on the server.
//定义队列
channel.queueDeclare(endpointName, false, false, false, null); //exchange
// channel.exchangeDeclare("fanout_test","fanout");
// //rount-key 空, 绑定队列
// channel.queueBind(endpointName,"fanout_test",""); //direct,topic
// channel.exchangeDeclare("direct_test","direct");
// channel.queueBind(endpointName,"direct_test","com.login.message");
//
channel.exchangeDeclare("topic_test","topic");
// channel.queueBind(endpointName,"topic_test","com.order.*");
channel.queueBind(endpointName,"topic_test","com.order.#");
} public void close() throws Exception{
this.channel.close();
this.connection.close();
} }
package com.redis.demo.rabbitmq;

import org.springframework.util.SerializationUtils;

import java.io.Serializable;

public class Producer extends EndPoint{

    public Producer(String endpointName) throws Exception {
super(endpointName);
} /**
* 发送消息
* @param object
* @throws Exception
*/
public void sendMessage(Serializable object) throws Exception {
//发送到队列
// channel.basicPublish("",endPointName, null, SerializationUtils.serialize(object)); //发送rount-key direct
// channel.basicPublish("direct_test","com.login.message",null,SerializationUtils.serialize(object)); //发送,群发各个业务线,匹配规则
channel.basicPublish("topic_test","com.order.create.success",null,SerializationUtils.serialize(object));
// channel.basicPublish("topic_test","com.order.sms",null,SerializationUtils.serialize(object));
// channel.basicPublish("topic_test","com.order.pay.ok",null,SerializationUtils.serialize(object)); }
}
package com.redis.demo.rabbitmq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.ShutdownSignalException;
import org.springframework.util.SerializationUtils; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; public class QueueConsumer extends EndPoint implements Runnable, Consumer {
public QueueConsumer(String endpointName) throws Exception {
super(endpointName);
} @Override
public void handleConsumeOk(String consumerTag) {
System.out.println("Consumer "+consumerTag +" registered");
} @Override
public void handleCancelOk(String s) { } @Override
public void handleCancel(String s) throws IOException { } @Override
public void handleShutdownSignal(String s, ShutdownSignalException e) { } @Override
public void handleRecoverOk(String s) {
System.out.println("handleRecoverOk "+s +" registered");
} @Override
public void handleDelivery(String s, Envelope envelope, AMQP.BasicProperties basicProperties, byte[] body) throws IOException {
System.out.println("handleDelivery "+s +" registered, type=" + basicProperties.getType());
Map map = (HashMap) SerializationUtils.deserialize(body);
System.out.println("Message Number "+ map.get("message number") + " received."); } @Override
public void run() {
try {
//start consuming messages. Auto acknowledge messages.
channel.basicConsume(endPointName, true,this);
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.redis.demo.rabbitmq;

import java.util.HashMap;

public class MqTest {
public static void main(String[] args) throws Exception{
//启动线程接收消息
QueueConsumer consumer = new QueueConsumer("queue");
Thread consumerThread = new Thread(consumer);
consumerThread.start();
System.out.println("消费端启动完成==============="); Producer producer = new Producer("queue"); //发送消息
for (int i = 0; i < 10; i++) {
HashMap message = new HashMap();
message.put("message number", i);
//发送map对象
producer.sendMessage(message);
System.out.println("Message Number "+ i +" send success.");
} }
}

3.pom jar导入

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


java rabbitmq模拟生产者,消费者demo的更多相关文章

  1. java多线程模拟生产者消费者问题,公司面试常常问的题。。。

    package com.cn.test3; //java多线程模拟生产者消费者问题 //ProducerConsumer是主类,Producer生产者,Consumer消费者,Product产品 // ...

  2. java简单模拟生产者消费者问题

    本文来自:http://www.cnblogs.com/happyPawpaw/archive/2013/01/18/2865957.html 引言 生产者和消费者问题是线程模型中的经典问题:生产者和 ...

  3. 第23章 java线程通信——生产者/消费者模型案例

    第23章 java线程通信--生产者/消费者模型案例 1.案例: package com.rocco; /** * 生产者消费者问题,涉及到几个类 * 第一,这个问题本身就是一个类,即主类 * 第二, ...

  4. java多线程解决生产者消费者问题

    import java.util.ArrayList; import java.util.List; /** * Created by ccc on 16-4-27. */ public class ...

  5. java+反射+多线程+生产者消费者模式+读取xml(SAX)入数据库mysql-【费元星Q9715234】

    java+反射+多线程+生产者消费者模式+读取xml(SAX)入数据库mysql-[费元星Q9715234] 说明如下,不懂的问题直接我[费元星Q9715234] 1.反射的意义在于不将xml tag ...

  6. Java设计模式之生产者消费者模式

    Java设计模式之生产者消费者模式 博客分类: 设计模式 设计模式Java多线程编程thread 转载 对于多线程程序来说,不管任何编程语言,生产者和消费者模型都是最经典的.就像学习每一门编程语言一 ...

  7. JAVA多线程之生产者 消费者模式 妈妈做面包案例

    创建四个类 1.面包类 锅里只可以放10个面包 ---装面包的容器2.厨房 kitchen 生产面包 和消费面包  最多生产100个面包3.生产者4消费者5.测试类 多线程经典案例 import ja ...

  8. 用Java写一个生产者-消费者队列

    生产者消费者的模型作用 通过平衡生产者的生产能力和消费者的消费能力来提升整个系统的运行效率,这是生产者消费者模型最重要的作用. 解耦,这是生产者消费者模型附带的作用,解耦意味着生产者和消费者之间的联系 ...

  9. Java里的生产者-消费者模型(Producer and Consumer Pattern in Java)

    生产者-消费者模型是多线程问题里面的经典问题,也是面试的常见问题.有如下几个常见的实现方法: 1. wait()/notify() 2. lock & condition 3. Blockin ...

  10. Java并发之:生产者消费者问题

    生产者消费者问题是Java并发中的常见问题之一,在实现时,一般可以考虑使用juc包下的BlockingQueue接口,至于具体使用哪个类,则就需要根据具体的使用场景具体分析了.本文主要实现一个生产者消 ...

随机推荐

  1. Java编程技巧之单元测试用例编写流程

    简介: 立足于"如何来编写单元测试用例",让大家"有章可循",快速编写出单元测试用例. 作者 | 常意来源 | 阿里技术公众号 温馨提示:本文较长,同学们可收藏 ...

  2. PyQt5编程学习之控件基类QObject深入

    一.QObject描述: PyQt几乎所有的类都是从QObject直接或间接继承的,QObject是所有PyQt类的基类,是PyQt对象模型的核心. 二.基类QObject的功能: (一)操作对象名称 ...

  3. [Blockchain] 去中心化与互联网分布式的联系与区别

    去中心化和传统分布式都是多机应用,这是它们的共同之处,但是背后有着不一样的用途. 我们所理解的传统分布式及其应用可以解决两个问题:冗余备份/扩容 和 并行计算. 而去中心化应用的目的是维护不可逆转数据 ...

  4. 2019-8-31-PowerShell-使用-WMI-获取信息

    title author date CreateTime categories PowerShell 使用 WMI 获取信息 lindexi 2019-08-31 16:55:58 +0800 201 ...

  5. MSSQL—存储过程分页

    SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON GO CREATE PROCEDURE [dbo].[GetPagingStr] @PRESQL VARCHAR( ...

  6. DE10-Lite加速度计使用教程

    DE10-Lite加速度计使用教程 1. 概述 DE10-Lite实验板上有一片5轴加速度计芯片ADXL345(通常称为G-sensor).它可以用来测量板子的倾斜角度.本文讲述如何以50次/秒的速度 ...

  7. 如何使用go module导入本地包

    go module是Go1.11版本之后官方推出的版本管理工具,并且从Go1.13版本开始,go module将是Go语言默认的依赖管理工具. 到今天Go1.14版本推出之后Go modules 功能 ...

  8. 2020年9月至10月 Splashtop 新功能

    ​ Splashtop 已为 Splashtop Business Access.Splashtop Remote Support.Splashtop SOS 和 Splashtop On-Prem ...

  9. 基于 OAI 部署私有的 4G EPS

    目录 文章目录 目录 前言 硬件设备要求 运行平台 RF 外设 可编程 SIM 卡 UE 终端 高精度参考时钟 操作系统要求 内核要求 CPU Frequency scaling All-In-One ...

  10. Selenium4自动化测试8--控件获取数据--上传、下载、https和切换分页

    10-上传 上传不能模拟用户在页面上选择本地文件,只能先把要上传的文件先准备好在代码里上传 import time from selenium.webdriver.support.select imp ...