上一节是使用rabbitAdmin的管理组件进行声明队列,交换器,绑定等操作,本节则是采用AMQP声明式配置来声明这些东西。AMQP声明主要是通过@Bean注解进行的。

配置:

 package com.zxy.demo.config;

 import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan(basePackages= {"com.zxy.demo.*"})
public class RabbitmqCofing {
// 注入连接工厂,spring的配置,springboot可以配置在属性文件中
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connection = new CachingConnectionFactory();
connection.setAddresses("192.168.10.110:5672");
connection.setUsername("guest");
connection.setPassword("guest");
connection.setVirtualHost("/");
return connection;
}
// 配置RabbitAdmin来管理rabbit
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
//用RabbitAdmin一定要配置这个,spring加载的是后就会加载这个类================特别重要
rabbitAdmin.setAutoStartup(true);
return rabbitAdmin;
}
//===========================以上结合测试rabbitAdmin部分=========================================================== //===========================以下为AMQP配置队列绑定等,spring容器加载时候就能够注入===========================================================
// 采用AMQP定义队列、交换器、绑定等
@Bean(name="direct.queue01")
public Queue queue001() {
return new Queue("direct.queue01", true, false, false);
}
@Bean(name="test.direct01")
public DirectExchange directExchange() {
return new DirectExchange("test.direct01", true, false, null);
}
@Bean
public Binding bind001() {
return BindingBuilder.bind(queue001()).to(directExchange()).with("mq.#");
}
@Bean(name="topic.queue01")
public Queue queue002() {
return new Queue("topic.queue01", true, false, false);
}
@Bean(name="test.topic01")
public TopicExchange topicExchange() {
return new TopicExchange("test.topic01", true, false, null);
}
@Bean
public Binding bind002() {
return BindingBuilder.bind(queue002()).to(topicExchange()).with("mq.topic");
}
@Bean(name="fanout.queue01")
public Queue queue003() {
return new Queue("fanout.queue", true, false, false);
}
@Bean(name="test.fanout01")
public FanoutExchange fanoutExchange() {
return new FanoutExchange("test.fanout01", true, false, null);
}
@Bean
public Binding bind003() {
return BindingBuilder.bind(queue003()).to(fanoutExchange());
} //===========================注入rabbitTemplate组件===========================================================
// 跟spring整合注入改模板,跟springboot整合的话只需要在配置文件中配置即可
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
return rabbitTemplate;
}
}

单元测试:

 package com.zxy.demo;

 import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class ForwardApplicationTests { @Test
public void contextLoads() {
}
@Autowired
private RabbitAdmin rabbitAdmin;
@Test
public void testAdmin() {
// 切记命名不能重复复
rabbitAdmin.declareQueue(new Queue("test.direct.queue"));
rabbitAdmin.declareExchange(new DirectExchange("test.direct"));
rabbitAdmin.declareBinding(new Binding("test.direct.queue", Binding.DestinationType.QUEUE, "test.direct", "mq.direct", null)); rabbitAdmin.declareQueue(new Queue("test.topic.queue", true,false, false));
rabbitAdmin.declareExchange(new TopicExchange("test.topic", true,false));
// 如果注释掉上面两句实现声明,直接进行下面的绑定竟然不行,该版本amqp-client采用的是5.1.2,将上面两行代码放开,则运行成功
rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue("test.topic.queue", true,false, false))
.to(new TopicExchange("test.topic", true,false)).with("mq.topic"));
// 经过实验确实是需要先声明,才可以运行通过
rabbitAdmin.declareQueue(new Queue("test.fanout.queue",true,false,false,null));
rabbitAdmin.declareExchange(new FanoutExchange("test.fanout", true, false, null));
rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue("test.fanout.queue", true, false,false))
.to(new FanoutExchange("test.fanout", true, false)));
rabbitAdmin.purgeQueue("test.direct.queue", false);//清空队列消息
}
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testTemplate() {
String body = "hello,test rabbitTemplage!";
MessageProperties properties = new MessageProperties();
properties.setContentEncoding("utf-8");
properties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
properties.setPriority(1);
properties.setHeader("nihao:", "yes!");
Message message = new Message(body.getBytes(), properties);
// MessagePostProcessor参数是在消息发送过程中动态修改消息属性的类
rabbitTemplate.convertAndSend("test.direct01", "mq.direct", message,new MessagePostProcessor() { @Override
public Message postProcessMessage(Message message) throws AmqpException {
// 修改属性
message.getMessageProperties().setHeader("nihao:", "no");
// 添加属性
message.getMessageProperties().setHeader("新添加属性:", "添加属性1");
return message;
}
}); // 发送objcet类型
rabbitTemplate.convertAndSend("test.topic01", "mq.topic", "send object type message!!!");
System.out.println("发送完毕!!!");
} }

