List of usage examples for org.springframework.integration.jdbc.lock DefaultLockRepository afterPropertiesSet
@Override public void afterPropertiesSet()
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 .ja va2s .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(); final DefaultLockRepository client2 = new DefaultLockRepository(dataSource); client2.afterPropertiesSet();/*from www. j av a2s . c o m*/ 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()); } }