Spring web集成rabbitmq代码实例

所属分类: 软件编程 / java 阅读数: 61
收藏 0 赞 0 分享

这篇文章主要介绍了Spring web集成rabbitmq代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

引入java包:

本项目中仅引入了四个java包:amqp-client-5.7.3.jar,spring-rabbit-2.2.2.RELEASE.jar,spring-retry-1.2.4.RELEASE.jar,spring-amqp-2.2.2.RELEASE.jar

spring-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"
    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">

  <!--配置connection-factory,指定连接rabbit server参数 -->
  <rabbit:connection-factory id="connectionFactory"
                username="guest" password="guest" host="localhost" port="5672" />

  <!--定义rabbit template用于数据的发送 -->
  <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
           exchange="exchangeTest" />

  <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->
  <rabbit:admin connection-factory="connectionFactory" />

  <!--定义queue -->
  <rabbit:queue name="queueTest" durable="true" auto-delete="false" exclusive="false" />

  <!-- 定义direct exchange,绑定queueTest -->
  <rabbit:direct-exchange name="exchangeTest" durable="true" auto-delete="false">
    <rabbit:bindings>
      <rabbit:binding queue="queueTest" key="queueTestKey"></rabbit:binding>
    </rabbit:bindings>
  </rabbit:direct-exchange>

  <!-- 消息接收者 -->
  <bean id="messageReceiver" class="club.codeapes.web.core.rabbitmq.RabbitMqMessageConsumer"></bean>

  <!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象-->
  <rabbit:listener-container connection-factory="connectionFactory">
    <rabbit:listener queues="queueTest" ref="messageReceiver"/>
  </rabbit:listener-container>

</beans>

spring中需要引入这个xml, 主要在总spring.xml。或者web.xml中需要引入下。

RabbitMqMessageConsumer

package club.codeapes.web.core.rabbitmq;

import club.codeapes.common.date.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

public class RabbitMqMessageConsumer implements MessageListener {

  @Override
  public void onMessage(Message message) {
    System.out.println("消费信息," + DateUtil.getNow("yyyy-MM-dd HH:mm:ss") + "---->" + message);
  }

}

RabbitMqMessageProducer:

package club.codeapes.web.core.rabbitmq;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


@Repository
public class RabbitMqMessageProducer{

  @Autowired
  private AmqpTemplate amqpTemplate;

  public void sendMessage(Object message) {
    System.out.println("to send message:" + message);
    amqpTemplate.convertAndSend("queueTestKey", message);
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多