RabbitMQ简单应用の简单队列

(1)首先创建一个maven项目:

pom.xml,重点是配置RabbitMQ

  1     <dependencies>
  2         <dependency>
  3             <groupId>junit</groupId>
  4             <artifactId>junit</artifactId>
  5             <version>4.11</version>
  6             <!-- 表示开发的时候引入,发布的时候不会加载此包 -->
  7             <scope>test</scope>
  8         </dependency>
  9         <!-- spring核心包 -->
 10         <dependency>
 11             <groupId>org.springframework</groupId>
 12             <artifactId>spring-core</artifactId>
 13             <version>${spring.version}</version>
 14         </dependency>
 15
 16         <dependency>
 17             <groupId>org.springframework</groupId>
 18             <artifactId>spring-web</artifactId>
 19             <version>${spring.version}</version>
 20         </dependency>
 21         <dependency>
 22             <groupId>org.springframework</groupId>
 23             <artifactId>spring-oxm</artifactId>
 24             <version>${spring.version}</version>
 25         </dependency>
 26         <dependency>
 27             <groupId>org.springframework</groupId>
 28             <artifactId>spring-tx</artifactId>
 29             <version>${spring.version}</version>
 30         </dependency>
 31
 32         <dependency>
 33             <groupId>org.springframework</groupId>
 34             <artifactId>spring-jdbc</artifactId>
 35             <version>${spring.version}</version>
 36         </dependency>
 37
 38         <dependency>
 39             <groupId>org.springframework</groupId>
 40             <artifactId>spring-webmvc</artifactId>
 41             <version>${spring.version}</version>
 42         </dependency>
 43         <dependency>
 44             <groupId>org.springframework</groupId>
 45             <artifactId>spring-aop</artifactId>
 46             <version>${spring.version}</version>
 47         </dependency>
 48
 49         <dependency>
 50             <groupId>org.springframework</groupId>
 51             <artifactId>spring-context-support</artifactId>
 52             <version>${spring.version}</version>
 53         </dependency>
 54
 55         <dependency>
 56             <groupId>org.springframework</groupId>
 57             <artifactId>spring-test</artifactId>
 58             <version>${spring.version}</version>
 59         </dependency>
 60         <!-- mybatis核心包 -->
 61         <dependency>
 62             <groupId>org.mybatis</groupId>
 63             <artifactId>mybatis</artifactId>
 64             <version>${mybatis.version}</version>
 65         </dependency>
 66         <!-- mybatis/spring包 -->
 67         <dependency>
 68             <groupId>org.mybatis</groupId>
 69             <artifactId>mybatis-spring</artifactId>
 70             <version>1.2.2</version>
 71         </dependency>
 72         <!-- 导入java ee jar 包 -->
 73         <dependency>
 74             <groupId>javax</groupId>
 75             <artifactId>javaee-api</artifactId>
 76             <version>7.0</version>
 77         </dependency>
 78         <!-- 导入Mysql数据库链接jar包 -->
 79         <dependency>
 80             <groupId>mysql</groupId>
 81             <artifactId>mysql-connector-java</artifactId>
 82             <version>5.1.30</version>
 83         </dependency>
 84         <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
 85         <dependency>
 86             <groupId>commons-dbcp</groupId>
 87             <artifactId>commons-dbcp</artifactId>
 88             <version>1.2.2</version>
 89         </dependency>
 90         <!-- JSTL标签类 -->
 91         <dependency>
 92             <groupId>jstl</groupId>
 93             <artifactId>jstl</artifactId>
 94             <version>1.2</version>
 95         </dependency>
 96         <!-- 日志文件管理包 -->
 97         <!-- log start -->
 98         <dependency>
 99             <groupId>log4j</groupId>
