In my humble opinion, best way is to override rabbitmq default configuration.
Main class should disable default rabbit configuration:
@SpringBootApplication
@EnableAutoConfiguration(exclude
{org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration.class})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
my rabbitmq properties are in rabbitmq.properties file:
...
rabbitmq.queue=my-queue
...
Then just create own configuration rabbitmq component:
@Component
@EnableRabbit
@PropertySource("classpath:rabbitmq.properties")
public class RabbitMQConfiguration
{
...
@Value("${rabbitmq.queue}")
private String queueName;
...
@Bean
public Queue queue() {
return new Queue(queueName, false);
}
...
Also consumer should be setuped:
@Component
@PropertySource("classpath:rabbitmq.properties")
public class MyConsumer
{
private static Logger LOG = LogManager.getLogger(MyConsumer.class.toString());
@RabbitListener(queues = {"${rabbitmq.queue}"})
public void receive(@Payload Object data) {
LOG.info("Message: " + data) ;
}
Now when client starts it would automatically create queue if it does not exists. If queue exists it do nothing.