public class WorkTest {
 @Test
 public void send() throws Exception{
   //获取连接
   Connection conn = ConnectionUtil.getConn();
   Channel chan = conn.createChannel();
   //声明队列
   chan.queueDeclare("work", false, false, false, null);
   for(int i=0;i<100;i++){
     String msg="1712,hello:"+i+"message";
     chan.basicPublish("", "work", null, msg.getBytes());
     System.out.println("第"+i+"条信息已经发送");
   }
   chan.close();
   conn.close();
 }
 @Test
 public void receive1() throws Exception{
   //获取连接,获取信道
   Connection conn = ConnectionUtil.getConn();
   Channel chan = conn.createChannel();
   chan.queueDeclare("work", false, false, false, null);
   //同一时刻服务器只发送一条消息给同一消费者,消费者空闲,才发送一条
   chan.basicQos(1);
   //定义消费者
   QueueingConsumer consumer=new QueueingConsumer(chan);
   //绑定队列和消费者的关系
   //queue
   //autoAck:消息被消费后,是否自动确认回执,如果false,不自动需要手动在
   //完成消息消费后进行回执确认,channel.ack,channel.nack
   //callback
   //chan.basicConsume(queue, autoAck, callback)
   chan.basicConsume("work", false, consumer);
   //监听
   while(true){
     Delivery delivery=consumer.nextDelivery();
     byte[] result = delivery.getBody();
     String msg=new String(result);
     System.out.println("接受到:"+msg);
     Thread.sleep(50);
     //返回服务器,回执
     chan.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
   }  
 }
 @Test
 public void receive2() throws Exception{
   //获取连接,获取信道
   Connection conn = ConnectionUtil.getConn();
   Channel chan = conn.createChannel();
   chan.queueDeclare("work", false, false, false, null);
   //同一时刻服务器只发送一条消息给同一消费者,消费者空闲,才发送一条
   chan.basicQos(1);
   //定义消费者
   QueueingConsumer consumer=new QueueingConsumer(chan);
   //绑定队列和消费者的关系
   //queue
   //autoAck:消息被消费后,是否自动确认回执,如果false,不自动需要手动在
   //完成消息消费后进行回执确认,channel.ack,channel.nack
   //callback
   //chan.basicConsume(queue, autoAck, callback)
   chan.basicConsume("work", false, consumer);
   //监听
   while(true){
     Delivery delivery=consumer.nextDelivery();
     byte[] result = delivery.getBody();
     String msg=new String(result);
     System.out.println("接受到:"+msg);
     Thread.sleep(150);
     //返回服务器,回执
     chan.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
   }
 }
 
}

  

publish/fanout发布订阅

public class FanoutTest {
 //交换机,有类型,发布订阅:fanout
 //路由模式:direct
 //主题模式:topic
 @Test
 public void send() throws Exception {
   //获取连接
   Connection conn = ConnectionUtil.getConn();
   Channel chan = conn.createChannel();
   //声明交换机
   //参数意义,1 交换机名称,2 类型:fanout,direct,topic
   chan.exchangeDeclare("fanoutEx", "fanout");
   //发送消息
   for(int i=0;i<100;i++){
     String msg="1712 hello:"+i+"msg";
     chan.basicPublish("fanoutEx", "", null, msg.getBytes());
     System.out.println("第"+i+"条信息已经发送");
   }
 }
 
