0
点赞
收藏
分享

微信扫一扫

Spring整合rabbitmq---消息接收

高子歌 2022-07-12 阅读 53


这两天一直在看rabbimq消息接收的设置。看到网上也有很多的例子,但是发现很多别人可以,到本地自己就不可以,也是比较让人苦恼,在前面的一篇文章中我们介绍了Spring-rabbitmq消息的发送,继续上一节中我们继续看消息的接收。

首先来看rabbitmq-receiver.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"
xmlns:context="http://www.springframework.org/schema/context"
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-1.6.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">

<description>rabbitmq 连接服务配置</description>

<!-- 基于注解的根包扫描路径 -->
<!-- <context:component-scan base-package="com.wdg.amqp" /> -->


<!-- 消息对象json转换类 -->
<bean id="jsonMessageConverter"
class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"></bean>

<!-- 连接配置 -->
<!-- 定义RabbitMQ的连接工厂 -->
<rabbit:connection-factory id="connectionFactory"
host="127.0.0.1" port="5672" username="guest" password="guest"
virtual-host="/" />

<rabbit:admin connection-factory="connectionFactory" />

<!-- spring template声明 -->
<rabbit:template id="amqpTemplate" exchange="wdgexchange"
connection-factory="connectionFactory" message-converter="jsonMessageConverter" />

<!-- 声明一个Que -->
<!-- durable:是否持久化 ; exclusive: 仅创建者可以使用的私有队列,断开后自动删除; auto_delete: 当所有消费客户端连接断开后,是否自动删除队列 -->
<rabbit:queue id="wdgqueue" name="wdgqueue" durable="true"
auto-delete="false" exclusive="false" />

<!-- 声明一个Exchange -->
<rabbit:direct-exchange name="wdgexchange"
durable="true" auto-delete="false" id="wdgexchange">
<rabbit:bindings>
<rabbit:binding queue="wdgqueue" key="*" />
</rabbit:bindings>
</rabbit:direct-exchange>

<rabbit:annotation-driven />

<!-- 消息接受者 -->
<bean id="receiverListen" class="com.wdg.amqp.AmqpReceiver"></bean>
<!-- 用于消息的监听的代理类MessageListenerAdapter -->
<bean id="receiveListenerAdapter"
class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
<constructor-arg ref="receiverListen" />
<property name="defaultListenerMethod" value="handleMessage"></property>
<property name="messageConverter" ref="jsonMessageConverter"></property>
</bean>


<bean id="listenerContainer"
class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
<property name="queueNames" value="wdgqueue"></property>
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="messageListener" ref="receiveListenerAdapter"></property>
</bean>




</beans>

这个是我们的xml的配置,我们来看看消息接收器的java代码:

package com.wdg.amqp;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Component
@Service
public class AmqpReceiver{


public void handleMessage(String message) {
System.out.println(message.toString());

}


}

消费者启动:

package com.wdg.amqp;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RabbitMqClient {

public static void main(String[] args) {
new ClassPathXmlApplicationContext("rabbitmq-receiver.xml");

}
}

上面就是整个接收器的过程,下面我们在rabbimq中推送一个消息:

Spring整合rabbitmq---消息接收_spring

 

我们看控制台:

Spring整合rabbitmq---消息接收_json_02

上面就是rabbitmq消息的接收了,希望对你有所帮助,接下来我们会继续讲在配置中的一些细节

                                                                              

                             

举报

相关推荐

0 条评论