Example usage for org.springframework.integration.jdbc.lock DefaultLockRepository DefaultLockRepository

List of usage examples for org.springframework.integration.jdbc.lock DefaultLockRepository DefaultLockRepository

Introduction

In this page you can find the example usage for org.springframework.integration.jdbc.lock DefaultLockRepository DefaultLockRepository.

Prototype

@Autowired
public DefaultLockRepository(DataSource dataSource) 

Source Link

Document

Constructor that initializes the client id that will be associated for all the locks persisted by the store instance to a random UUID .

Usage

From source file:org.springframework.integration.jdbc.lock.JdbcLockRegistryDifferentClientTests.java

@Test
public void testOnlyOneLock() throws Exception {

    for (int i = 0; i < 100; i++) {

        final List<String> locked = new ArrayList<String>();
        final CountDownLatch latch = new CountDownLatch(20);
        ExecutorService pool = Executors.newFixedThreadPool(6);
        ArrayList<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>();
        for (int j = 0; j < 20; j++) {
            final DefaultLockRepository client = new DefaultLockRepository(this.dataSource);
            client.afterPropertiesSet();
            this.context.getAutowireCapableBeanFactory().autowireBean(client);
            Callable<Boolean> task = () -> {
                Lock lock = new JdbcLockRegistry(client).obtain("foo");
                try {
                    if (locked.isEmpty() && lock.tryLock()) {
                        if (locked.isEmpty()) {
                            locked.add("done");
                            return true;
                        }// w  w  w  .ja va  2  s. co m
                    }
                } finally {
                    try {
                        lock.unlock();
                    } catch (Exception e) {
                        // ignore
                    }
                    latch.countDown();
                }
                return false;
            };
            tasks.add(task);
        }
        logger.info("Starting: " + i);
        pool.invokeAll(tasks);

        assertTrue(latch.await(10, TimeUnit.SECONDS));
        // eventually they both get the lock and release it
        assertEquals(1, locked.size());
        assertTrue(locked.contains("done"));

    }

}

From source file:org.springframework.integration.jdbc.lock.JdbcLockRegistryDifferentClientTests.java

@Test
public void testExclusiveAccess() throws Exception {
    DefaultLockRepository client1 = new DefaultLockRepository(dataSource);
    client1.afterPropertiesSet();//  w w  w .  j a v a 2 s.co m
    final DefaultLockRepository client2 = new DefaultLockRepository(dataSource);
    client2.afterPropertiesSet();
    Lock lock1 = new JdbcLockRegistry(client1).obtain("foo");
    final BlockingQueue<Integer> data = new LinkedBlockingQueue<Integer>();
    final CountDownLatch latch1 = new CountDownLatch(1);
    lock1.lockInterruptibly();
    Executors.newSingleThreadExecutor().execute(() -> {
        Lock lock2 = new JdbcLockRegistry(client2).obtain("foo");
        try {
            latch1.countDown();
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            lock2.lockInterruptibly();
            stopWatch.stop();
            data.add(4);
            Thread.sleep(10);
            data.add(5);
            Thread.sleep(10);
            data.add(6);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } finally {
            lock2.unlock();
        }
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    data.add(1);
    Thread.sleep(1000);
    data.add(2);
    Thread.sleep(1000);
    data.add(3);
    lock1.unlock();
    for (int i = 0; i < 6; i++) {
        Integer integer = data.poll(10, TimeUnit.SECONDS);
        assertNotNull(integer);
        assertEquals(i + 1, integer.intValue());
    }
}