我正在尝试更改以支持优先级队列的以下配置。 基于完成的研究,它说我应该有两个队列,每个优先级。 我从以下内容调整了配置:
@Configuration
public class FixedReplyQueueConfig {
@Bean
public ConnectionFactory rabbitConnectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost("localhost");
connectionFactory.setUsername("urbanbuz");
connectionFactory.setPassword("ub");
connectionFactory.setVirtualHost("urbanbuzvhost");
return connectionFactory;
}
/**
* @return Rabbit template with fixed reply queue.
*/
@Bean
public RabbitTemplate fixedReplyQRabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(rabbitConnectionFactory());
template.setExchange(ex().getName());
template.setRoutingKey("test");
template.setReplyQueue(replyQueue());
return template;
}
/**
* @return The reply listener container - the rabbit template is the listener.
*/
@Bean
public SimpleMessageListenerContainer replyListenerContainer() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(rabbitConnectionFactory());
container.setQueues(replyQueue());
container.setMessageListener(fixedReplyQRabbitTemplate());
return container;
}
/**
* @return The listener container that handles the request and returns the reply.
*/
@Bean
public SimpleMessageListenerContainer serviceListenerContainer() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(rabbitConnectionFactory());
container.setQueues(requestQueue());
container.setMessageListener(new MessageListenerAdapter(new PojoListener()));
return container;
}
@Bean
public DirectExchange ex() {
return new DirectExchange("ub.exchange", false, true);
}
@Bean
public Binding binding() {
return BindingBuilder.bind(requestQueue()).to(ex()).with("test");
}
@Bean
public Queue requestQueue() {
return new Queue("ub.request");
}
@Bean
public Queue replyQueue() {
return new Queue("ub.reply");
}
/**
* @return an admin to handle the declarations.
*/
@Bean
public RabbitAdmin admin() {
return new RabbitAdmin(rabbitConnectionFactory());
}
}
以下内容:
@Configuration
public class FixedReplyQueueConfig {
@Bean
public ConnectionFactory rabbitConnectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost("localhost");
connectionFactory.setUsername("urbanbuz");
connectionFactory.setPassword("ub");
connectionFactory.setVirtualHost("urbanbuzvhost");
return connectionFactory;
}
/**
* @return Rabbit template with fixed reply queue.
*/
@Bean
public RabbitTemplate fixedReplyQRabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(rabbitConnectionFactory());
template.setExchange(ex().getName());
template.setRoutingKey("high");
template.setRoutingKey("normal");
template.setReplyQueue(replyQueue());
return template;
}
/**
* @return The reply listener container - the rabbit template is the listener.
*/
@Bean
public SimpleMessageListenerContainer replyListenerContainer() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(rabbitConnectionFactory());
container.setQueues(replyQueue());
container.setMessageListener(fixedReplyQRabbitTemplate());
return container;
}
/**
* @return The listener container that handles the request and returns the reply.
*/
@Bean
public SimpleMessageListenerContainer serviceListenerContainer() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(rabbitConnectionFactory());
container.setQueues(requestQueueHigh(), requestQueue());
container.setMessageListener(new MessageListenerAdapter(new PojoListener()));
return container;
}
@Bean
public DirectExchange ex() {
return new DirectExchange("ub.exchange", false, true);
}
@Bean
public Binding binding() {
return BindingBuilder.bind(requestQueue()).to(ex()).with("normal");
}
@Bean
public Binding bindingHigh() {
return BindingBuilder.bind(requestQueueHigh()).to(ex()).with("high");
}
@Bean
public Queue requestQueue() {
return new Queue("ub.request");
}
@Bean
public Queue requestQueueHigh() {
return new Queue("ub.request.high");
}
@Bean
public Queue replyQueue() {
return new Queue("ub.reply");
}
/**
* @return an admin to handle the declarations.
*/
@Bean
public RabbitAdmin admin() {
return new RabbitAdmin(rabbitConnectionFactory());
}
}
我是否以正确的方式解决这个问题? 现在我应该如何让消费者消费一个呢?
这就是我打电话给测试的方式:
public class App {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(FixedReplyQueueConfig.class);
RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
rabbitTemplate.convertSendAndReceive("ub.exchange", "normal" , "yalla");
rabbitTemplate.convertSendAndReceive("ub.exchange", "high" , "hello");
}
}
这是我的pojo课程:
public class PojoListener {
public String handleMessage(String foo) {
System.out.println("IN MESSAGE RECEIVER");
return "it's the weekend!!!!!!!!!!!!";
}
}
这两条消息都通过发送方式发送,但不确定如何使用已实现的配置和类实现优先级。
请注意,我正在尝试避免使用任何插件来支持优先级。
答案 0 :(得分:1)
Spring AMQP(BlockingQueueConsumer
)的代码如下所示:
for (String queueName : queues) {
if (!this.missingQueues.contains(queueName)) {
consumeFromQueue(queueName);
}
}
Os,如果你的配置是:
container.setQueues(requestQueueHigh(), requestQueue());
您真的会收到来自requestQueueHigh()
requestQueue()
以上的邮件。它独立于concurrency
上的ListenerContainer
。
您可以使用多条消息对其进行测试,但是从应用程序开始时使用stopped
侦听器。发送这些消息后,最终会有start()
个侦听器容器。