springboot + rabbitmq topic模式(通配符)

springboot + rabbitmq topic模式(通配符)

配置类

@Configuration
public class RabbitMqConfig4 {

	@Bean
	public Queue queueA(){

		return new Queue("queueA");
	}

	@Bean
	public Queue queueB(){

		return new Queue("queueB");
	}

	@Bean
	public Queue queueAll(){

		return new Queue("queueAll");
	}


	@Bean
	public TopicExchange topicExchange(){

		return new TopicExchange("topicExchange");
	}

	@Bean
	public Binding queueABinding(){

		return BindingBuilder.bind(queueA()).to(topicExchange()).with("queue.A");
	}

	@Bean
	public Binding queueBBinding(){

		return BindingBuilder.bind(queueB()).to(topicExchange()).with("queue.B");
	}

	@Bean
	public Binding queueAllBinding(){

		return BindingBuilder.bind(queueAll()).to(topicExchange()).with("queue.#");
	}
}

发送消息 - 生产者

@Service
public class RabbitMqService4 {

	@Autowired
	RabbitTemplate rabbitTemplate;

	public void send(String routingKey){

		rabbitTemplate.convertAndSend("topicExchange",routingKey,"value:" + routingKey);
	}
}

接收消息 - 消费者

@Component
public class RabbitMqComponent4 {

	@RabbitListener(queues = "queueA")
	public void listerQueueA(String message){
		System.out.println("queueA" + message);
	}

	@RabbitListener(queues = "queueB")
	public void listerQueueB(String message){
		System.out.println("queueB" + message);
	}

	@RabbitListener(queues = "queueAll")
	public void listerQueueAll(String message){
		System.out.println("queueAll" + message);
	}

}
发布了43 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43866295/article/details/86705693