Example usage for org.springframework.integration.redis.util RedisLockRegistry RedisLockRegistry

List of usage examples for org.springframework.integration.redis.util RedisLockRegistry RedisLockRegistry

Introduction

In this page you can find the example usage for org.springframework.integration.redis.util RedisLockRegistry RedisLockRegistry.

Prototype

public RedisLockRegistry(RedisConnectionFactory connectionFactory, String registryKey, long expireAfter) 

Source Link

Document

Constructs a lock registry with the supplied lock expiration.

Usage

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable//from   www. j  a v a  2  s. c om
public void testExpireNoLockInStore() throws Exception {
    RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey,
            100);
    Lock foo = registry.obtain("foo");
    foo.lockInterruptibly();
    waitForExpire("foo");
    try {
        foo.unlock();
        fail("Expected exception");
    } catch (IllegalStateException e) {
        assertThat(e.getMessage(), containsString("Lock was released due to expiration"));
    }
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable/*from w w  w .j  a v a2  s  .com*/
public void testExpireDuringSecondObtain() throws Exception {
    RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey,
            100);
    registry.setUseWeakReferences(true);
    Lock foo = registry.obtain("foo");
    foo.lockInterruptibly();
    waitForExpire("foo");
    Lock foo1 = registry.obtain("foo");
    assertNotSame(foo, foo1);

    try {
        foo.unlock();
        fail("IllegalStateException");
    } catch (IllegalStateException e) {
        assertThat(e.getMessage(), containsString("Lock is not locked"));
    }
}

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable//from  w  ww . j  a  v  a2  s  .c o  m
public void testExpireNewLockInStore() throws Exception {
    RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey,
            100);
    Lock foo1 = registry.obtain("foo");
    foo1.lockInterruptibly();
    waitForExpire("foo");
    Lock foo2 = registry.obtain("foo");
    assertNotSame(foo1, foo2);
    foo2.lockInterruptibly();
    try {
        foo1.unlock();
        fail("Expected exception");
    } catch (IllegalStateException e) {
        assertThat(e.getMessage(), containsString("Lock is not locked"));
    }
    foo2.unlock();
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable// w  w  w. jav  a 2s.c  om
public void testThreadLocalListLeaks() {
    RedisLockRegistry registry = new RedisLockRegistry(this.getConnectionFactoryForTest(), this.registryKey,
            100);
    registry.setUseWeakReferences(true);

    for (int i = 0; i < 10; i++) {
        registry.obtain("foo" + i);
    }
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());

    for (int i = 0; i < 10; i++) {
        Lock lock = registry.obtain("foo" + i);
        lock.lock();
    }
    assertEquals(10,
            ((Collection<?>) TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get())
                    .size());
    assertNull(TestUtils.getPropertyValue(registry, "weakThreadLocks", ThreadLocal.class).get());

    for (int i = 0; i < 10; i++) {
        Lock lock = registry.obtain("foo" + i);
        assertNotNull(TestUtils.getPropertyValue(lock, "thread", Thread.class));
        lock.unlock();
    }
    assertNull(TestUtils.getPropertyValue(registry, "hardThreadLocks", ThreadLocal.class).get());
}

From source file:org.springframework.integration.redis.util.RedisLockRegistryTests.java

@Test
@RedisAvailable//from w w w  .  ja v a 2  s  . c  om
public void testExpireNotChanged() throws Exception {
    RedisConnectionFactory connectionFactory = this.getConnectionFactoryForTest();
    final RedisLockRegistry registry = new RedisLockRegistry(connectionFactory, this.registryKey, 10000);
    Lock lock = registry.obtain("foo");
    lock.lock();

    Long expire = getExpire(registry, "foo");

    Future<Object> result = Executors.newSingleThreadExecutor().submit(() -> {
        Lock lock2 = registry.obtain("foo");
        assertFalse(lock2.tryLock());
        return null;
    });
    result.get();
    assertEquals(expire, getExpire(registry, "foo"));
    lock.unlock();
}