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

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

Introduction

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

Prototype

public JdbcLockRegistry(LockRepository client) 

Source Link

Usage

From source file:org.springframework.cloud.task.configuration.SingleInstanceTaskListener.java

private LockRegistry getDefaultLockRegistry(long executionId) {
    DefaultLockRepository lockRepository = new DefaultLockRepository(this.dataSource,
            String.valueOf(executionId));
    lockRepository.setPrefix(this.taskProperties.getTablePrefix());
    lockRepository.setTimeToLive(this.taskProperties.getSingleInstanceLockTtl());
    lockRepository.afterPropertiesSet();
    return new JdbcLockRegistry(lockRepository);
}

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;
                        }/*from w ww  .j a  va  2 s  .c om*/
                    }
                } 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();//from   ww  w.  ja  va2s.c o 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());
    }
}