Background-info
I’m using the spring-boot-starter-amqp
dependency to connect to RabbitMQ.
In my application.yaml I have:
spring:
rabbitmq:
host: <host>
port: 5672
username: <username>
password: <password>
I have a Configuration class where I define a AmqpTemplate like this:
@Bean
public AmqpTemplate amqpSenderTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(jsonConverter());
return rabbitTemplate;
}
and I have listeners defined like this:
@Component
@RequiredArgsConstructor
public class EventListener {
private final SomeService someService;
@RabbitListener(queues = "${rabbitmq.event.someQueue}")
public void process(SomeEvent event) {
this.someService.process(event);
}
We inject the username and password into our containers using a configtree:
spring.config.import: configtree:/etc/apiconfig, ...
The configtree contains the secrets which are dynamic secrets from Hashicorp Vault.
Question/Issue
The RabbitMQ credentials are often rotated for security reasons and re-injected using the configtree.
I don’t understand how I can refresh my Amqp connection so that it can handle the credential rotation without having to restart the container.
So, the connection needs to be reset for both receiving (@rabbitlistener) and sending (amqpTemplate).
Can anyone point me in the right direction please?
I already looked into @RefreshScope and spring-bus but I can’t figure out:
- What I should exactly refresh/recreate
- How I can automatically detect and trigger the recreation without having to manually call a refresh endpoint.
Any code example is very much appreciated!
Please note that the setup using configtree with Hashicorp cannot be changed.