8.3 RabbitMQ消息消费

一般消费消息的应用会单独部署,不会和发布消息的应用部署到一起,所以本节也单独讲一下。

1.maven依赖、application.properties配置和上一节一样

2.applicationContext-rabbitmq.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/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/rabbit
   http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

   <description>rabbitmq 连接服务配置</description>
   <context:property-placeholder ignore-unresolvable="true" location="classpath:/application.properties"/>
   <rabbit:connection-factory id="rabbitConnectionFactory" host="${rabbit.host}" port="${rabbit.port}"
                              username="${rabbit.username}" password="${rabbit.password}"
                              virtual-host="${rabbit.vhost}" channel-cache-size="50"/>
   <!-- 消费者 -->
   <bean name="rabbitmqService" class="com.critc.service.RabbitmqService"></bean>
   <rabbit:queue id="test_mq" name="test_mq" durable="true" auto-delete="false" exclusive="false" />
   <!-- 配置监听 -->
   <rabbit:listener-container connection-factory="rabbitConnectionFactory" acknowledge="auto" >
       <rabbit:listener queues="test_mq" ref="rabbitmqService" />
   </rabbit:listener-container>
</beans>

这里面定义了一个消费者和一个监听器来处理消息

3.RabbitmqService 消费类

@Service
public class RabbitmqService implements MessageListener {
    public void onMessage(Message message) {
        try {
            String msg = new String(message.getBody(), "utf-8");
            System.out.println("消息消费者 = " + msg);
        } catch (Exception e) {
        }
    }
}

这里面需要强调一点,传到这里面的Message是一个对象,包含消息头、消息体等各种信息,需要进行一下转码,取到消息body,然后进行处理。

4.web.xml 启动加载配置

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext-rabbitmq.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

启动时加载配置,启动监听。

5.启动执行

rabbitmq监听.png

控制台会输出消息body,后续可以处理这些消息

源码下载

本工程详细源码

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,705评论 25 709
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,974评论 6 342
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,261评论 4 61
  • 凌幽/文 我从不怀念 即使回不到从前 当又想起你的脸 已时过境迁 你的浅...
    洛凌幽阅读 411评论 7 4