RabbitMQ的基本使用

pom.xml

            org.springframework.boot

            spring-boot-starter-amqp

application.yml

spring:

  rabbitmq:

    host: localhost

    port: 5672

    password: guest

    username: guest


接收MQ消息

@Slf4j

@Component

public class MqReceiver {

    //方式一 @RabbitListener(queues = "myQueue")

    //方式二 自动创建队列 @RabbitListener(queuesToDeclare = @Queue("myQueue"))

    //方式三 自动创建只接收key消息的exchange和Queue绑定

    @RabbitListener(bindings= @QueueBinding( key="computer", value=@Queue("computerOrder"), exchange=@Exchange("myOrder") ))

    public void process(String message){

        log.info("computer MqReceiver:{}",message);

    }

}


发送MQ消息

@Slf4j

@Component

public class MqSender {

    @Autowired

    private AmqpTemplate amqpTemplate;

    public void send(){

        amqpTemplate.convertAndSend("myOrder", "computer", "要发送的内容");

    }

}

你可能感兴趣的:(RabbitMQ的基本使用)