 @Test
 public void receiv01() throws Exception{
   //获取连接
   Connection conn = ConnectionUtil.getConn();
   Channel chan = conn.createChannel();
   //生命队列
   chan.queueDeclare("fanout01", false, false, false, null);
   //声明交换机
   chan.exchangeDeclare("fanoutEx", "fanout");
   //绑定队列到交换机
   //参数 1 队列名称,2 交换机名称 3 路由key
   chan.queueBind("fanout01", "fanoutEx", "");
   chan.basicQos(1);
   //定义消费者
   QueueingConsumer consumer=new QueueingConsumer(chan);
   //消费者与队列绑定
   chan.basicConsume("fanout01",false, consumer);
   while(true){
     Delivery delivery= consumer.nextDelivery();
     System.out.println("一号消费者接收到"+
     new String(delivery.getBody()));
     chan.basicAck(delivery.getEnvelope().
         getDeliveryTag(), false);
   }
 }
 @Test
 public void receiv02() throws Exception{
   //获取连接
   Connection conn = ConnectionUtil.getConn();
   Channel chan = conn.createChannel();
   //生命队列
   chan.queueDeclare("fanout02", false, false, false, null);
   //声明交换机
   chan.exchangeDeclare("fanoutEx", "fanout");
   //绑定队列到交换机
   //参数 1 队列名称,2 交换机名称 3 路由key
   chan.queueBind("fanout02", "fanoutEx", "");
   chan.basicQos(1);
   //定义消费者
   QueueingConsumer consumer=new QueueingConsumer(chan);
   //消费者与队列绑定
   chan.basicConsume("fanout02",false, consumer);
   while(true){
     Delivery delivery= consumer.nextDelivery();
     System.out.println("二号消费者接收到"+new String(delivery.getBody()));
     chan.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
   }
 }
}

  

routing路由模式

public class RoutingTopicTest {
 
 @Test
 public void routingSend() throws Exception{
   //获取连接
   Connection conn = ConnectionUtil.getConn();
   Channel chan = conn.createChannel();
   //声明交换机
   //参数意义,1 交换机名称,2 类型:fanout,direct,topic
   chan.exchangeDeclare("directEx", "direct");
   //发送消息
   String msg="路由模式的消息";
   chan.basicPublish("directEx", "jt1713", 
       null, msg.getBytes());
 }
 @Test
 public void routingRec01() throws Exception{
   System.out.println("一号消费者等待接收消息");
   //获取连接
   Connection conn = ConnectionUtil.getConn();
   Channel chan = conn.createChannel();
   //声明队列
   chan.queueDeclare("direct01", false, false, false, null);
   //声明交换机
   chan.exchangeDeclare("directEx", "direct");
   //绑定队列到交换机
   //参数 1 队列名称,2 交换机名称 3 路由key
   chan.queueBind("direct01", "directEx", "jt1712");
   chan.basicQos(1);
   //定义消费者
   QueueingConsumer consumer=new QueueingConsumer(chan);
   //消费者与队列绑定
   chan.basicConsume("direct01",false, consumer);
   while(true){
     Delivery delivery= consumer.nextDelivery();
     System.out.println("一号消费者接收到"+
     new String(delivery.getBody()));
     chan.basicAck(delivery.getEnvelope().
         getDeliveryTag(), false);
   }
 }
 @Test
 public void routingRec02() throws Exception{
   System.out.println("二号消费者等待接收消息");
   //获取连接
   Connection conn = ConnectionUtil.getConn();
   Channel chan = conn.createChannel();
   //声明队列
   chan.queueDeclare("direct02", false, false, false, null);
   //声明交换机
   chan.exchangeDeclare("directEx", "direct");
   //绑定队列到交换机
   //参数 1 队列名称,2 交换机名称 3 路由key
   chan.queueBind("direct02", "directEx", "jt1711");
   chan.basicQos(1);
   //定义消费者
   QueueingConsumer consumer=new QueueingConsumer(chan);
   //消费者与队列绑定
   chan.basicConsume("direct02",false, consumer);
   while(true){
     Delivery delivery= consumer.nextDelivery();
     System.out.println("二号消费者接收到"+
     new String(delivery.getBody()));
     chan.basicAck(delivery.getEnvelope().
         getDeliveryTag(), false);
   }
 }
}

  

topic主题模式

*号代表单个词语
#代表多个词语

其他的内容与routing路由模式一致

