JAVA技术交流QQ群:170933152

首先新建一个SpringBoot的工程,空的就可以:
可以用idea
也可以用eclipse,也可以用sts,这类工具都可以
E:\StsWorkSpace\spring-boot-rabbitmq-test

然后看配置:
首先在application.properties中写入rabbitmq的配置
E:\StsWorkSpace\spring-boot-rabbitmq-test
\src\main\resources\application.properties

#spring.application.name=spring-boot-rabbitmq

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

这里注意:端口号是5672,本地在浏览器访问rabbitmq服务器的时候是:
http://localhost:15672/#/queues/%2F/direct 这个地址,但是配置的时候,用5672,用15672是会连接不上的

然后再写个配置类:
E:\StsWorkSpace\spring-boot-rabbitmq-test\src\main\java\io
\credream\rabbitmq\config\RabbitDirectConfig.java
package io.credream.rabbitmq.config;
import java.util.HashMap;
import java.util.Map;

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

/**
 * 描述: 配置默认的交换机模式
 *
 * Direct Exchange是RabbitMQ默认的交换机模式,也是最简单的模式,根据key全文匹配去寻找队列。
 *
 * @author lidewei
 * @create 2017-10-25 0:09
 **/
@Configuration
public class RabbitDirectConfig {
//注意这里是给交换机配置了10个优先级,数字越大越优先,优先级最大可以设置255,官方建议设置0到10
//https://www.rabbitmq.com/priority.html这里有关于优先级的设置,说明
    private static final int  MAX_PRIORITY=10;
//2.这里新建一个queue,名字可以自己起名,注意这里的hello就是routekey,可以通过
//它来找到这个队列
    @Bean
    public Queue helloQueue() {
        return new Queue("hello");
    }
//3.第二个queue
    @Bean
    public Queue directQueue() {
        return new Queue("direct");
    }

//4.这里配置交换机,模式,默认用的directExchange,还有其他模式,复杂一些可以查阅
    //-------------------配置默认的交换机模式,可以不需要配置以下-----------------------------------
    @Bean
    DirectExchange directExchange() {
         Map<String,Object> args = new HashMap<String, Object>();  
         args.put("x-max-priority",MAX_PRIORITY); //队列的属性参数 有10个优先级别
             //5.这里注意,就是通过这个方法来给交换机绑定优先级的args,这个是参数列表,里面有优先级
    //可以看到他说 core as of version 3.5.0. ,3.5.0之后的版本才支持,优先级设定
         return new DirectExchange("directExchange",true,false,args);
    }
//
  //6.这里给交换机绑定一个队列的key "direct",当消息匹配到就会放到这个队列中
    @Bean
    Binding bindingExchangeDirectQueue(Queue directQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue).to(directExchange).with("direct");
    }
    //7.由于这里咱们建立了两个队列,所以。都需要加入到交换机中,这里做了两次绑定
    @Bean
    Binding bindingExchangeHelloQueue(Queue helloQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(helloQueue).to(directExchange).with("hello");
    }
  // 推荐使用 helloQueue() 方法写法,这种方式在 Direct Exchange 模式 多此一举,没必要这样写
    //---------------------------------------------------------------------------------------------
}

到这里:
可以看到使用的流程是
a 先配置rabbitmq 配置文件
b 写配置类,生成队列queue,然后,写交换机,然后把queue,放到交换机中去

好,接下来,写接收者:
E:\StsWorkSpace\spring-boot-rabbitmq-test\src\main\java\io\
credream\rabbitmq\direct\DirectReceiver.java
package io.credream.rabbitmq.direct;

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

/**
 * 描述: 接收者
 * @author: lidewei
 * @create: 2017/10/25 0:49
 */
@Component //2.注册到spring中
@RabbitListener(queues = "direct") //要监听哪个队列,当然这里可以传入一个string数组
//可以去看源码:String[] queues() default {};

