31 Temmuz 2023 Pazartesi

SpringIntegration Redis @RedisLockable Anotasyonu

Giriş
Kilidi kodla bırakmak için SpringIntegration Redis RedisLockRegistry Sınıfı yazısına bakabilirsiniz

Maven
Şu satırı dahil ederiz
<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-redis</artifactId>
    <version>5.5.0</version>
</dependency>
Örnek
application.properties şöyle olsun
@Configuration
public class AppConfig {

  @Bean
  public RedisConnectionFactory redisConnectionFactory() {
    return new JedisConnectionFactory();
  }
}


@Service
public class MyService {

  @RedisLockable(key = "my-lock-key", leaseTime = 60_000, waitTime = 5_000)
  public void doSomethingWithLock() throws InterruptedException {
    // Do something with the lock
  }
}
Şöyle yaparız
@Configuration
public class AppConfig {

  @Bean
  public RedisConnectionFactory redisConnectionFactory() {
    return new JedisConnectionFactory();
  }
}


@Service
public class MyService {

  @RedisLockable(key = "my-lock-key", leaseTime = 60_000, waitTime = 5_000)
  public void doSomethingWithLock() throws InterruptedException {
    // Do something with the lock
  }
}
Açıklaması şöyle
In this example, we annotate the doSomethingWithLock method with the @RedisLockable annotation, which specifies the key to use for the lock, the lease time (in milliseconds), and the wait time (in milliseconds). The lease time determines how long the lock should be held before it is automatically released, while the wait time determines how long the method should wait to acquire the lock before giving up and throwing an exception.





Hiç yorum yok:

Yorum Gönder