Spring-amqp是对AMQP的一些概念的一些抽象,Spring-rabbit是对RabbitMQ操作的封装实现。
主要有几个核心类RabbitAdmin
、RabbitTemplate
、SimpleMessageListenerContainer
等
RabbitAdmin
类完成对Exchange、Queue、Binding的操作,在容器中管理 了RabbitAdmin
类的时候,可以对Exchange、Queue、Binding进行自动声明。
RabbitTemplate
类是发送和接收消息的工具类。
SimpleMessageListenerContainer
是消费消息的容器。
目前一些比较新的项目会使用基于注解的方式,而比较老的一些项目可能还是基于配制文件的方式。
此处使用的Spring依赖为:
<dependency>
<groupId>org.springframework.amqpgroupId>
<artifactId>spring-rabbitartifactId>
<version>2.2.7.RELEASEversion>
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
<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>
<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>
<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,而不是队列的名称。
修改后:
<rabbit:direct-exchange name="ex.direct" durable="false" auto-delete="false" id="directExchange" >
<rabbit:bindings>
<rabbit:binding queue="msg1" key="routing.q1" >rabbit:binding>
rabbit:bindings>
rabbit:direct-exchange>