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

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;
        }
    });
        }
    }

}


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


 

猜你喜欢

转载自blog.csdn.net/lidew521/article/details/81146582