Spring-amqp是对AMQP的一些概念的一些抽象,Spring-rabbit是对RabbitMQ操作的封装实现。
主要有几个核心类RabbitAdmin
、RabbitTemplate
、SimpleMessageListenerContainer
等
RabbitAdmin
类完成对Exchange、Queue、Binding的操作,在容器中管理 了RabbitAdmin
类的时候,可以对Exchange、Queue、Binding进行自动声明。
RabbitTemplate
类是发送和接收消息的工具类。
SimpleMessageListenerContainer
是消费消息的容器。
目前一些比较新的项目会使用基于注解的方式,而比较老的一些项目可能还是基于配制文件的方式。
此处使用的Spring依赖为:
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
消息的生产者
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Product {
public static void main(String[] args) throws Exception {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-rabbit.xml");
RabbitTemplate template = context.getBean(RabbitTemplate.class);
MessagePropertiesBuilder propertiesBuilder = MessagePropertiesBuilder.newInstance();
propertiesBuilder.setContentEncoding("gbk");
propertiesBuilder.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
Message msg = MessageBuilder.withBody("hello world".getBytes("gbk"))
.andProperties(propertiesBuilder.build()).build();
template.convertAndSend("ex.direct", "routing.q1", msg);
context.close();
}
}
spring-rabbit.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<!--配制连接工厂-->
<rabbit:connection-factory id="connectFactory"
host="node1" virtual-host="/"
username="root" password="123456"
port="5672"
></rabbit:connection-factory>
<!--用于自动向RabbitMQ声明队列、交换器、绑定 等操作工具类-->
<rabbit:admin id="rabbitAdmin" connection-factory="connectFactory"></rabbit:admin>
<!--用于简化操作的模板类-->
<rabbit:template connection-factory="connectFactory" id="rabbitTemplate" />
<!--声明队列队列-->
<rabbit:queue id="msg1" name="queue.msg" durable="false" exclusive="false" auto-delete="false" ></rabbit:queue>
<!--声明交换器-->
<rabbit:direct-exchange name="ex.direct" durable="false" auto-delete="false" id="directExchange" >
<rabbit:bindings>
<!--key表示绑定键-->
<!--queue表示将交换器绑定到哪个消息队列,使用队列换id,不要使用Bean的name-->
<!--exchange表示交接交换器绑定到哪个交换器。-->
<rabbit:binding queue="msg1" key="routing.q1" ></rabbit:binding>
</rabbit:bindings>
</rabbit:direct-exchange>
</beans>
运行生产者的代码,便可查看数据已经发送成功
[root@nullnull-os ~]# rabbitmqctl list_exchanges --formatter pretty_table
Listing exchanges for vhost / ...
┌────────────────────┬─────────┐
│ name │ type │
├────────────────────┼─────────┤
│ amq.fanout │ fanout │
├────────────────────┼─────────┤
│ ex.busi.topic │ topic │
├────────────────────┼─────────┤
│ amq.rabbitmq.trace │ topic │
├────────────────────┼─────────┤
│ amq.headers │ headers │
├────────────────────┼─────────┤
│ amq.topic │ topic │
├────────────────────┼─────────┤
│ amq.direct │ direct │
├────────────────────┼─────────┤
│ ex.direct │ direct │
├────────────────────┼─────────┤
│ │ direct │
├────────────────────┼─────────┤
│ ex.routing │ direct │
├────────────────────┼─────────┤
│ amq.match │ headers │
└────────────────────┴─────────┘
[root@nullnull-os ~]# rabbitmqctl list_bindings --formatter pretty_table
Listing bindings for vhost /...
┌─────────────┬─────────────┬──────────────────┬──────────────────┬─────────────┬───────────┐
│ source_name │ source_kind │ destination_name │ destination_kind │ routing_key │ arguments │
├─────────────┼─────────────┼──────────────────┼──────────────────┼─────────────┼───────────┤
│ │ exchange │ queue.msg │ queue │ queue.msg │ │
├─────────────┼─────────────┼──────────────────┼──────────────────┼─────────────┼───────────┤
│ ex.direct │ exchange │ queue.msg │ queue │ routing.q1 │ │
└─────────────┴─────────────┴──────────────────┴──────────────────┴─────────────┴───────────┘
[root@nullnull-os ~]# rabbitmqctl list_queues --formatter pretty_table
Timeout: 60.0 seconds ...
Listing queues for vhost / ...
┌───────────┬──────────┐
│ name │ messages │
├───────────┼──────────┤
│ queue.msg │ 1 │
└───────────┴──────────┘
[root@nullnull-os ~]#
可以观察到数据已经成功的发送了。
遇到的问题:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.amqp.rabbit.config.BindingFactoryBean#0': Cannot resolve reference to bean 'queue.msg' while setting bean property 'destinationQueue'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'queue.msg' available
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:342)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1699)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1444)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:876)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at com.nullnull.learn.Product.main(Product.java:18)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'queue.msg' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:814)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1282)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330)
... 15 more
问题原因:
<rabbit:direct-exchange name="ex.direct" durable="false" auto-delete="false" id="directExchange" >
<rabbit:bindings>
<rabbit:binding queue="queue.msg" key="routing.q1" ></rabbit:binding>
</rabbit:bindings>
</rabbit:direct-exchange>
此处要配制的是队列的id,而不是队列的名称。文章来源:https://www.toymoban.com/news/detail-697195.html
修改后:文章来源地址https://www.toymoban.com/news/detail-697195.html
<!--声明交换器-->
<rabbit:direct-exchange name="ex.direct" durable="false" auto-delete="false" id="directExchange" >
<rabbit:bindings>
<!--key表示绑定键-->
<!--queue表示将交换器绑定到哪个消息队列,使用队列换id,不要使用Bean的name-->
<!--exchange表示交接交换器绑定到哪个交换器。-->
<rabbit:binding queue="msg1" key="routing.q1" ></rabbit:binding>
</rabbit:bindings>
</rabbit:direct-exchange>
到了这里,关于Spring整合RabbitMQ-配制文件方式-1-消息生产者的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!