【问题标题】:RabbitMQ fixed reply and consumer configutationRabbitMQ 固定回复和消费者配置
【发布时间】:2014-11-26 13:43:11
【问题描述】:

我的目标是实现以下目标:php 代码将请求发送到队列 - java 代码从代码中读取 - java 代码将回复发送到固定回复队列 - php 代码读取回复。我已经设置了以下测试(生产者现在在 java 中):

POJO:

public class PojoListener {

public String handleMessage(String foo) {
    System.out.println("IN MESSAGE RECEIVER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    return foo.toUpperCase();
}
}

配置:

@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 SimpleMe ssageListenerContainer();
    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;
}

/**
 * @return a non-durable auto-delete exchange.
 */
@Bean
public DirectExchange ex() {
    return new DirectExchange("ub.exchange", false, true);
}

@Bean
public Binding binding() {
    return BindingBuilder.bind(requestQueue()).to(ex()).with("test");
}

/**
 * @return an anonymous (auto-delete) queue.
 */
@Bean
public Queue requestQueue() {
    return new Queue("ub.request");
}

/**
 * @return an anonymous (auto-delete) queue.
 */
@Bean
public Queue replyQueue() {
    return new Queue("ub.reply");
}

/**
 * @return an admin to handle the declarations.
 */
@Bean
public RabbitAdmin admin() {
   return new RabbitAdmin(rabbitConnectionFactory());
}
}

在main方法中调用:

public class App {  
public static void main(String[] args) {        
    ApplicationContext context = new AnnotationConfigApplicationContext(FixedReplyQueueConfig.class);
    RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);

    String response = (String) rabbitTemplate.convertSendAndReceive("yalla");
    System.out.println("response" + response);
}
}

我有两个问题:

当我运行它时,我收到以下错误:RabbitTemplate [ERROR] No correlation header in reply 虽然我看到两个队列都收到了消息。

第二个问题是我如何只运行消费者代码(监听器)而不发送消息(因为最终调用者不会是我的 java 代码)?

【问题讨论】:

    标签: java rabbitmq amqp spring-amqp spring-rabbit


    【解决方案1】:

    这看起来像是基于框架测试用例,显然有效。

    您是否正在向 ub.reply 发送任何其他消息?是空的吗?

    获取该日志消息的唯一方法是模板收到的回复没有正确填充相关 ID 属性。

    您可以只运行应用程序并删除所有客户端代码,容器将侦听入站请求。

    【讨论】:

    • 不,我没有向队列发送任何其他内容,但打印时仍然得到空回复: String response = (String) rabbitTemplate.convertSendAndReceive("trial");并且缺少标题数据的相同错误。对于第二个问题:为了运行生产者,我调用 rabbitTemplate.convertSendAndReceive("trial"),对于只喜欢调用侦听器开始的消费者的调用是什么?
    • 我编辑了代码以准确反映我如何调用生产者,也许那里有什么问题导致空响应和标头问题?
    • 是的,代码进入处理程序,但是如果您在 App 类中的 main 方法中打印结果,它会在抛出“RabbitTemplate [ERROR] No correlation header in reply”错误后立即返回 null
    • 哦,我现在意识到了响应部分。奇怪的是它在我的机器上不起作用,怀疑这是一个设置问题。你使用的调用类和我的一样吗?
    • 相同 - 正如我所说,我只更改了凭据和虚拟主机。 Gist here.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 2016-11-14
    • 1970-01-01
    • 2011-04-15
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多