RabbitMQ固定答复和使用者配置 [英] RabbitMQ fixed reply and consumer configutation

查看:277
本文介绍了RabbitMQ固定答复和使用者配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm aiming to achieve the following: php code sends request to queue - java code reads from code - java code sends reply to fixed reply queue - php code reads the reply. I have set up the following test (producer is for now in 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());
}
}

调用主方法:

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]尽管我看到两个队列都收到了消息,但没有相关头回复.

When I run this I get the following error: RabbitTemplate [ERROR] No correlation header in reply though I see that both queues got the message.

第二个问题是我如何仅在不发送消息的情况下运行使用者代码(侦听器)(因为最终调用者将不是我的Java代码)?

Second question is how to I run the consumer code (the listener) only without sending a message (since eventually the caller will not be my java code)?

推荐答案

这似乎是基于框架测试用例的,显然很有效.

That looks like it's based on the framework test case, which clearly works.

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

Are you sending any other messages to ub.reply? Is it empty?

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

The only way you can get that log message is if the template receives a reply that does not have a properly populated correlation id property.

您只需运行应用程序并删除所有客户端代码,容器就会侦听入站请求.

You can just run the application and remove all the client side code, the container will listen for inbound requests.

这篇关于RabbitMQ固定答复和使用者配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