public class RoutingTopicTest {
 
 
 @Test
 public void routingRec02() throws Exception{
   System.out.println("二号消费者等待接收消息");
   //获取连接
   Connection conn = ConnectionUtil.getConn();
   Channel chan = conn.createChannel();
   //声明队列
   chan.queueDeclare("direct02", false, false, false, null);
   //声明交换机
   chan.exchangeDeclare("directEx", "direct");
   //绑定队列到交换机
   //参数 1 队列名称,2 交换机名称 3 路由key
   chan.queueBind("direct02", "directEx", "jt1711");
   chan.basicQos(1);
   //定义消费者
   QueueingConsumer consumer=new QueueingConsumer(chan);
   //消费者与队列绑定
   chan.basicConsume("direct02",false, consumer);
   while(true){
     Delivery delivery= consumer.nextDelivery();
     System.out.println("二号消费者接收到"+
     new String(delivery.getBody()));
     chan.basicAck(delivery.getEnvelope().
         getDeliveryTag(), false);
   }
 }
 
 @Test
 public void topicSend() throws Exception{
   //获取连接
   Connection conn = ConnectionUtil.getConn();
   Channel chan = conn.createChannel();
   //声明交换机
   //参数意义,1 交换机名称,2 类型:fanout,direct,topic
   chan.exchangeDeclare("topicEx", "topic");
   //发送消息
   String msg="主题模式的消息";
   chan.basicPublish("topicEx", "jt1712.add.update", 
       null, msg.getBytes());
 }
 @Test
 public void topicRec01() throws Exception{
   System.out.println("一号消费者等待接收消息");
   //获取连接
   Connection conn = ConnectionUtil.getConn();
   Channel chan = conn.createChannel();
   //声明队列
   chan.queueDeclare("topic01", false, false, false, null);
   //声明交换机
   chan.exchangeDeclare("topicEx", "topic");
   //绑定队列到交换机
   //参数 1 队列名称,2 交换机名称 3 路由key
   chan.queueBind("topic01", "topicEx", "jt1712");
   chan.basicQos(1);
   //定义消费者
   QueueingConsumer consumer=new QueueingConsumer(chan);
   //消费者与队列绑定
   chan.basicConsume("topic01",false, consumer);
   while(true){
     Delivery delivery= consumer.nextDelivery();
     System.out.println("一号消费者接收到"+
     new String(delivery.getBody()));
     chan.basicAck(delivery.getEnvelope().
         getDeliveryTag(), false);
   }
 }
 @Test
 public void topicRec02() throws Exception{
   System.out.println("二号消费者等待接收消息");
   //获取连接
   Connection conn = ConnectionUtil.getConn();
   Channel chan = conn.createChannel();
   //声明队列
   chan.queueDeclare("topic02", false, false, false, null);
   //声明交换机
   chan.exchangeDeclare("topicEx", "topic");
   //绑定队列到交换机
   //参数 1 队列名称,2 交换机名称 3 路由key
   chan.queueBind("topic02", "topicEx", "jt1712.#");
   chan.basicQos(1);
   //定义消费者
   QueueingConsumer consumer=new QueueingConsumer(chan);
   //消费者与队列绑定
   chan.basicConsume("topic02",false, consumer);
   while(true){
     Delivery delivery= consumer.nextDelivery();
     System.out.println("二号消费者接收到"+
     new String(delivery.getBody()));
     chan.basicAck(delivery.getEnvelope().
         getDeliveryTag(), false);
   }
 }
}

  

