Spring Boot教程(14)-使用异步消息服务-AMQP(RabbitMQ)

Spring Boot教程(1)创建Spring项目

Spring Boot教程(2) – 运行第一个项目

Spring Boot教程(3)-配置文件-多环境配置

Spring Boot教程(4)-日志配置-logback和使用。

Spring Boot教程(5)-web应用开发-模板引擎Thymeleaf

Spring Boot教程(6)-web应用开发-错误处理

Spring Boot教程(7)-文件上传

Spring Boot教程(8)-使用SQL关系型数据库-JdbcTemplate

Spring Boot教程(9)-Spring-data-jpa

Spring Boot教程(10)-使用SQL关系型数据库-h2嵌入式数据库的使用

Spring Boot教程(11)-redis

Spring Boot教程(12)-redis缓存设置,幂等性防重复提交

Spring Boot教程(13)-使用异步消息服务-JMS(ActiveMQ)

Spring Boot教程(14)-使用异步消息服务-AMQP(RabbitMQ)

Spring Boot教程(15)-集成mybatis

Spring Boot教程(16)-集成Druid阿里数据库连接池

Spring Boot教程(17)-Spring Boot集成Swagger

需要安装RabbitMQ服务要不然会报错链接失败

RabbitMQ下载地址:http://www.rabbitmq.com/download.html

 

一、 添加依赖

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

二、配置文件

默认注释,调用本地服务。

# RABBIT (RabbitProperties)
#spring.rabbitmq.host=localhost
#spring.rabbitmq.port=5672
#spring.rabbitmq.password=
#spring.rabbitmq.username=

三、main方法加注解

@EnableRabbit

四、配置类

package com.wsj.springbootdemo.config;

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


/**
 * 项目名称:AmqpConfiguration;
 * 类 名 称:AmqpConfiguration;
 * 类 描 述:TODO ;
 * 创 建 人:Angus;
 * 创建时间:2020/5/7 15:58;
 *
 * @version:1.0
 **/
@Configuration
public class AmqpConfiguration {

    @Bean
    public Queue queue() {
        return new Queue("wsj.queue");
    }

}

 

package com.wsj.springbootdemo.component;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 项目名称:WsjAmqpComponent;
 * 类 名 称:WsjAmqpComponent;
 * 类 描 述:TODO ;
 * 创 建 人:Angus;
 * 创建时间:2020/5/7 15:59;
 *
 * @version:1.0
 **/
@Component
public class WsjAmqpComponent {
    @Autowired
    private AmqpTemplate amqpTemplate;

    public void send(String msg) {
        this.amqpTemplate.convertAndSend("wsj.queue", msg);
    }

    @RabbitListener(queues = "wsj.queue")
    public void receiveQueue(String text) {
        System.out.println("接受到:" + text);
    }

}

五、测试

package com.wsj.springbootdemo.component;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class WsjAmqpComponentTest {

    @Autowired
    private WsjAmqpComponent wsjAmqpComponent;
    @Test
    void send() {
        wsjAmqpComponent.send("hello world");
    }
}

github地址:https://github.com/itwsj/springbootdemo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值