springboot整合rabbitmq优先队列

优先队列,是消息阻塞中形成的,所以要先生产消息,后开启消费者。否则生产一条消息消费一条消息,不存在阻塞,无法排序

配置类

package com.cherry.rabbitmqttl.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@Component
public class PriorityConfig {

    public static final String EXCHANGE = "priority-exchange";

    public static final String QUEUE = "priority-queue";

    public static final String ROUTING_KEY = "priority_key";

    /**
     * 定义优先级队列
     */
    @Bean
    Queue queue() {
        Map<String, Object> args= new HashMap<>();
        args.put("x-max-priority", 10); //设置优先级,范围0-255,此处设为10,则允许优先级的范围为0-10
        return new Queue(QUEUE, false, false, false, args);
    }

    /**
     * 定义交换器
     */
    @Bean
    DirectExchange exchange() {
        return new DirectExchange(EXCHANGE);
    }


    @Bean
    Binding binding(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }

}

消费者

package com.cherry.rabbitmqttl.consumer;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 优先级应该开启生产者先生产消息,再开启消费者
 */
@Component
@RabbitListener(queues ="priority-queue")
public class PriorityConsumer {
    @RabbitHandler
    public void hand(String str){
        System.out.println("接受到了一个消息:"+str);
    }
}


生产者

package com.cherry.rabbitmqttl.producer;

import com.cherry.rabbitmqttl.config.PriorityConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PriorityProducer {

    @Autowired
    RabbitTemplate template;


    public void test(){
        for(int i=1;i<10;i++){
            int finalI = i;
            template.convertAndSend(PriorityConfig.EXCHANGE,PriorityConfig.ROUTING_KEY,"queue:"+i, message -> {
                message.getMessageProperties().setPriority(finalI);
                return message;
            });
        }

    }
}

发消息

package com.cherry.rabbitmqttl.controller;

import com.cherry.rabbitmqttl.producer.PriorityProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 优先级应该开启生产者先生产消息,再开启消费者
 */

@Slf4j
@RequestMapping("priority")
@RestController
public class PriorityController {
    @Autowired
    private PriorityProducer priorityProducer;

    @GetMapping("/sendMsg/{message}")
    public void sendMsg(@PathVariable String message){
        priorityProducer.test();
    }
}

yml配置

spring:
  rabbitmq:
    host: 192.168.64.137
    port: 5672
    username: gg
    password: 123
    virtual-host: /

pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>
    <groupId>com.cherry</groupId>
    <artifactId>rabbitmqttl</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>rabbitmqttl</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--RabbitMQ 依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!--RabbitMQ 测试依赖-->
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

你可能感兴趣的:(rabbitmq,java-rabbitmq,rabbitmq,spring,boot)