S01---快速开始体验 rabbitmq . 2019/5/29

1. 在 SpringBoot 项目中添加依赖

<!--添加支持mq的-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2. 安装 一个 rabbitmq 

3   在 SpringBoot 的配置文件里填写 rabbitmq 相关信息

spring.application.name=rabbitmq-demo
spring.rabbitmq.host=192.168.12.102
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin

4  配置 一个消息队列

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

@Configuration
public class SenderConf {

    /**
     *  配置Queue(消息队列).那注意由于采用的是Direct模式,需要在配置Queue的时候,指定一个键,使其和交换机绑定.
     * @return
     */
    @Bean
    public Queue queue() {
        return new Queue("queue");
    }
}

4   再弄一个很简单的消息发送的工具类

@Component
public class MQSendUtil {
   @Autowired
   private AmqpTemplate template;

 

    /**
     * 向 rabbitmq 中发送一个消息队列
     */
   public void sendMsg(){
       template.convertAndSend("queue","hello world");
   }
}

5 再搞一个消息接收的工具类

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

@Component
public class MQReceiveUtil {

    @RabbitListener(queues="queue")    //监听器监听指定的Queue
    public void processMs(String str) {
        System.out.println("Received Msg:"+ str);
    }
}

当你发送消息到 rabbitmq 的时候,就会接收到消息。

附录我的源代码:

https://gitee.com/coder_xiaozhao/rabbitmq-demo

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值