如何使用注释在Spring AMQP中将队列/消息持久性设置为false?

时间:2013-08-28 13:03:01

标签: queue rabbitmq spring-amqp

我编写了示例spring amqp producer,它运行在RabbitMQ服务器上,该服务器使用Spring AMQP发送消息并使用MessageListener消费这些消息。在这里,我想将队列和消息持久性设置为false。你能不能请任何人帮助我如何使用注释将“持久”标志设置为假。

以下是示例代码

@Configuration

public class ProducerConfiguration {

protected final String queueName = "hello.queue";

@Bean
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate template = new RabbitTemplate(connectionFactory());
    template.setRoutingKey(this.queueName);
    template.setQueue(this.queueName);
    return template;
}

@Bean
public ConnectionFactory connectionFactory() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
    connectionFactory.setUsername("guest");
    connectionFactory.setPassword("guest");
    return connectionFactory;
}

}

public class Producer {

public static void main(String[] args) throws Exception {
    new Producer().send();
}

public void send() {

    ApplicationContext context = new AnnotationConfigApplicationContext(
            ProducerConfiguration.class);
    RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
    for (int i = 1; i <= 10; i++) {
        rabbitTemplate.convertAndSend(i);
    }
}

}

先谢谢。

1 个答案:

答案 0 :(得分:1)

@Configuration
public class Config {

    @Bean
    public ConnectionFactory connectionFactory() {
        return new CachingConnectionFactory();
    }

    @Bean
    public Queue foo() {
        return new Queue("foo", false);
    }

    @Bean
    public RabbitAdmin rabbitAdmin() {
        return new RabbitAdmin(connectionFactory());
    }
}

兔子管理员将在第一次打开连接时声明队列。请注意,您无法将队列从持久更改为不;先删除它。