RabbitMQ fixed reply and consumer configutation

耗尽温柔 提交于 2019-12-11 13:08:31

问题


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:

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

Call in main method:

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);
}
}

I have two questions:

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.

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)?


回答1:


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

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

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.



来源:https://stackoverflow.com/questions/27150773/rabbitmq-fixed-reply-and-consumer-configutation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!