SpringBoot-RabbitMq 简单实例

添加maven依赖:

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

创建队列:

package com.example.demo;

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


//创建队列
@Configuration
public class RabbitConfig {
    @Bean
    public Queue queue(){
        return new Queue("hello");
    }
}

消息发送:

package com.example.demo;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RabbitController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @RequestMapping("/rm")
    public void sendMsg(){
        rabbitTemplate.convertAndSend("hello","你好你好hello");
    }
}
消息接收:

package com.example.demo;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.stereotype.Component;
//接收消息
@Component
@org.springframework.amqp.rabbit.annotation.RabbitListener(queues = "hello")
public class RabbitListener {
    @RabbitHandler
    public void getMsg(String msg){
        System.out.println("接收到的消息:"+msg);
    }
}

测试  (http://localhost:8080/rm):

在RabbitMq  Web管理端查看:

尝试一对多:

控制层:

  

package com.example.demo;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RabbitController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @RequestMapping("/rm")
    public void sendMsg(){
        for(int i=0;i<50;i++){
            rabbitTemplate.convertAndSend("queue1",""+i);
        }

    }
}
消息接收:

package com.example.demo;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
//接收消息
@Component

public class RabbitReceive {
    @RabbitListener(queues="queue1")
    public void listener1(String msg){
        System.out.println("receiver1接收到的消息:"+msg);
    }
    @RabbitListener(queues = "queue1")
    public void listener2(String msg){
        System.out.println("receiver2接收到的消息:"+msg);
    }
}

测试:

receiver2接收到的消息:0
receiver1接收到的消息:1
receiver2接收到的消息:2
receiver1接收到的消息:3
receiver2接收到的消息:5
receiver1接收到的消息:4
receiver2接收到的消息:6
receiver1接收到的消息:7
receiver1接收到的消息:8
receiver2接收到的消息:9
receiver2接收到的消息:10
receiver1接收到的消息:11
receiver2接收到的消息:12
receiver1接收到的消息:13
receiver1接收到的消息:15
receiver2接收到的消息:14
receiver1接收到的消息:16
receiver2接收到的消息:17
receiver1接收到的消息:19
receiver2接收到的消息:18
receiver1接收到的消息:20
receiver2接收到的消息:21
receiver1接收到的消息:22
receiver2接收到的消息:23
receiver2接收到的消息:25
receiver1接收到的消息:24
receiver1接收到的消息:27
尝试多对多:

控制层:

package com.example.demo;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RabbitController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @RequestMapping("/rm")
    public void sendMsg(){
        for(int i=0;i<20;i++){
            rabbitTemplate.convertAndSend("queue1","1:"+i);
            rabbitTemplate.convertAndSend("queue1","2:"+i);
        }

    }
}
消息接收:

package com.example.demo;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
//接收消息
@Component

public class RabbitReceive {
    @RabbitListener(queues="queue1")
    public void listener1(String msg){
        System.out.println("receiver1接收到的消息:"+msg);
    }
    @RabbitListener(queues = "queue1")
    public void listener2(String msg){
        System.out.println("receiver2接收到的消息:"+msg);
    }
}
测试:
receiver1接收到的消息:2:0
receiver2接收到的消息:1:0
receiver1接收到的消息:1:1
receiver2接收到的消息:2:1
receiver1接收到的消息:1:2
receiver2接收到的消息:2:2
receiver2接收到的消息:2:3
receiver1接收到的消息:1:3
receiver2接收到的消息:1:4
receiver1接收到的消息:2:4
receiver2接收到的消息:1:5
receiver1接收到的消息:2:5
receiver2接收到的消息:1:6
receiver1接收到的消息:2:6
receiver2接收到的消息:1:7
receiver1接收到的消息:2:7
receiver2接收到的消息:1:8
receiver1接收到的消息:2:8
receiver2接收到的消息:1:9
receiver1接收到的消息:2:9
receiver2接收到的消息:1:10
receiver1接收到的消息:2:10
receiver2接收到的消息:1:11
receiver1接收到的消息:2:11
receiver2接收到的消息:1:12
receiver1接收到的消息:2:12
receiver2接收到的消息:1:13
receiver1接收到的消息:2:13
receiver2接收到的消息:1:14
receiver1接收到的消息:2:14
receiver1接收到的消息:2:15
在RabbitMq  Web管理端查看:







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值