我写了样本spring amqp生产者,该生产者在RabbitMQ服务器上运行,该服务器使用Spring AMQP发送消息并使用MessageListener消耗这些消息。在这里,我想将队列和消息的持久性设置为false。能否请任何人帮助我,如何使用注释将“ durable”标志设置为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);
}
}
}
预先感谢。
@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());
}
}
Rabbit管理员将在第一次打开连接时声明队列。请注意,您不能将队列从持久更改为否。首先将其删除。