一:介绍

1.优缺点

  简单,但是耦合性较高。

  这种模式是生产者与消费者一一对应,就是一个产生者,有一个消费者来消费。

  如果,多个消费者想消费一个队列中的消息就不适合了。这种情况在后面会接着介绍。

2.进入官网

  进入get start

  

  然后进入Tutorials

  

  发现简单消息队列

  

二:新建项目

1.新建maven项目

  

2.pomwenjian  

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>mq</groupId>
<artifactId>rabbitmqTest</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> </dependencies> </project>

3.公共类

  获取连接

 package com.mq.utils;

 import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory; public class ConnectionUtil {
/**
* 获取connection连接
*/
public static Connection getConnection()throws Exception{
//定义一个连接工厂
ConnectionFactory factory=new ConnectionFactory();
//设置服务地址
factory.setHost("127.0.0.1");
//设置AMQP端口
factory.setPort(5672);
//vhost
factory.setVirtualHost("/cjhost");
//用户名
factory.setUsername("caojun");
//密码
factory.setPassword("123456");
//返回连接
return factory.newConnection();
}
}

4.项目结构

  

三:生产者

1.程序

 package com.mq.send;

 import com.mq.utils.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection; public class SimpleSend {
private static final String QUENE_NAME="test_simple_queue";
public static void main(String[] args) throws Exception {
//获取一个连接
Connection connection= ConnectionUtil.getConnection();
//从连接中获取一个通道
Channel channel=connection.createChannel();
//创建队列声明
channel.queueDeclare(QUENE_NAME,false,false,false,null); //消息
String strBody="Hello Mq"; //发送
channel.basicPublish("",QUENE_NAME,null,strBody.getBytes());
System.out.println("send strBody:"+strBody); //关闭连接
channel.close();
connection.close();
}
}

2.运行

  控制台:

  

  管理平台:

  

3.使用管理平台获取消息

  这个时候,队列中的消息就会被消费掉。

  

四:消费者

1.程序一

  这个程序中的API是3.4之前的,现在还能用

 package com.mq.receive;

 import com.mq.utils.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery; public class SimpleReceive {
private static final String QUENE_NAME="test_simple_queue";
public static void main(String[] args) throws Exception {
//获取一个连接
Connection connection= ConnectionUtil.getConnection();
//创建通道
Channel channel=connection.createChannel();
//定义消费者
QueueingConsumer consumer=new QueueingConsumer(channel);
//监听队列
channel.basicConsume(QUENE_NAME,true,consumer);
while (true){
Delivery delivery=consumer.nextDelivery();
String strBody=new String(delivery.getBody());
System.out.println("receive strBody:"+strBody);
}
}
}

2.效果

  

3.程序二

  这个是新的api

 package com.mq.receive;

 import com.mq.utils.ConnectionUtil;
import com.rabbitmq.client.*;
import com.rabbitmq.client.QueueingConsumer.Delivery; import java.io.IOException; public class SimpleReceive {
private static final String QUENE_NAME = "test_simple_queue"; public static void main(String[] args) throws Exception {
newApi();
}
public static void newApi()throws Exception{
//获取一个连接
Connection connection = ConnectionUtil.getConnection();
//创建通道
Channel channel = connection.createChannel();
//创建队列声明
channel.queueDeclare(QUENE_NAME,false,false,false,null);
//创建消费者
DefaultConsumer consumer=new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String strBody=new String(body,"utf-8");
System.out.println("receive new strBody:"+strBody);
}
};
//监听队列
channel.basicConsume(QUENE_NAME,true,consumer);
} /**
* 这个是老的API
* @throws Exception
*/
public static void oldApi() throws Exception {
//获取一个连接
Connection connection = ConnectionUtil.getConnection();
//创建通道
Channel channel = connection.createChannel();
//定义消费者
QueueingConsumer consumer = new QueueingConsumer(channel);
//监听队列
channel.basicConsume(QUENE_NAME, true, consumer);
while (true) {
Delivery delivery = consumer.nextDelivery();
String strBody = new String(delivery.getBody());
System.out.println("receive strBody:" + strBody);
}
}
}

4.效果  

  