100             <artifactId>log4j</artifactId>
101             <version>${log4j.version}</version>
102         </dependency>
103
104
105         <!-- 格式化对象,方便输出日志 -->
106         <dependency>
107             <groupId>com.alibaba</groupId>
108             <artifactId>fastjson</artifactId>
109             <version>1.1.41</version>
110         </dependency>
111         <dependency>
112             <groupId>org.slf4j</groupId>
113             <artifactId>slf4j-api</artifactId>
114             <version>${slf4j.version}</version>
115         </dependency>
116         <dependency>
117             <groupId>net.sf.json-lib</groupId>
118             <artifactId>json-lib</artifactId>
119             <version>2.4</version>
120             <classifier>jdk15</classifier>
121         </dependency>
122         <!-- https://mvnrepository.com/artifact/org.apache.ant/ant -->
123         <dependency>
124             <groupId>org.apache.ant</groupId>
125             <artifactId>ant</artifactId>
126             <version>1.8.2</version>
127         </dependency>
128         <dependency>
129             <groupId>org.slf4j</groupId>
130             <artifactId>slf4j-log4j12</artifactId>
131             <version>${slf4j.version}</version>
132         </dependency>
133         <!-- log end -->
134         <!-- 映入JSON -->
135         <dependency>
136             <groupId>org.codehaus.jackson</groupId>
137             <artifactId>jackson-mapper-asl</artifactId>
138             <version>1.9.13</version>
139         </dependency>
140         <!-- 上传组件包 -->
141         <dependency>
142             <groupId>commons-fileupload</groupId>
143             <artifactId>commons-fileupload</artifactId>
144             <version>1.3.1</version>
145         </dependency>
146         <dependency>
147             <groupId>commons-io</groupId>
148             <artifactId>commons-io</artifactId>
149             <version>2.4</version>
150         </dependency>
151         <dependency>
152             <groupId>commons-codec</groupId>
153             <artifactId>commons-codec</artifactId>
154             <version>1.9</version>
155         </dependency>
156         <dependency>
157             <groupId>net.sourceforge.jexcelapi</groupId>
158             <artifactId>jxl</artifactId>
159             <version>2.6.12</version>
160         </dependency>
161         <!-- https://mvnrepository.com/artifact/org.json/json -->
162         <dependency>
163             <groupId>org.json</groupId>
164             <artifactId>json</artifactId>
165             <version>20171018</version>
166         </dependency>
167         <dependency>
168             <groupId>com.sun.jna</groupId>
169             <artifactId>jna</artifactId>
170             <version>3.0.9</version>
171         </dependency>
172         <dependency>
173             <groupId>com.examples</groupId>
174             <artifactId>examples</artifactId>
175             <version>1.1.2</version>
176         </dependency>
177         <dependency>
178             <groupId>com.rabbitmq</groupId>
179             <artifactId>amqp-client</artifactId>
180             <version>3.5.1</version>
181         </dependency>
182         <dependency>
183             <groupId>org.springframework.amqp</groupId>
184             <artifactId>spring-rabbit</artifactId>
185             <version>1.4.5.RELEASE</version>
186         </dependency>
187     </dependencies> 

项目结构:

(1)首先创建获取MQ的链接工厂ConnectionUtils

 1 package com.mmr.rabbitmq.util;
 2
 3 import java.io.IOException;
 4
 5 import com.rabbitmq.client.Connection;
 6 import com.rabbitmq.client.ConnectionFactory;
 7
 8 public class ConnectionUtils {
 9     /**
10      * @desc 获取Mq 的链接
11      * @author zp
12      * @throws IOException
13      * @date 2018-7-19
14      */
15     public static  Connection getConnection() throws IOException {
16         // 1.定义一个链接工厂
17         ConnectionFactory factroy = new ConnectionFactory();
18
19         // 2.设置服务地址
20         factroy.setHost("127.0.0.1");
21
22         // 3.设置端口号
23         factroy.setPort(5672);
24
25         // 4.vhost  设置数据库
26         factroy.setVirtualHost("vhtest");
27
28         // 5.设置用户名
29         factroy.setUsername("jerry");
30
31         // 6. 设置密码
32         factroy.setPassword("123456");
33
34         // 7.返回链接
35         return factroy.newConnection();
36     }
37 }

