Spring Web integration rabbitmq code instance


This article mainly introduces the spring Web integration rabbitmq code example. The example code is introduced in detail in this article, which has a certain reference learning value for everyone’s study or work. You can refer to the following for your friends

Introduce java package:

In this project, only four Java packages are introduced: 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


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

  <! -- configure connection factory, specify the connection rabbit server parameter -- >
                username="guest" password="guest" host="localhost" port="5672" />

  <! -- define rabbit template for data sending -- >
  <rabbit:template connection-factory="connectionFactory"
           exchange="exchangeTest" />

  <! -- by specifying the following admin information, the exchange and queue in the current producer will be automatically generated on the rabbitmq server -- >
  <rabbit:admin connection-factory="connectionFactory" />

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

  <! -- define direct exchange, bind queuetest -- >
  <rabbit:direct-exchange name="exchangeTest" durable="true" auto-delete="false">
      <rabbit:binding queue="queueTest" key="queueTestKey"></rabbit:binding>

  <! -- message receiver -- >

  <! -- queue listener observation listening mode when a message arrives, it will notify the listener on the corresponding queue -- >
  <rabbit:listener-container connection-factory="connectionFactory">
    <rabbit:listener queues="queueTest" ref="messageReceiver"/>


This XML needs to be introduced into spring, mainly in total spring.xml. Or web.xml.


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 {

  public void onMessage(Message message) {
    System. Out. Println ("consumption information," + dateutil. Getnow ("yyyy MM DD HH: mm: SS") + "---- >" + message);



package club.codeapes.web.core.rabbitmq;

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

public class RabbitMqMessageProducer{

  private AmqpTemplate amqpTemplate;

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


The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.

Recommended Today

JS function

1. Ordinary function Grammar: Function function name (){ Statement block } 2. Functions with parameters Grammar: Function function name (parameter list){ Statement block } 3. Function with return value Grammar: Function function name (parameter list){ Statement block; Return value; } Allow a variable to accept the return value after calling the function Var variable name […]