一:概述

1.官网

  https://spring.io/

2.进入project

  

3.找到spring AMQP

  

二:程序

1.结构

  

2.pom

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6.  
  7. <groupId>SpringRabbitmq</groupId>
  8. <artifactId>mq</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. <dependencies>
  11. <!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
  12. <dependency>
  13. <groupId>com.rabbitmq</groupId>
  14. <artifactId>amqp-client</artifactId>
  15. <version>3.6.2</version>
  16. </dependency>
  17. <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
  18. <dependency>
  19. <groupId>org.slf4j</groupId>
  20. <artifactId>slf4j-api</artifactId>
  21. <version>1.7.10</version>
  22. </dependency>
  23. <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
  24. <dependency>
  25. <groupId>org.slf4j</groupId>
  26. <artifactId>slf4j-log4j12</artifactId>
  27. <version>1.7.5</version>
  28. <scope>test</scope>
  29. </dependency>
  30. <!-- https://mvnrepository.com/artifact/log4j/log4j -->
  31. <dependency>
  32. <groupId>log4j</groupId>
  33. <artifactId>log4j</artifactId>
  34. <version>1.2.17</version>
  35. </dependency>
  36. <!-- https://mvnrepository.com/artifact/junit/junit -->
  37. <dependency>
  38. <groupId>junit</groupId>
  39. <artifactId>junit</artifactId>
  40. <version>4.11</version>
  41. <scope>test</scope>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.amqp</groupId>
  45. <artifactId>spring-rabbit</artifactId>
  46. <version>2.0.2.RELEASE</version>
  47. </dependency>
  48. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  49. <dependency>
  50. <groupId>org.springframework</groupId>
  51. <artifactId>spring-context</artifactId>
  52. <version>5.0.3.RELEASE</version>
  53. </dependency>
  54.  
  55. </dependencies>
  56. <build>
  57. <plugins>
  58. <plugin>
  59. <groupId>org.apache.maven.plugins</groupId>
  60. <artifactId>maven-compiler-plugin</artifactId>
  61. <configuration>
  62. <source>1.7</source>
  63. <target>1.7</target>
  64. </configuration>
  65. </plugin>
  66. </plugins>
  67. </build>
  68. </project>

3.Java

  1. package spring;
  2.  
  3. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.AbstractApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. import org.springframework.context.support.GenericXmlApplicationContext;
  8.  
  9. public class SpringMain {
  10. public static void main(String[] args)throws Exception{
  11. ApplicationContext context = new GenericXmlApplicationContext("classpath:rabbit-context.xml");
  12. //RabbitMq模板
  13. RabbitTemplate template=context.getBean(RabbitTemplate.class);
  14. //发送消息
  15. template.convertAndSend("spring.MyConsumer");
  16. Thread.sleep(2000);
  17.  
  18. }
  19. }

4.Java

  1. package spring;
  2.  
  3. public class MyConsumer {
  4. public void listen(String foo) {
  5. System.out.println("消费者:"+foo);
  6. }
  7. }

5.rabbit-context.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.7.xsd">
  7. <!-- 定义MQ工厂-->
  8. <rabbit:connection-factory id="connectionFactory"
  9. host="127.0.0.1" port="5672" virtual-host="/cjhost"
  10. username="caojun" password="123456"/>
  11. <!-- 定义MQ模板,指定要连接的工厂以及交换机-->
  12. <rabbit:template id="amqpTemplate"
  13. connection-factory="connectionFactory"
  14. exchange="myExchange"/>
  15. <!-- MQ管理,包括队列,交换机声明等-->
  16. <rabbit:admin connection-factory="connectionFactory" />
  17. <!--定义队列-->
  18. <rabbit:queue name="myQueue" auto-declare="true" durable="true" />
  19. <!--定义交换机-->
  20. <rabbit:topic-exchange name="myExchange" auto-declare="true">
  21. <rabbit:bindings>
  22. <rabbit:binding queue="myQueue" pattern="foo.*" />
  23. </rabbit:bindings>
  24. </rabbit:topic-exchange>
  25.  
  26. <!--队列监听-->
  27. <rabbit:listener-container connection-factory="connectionFactory">
  28. <rabbit:listener ref="foo" method="listen" queue-names="myQueue" />
  29. </rabbit:listener-container>
  30.  
  31. <bean id="foo" class="spring.MyConsumer" />
  32. </beans>

·

