List of usage examples for org.springframework.integration.jdbc.lock JdbcLockRegistry obtain
@Override
public Lock obtain(Object lockKey)
From source file:org.springframework.integration.jdbc.lock.JdbcLockRegistryDifferentClientTests.java
@Test public void testSecondThreadLoses() throws Exception { for (int i = 0; i < 100; i++) { final JdbcLockRegistry registry1 = this.registry; final JdbcLockRegistry registry2 = this.child.getBean(JdbcLockRegistry.class); final Lock lock1 = registry1.obtain("foo"); final AtomicBoolean locked = new AtomicBoolean(); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); final CountDownLatch latch3 = new CountDownLatch(1); lock1.lockInterruptibly();/*from ww w . j a v a 2 s . c o m*/ Executors.newSingleThreadExecutor().execute(() -> { Lock lock2 = registry2.obtain("foo"); try { latch1.countDown(); lock2.lockInterruptibly(); latch2.await(10, TimeUnit.SECONDS); locked.set(true); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { lock2.unlock(); latch3.countDown(); } }); assertTrue(latch1.await(10, TimeUnit.SECONDS)); assertFalse(locked.get()); lock1.unlock(); latch2.countDown(); assertTrue(latch3.await(10, TimeUnit.SECONDS)); assertTrue(locked.get()); } }
From source file:org.springframework.integration.jdbc.lock.JdbcLockRegistryDifferentClientTests.java
@Test public void testBothLock() throws Exception { for (int i = 0; i < 100; i++) { final JdbcLockRegistry registry1 = this.registry; final JdbcLockRegistry registry2 = this.child.getBean(JdbcLockRegistry.class); final List<String> locked = new ArrayList<String>(); final CountDownLatch latch = new CountDownLatch(2); ExecutorService pool = Executors.newFixedThreadPool(2); pool.execute(() -> {/*from w w w .j av a 2 s .co m*/ Lock lock = registry1.obtain("foo"); try { lock.lockInterruptibly(); locked.add("1"); latch.countDown(); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } finally { try { lock.unlock(); } catch (Exception e2) { // ignore } } }); pool.execute(() -> { Lock lock = registry2.obtain("foo"); try { lock.lockInterruptibly(); locked.add("2"); latch.countDown(); } catch (InterruptedException e1) { Thread.currentThread().interrupt(); } finally { try { lock.unlock(); } catch (Exception e2) { // ignore } } }); assertTrue(latch.await(10, TimeUnit.SECONDS)); // eventually they both get the lock and release it assertTrue(locked.contains("1")); assertTrue(locked.contains("2")); } }