(2)其次创建消息生产者Send,这里消息生产者每发送一次消息,我们就可以通过rabbitmq(http://localhost:15672)的服务Queues进行查看

 1 package com.mmr.rabbitmq.simple;
 2
 3 import java.io.IOException;
 4
 5 import com.mmr.rabbitmq.util.ConnectionUtils;
 6 import com.rabbitmq.client.Channel;
 7 import com.rabbitmq.client.Connection;
 8
 9 public class Send {
10     private static final String QUEUE_NAME = "test_simple_queue";
11     public static void main(String[] args) throws IOException {
12         // 1.获取一个链接
13         Connection connection = ConnectionUtils.getConnection();
14
15         // 2.获取一个通道
16         Channel channel = connection.createChannel();
17
18         // 3.创建队列 创建队列声明
19         channel.queueDeclare(QUEUE_NAME, false, false, false, null);
20
21         //
22         String msg = "hello simple";
23
24         channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
25
26         System.out.println("---send msg :"+msg);
27
28         channel.close();
29
30         connection.close();
31     }
32 }

(3)最后创建消息消费者Recv,这里我们通过while循环可以获取每次rabbitmq(http://localhost:15672)的服务Queues接收到消息

 1 package com.mmr.rabbitmq.simple;
 2
 3 import java.io.IOException;
 4
 5 import com.mmr.rabbitmq.util.ConnectionUtils;
 6 import com.rabbitmq.client.Channel;
 7 import com.rabbitmq.client.Connection;
 8 import com.rabbitmq.client.ConsumerCancelledException;
 9 import com.rabbitmq.client.QueueingConsumer;
10 import com.rabbitmq.client.QueueingConsumer.Delivery;
11 import com.rabbitmq.client.ShutdownSignalException;
12
13 /**
14  * @desc 消费者获取消息
15  * @author zp
16  * @date 2018-7-19
17  */
18 public class Recv {
19     private static final String QUEUE_NAME = "test_simple_queue";
20     public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {
21         // 获取链接
22         Connection  connection = ConnectionUtils.getConnection();
23
24         // 创建通道
25         Channel channel = connection.createChannel();
26
27         // 定义队列消费者
28         QueueingConsumer consumer = new QueueingConsumer(channel);
29
30         // 监听队列
31         channel.basicConsume(QUEUE_NAME, true, consumer);
32
33         while (true) {
34             Delivery delivery = consumer.nextDelivery();// 下一个到达的
35
36             String msgString = new String(delivery.getBody());
37
38             System.out.println("[recv] msg:"+msgString);
39         }
40     }
41 }

这样一个简单的队列应用就完成了。

原文地址:https://www.cnblogs.com/pengpengzhang/p/9334779.html

时间: 2024-08-02 19:44:40

RabbitMQ简单应用の简单队列的相关文章

rabbitmq的java简单实现

1,安装rabbitmq.我的是ubuntu14.04,在官网上面下载最新的安装文件http://www.rabbitmq.com/install-debian.html 2.安装完之后  启动rabbitmq, sudo rabbitmq-server 3.下载jar包 4.最简单的hello world的实现 Sender类 package com.lubby.test; import java.io.IOException; import com.rabbitmq.client.Chann

计算机程序的思维逻辑 (61) - 内存映射文件及其应用 - 实现一个简单的消息队列

本节介绍内存映射文件,内存映射文件不是Java引入的概念,而是操作系统提供的一种功能,大部分操作系统都支持. 我们先来介绍内存映射文件的基本概念,它是什么,能解决什么问题,然后我们介绍如何在Java中使用,我们会设计和实现一个简单的.持久化的.跨程序的消息队列来演示内存映射文件的应用. 基本概念 所谓内存映射文件,就是将文件映射到内存,文件对应于内存中的一个字节数组,对文件的操作变为对这个字节数组的操作,而字节数组的操作直接映射到文件上.这种映射可以是映射文件全部区域,也可以是只映射一部分区域.

Lua 下实现一个简单的消息队列

Lua 下实现一个简单的消息队列,如下简单的几条代码就可以了. local q1 = {} local q2 = {} -- 产生消息只需要 table.insert(q1, msg) -- 分发消息需要两层循环, 可以处理 dispatch 过程中产生的新消息 while q1[1] do q1,q2 = q2,q1 for i=1,#q2 do dispatch(q2[i]) q2[i] = nil end end

简单的动画队列模型

给新手写了一个便于理解的动画链式 var anmin = {} anmin.list = []; anmin.run = function(nextfun){ anmin.list.push(nextfun); //加入动画正在触发 (function fire() { if (!anmin.fireing) { var onceRun = anmin.list.shift() if (onceRun) { anmin.fireing = true; onceRun(function() { a

创建和订阅简单的观察队列

你没有必要自己手动实现Iobservable<T>接口来创建观察队列,同样的,你也没有必要实现Iobserver<T>接口来订阅这个队列.通过安装RX库,RX提供请多静态的方法来创建带有一个参数或多个或没有参数的简单队列.你可以很方便的使用这些静态方法.另外,RX还提供了订阅扩展方法来实现多种多样的OnNext,OnError,OnCompleted句柄委托.创建和订阅简单的观察队列下面的例子使用观察者类型的范围操作来创建简单的观察集合,通过Observable类订阅方法来订阅这个

简单数据结构之队列模拟

1 /************************************************************************************** 2 * Function : 模拟队列 3 * Create Date : 2014/04/23 4 * Author : NTSK13 5 * Email : [email protected] 6 * Copyright : 欢迎大家和我一起交流学习,转载请保持源文件的完整性. 7 * 任何单位和个人不经本人允许不

简单的js队列

简单的js队列 /** * [Queue] * @param {[Int]} size [队列大小] */ function Queue(size) { var list = []; //向队列中添加数据 this.push = function(data) { if (data==null) { return false; } //如果传递了size参数就设置了队列的大小 if (size != null && !isNaN(size)) { if (list.length == siz

java condition 实现简单的阻塞队列

上一篇文章介绍了condition的使用方法 https://www.cnblogs.com/liumy/p/11563772.html 这一篇文章介绍如何用condition来实现一个简单的阻塞队列 消费者 生产者模式. 消费者 生产者模式就是 生产者生产某些对象,消费者来消费这些对象.其中用对象数组来保存这些对象,既然是数组,在初始化的时候需要指定数组的大小. 在生产者生产的时候需要检查数组是否已经满了,如果满了,那么生产者会被挂起,等到有消费者消费对象时,再进行生产. 当消费者消费的时候,

第二百九十一节,RabbitMQ多设备消息队列

RabbitMQ多设备消息队列-安装与简介 RabbitMQ简介 解释RabbitMQ,就不得不提到AMQP(Advanced Message Queuing Protocol)协议. AMQP协议是一种基于网络的消息传输协议,它能够在应用或组织之间提供可靠的消息传输.RabbitMQ是该AMQP协议的一种实现,利用它,可以将消息安全可靠的从发 送方传输到接收方.简单的说,就是消息发送方利用RabbitMQ将信息安全的传递给接收方. RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息

RabbitMQ五:生产者--队列--多消费者

一.生成者-队列-多消费者(前言) 上篇文章,我们做了一个简单的Demo,一个生产者对应一个消费者,本篇文章就介绍 生产者-队列-多个消费者,下面简单示意图 P 生产者    C 消费者  中间队列 需求背景:工厂某部门需要生产n个零件,部门下面有2个小组,每个小组需要生产n/2个 公平派遣 每个小组的情况下,当所有奇怪的信息都很重,甚至信息很轻的时候,一个工作人员将不断忙碌,另一个工作人员几乎不会做任何工作.那么,RabbitMQ不知道什么,还会平均分配消息. 这是因为当消息进入队列时,Rab