Spring Cloud Stream自动重新连接到Rabbit MQ
我有一个连接到RabbitMQ服务器的Spring云流应用程序。我们在rabbitmq中使用rabbitmq验证后端uaa插件,但这不是问题所在 该插件检查oAuth2.0 JWT令牌,该令牌在创建与Rabbit MQ的连接时作为用户名发送 为此,我在Spring应用程序中有以下代码:Spring Cloud Stream自动重新连接到Rabbit MQ,spring,rabbitmq,spring-amqp,spring-cloud-stream,spring-rabbit,Spring,Rabbitmq,Spring Amqp,Spring Cloud Stream,Spring Rabbit,我有一个连接到RabbitMQ服务器的Spring云流应用程序。我们在rabbitmq中使用rabbitmq验证后端uaa插件,但这不是问题所在 该插件检查oAuth2.0 JWT令牌,该令牌在创建与Rabbit MQ的连接时作为用户名发送 为此,我在Spring应用程序中有以下代码: @Bean @Primary ConnectionFactory connectionFactory() throws Exception { //These lines get the token f
@Bean
@Primary
ConnectionFactory connectionFactory() throws Exception {
//These lines get the token from the UAA automatically.
MyTokenService tokenService = new MyTokenService();
String token= tokenService.obtainAccessToken(); // HTTP POST request to the UAA
AbstractConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
connectionFactory.setUsername(token);
return connectionFactory;
}
问题是令牌每小时都会过期,因此必须重新加载连接的用户名(再次调用tokenService.obtainAccessToken())
这是自动完成的吗?如何确保每次都会重新加载连接 否,
@Bean
定义在上下文初始化期间加载一次
您应该能够向连接工厂添加一个
ConnectionListener
,并将用于重新配置连接工厂的逻辑放入onClose()
方法中(可能还有onShutDown()
自2.0以来)。否,在上下文初始化期间加载一次@Bean
定义
您应该能够向连接工厂添加一个ConnectionListener
,并将重新配置连接工厂的逻辑放在onClose()
方法中(可能还有onShutDown()
自2.0以来)