public class DirectReceiver {

@RabbitHandler
    public void process(String message) {
        System.out.println("接收者 DirectReceiver," + message);
        //1.这样写了以后,当对应的队列中有消息的时候,就会自动捕捉,接受
try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

然后:
SpringBoot启动类
E:\StsWorkSpace\spring-boot-rabbitmq-test\src\main\java\io\credream\rabbitmq\run\Startup.java
package io.credream.rabbitmq.run;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * 描述: 启动服务
 *
 * @author: lidewei
 * @create: 2017/10/23 14:14
 */
@SpringBootApplication
@ComponentScan(value = {"io.credream.rabbitmq"})
public class Startup {

public static void main(String[] args) {
        SpringApplication.run(Startup.class, args);
    }
}

然后再去编写测试类:
E:\StsWorkSpace\spring-boot-rabbitmq-test\src\test\java\io\
credream\rabbitmq\test\RabbitDirectTest.java
package io.credream.rabbitmq.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import io.credream.rabbitmq.run.Startup;

/**
 * 描述: 默认的交换机模式
 *
 * @author: yanpenglei
 * @create: 2017/10/25 1:03
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Startup.class)
public class RabbitDirectTest {

@Autowired
    private AmqpTemplate rabbitTemplate;

@Test
    public void sendHelloTest() {

String context = "此消息在,默认的交换机模式队列下,有 helloReceiver 可以收到";

String routeKey = "hello";
        String exchange = "directExchange";
        context = "routeKey:" + routeKey + ",context:" + context;

System.out.println("sendHelloTest : " + context);
for(int i=0;i<100;i++) {
//      try {
//            Thread.sleep(10);
//        } catch (InterruptedException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
      this.rabbitTemplate.convertAndSend(exchange, routeKey, context, new MessagePostProcessor() {
            
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setPriority(10);
                return message;
            }
        });
}

}

@Test
    public void sendDirectTest() {

String context = "此消息在,默认的交换机模式队列下,有 DirectReceiver 可以收到";

String routeKey = "direct";

String exchange = "directExchange";

context = "context:" + exchange + ",routeKey:" + routeKey + ",context:" + context;

System.out.println("sendDirectTest : " + context);

// 推荐使用 sendHello() 方法写法,这种方式在 Direct Exchange 多此一举,没必要这样写
        //this.rabbitTemplate.convertAndSend(exchange, routeKey, context);
       //2.通过这种方式设置rabbitmq消息优先级
        for(int i=0;i<100;i++) {
//              try {
//                  Thread.sleep(10);
//              } catch (InterruptedException e) {
//                  // TODO Auto-generated catch block
//                  e.printStackTrace();
//              }
        this.rabbitTemplate.convertAndSend(exchange, routeKey, context, new MessagePostProcessor() {
        @Override
        public Message postProcessMessage(Message message) throws AmqpException {
            message.getMessageProperties().setPriority(1);
            return message;
        }
    });
        }
    }

}

这里写了两个测试类:当然,使用一个就可以
---------------------

RabbitMq学习笔记002---RabbitMq在SpringBoot中的应用_配置_使用_并且设置优先级相关推荐

  1. RabbitMQ 学习笔记

    RabbitMQ 学习笔记 RabbitMQ 学习笔记 1. 中间件 1.1 什么是中间件 1.2 为什么要使用消息中间件 1.3 中间件特点 1.4 在项目中什么时候使用中间件技术 2. 中间件技术 ...

  2. Rabbitmq学习笔记(尚硅谷2021)

    Rabbitmq学习笔记 (尚硅谷) 1.MQ 的概念 1.1 什么是 MQ? 1.2 为什么要用 MQ? 削峰 解耦 异步 1.3 MQ 的分类 ActiveMQ Kafka RocketMQ Ra ...

  3. Rabbitmq学习笔记教程-尚硅谷

    Rabbitmq学习笔记 (尚硅谷) 尚硅谷 rabbitmq 教程 1.MQ 的概念 1.1 什么是 MQ? 存放消息的队列,互联网架构中常见的一种服务与服务之间通信的方式. 1.2 为什么要用 M ...

  4. RabbitMQ学习笔记(3)----RabbitMQ Worker的使用

    1. Woker队列结构图 这里表示一个生产者生产了消息发送到队列中,但是确有两个消费者在消费同一个队列中的消息. 2. 创建一个生产者 Producer如下: package com.wangx.r ...

  5. RabbitMQ学习笔记(高级篇)

    RabbitMQ学习笔记(高级篇) 文章目录 RabbitMQ学习笔记(高级篇) RabbitMQ的高级特性 消息的可靠投递 生产者确认 -- confirm确认模式 生产者确认 -- return确 ...

  6. RabbitMQ学习笔记四:RabbitMQ命令(附疑难问题解决)

    RabbitMQ学习笔记四:RabbitMQ命令(附疑难问题解决) 参考文章: (1)RabbitMQ学习笔记四:RabbitMQ命令(附疑难问题解决) (2)https://www.cnblogs. ...

  7. Redis学习笔记(二)SpringBoot整合

    Redis学习笔记(二) SpringBoot整合 测试 导入依赖 查看底层 配置连接 测试连接 自定义`RedisTemplate` 在开发中,一般都是以json来传输对象: 所以实际开发中所有对象 ...

  8. springboot学习笔记:12.解决springboot打成可执行jar在linux上启动慢的问题

    springboot学习笔记:12.解决springboot打成可执行jar在linux上启动慢的问题 参考文章: (1)springboot学习笔记:12.解决springboot打成可执行jar在 ...

  9. JavaScript学习笔记06【高级——JavaScript中的事件】

    w3school 在线教程:https://www.w3school.com.cn JavaScript学习笔记01[基础--简介.基础语法.运算符.特殊语法.流程控制语句][day01] JavaS ...

  10. Sharepoint学习笔记---如何在Sharepoint2010网站中整合Crystal Report水晶报表(显示数据 二)...

    在Sharepoint学习笔记---如何在Sharepoint2010网站中整合Crystal Report水晶报表(显示数据一)中,解释了如何把Crystal Report整合到Sharepoint ...

最新文章

  1. PostgreSQL学习手册(五) 函数和操作符
  2. python excel 操作
  3. centos 安装 redmine 2.6.0.stable
  4. JavaScript高程第十章:DOM(上)
  5. r语言做断轴_R语言用nls做非线性回归以及函数模型的参数估计
  6. STM32F7xx —— FatFS(W25QXX)
  7. mySQL字符串字段区别_MySQL类型之(字符串列类型区分、数据类型区分)
  8. MySQL笔记(六)视图 view
  9. android UI 标签
  10. 「镁客·请讲」归墟电子王景阳:以桌面小型机器人切入市场,沿着“机器人+教育”的方向前进...
  11. 【xubuntu】 在xubuntu系统上开启自动登陆,并自动启动一个应用程序。
  12. 人机交互系统(4.1)——深度学习在人脸检测中的应用
  13. 云主机创建网络失败:Unable to create the network. No tenant network is available for allocation.
  14. 无线通信-信道模型概念(Wireless Communication Overview)
  15. 迷你播放器--第一阶段(3)--MediaPlayer的封装
  16. bzoj4768: wxh loves substring //后缀平衡树
  17. 第三章 part3 几个小知识点
  18. 阿里程序员写了一个新手都写不出的低级bug,被骂惨了。
  19. 根据经纬度,距离范围,取随机经纬度
  20. 【金融】银行间质押式回购

热门文章

  1. NYOJ-布线问题(最短路)
  2. C++:如何更改visual studio 2017的主题颜色?
  3. Qt图形测绘窗口部件介绍
  4. 毕设日志——pytorch版本faster rcnn运行代码前的环境配置2019.4.9
  5. SUSE Enterprise Server 12 SP3 64 设置防火墙开放8080端口
  6. [转] js中的钩子机制(hook)
  7. BZOJ1101 洛谷3455:[POI2007]ZAP——题解
  8. Android 三星手机不能调起应用市场
  9. windows下安装whmcs会经常遇到两个问题
  10. 深入浅出三剑客之awk必杀技一例