simple简单消息队列的更多相关文章

  1. Redis实现简单消息队列

    http://www.jianshu.com/p/9c04890615ba 任务异步化 打开浏览器,输入地址,按下回车,打开了页面.于是一个HTTP请求(request)就由客户端发送到服务器,服务器 ...

  2. 用 Redis 实现 PHP 的简单消息队列

    参考:PHP高级编程之消息队列 消息队列就是在消息的传输过程中,可以保存消息的容器. 常见用途: 存储转发:异步处理耗时的任务 分布式事务:多个消费者消费同一个消息队列 应对高并发:通过消息队列保存任 ...

  3. redis(五)---- 简单消息队列

    消息队列一个消息的链表,是一个异步处理的数据处理引擎.不仅能够提高系统的负荷,还能够改善因网络阻塞导致的数据缺失.一般用于邮件发送.手机短信发送,数据表单提交.图片生成.视频转换.日志储存等. red ...

  4. RabbitMq(2) 简单消息队列

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

  5. redis简单消息队列

    <?php $redis = new Redis(); $redis->connect('127.0.0.1',6379); $redis->flushall(); $redis-& ...

  6. HTTPSQS(HTTP Simple Queue Service)消息队列

    HTTPSQS(HTTP Simple Queue Service)是一款基于 HTTP GET/POST 协议的轻量级开源简单消息队列服务,使用 Tokyo Cabinet 的 B+Tree Key ...

  7. Redis使用总结(3):实现简单的消息队列

    参考Redis实现简单消息队列 Redis提供了两种方式来作消息队列.一个是使用生产者消费模式模式,另外一个方法就是发布订阅者模式.前者会让一个或者多个客户端监听消息队列,一旦消息到达,消费者马上消费 ...

  8. PDO和消息队列的一点个人理解

    什么是消息队列,百度百科说,···消息队列····是在消息的传输过程中保存消息的容器. 看着网上林林总总的文章,都说是为了应对高并发,处理数据量超级大的一种数据容器,也可以说是利用各种方式,先把数据存 ...

  9. RocketMQ 消息队列单机部署及使用

    转载请注明来源:http://blog.csdn.net/loongshawn/article/details/51086876 相关文章: <RocketMQ 消息队列单机部署及使用> ...

随机推荐

  1. metasploit中meterpreter命令

    meterpreter是Metasploit框架中的一个杀手锏,通常作为漏洞溢出后的攻击载荷所使用,攻击载荷在触发漏洞后能够返回给我们一个控制通道. 常见的meterpreter命令 run scri ...

  2. C# print2flash3文件转化

    1.下载print2flash3 并且安装print2flash3 2.转换工具类 (1)需要导入using Print2Flash3; 这个程序集 using System; using Syste ...

  3. position属性absolute与relative 的区别

    连接:https://www.cnblogs.com/duyanli/p/3534005.html 每次要用到Position属性时,总要去搜索下,这两个属性值的区别:今天就直接复制网上的结果,以便以 ...

  4. rest framework错误笔记——AssertionError: Cannot apply DjangoModelPermissionsOrAnonReadOnly on a view that does not set `.queryset` or have a `.get_queryset()` method.

    用到@api_view装饰器时,访问路由查看api数据时,报错: AssertionError: Cannot apply DjangoModelPermissionsOrAnonReadOnly o ...

  5. POJ1258 Agri-Net【最小生成树】

    题意: 有n个农场,已知这n个农场都互相相通,有一定的距离,现在每个农场需要装光纤,问怎么安装光纤能将所有农场都连通起来,并且要使光纤距离最小,输出安装光纤的总距离. 思路: 又是一个最小生成树,因为 ...

  6. FPN-Feature Pyramid Networks for Object Detection

    FPN-Feature Pyramid Networks for Object Detection 标签(空格分隔): 深度学习 目标检测 这次学习的论文是FPN,是关于解决多尺度问题的一篇论文.记录 ...

  7. Mybatis进阶学习笔记——输入映射

    1.输入映射 输入映射支持的类型: 1) 基本的类型,int,String,double 等(*)2) JavaBean 类型(*)3) 包装JavaBean 类型(对象里面包含另一个对象) 1.1基 ...

  8. 2018-2019-2 网络对抗技术 20165227 Exp1 PC平台逆向破解

    2018-2019-2 网络对抗技术 20165227 Exp1 PC平台逆向破解 实验内容及步骤 实验一:直接修改程序机器指令,改变程序执行流程 知识要求:Call指令,EIP寄存器,指令跳转的偏移 ...

  9. weblogic对JSP预编译、weblogic读取JSP编译后的class文件、ant中weblogic.jspc预编译JSP

    我们都知道在weblogic中JSP是每次第一次访问的时候才会编译,这就造成第一次访问某个JSP的时候性能下降,有时候我们也希望JSP被编译成class然后打包在jar中实现隐藏JSP的功能,下面介绍 ...

  10. 【Python学习笔记】调整matplotlib的图例legend的位置

    有时默认的图例位置不符合我们的需要,那么我们可以使用下面的代码对legend位置进行调整. plt.legend(loc='String or Number', bbox_to_anchor=(num ...