简体   繁体   中英

Spring Cloud Stream Automatically reconnect to the Rabbit MQ

I have a Spring Cloud Stream application that connects to a RabbitMQ server. We are using the rabbitmq-auth-backend-uaa plugin in Rabbit MQ, but this is not the problem.

This plugin checks an oAuth2.0 JWT token that is sent as the username when the connection with the Rabbit MQ is created.

To do so, I have this code in my Spring application:

@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;
}

The problem is that the token will expire every hour and therefore the username of the connection has to be reloaded (Calling tokenService.obtainAccessToken() again).

Is this done automatically? How can I be sure that the connection will be reloaded every time?

No, a @Bean definition is loaded once, during context initialization.

You should be able to add a ConnectionListener to the connection factory and put your logic to reconfigure the connection factory in the onClose() method (and perhaps onShutDown() since 2.0).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM