月光代碼園

記錄、分享、交流

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

RabbitMQ #1 demo

spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

Config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.example.demo;
 
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.context.annotation.Configuration;
 
import java.util.HashMap;
import java.util.Map;
 
@Configuration
public class RabbitMQCongig {
 
 
    @Bean
    public Queue testQueue() {
        return new Queue("testQueue");
    }
 
    @Bean
    public DirectExchange testExchange(){
        return new DirectExchange("testExchange");
    }
 
    @Bean
    public Binding bindTestQueue() {
        return BindingBuilder.bind(testQueue()).to(testExchange()).with("testRoutingKey");
    }
}

  

Sender

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.example.demo;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Component
public class Sender {
 
    @Autowired
    public RabbitTemplate rabbitTemplate;
 
    Logger logger = LoggerFactory.getLogger(this.getClass());
 
    public void send(String msg) {
        logger.info("=========================== 发送消息开始" + " ===========================");
        logger.info("发送消息:【 " + msg + " 】");
        rabbitTemplate.convertAndSend("testExchange","testRoutingKey", msg);
        logger.info("=========================== 发送消息结束" + " ===========================");
    }
}

  

消费者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.example.demo;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
 
@Component
@RabbitListener(queues = "testQueue")
public class Receiver {
 
    Logger logger = LoggerFactory.getLogger(this.getClass());
 
    @RabbitHandler
    public void process(String msg){
        logger.info("=========================== 接收消息开始" + " ===========================");
        logger.info("接收消息:【 " + msg + " 】");
        logger.info("=========================== 接收消息结束" + " ===========================");
    }
}

  

posted on   bangdikka  阅读(17)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示