Spring Web integration rabbitmq code instance

Time:2020-4-6

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

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

  <! -- configure connection factory, specify the connection rabbit server parameter -- >
  <rabbit:connection-factory
                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:bindings>
      <rabbit:binding queue="queueTest" key="queueTestKey"></rabbit:binding>
    </rabbit:bindings>
  </rabbit:direct-exchange>

  <! -- message receiver -- >
  <bean></bean>

  <! -- 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"/>
  </rabbit:listener-container>

</beans>

This XML needs to be introduced into spring, mainly in total spring.xml. Or 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 ("consumption information," + 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);
  }

}

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 […]