MQ的demo的更多相关文章

  1. SpringCloud SpringBoot 前后端分离企业级微服务架构源码赠送

    基于SpringBoot2.x.SpringCloud和SpringCloudAlibaba并采用前后端分离的企业级微服务敏捷开发系统架构.并引入组件化的思想实现高内聚低耦合,项目代码简洁注释丰富上手 ...

  2. RabbitMQ:从零开始

    目录 一.介绍 二.安装 三.基本配置 四.Java Demo 五.基础API使用 六.ACK机制 七.消息的持久化 八.消息的公平分发 九.消息的优先级 十.消息的路由分发 十一.Spring集成 ...

  3. MVC+MQ+WinServices+Lucene.Net Demo

    前言: 我之前没有接触过Lucene.Net相关的知识,最近在园子里看到很多大神在分享这块的内容,深受启发.秉着“实践出真知”的精神,再结合公司项目的实际情况,有了写一个Demo的想法,算是对自己能力 ...

  4. ActiveMQ入门实例Demo

    前面我们已经搭建和配置好了ActiveMQ,下面来看一个Demo,体验一下MQ. JMS 消息模型 JMS消息服务应用程序结构支持两种模型:点对点模型,发布者/订阅者模型. (1)点对点模型(Queu ...

  5. JMS与MQ详解(有项目)

    <一>m2mGW项目 1.ActiveMQ概述     企业消息软件从80年代起就存在,它不只是一种应用间消息传递风格,也是一种集成风格.因此,消息传递可以满足应用间的通知和互相操作.但是 ...

  6. socket.io简单说明及在线抽奖demo

    socket.io简单说明及在线抽奖demo socket.io 简介 Socket.IO可以实现实时双向的基于事件的通信. 它适用于各种平台,浏览器或设备,也同样注重可靠性和速度. socket.i ...

  7. kafka2.9.2的伪分布式集群安装和demo(java api)测试

    目录: 一.什么是kafka? 二.kafka的官方网站在哪里? 三.在哪里下载?需要哪些组件的支持? 四.如何安装? 五.FAQ 六.扩展阅读   一.什么是kafka? kafka是LinkedI ...

  8. ubuntu12.04+kafka2.9.2+zookeeper3.4.5的伪分布式集群安装和demo(java api)测试

    博文作者:迦壹 博客地址:http://idoall.org/home.php?mod=space&uid=1&do=blog&id=547 转载声明:可以转载, 但必须以超链 ...

  9. HBase(二): c#访问HBase之股票行情Demo

    上一章完成了c#访问hbase的sdk封装,接下来以一个具体Demo对sdk进行测试验证.场景:每5秒抓取指定股票列表的实时价格波动行情,数据下载后,一方面实时刷新UI界面,另一方面将数据放入到在内存 ...

随机推荐

  1. 学习selenium的过程

  2. idea配置springBoot项目热加载

    1.在application.properties中禁用模板引擎缓存 比如freemarker:spring.freemarker.cache=false 2.在pom.xml中添加依赖 <de ...

  3. 2016-2017-2 20155312 实验二《Java面向对象程序设计》实验报告

    知识总结 伪代码 产品代码 Java编程时,程序员对类实现的测试叫单元测试. 测试用例是为某个特殊目标而编制的一组测试输入.执行条件以及预期结果,以便测试某个程序路径或核实是否满足某个特定需求. 先写 ...

  4. 7个鲜为人知却超实用的PHP函数--转(柒捌玖零)

    PHP有许多内置函数,其中大多数函数都被程序员广泛使用.但也有一些函数隐藏在角落,本文将向大家介绍7个鲜为人知,但用处非常大的函数. 没用过的程序员不妨过来看看. 1.highlight_string ...

  5. 783. Minimum Distance Between BST Node

    方法一,非递归方法,中序遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *l ...

  6. Linux 第三天

    2.文件处理命令 1)touch 创建空文件 语法:touch文件名 2)cat 显示文件内容 英文原意:concatenate 语法:cat 文件名 常用选项: -n:number,显示行号 3)t ...

  7. vue-cli引入mui的步骤

    不用npm安装了 1.mui官方GitHub下载mui所需文件 https://github.com/dcloudio/mui 把下载来的dist文件夹整个复制到static文件夹中 2.在index ...

  8. error C4430: error 2141

    c:\evan\workspace\1\1\netwowkippack.h(50) : error C2146: 语法错误 : 缺少“;”(在标识符“nSourPort”的前面) c:\evan\wo ...

  9. javase jdk 环境变量 涵义

    jdk环境变量配置:path:jdk安装所在目录下的bin路径-->因为环境变量path下放置的是操作系统执行的.exe文件,jdk中bin中放的是可执行的.exe文件,所以要把这个路径放置到p ...

  10. Codeforces Round#415 Div.2

    A. Straight «A» 题面 Noora is a student of one famous high school. It's her final year in school - she ...