Rabbitmq与spring整合之重要组件介绍——AMQP声明式配置&RabbitTemplate组件的更多相关文章

  1. spring整合mybatis,ioc容器及声明式事务配置

    步骤: 1.创建jdbc.properties文件,用来管理存放连接数据库的相关信息 jdbc.properties:jdbc.user=root jdbc.password=123456 jdbc. ...

  2. 全面分析 Spring 的编程式事务管理及声明式事务管理

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  3. 全面分析 Spring 的编程式事务管理及声明式事务管理--转

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  4. spring事务管理——编程式事务、声明式事务

    本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本教程假定您已经掌握了 ...

  5. 事务管理(下) 配置spring事务管理的几种方式(声明式事务)

    配置spring事务管理的几种方式(声明式事务) 概要: Spring对编程式事务的支持与EJB有很大的区别.不像EJB和Java事务API(Java Transaction API, JTA)耦合在 ...

  6. Rabbitmq与spring整合之重要组件介绍——rabbitAdmin组件

    rabbitAdmin组件是一个管理组件,主要是用户通过该组件进行rabbitmq的队列交换器虚拟主机等等进行操作.这里面有些教程说不用声明可以直接绑定,但是本博主运行时,不生命情况下就会报错,可能是 ...

  7. Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务

    首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入ho ...

  8. spring cloud 系列第4篇 —— feign 声明式服务调用 (F版本)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.feign 简介 在上一个用例中,我们使用ribbon+restTem ...

  9. Spring注解驱动开发(四)-----aop、声明式事务

    AOP 概念 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式:-----基于动态代理 一个aop示例 1.导入aop模块:Spring AOP:(spring-aspects ...

随机推荐

  1. redis requires Ruby version >= 2.2.2 系统默认 ruby 版本过低,导致 Redis 接口安装失败

    安装 Redis 接口时异常 ,系统 ruby 版本过低 ! 输入命令 " gem install redis " 出现 " ERROR:  Error installi ...

  2. T-SQL常用的函数

    http://blog.sina.com.cn/s/blog_4af01cd50100hsac.html

  3. SpringBoot与Jpa入门

    一.JPA简介 目前JPA主要实现由hibernate和openJPA等. Spring Data JPA 是Spring Data 的一个子项目,它通过提供基于JPA的Repository极大了减少 ...

  4. 吴裕雄--天生自然PYTHON爬虫:爬取某一大型电商网站的商品数据(效率优化以及代码容错处理)

    这篇博文主要是对我的这篇https://www.cnblogs.com/tszr/p/12198054.html爬虫效率的优化,目的是为了提高爬虫效率. 可以根据出发地同时调用多个CPU,每个CPU运 ...

  5. 【转】CentOS6开启BBR加速

    1.查看机器内核 BBR 算法需要 Linux 4.9 及以上的内核支持,所以想要使用该方式的需要先升级内核版本. 在 Cent OS 7 上的 Linux 内核是 3.10, 使用 uname -r ...

  6. Java基础 -4.3

    While循环结构 while循环 public static void main(String[] args) { while(布尔表达式) { 条件满足时执行; 修改循环条件; } } do wh ...

  7. LeetCode 19. Remove Nth Node From End of List(删除链表中倒数第N个节点)

    题意:删除链表中倒数第N个节点. 法一:递归.每次统计当前链表长度,如果等于N,则return head -> next,即删除倒数第N个节点:否则的话,问题转化为子问题“对head->n ...

  8. [经验] Java 服务端 和 C# 客户端 实现 Socket 通信

    由于项目需要, 我需要通过 Java 开发的服务端对 C# 作为脚本语言开发的 unity 项目实现控制 话不多说, 直接上代码 首先, 我们先来构建服务端的代码, 服务端我们使用 Java 语言 i ...

  9. WebMagic基础与Maven管理依赖

    2. 快速开始 WebMagic主要包含两个jar包:webmagic-core-{version}.jar和webmagic-extension-{version}.jar.在项目中添加这两个包的依 ...

  10. C语言for循环嵌套示例

    打印九九乘法表 #include <stdio.h> int main() {   int n,i,j; for (i=1;i<=9;i++) printf("%-4d&q ...