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. iLogtail使用入门-iLogtail本地配置模式部署(For Kafka Flusher)

    ​简介:iLogtail使用入门-iLogtail本地配置模式部署(For Kafka Flusher). 阿里已经正式开源了可观测数据采集器iLogtail.作为阿里内部可观测数据采集的基础设施,i ...

  2. 所有前端都要看的2D游戏化互动入门基础知识

    简介: 在非游戏环境中将游戏的思维和游戏的机制进行整合运用,以引导用户互动和使用 本文作者:淘系前端团队-Eva.js作者-明非 背景 现在越来越多的公司和 App 开始使用游戏化的方式去做产品了,所 ...

  3. [FE] uni-app 动态改变 navigationBarTitleText 导航标题

    改导航文字: uni.setNavigationBarTitle({ title: 'xx' }); 改 tabBar 文字: uni.setTabBarItem({ index: 0, text: ...

  4. 17.prometheus服务发现&基于文件的服务发现

    一.服务发现 Prometheus 中是如何使用服务发现来查找和抓取目标的.我们知道在 Prometheus 配置文件中可以通过一个 static_configs 来配置静态的抓取任务,但是在云环境下 ...

  5. k8s之dns问题

    问题1: 描述:pod新建好后,无法ping通域名(无论是外网域名还是内网域名),但是可以ping通IP(包含外网IP和内网IP),不包括kube-dns的IP,和pod同一网段IP可以ping通 # ...

  6. P10118 『STA - R4』And

    P10118 『STA - R4』And 题意:给定 A,B,求 \(\sum y - x\),其中 x,y 满足: x < y x + y = A x & y = B 对于加运算和与运 ...

  7. 一键接入大模型:One-Api本地安装配置实操

    前言 最近准备学习一下 Semantic Kernel, OpenAI 的 Api 申请麻烦,所以想通过 One-api 对接一下国内的在线大模型,先熟悉一下 Semantic Kernel 的基本用 ...

  8. 自定义Lua解析器管理器-------演化脚本V0.5

    [3]自定义Lua解析器管理器-------演化脚本V0.5 方便我们在项目中使用Lua解析方法,我们封装管理一个lua解析器,管理LuaState的方法执行. 解析器脚本: using LuaInt ...

  9. neo4j的安装部署

    Linux下载neo4j 直接在服务器上使用命令下载: curl -O http://dist.neo4j.org/neo4j-community-3.4.5-unix.tar.gz 安装Neo4j ...

  10. C语言:算法题判断是否有效字符({[]})---括号

    给定一个只包括 '(',')','{','}','[',']'的字符串 s ,判断字符串是否有效. 有效字符串需满足:                  左括号必须用相同类型的右括号闭合.       ...