amqp rabbitmq

Today we will look into Spring AMQP RabbitMQ example application. We have already discussed some “Spring AMQP Basics Theoretically” and “How to install and setup RabbitMQ Server” in my previous posts. Please refer them in the following:

今天,我们将研究Spring AMQP RabbitMQ示例应用程序。 在之前的文章中,我们已经讨论了“ Spring AMQP基础知识”和“如何安装和设置RabbitMQ Server”。 请参考以下内容:

  • Spring AMQPSpringAMQP
  • Spring RabbitMQ春天兔子MQ

In this post, we are going to develop a Spring AMQP RabbitMQ Messaging application. Let us start it now.

在本文中,我们将开发一个Spring AMQP RabbitMQ Messaging应用程序。 让我们现在开始。

Spring AMQP RabbitMQ示例 (Spring AMQP RabbitMQ Example)

Let us start developing a Spring AMQP RabbitMQ Messaging application using Maven, Eclipse IDE and RabbitMQ Server. It is same for all other Java IDEs.

让我们开始使用Maven,Eclipse IDE和RabbitMQ Server开发Spring AMQP RabbitMQ Messaging应用程序。 对于所有其他Java IDE都是一样的。

Please do the following the steps one by one:

请一一执行以下步骤:

  1. Create a Maven Java project in Eclipse IDE在Eclipse IDE中创建Maven Java项目
  2. Develop Spring AMQP Publisher program开发Spring AMQP Publisher程序
  3. package com.tp.spring.amqp.rabbit;import org.springframework.amqp.core.AmqpTemplate;
    import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringAMQPRabbitSender {private final String SENDER_XML = "springamqp-rabbit-sender-context.xml";public static void main(String[] args) throws Exception {AmqpTemplate amqpTemplate = (AmqpTemplate)(new ClassPathXmlApplicationContext(SENDER_XML)).getBean("amqpTemplate");int messagCount = 0;while (messagCount < 10){amqpTemplate.convertAndSend("tp.routingkey.1", "Message # " + messagCount++);}System.out.println( messagCount + " message(s) sent successfully.");}
    }
  4. Configure Spring AMQP Publisher required beans : springamqp-rabbit-sender-context.xml配置Spring AMQP Publisher所需的bean:s​​pringamqp-rabbit-sender-context.xml
  5. <?xml version="1.0"encoding="UTF-8"?>
    <beans xmlns="https://www.springframework.org/schema/beans"xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"xmlns:rabbit="https://www.springframework.org/schema/rabbit"xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.1.xsd https://www.springframework.org/schema/rabbit https://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd"><rabbit:connection-factory id="connectionFactory" host="localhost" username="tpuser" password="tpuser"/><rabbit:admin connection-factory="connectionFactory"/><rabbit:template id="amqpTemplate" connection-factory="connectionFactory"exchange="tpExchange"/>
    </beans>
  6. Develop Spring AMQP Consumer(Spring MDP) program开发Spring AMQP Consumer(Spring MDP)程序
  7. package com.tp.spring.amqp.rabbit;import org.springframework.amqp.core.Message;
    import org.springframework.amqp.core.MessageListener;
    // Spirng MDP(Message Driven POJO)
    public class SpringAMQPRabbitAyncListener implements MessageListener {@Overridepublic void onMessage(Message message) {System.out.println("Listener received message = " + new String(message.getBody()));}
    }
  8. Configure Spring AMQP Consumer required beans : springamqp-rabbt-listener-context.xml配置Spring AMQP Consumer需要的bean:s​​pringamqp-rabbt-listener-context.xml
  9. <?xmlversion="1.0"encoding="UTF-8"?>
    <beans xmlns="https://www.springframework.org/schema/beans"xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"xmlns:rabbit="https://www.springframework.org/schema/rabbit"xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.1.xsd https://www.springframework.org/schema/rabbithttps://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd"><rabbit:connection-factory id="connectionFactory" host="localhost" username="tpuser" password="tpuser"/><rabbit:admin connection-factory="connectionFactory"/><rabbit:queue id="tpQueue"/><rabbit:topic-exchange id="tpExchange" name="tpExchange"><rabbit:bindings><rabbit:binding queue="tpQueue" pattern="tp.routingkey.1"></rabbit:binding></rabbit:bindings></rabbit:topic-exchange><bean id="asyncListener" class="com.tp.spring.amqp.rabbit.SpringAMQPRabbitAyncListener"/><rabbit:listener-container id="myListenerContainer" connection-factory="connectionFactory"><rabbit:listener ref="asyncListener" queue-names="tpQueue"/></rabbit:listener-container></beans>
  10. Develop Spring AMQP Rabbit Container program to initialize Spring IOC Container开发Spring AMQP Rabbit Container程序以初始化Spring IOC Container
  11. package com.tp.spring.amqp.rabbit;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringAMQPRabbitlListenerContainer {public static void main(String[] args) {// Initialize Spring IOC Containernew ClassPathXmlApplicationContext("springamqp-rabbt-listener-context.xml");}
    }
  12. Final pom.xml file最终的pom.xml文件
  13. <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.tp</groupId><artifactId>spring-amqp-rabbitmq</artifactId><name>spring-amqp-rabbitmq</name><packaging>jar</packaging><version>1.0.0</version><properties><java-version>1.6</java-version><org.springframework-version>3.1.1.RELEASE</org.springframework-version><org.aspectj-version>1.6.10</org.aspectj-version><org.slf4j-version>1.6.6</org.slf4j-version></properties><dependencies><!-- Spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${org.springframework-version}</version><exclusions><!-- Exclude Commons Logging in favor of SLF4j --><exclusion><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${org.springframework-version}</version></dependency><!-- AspectJ --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>${org.aspectj-version}</version></dependency>    <!-- Logging --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${org.slf4j-version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>${org.slf4j-version}</version><scope>runtime</scope></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${org.slf4j-version}</version><scope>runtime</scope></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.15</version><exclusions><exclusion><groupId>javax.mail</groupId><artifactId>mail</artifactId></exclusion><exclusion><groupId>javax.jms</groupId><artifactId>jms</artifactId></exclusion><exclusion><groupId>com.sun.jdmk</groupId><artifactId>jmxtools</artifactId></exclusion><exclusion><groupId>com.sun.jmx</groupId><artifactId>jmxri</artifactId></exclusion></exclusions><scope>runtime</scope></dependency><!-- @Inject --><dependency><groupId>javax.inject</groupId><artifactId>javax.inject</artifactId><version>1</version></dependency><!-- Servlet --><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.1</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!-- Test --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.7</version><scope>test</scope></dependency><dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit</artifactId><version>1.1.1.RELEASE</version></dependency><dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-amqp</artifactId><version>1.1.4.RELEASE</version></dependency></dependencies><build><plugins><plugin><artifactId>maven-eclipse-plugin</artifactId><version>2.9</version><configuration><additionalProjectnatures><projectnature>org.springframework.ide.eclipse.core.springnature</projectnature></additionalProjectnatures><additionalBuildcommands><buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand></additionalBuildcommands><downloadSources>true</downloadSources><downloadJavadocs>true</downloadJavadocs></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.5.1</version><configuration><source>1.6</source><target>1.6</target><compilerArgument>-Xlint:all</compilerArgument><showWarnings>true</showWarnings><showDeprecation>true</showDeprecation></configuration></plugin><plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>1.2.1</version><configuration><mainClass>org.test.int1.Main</mainClass></configuration></plugin></plugins></build>
    </project>
  14. Our Final Maven Project Structure我们最终的Maven项目结构

使用RabbitMQ服务器测试Spring AMQP RabbitMQ示例 (Test Spring AMQP RabbitMQ Example with RabbitMQ Server)

  1. Run AMQP Publisher and observe messages in RabbitMQ Queue运行AMQP Publisher并观察RabbitMQ队列中的消息
  2. Here we can see that AMQP Publisher sent 10 messages successfully.

    在这里我们可以看到AMQP Publisher成功发送了10条消息。

  3. RabbitMQ console is showing 10 messages in the queueRabbitMQ控制台在队列中显示10条消息
  4. Here we can see that our RabbitMQ queue has received 10 messages successfully from AMQP Publisher.

    在这里,我们可以看到RabbitMQ队列已成功从AMQP Publisher接收到10条消息。

  5. Run AMQP Consumer and observe messages in Eclipse IDE运行AMQP Consumer,并在Eclipse IDE中观察消息
  6. Here we can see that AMQP Consumer receives each message one by one from RabbitMQ queue.

    在这里,我们可以看到AMQP消费者从RabbitMQ队列中逐条接收每个消息。

  7. RabbitMQ console is showing 0 messages in the queueRabbitMQ控制台在队列中显示0条消息
  8. Here we can observe that RabbitMQ queue has 0 messages that means AMQP Consumer has received all messages successfully.

    在这里我们可以观察到RabbitMQ队列中有0条消息,这意味着AMQP消费者已成功接收了所有消息。

NOTE: With this knowledge of Spring AMQP RabbitMQ Messaging, you can read more about Spring AMQP API and learn new things. And also go through RabbitMQ Server documentation to get more details about Exchanges, Queues etc.

注意:有了Spring AMQP RabbitMQ消息的知识,您可以阅读有关Spring AMQP API的更多信息并学习新知识。 并仔细阅读RabbitMQ Server文档以获取有关Exchange,队列等的更多详细信息。

NOTE: As I told you in my previous post, both Spring AMQP API and RabbitMQ Server are from The Pivotal Team.

注意:正如我在上一篇文章中告诉您的那样,Spring AMQP API和RabbitMQ Server均来自The Pivotal Team

That’s it all about developing Spring AMQP RabbitMQ Messaging Example. We will discuss and develop Spring AMQP ActiveMQ Messaging Example in my coming posts.

这就是开发Spring AMQP RabbitMQ消息传递示例的全部内容。 我们将在我的后续文章中讨论和开发Spring AMQP ActiveMQ消息传递示例。

Further Reading: Apache ActiveMQ.

进一步阅读: Apache ActiveMQ 。

翻译自: https://www.journaldev.com/11713/spring-amqp-rabbitmq-example

amqp rabbitmq

amqp rabbitmq_Spring AMQP RabbitMQ示例相关推荐

  1. Spring AMQP RabbitMQ示例

    Spring AMQP RabbitMQ示例 今天我们将研究Spring AMQP RabbitMQ示例应用程序.我们之前的帖子中已经讨论了一些"Spring AMQP基础知识理论" ...

  2. Amqp整合com.rabbitmq.client.ShutdownSignalException: channel error; protocol method异常处理

    Amqp整合com.rabbitmq.client.ShutdownSignalException: channel error; protocol method异常处理 参考文章: (1)Amqp整 ...

  3. AMQP 协议及 RabbitMQ 的 Java 用例

    AMQP 协议 AMQP is the Internet Protocol for Business Messaging (AMQP) Version 1.0 AMQP Working Group 0 ...

  4. Spring boot Rabbitmq 示例

    Spring boot Rabbitmq 示例 简介     Spring boot RabbitMQ 简单程序示例 编写详情 RabbitMQ docker     避免麻烦,直接使用docker启 ...

  5. amqp activemq_Spring AMQP ActiveMQ教程(第1部分)

    amqp activemq Welcome to Spring AMQP ActiveMQ Tutorial. Earlier we looked into installing Apache Act ...

  6. rabbitmq 示例_PHP和RabbitMQ:高级示例

    rabbitmq 示例 In part 1 we covered the theory and a simple use case of the AMQP protocol in PHP with R ...

  7. rabbitmq python amqp user_python 与rabbitmq

    一.rabbitmq简介.安装 简介: MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专 ...

  8. 理解AMQP协议和RabbitMQ的性能和可靠平衡

    前言 在之前的博客中,已经使用Pika包实践操作过RabbitMQ了,借用了几个不同的Exchange实现不同功能的生产-消费模式,但是对RabbitMQ的细节还缺乏更进一步的理解.今天从AMQP协议 ...

  9. 【go-zero】go-zero 与 amqp go整合 Rabbitmq 实现消息推送 go 消息队列 (best practice)

    目录 RabbitMq安装与go-zero框架整合使用 一.RabbitMq 1.打开 RabbitMq 进行简单地配置 2.Admin创建一个virtual Host 3.Exchanges创建交换 ...

最新文章

  1. 在UE5创造一个多山的松树森林场景学习教程
  2. 2015计算机硕士考研非统考的学校,2015考研计算机专业统考非统考学校统计
  3. linux中LVM动态扩容和管理
  4. MySQL查询的进阶操作--排序查询
  5. PHPWind的版权等信息去除的方法
  6. 关于oracle sql语句查询时表名和字段名要加双引号的问题
  7. 建立单链表 单链表的插入_单链列表插入
  8. 哈希表(hash table)及其应用举例
  9. xx云网络实施方案案例
  10. Intellij-工程目录下隐藏不想显示的文件和文件夹
  11. 数据库学习----MySQL(一)
  12. TensorFlow2学习笔记:3、鸢尾花数据集载入
  13. Rational Rose—概述
  14. 小学生认识计算机网络教案,小学信息技术四年级教案
  15. 题解 [LuoguP5560][Celeste-B]Golden Feather
  16. 华东理工大学计算机专业研究生,华东理工大学硕士研究生培养方案计算机科学与技术一级学科(学科代码:0812)信息科学与工程学院...
  17. BOM对非标制造企业成本管控的重要性
  18. 2019年Q4三星和华为都败了,谁也想不到第一名是它
  19. GICv3_LPI机制
  20. 旅游网-去哪儿网景点评论爬取

热门文章

  1. 使用 Django 的日志模块,同时发送错误邮件到163邮箱
  2. Day04:循环结构(while、do-while、for)
  3. 【CSS3】CSS多列属性(Multi-column)
  4. (转载)Android项目实战(二十七):数据交互(信息编辑)填写总结
  5. saltstack系列2之zabbix-agent自动化部署
  6. 自由缩放属性-resize(禁止textarea的自由缩放尺寸功能)
  7. OpenStack点滴01-概览
  8. UCOSII学习笔记[开篇]
  9. 谈谈我们的学习和我们的Blog
  10. stl之bit_vector原理及应用