RabbitMQ的固定答复和消费configutation(RabbitMQ fixed reply

2019-10-21 08:08发布

我的目标实现如下: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 [错误]在回复暂时没有相关头,虽然我看到两个队列得到的消息。

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

Answer 1:

这看起来像它的框架基础上测试的情况下,这显然作品。

你发送任何其他消息ub.reply? 它是空的?

你可以得到日志消息的唯一途径是,如果模板收到没有正确填写相关id属性的答复。

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



文章来源: RabbitMQ fixed reply and consumer configutation