0

I was using CachingConnectionFactory and making it a bean so that the queues and the exchanges will be created automatically if they don't exist. I created another bean RabbitTemplate and inside it I set an exchange.

@Bean
public RabbitTemplate receiptRabbitTemplate() throws URISyntaxException {
    RabbitTemplate template = new RabbitTemplate(cachingConnectionFactory(rabbitMQServiceScheme,
            rabbitMQServiceHostName, rabbitMQServiceContextRoot, rabbitMQServiceUser, rabbitMQServicePassword));
    template.setMandatory(true);
    template.setChannelTransacted(true);
    Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JodaModule());
    objectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    converter.setJsonObjectMapper(objectMapper);
    template.setMessageConverter(converter);
    template.setRetryTemplate(retryTemplate());
    template.setExchange("test.exchange"); //--------------------<
    return template;
}

Now I'm adding another RabbitTemplate so that I can send message to another exchange. Since it's in the same virtual machine, I'm using the same ConnectionFactory bean.

@Bean
public RabbitTemplate receiptRabbitTemplate() throws URISyntaxException {
    RabbitTemplate template = new RabbitTemplate(cachingConnectionFactory(rabbitMQServiceScheme,
            rabbitMQServiceHostName, rabbitMQServiceContextRoot, rabbitMQServiceUser, rabbitMQServicePassword));
    template.setMandatory(true);
    template.setChannelTransacted(true);
    Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JodaModule());
    objectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    converter.setJsonObjectMapper(objectMapper);
    template.setMessageConverter(converter);
    template.setRetryTemplate(retryTemplate());
    template.setExchange("anothertest.exchange");//---------------<
    return template;
}

But when I try to send message, only one of the exchange is being created. And for the other I get error

Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange

Any pointers on what I'm doing wrong?

user3344591
  • 567
  • 5
  • 21

1 Answers1

0

In order for any exchange to be declared, you need a RabbitAdmin bean and each exchange must be defined as a bean (not just set in the template).

See Configuring the broker.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179