spring继承Rabbitmq client-----------------------待研究的更多相关文章

  1. 从头开始搭建一个Spring boot+RabbitMQ环境

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  2. Spring boot+RabbitMQ环境

    Spring boot+RabbitMQ环境 消息队列在目前分布式系统下具备非常重要的地位,如下的场景是比较适合消息队列的: 跨系统的调用,异步性质的调用最佳. 高并发问题,利用队列串行特点. 订阅模 ...

  3. 五.Spring与RabbitMQ集成--HelloWorld

    spring对RabbitMQ做了很好的集成,我们称之为spring AMQP,其官方文档写得十分详尽,文档地址:https://docs.spring.io/spring-amqp/referenc ...

  4. Caused by: com.rabbitmq.client.ShutdownSignalException: connection error

    周五下午的时候升级了一个环境,跑了批处理sh升级脚本后,启动时报下列错误: INFO | jvm 1 | 2017/02/24 17:39:09 | java.io.IOException INFO ...

  5. 六.Spring与RabbitMQ集成--HelloWorld

    spring对RabbitMQ做了很好的集成,我们称之为spring AMQP,其官方文档写得十分详尽,文档地址:https://docs.spring.io/spring-amqp/referenc ...

  6. RabbitMQ.Client API (.NET)中文文档

    主要的名称空间,接口和类 核心API中定义接口和类 RabbitMQ.Client 名称空间: 1 using RabbitMQ.Client; 核心API接口和类 IModel :表示一个AMQP ...

  7. 170613、Spring整合RabbitMQ实例

    一.rabbitMQ简介 1.1.rabbitMQ的优点(适用范围)1. 基于erlang语言开发具有高可用高并发的优点,适合集群服务器.2. 健壮.稳定.易用.跨平台.支持多种语言.文档齐全.3. ...

  8. spring boot rabbitmq 多MQ配置 自动 创建 队列 RPC

      源码地址:https://github.com/hutuchong518/RabbitmqStudy 需求:   spring boot 整合 rabbitmq rpc功能, 需要将 请求和响应 ...

  9. Spring Boot RabbitMQ 延迟消息实现完整版

    概述 曾经去网易面试的时候,面试官问了我一个问题,说 下完订单后,如果用户未支付,需要取消订单,可以怎么做 我当时的回答是,用定时任务扫描DB表即可.面试官不是很满意,提出: 用定时任务无法做到准实时 ...

随机推荐

  1. CF1009F Dominant Indices

    传送门 还是放个链接让泥萌去学一下把 orzYYB 题目中要求的\(f_{x,j}\),转移是\(f_{x,j}=\sum_{y=son_x} f_{y,j-1}\),所以这个东西可以用长链剖分优化, ...

  2. webstorm11.0.3连接ftp

    一.工具 webstorm11.0.3 ftp工具:Beyond Compare 3 二.步骤 1.打开项目工程,按照下图中路径,打开ftp配置界面 2.在ftp配置界面 (1)右上角+号新建配置,选 ...

  3. KEYCODE_DPAD_CENTER 和 KEYCODE_ENTER

    KEYCODE_DPAD_CENTER或者是KEYCODE_ENTER时,会在view优先处理isConfirmKey,而在activity中的onKeyDown和up中接收不到该按键消息

  4. web前端最全各类资源

    链接:http://www.sohu.com/a/157593700_132276

  5. 矩阵的SVD分解

    转自 http://blog.csdn.net/zhongkejingwang/article/details/43053513(实在受不了CSDN的广告) 在网上看到有很多文章介绍SVD的,讲的也都 ...

  6. 【逆向工具】使用x64dbg+spy去除WinRAR5.40(64位)广告弹框

    1 学习目标 WinRAR5.40(64位)的弹框广告去除,由于我的系统为x64版本,所以安装了WinRAR(x64)版本. OD无法调试64位的程序,可以让我熟悉x64dbg进行调试的界面. 其次是 ...

  7. MySQL— 索引

    目录 一.索引 二.索引类型 三.索引种类 四.操作索引 五.创建索引的时机 六.命中索引 七.其它注意事项 八.LIMIT分页 九.执行计划 十.慢查询日志 一.索引 MySQL索引的建立对于MyS ...

  8. Ubuntu 16.04配置国内高速apt-get更新源【转】

    转自:https://blog.csdn.net/twang0x80/article/details/79782753 Ubuntu 16.04下载软件速度有点慢,因为默认的是从国外下载软件,那就更换 ...

  9. C++:explicit关键字

    在C++中,如果一个类的构造函数只有一个形参,在这种情况下,可以直接将一个对应于构造函数参数类型的数据直接赋值给类变量,编译器在编译时会自动进行类型转换,将对应于构造函数参数类型的数据转换为类的对象, ...

  10. 002_mtr_a network diagnostic tool

    一. mtr combines the functionality of the traceroute and ping programs in a single network diagnostic ...