springboot集成rabbitmq-发布订阅模式

本篇文章只涉及到代码实现,具体rabbitmq原理可以参考前面文章<<RabbitMQ六种队列模式>>。

一、定义队列名称

package com.shuofeng.config;

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

//发布订阅模式的配置,包括两个队列和对应的订阅者,发布者的交换机类型使用fanout(子网广播),两根网线binding用来绑定队列到交换机
@Configuration
public class PublishSubscribeConfig {
	
	@Bean
	public Queue myQueue1() {
		Queue queue = new Queue("queue1");
		return queue;
	}
	
	@Bean
	public Queue myQueue2() {
		Queue queue = new Queue("queue2");
		return queue;
	}
	
	@Bean
	public FanoutExchange fanoutExchange() {
		FanoutExchange fanoutExchange = new FanoutExchange("fanout");
		return fanoutExchange;
	}
	
	@Bean
	public Binding binding1() {
		Binding binding = BindingBuilder.bind(myQueue1()).to(fanoutExchange());
		return binding;
	}
	
	@Bean
	public Binding binding2() {
		Binding binding = BindingBuilder.bind(myQueue2()).to(fanoutExchange());
		return binding;
	}
}

启动加载并创建队列,交换机,同时把队列绑定到交换机。

二、定义发送对象MAIL

package com.shuofeng.dto;

import java.io.Serializable;

public class Mail implements Serializable {

    private static final long serialVersionUID = -8140693840257585779L;
    private String mailId;
    private String country;
    private Double weight;


    public Mail() {
    }

    public Mail(String mailId, String country, double weight) {
        this.mailId = mailId;
        this.country = country;
        this.weight = weight;
    }

    public String getMailId() {
        return mailId;
    }

    public void setMailId(String mailId) {
        this.mailId = mailId;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Mail [mailId=" + mailId + ", country=" + country + ", weight="
                + weight + "]";
    }

}

三、定义生产者

package com.shuofeng.producer;

import com.shuofeng.dto.Mail;

public interface Publisher {
	public void publishMail(Mail mail);
}
package com.shuofeng.producer.impl;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shuofeng.dto.Mail;
import com.shuofeng.producer.Publisher;

/**
 * 发布订阅模式
 * @author menglingwei
 *
 */
@Service
public class PublisherImpl implements Publisher {
	
	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Override
	public void publishMail(Mail mail) {
		rabbitTemplate.convertAndSend("fanout", "", mail);
	}

}

四、定义消费者

package com.shuofeng.customer;

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

import com.shuofeng.dto.Mail;

@Component
public class QueueListener1 {
	
	@RabbitListener(queues = "queue1")
	public void displayMail(Mail mail) throws Exception {
		System.out.println("队列监听器1号收到消息" + mail.toString());
	}

}

五、测试

package com.shuofeng;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.web.WebAppConfiguration;

import com.shuofeng.dto.Mail;
import com.shuofeng.producer.impl.ProducerImpl;
import com.shuofeng.producer.impl.PublisherImpl;

@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
@WebAppConfiguration
public class ProducerImplTest {
	
	@SpyBean
	private ProducerImpl producerImpl;
	
	@SpyBean
	private PublisherImpl publisherImpl;
	
	@Test
	public void testSendMail() {
		Mail mail = new Mail();
		mail.setMailId("001");
		mail.setCountry("CHINA");
		mail.setWeight(2.2);
		this.producerImpl.sendMail("myqueue", mail);
	}
	
	@Test
	public void testSendMailByQueue1() {
		Mail mail = new Mail();
		mail.setMailId("Queue1");
		mail.setCountry("CHINA");
		mail.setWeight(2.2);
		this.publisherImpl.publishMail(mail);
	}
	
	@Test
	public void testSendMailByQueue2() {
		Mail mail = new Mail();
		mail.setMailId("Queue2");
		mail.setCountry("CHINA");
		mail.setWeight(2.2);
		this.publisherImpl.publishMail(mail);
	}
	
}

运行结果如下图:

相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页