首页 > 编程语言 >java——spring boot集成RabbitMQ——spring boot实现发布订阅模式——消费者

java——spring boot集成RabbitMQ——spring boot实现发布订阅模式——消费者

时间:2023-02-25 13:55:06浏览次数:36  
标签:java amqp spring boot springframework import org

 

 

 

 

 

 

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springrmq</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/>
    </parent>


    <dependencies>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

    </dependencies>


</project>

 

 

 

 

 

 

 

 

配置文件:

MyRabbitConfig
package org.example.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyRabbitConfig
{

    public final static String FANOUT_NAME ="amqp-fanout";

    @Bean
    FanoutExchange fanoutExchange(){
        return new FanoutExchange(FANOUT_NAME,true,false);
    }

    @Bean
    Queue queueOne(){
        return new Queue("queue-one");
    }

    @Bean
    Queue queueTwo(){
        return new Queue("queue-two");
    }

    @Bean
    Binding bindingOne(){
        return BindingBuilder.bind(queueOne()).to(fanoutExchange());
    }

    @Bean
    Binding bindingTwo(){
        return BindingBuilder.bind(queueTwo()).to(fanoutExchange());
    }

}

 

 

 

 

 

 

 

监听-接收消息:

 

FanoutReceiver
package org.example.receiver;

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

/**
 * 配置消费者
 * 两个消费者分别监听两个不同的队列
 */
@Component
public class FanoutReceiver {

    @RabbitListener(queues = "queue-one")
    public void handler1(String msg)
    {

        System.out.println("第一个队列>>>"+msg);

    }

    @RabbitListener(queues = "queue-two")
    public void handler2(String msg)
    {

        System.out.println("第二个队列>>>"+msg);


    }


}

 

 

 

 

 

 

 

 

 

启动后就可以接收消息了:

 

 

 

 

 

 

 

 

 

 

标签:java,amqp,spring,boot,springframework,import,org
From: https://www.cnblogs.com/xiaobaibailongma/p/17154279.html

相关文章