声明:本站文章均为作者个人原创,图片均为实际截图。如有需要请收藏网站,禁止转载,谢谢配合!!!

准备:

windows安装erlang和rabbitmq

1、引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2、配置 rabbitmq

spring:
  rabbitmq:
    username: guest
    password: guest
    virtual-host: /
    host: 127.0.0.1
    port: 5672
    listener:
      simple:
        acknowledge-mode: manual
        retry:
          max-attempts: 5

3、配置队列信息

package com.timi.t1.config;


import com.rabbitmq.client.Channel;
import com.timi.t1.constant.AllProjectConstant;
import com.timi.t1.exception.GlobalException;
import com.timi.t1.service.TapdService;
import com.timi.t1.utils.LogUtil;
import com.timi.t1.utils.ProjectUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;


@Configuration
public class RabbitmqConfig {

    public static final String Queue_SetTapdCreateFrom = "Queue_SetTapdCreateFrom";

    @Bean
    public Queue setTapdCreateFromQueue(){
        return new Queue(Queue_SetTapdCreateFrom, true);
    }

    @RabbitListener(queues = Queue_SetTapdCreateFrom)
    public void setTapdCreateFromReceive(String msg, Channel channel, Message message) throws IOException {
        System.out.println("Queue_SetTapdCreateFrom Receive : " + msg);
    }
}

4、生产者

@Resource
private RabbitTemplate rabbitTemplate;

rabbitTemplate.convertAndSend(RabbitmqConfig.Queue_SetTapdCreateFrom, "123456");