生产者代码
package com.example.newrabbitmq;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest( classes = NewRabbitMqApplication.class)
@RunWith( SpringRunner.class )
class NewRabbitMqApplicationTests {
@Autowired
private RabbitTemplate rabbitTemplate;
// 广播模式
@Test
void contextLoads3() {
rabbitTemplate.convertAndSend( "springboot_fanout","", "正在发送广播消息");
}
}
消费者代码
package com.example.newrabbitmq.serive;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class Customer3 {
@RabbitListener( bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "springboot_fanout", type = "fanout")
)
})
public void test1(String s) throws InterruptedException {
Thread.sleep(100);
System.out.println( "消费者1" + s);
}
@RabbitListener( bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "springboot_fanout", type = "fanout")
)
})
public void test2(String s){
System.out.println( "消费者2" + s);
}
}
运行截图
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)