Spring 和 AMQP RabbitMQ 主题交换不工作
Spring and AMQP RabbitMQ topic exchange not working
我正在尝试在我的 spring 应用程序上设置主题交换。
这是我的上下文配置:
@Configuration
public class IntegrationConfig {
public final static String queueName = "my-queue";
@Bean
AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
@Bean
Queue queue() {
return new Queue(queueName);
}
@Bean
TopicExchange exchange() {
return new TopicExchange("my-exchange", false, true);
}
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("ru.interosite.*");
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
container.setMessageListener(listenerAdapter);
return container;
}
@Bean
ImageUploadReceiver receiver() {
return new ImageUploadReceiver();
}
@Bean
MessageListenerAdapter listenerAdapter(ImageUploadReceiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
}
这是接收者class:
public class ImageUploadReceiver {
private CountDownLatch latch = new CountDownLatch(1);
public void receiveMessage(String message) {
System.out.println("Received ");
latch.countDown();
}
public CountDownLatch getLatch() {
return latch;
}
}
这是发件人代码:
@RequestMapping("/sendmessage")
@ResponseBody
public String sendMessage() {
rabbitTemplate.convertAndSend("ru.interosite.1", "ttt1233");
try {
imageUploadReceiver.getLatch().await(3, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Msg received";
}
所以我正在使用绑定键 "ru.interosite.1" 将消息发送到主题交换到使用模式 "ru.interosite.*" 绑定的队列。我在尝试 https://www.rabbitmq.com/tutorials/tutorial-five-java.html 中的示例时使用了这些键和模式,它们工作正常。
但在 String AMQP 内部它不起作用,即接收器永远不会被调用。仅当绑定密钥和模式与我使用的 DirectExchange 完全相同时才会调用它。
我是不是漏掉了什么?
您没有显示 RabbitTemplate
的配置,但我猜它是默认选项。
要向 my-exchange
发送消息,您必须直接指定它:
rabbitTemplate.convertAndSend("my-exchange", "ru.interosite.1", "ttt1233");
你也可以在rabbit模板中这样设置兑换:
@Configuration
public class IntegrationConfig {
// ... as above
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
template.setExchange("my-exchange");
return template;
}
}
因此您可以发送消息:
public class MyController {
@Autowired
RabbitTemplate rabbitTemplate;
@RequestMapping("/sendmessage")
@ResponseBody
public String sendMessage() {
rabbitTemplate.convertAndSend("ru.interosite.1", "ttt1233");
// ... as above
}
}
我正在尝试在我的 spring 应用程序上设置主题交换。 这是我的上下文配置:
@Configuration
public class IntegrationConfig {
public final static String queueName = "my-queue";
@Bean
AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
@Bean
Queue queue() {
return new Queue(queueName);
}
@Bean
TopicExchange exchange() {
return new TopicExchange("my-exchange", false, true);
}
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("ru.interosite.*");
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
container.setMessageListener(listenerAdapter);
return container;
}
@Bean
ImageUploadReceiver receiver() {
return new ImageUploadReceiver();
}
@Bean
MessageListenerAdapter listenerAdapter(ImageUploadReceiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
}
这是接收者class:
public class ImageUploadReceiver {
private CountDownLatch latch = new CountDownLatch(1);
public void receiveMessage(String message) {
System.out.println("Received ");
latch.countDown();
}
public CountDownLatch getLatch() {
return latch;
}
}
这是发件人代码:
@RequestMapping("/sendmessage")
@ResponseBody
public String sendMessage() {
rabbitTemplate.convertAndSend("ru.interosite.1", "ttt1233");
try {
imageUploadReceiver.getLatch().await(3, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Msg received";
}
所以我正在使用绑定键 "ru.interosite.1" 将消息发送到主题交换到使用模式 "ru.interosite.*" 绑定的队列。我在尝试 https://www.rabbitmq.com/tutorials/tutorial-five-java.html 中的示例时使用了这些键和模式,它们工作正常。
但在 String AMQP 内部它不起作用,即接收器永远不会被调用。仅当绑定密钥和模式与我使用的 DirectExchange 完全相同时才会调用它。
我是不是漏掉了什么?
您没有显示 RabbitTemplate
的配置,但我猜它是默认选项。
要向 my-exchange
发送消息,您必须直接指定它:
rabbitTemplate.convertAndSend("my-exchange", "ru.interosite.1", "ttt1233");
你也可以在rabbit模板中这样设置兑换:
@Configuration
public class IntegrationConfig {
// ... as above
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
template.setExchange("my-exchange");
return template;
}
}
因此您可以发送消息:
public class MyController {
@Autowired
RabbitTemplate rabbitTemplate;
@RequestMapping("/sendmessage")
@ResponseBody
public String sendMessage() {
rabbitTemplate.convertAndSend("ru.interosite.1", "ttt1233");
// ... as above
}
}