アビ:
私はこのようなレタス(io.lettuce.core.api)の設定で春ブートアプリを初期化しています
@Configuration
class RedisConfiguration {
@Value("${spring.redis.host}")
private String redisHostname;
@Value("${spring.redis.port}")
private int redisPort;
private StatefulRedisConnection<String, String> redisConnection;
private static RedisClient redisClient;
@Bean
public RedisCommands connectionFactory() {
RedisURI redisURI = RedisURI.create(redisHostname,redisPort);
redisClient = RedisClient.create(redisURI);
redisConnection = redisClient.connect();
RedisCommands<String, String> syncCommands =
redisConnection.sync();
return syncCommands;
}
}
私は、呼び出したいredisClient.shutdown();
ときに、アプリケーションのシャットダウン、または終了します。Redisの接続を終了するには、適切な場所とは何ですか?
mp911de:
2つのオプションがあります。
- 使い方
@PreDestroy
:
@PreDestroy
public StatefulRedisConnection<String, String> redisConnection() {
redisConnection.close();
redisClient.close();
}
- 経由
@Bean
する方法
公開することを確認しますRedisClient
とStatefulRedisConnection
豆など。コマンドインタフェースは、( RedisCommands
)を露出していないclose()
方法を。
@Configuration
class RedisConfiguration {
@Value("${spring.redis.host}")
private String redisHostname;
@Value("${spring.redis.port}")
private int redisPort;
@Bean(destroyMethod = "close")
public StatefulRedisConnection<String, String> redisClient() {
RedisURI redisURI = RedisURI.create(redisHostname,redisPort);
return RedisClient.create(redisURI);
redisConnection = redisClient.connect();
}
@Bean(destroyMethod = "close")
public StatefulRedisConnection<String, String> redisConnection(RedisClient client) {
return client.connect();
}
@Bean
public RedisCommands redisCommands(StatefulRedisConnection<String, String> connection) {
return connection.sync();
}
}
一方、第一の方法は、短い@Bean
アプローチは、あなたのアプリケーションでは、中間オブジェクトと対話することができます。