优质广告供应商

广告是为了更好地支持作者创作

2-1 RabbitTemplate

简介

  1. RabbitTemplate是RabbitMQ在与SpringAMQP整合的时,Spring提供的即时消息模板
  2. RabbitTemplate提供了可靠性消息投递方法、回调监听消息接口ConfirmCallback、返回值确认接口ReturnCallback等等

Fanout Exchange 结合 RabbitTemplate使用示例

package com.finlay.scaffold.mq;

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

@Configuration
public class MqQueueConfiguration {

    @Bean
    public Queue fanoutQueue() {
        Queue queue = new Queue("fanout.que", false, false, false);
        return queue;
    }

    @Bean
    public FanoutExchange fanoutExchange() {
        FanoutExchange exchange = new FanoutExchange("fanout.ex", false, false);
        return exchange;
    }


    @Bean
    public Binding fanoutBinding(FanoutExchange fanoutExchange, Queue fanoutQueue) {
        //绑定消费规则
        Binding binding = BindingBuilder.bind(fanoutQueue).to(fanoutExchange);
        return binding;
    }


}

package com.finlay.scaffold.mq;


import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class FanoutSender {

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void send() throws Exception {

        //1、自定义消息属性
        MessageProperties properties = new MessageProperties();
        //设置消息过期时间
        properties.setExpiration("2000");
        //在header 附加消息
        Map<String, Object> headers = properties.getHeaders();
        headers.put("describe", "信息描述");
        headers.put("type", "自定义信息类型");


        // 2、创建消息(消息可以随意,接收类型是Object)
        String msg = "Hello FanoutExchange ...";//body 正文
        Message message = new Message(msg.getBytes(), properties);

        // 3、发送消息,MessagePostProcessor 函数式接口, 至于convertAndSend具体的参数,可以进入到其实现类查看
        amqpTemplate.convertAndSend("fanout.ex", "", msg, msgPost -> (message));
        System.out.println("fanout : 发送消息成功~~~~~~~~~~~~");
    }
}
package com.finlay.scaffold.mq;


import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
public class FanoutReceiver {

    @RabbitListener(queues = "fanout.que")
    @RabbitHandler
    public void rec(String msg, Channel channel, @Headers Map<String, Object> headers) throws IOException {
        Long tag = null;
        try {
           //消费者并行处理限制【在AutoACK的情况下,Qos是不会生效的,一定要设置手动签收】
            channel.basicQos(1);

            //body
            System.out.println("receiver: " + msg);
            //header
            Object headerMsg1 = headers.get("describe");
            Object headerMsg2 = headers.get("type");
            System.out.println("receiver header: " + headerMsg1);
            System.out.println("receiver header: " + headerMsg2);

            tag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
            channel.basicAck(tag, false);
        } catch (Exception e) {
            // TODO 消费失败,那么我们可以进行容错处理,比如转移当前消息进入其它队列
//                channel.basicNack 与 channel.basicReject 的区别在于basicNack可以拒绝多条消息
//                而basicReject一次只能拒绝一条消息

//                tag:消息标识
//                false:是否批量.true:将一次性拒绝所有小于deliveryTag的消息
//                true:重新排队
            channel.basicNack(tag, false, true);//重排并不是放在最后
        }
    }
}
package api.test;

import com.finlay.scaffold.mq.FanoutSender;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class Test1 extends ApplicationTest {

    @Autowired
    private FanoutSender fanoutSender;

    @Test
    public void localStoreByUserId() throws Exception {
        fanoutSender.send();
    }

}

结果

image.png

优质广告供应商

广告是为了更好地支持作者创作