记住四个注解 

存:

rabbitTemplate.convertAndSend("bw","我要红包");

取:

@Component
@RabbitListener(queues="bw")

@RabbitHandler

1、依赖

    <!--  这是rabbitmy依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

2、配置

#这是rabbitmq的配置
spring:
     rabbitmq:
        host: localhost

3、生产者代码

package com.bw;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqDemoApplicationTests {
    @Resource private RabbitTemplate rabbitTemplate;
    @Test
    public void contextLoads() {

        rabbitTemplate.convertAndSend("bw","我要红包");
    }

}

4、消费者代码

package com.bw;

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

@Component
@RabbitListener(queues="bw")
public class Customer {
    @RabbitHandler
    public void showMessage(String message){
        System.out.println("bw接收到消息:"+message);
    }


}

3、生产者

4、消费者

12-14 00:33
查看更多