Package com.hazelcast.core

Examples of com.hazelcast.core.ILock


        final HazelcastInstance instance1 = nodeFactory.newHazelcastInstance();
        final HazelcastInstance instance2 = nodeFactory.newHazelcastInstance();

        warmUpPartitions(keyOwner, instance1, instance2);
        final String key = generateKeyOwnedBy(keyOwner);
        final ILock lock1 = instance1.getLock(key);
        lock1.lock();

        final CountDownLatch latch = new CountDownLatch(1);
        new Thread(new Runnable() {
            public void run() {
                final ILock lock = instance2.getLock(key);
                lock.lock();
                latch.countDown();
            }
        }).start();

        Thread.sleep(1000);
View Full Code Here


    @Test(timeout = 100000)
    public void testScheduledLockActionForDeadMember() throws Exception {
        final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
        final HazelcastInstance h1 = nodeFactory.newHazelcastInstance();
        final ILock lock1 = h1.getLock("default");
        final HazelcastInstance h2 = nodeFactory.newHazelcastInstance();
        final ILock lock2 = h2.getLock("default");

        assertTrue(lock1.tryLock());

        final AtomicBoolean error = new AtomicBoolean(false);
        Thread thread = new Thread(new Runnable() {
            public void run() {
                try {
                    lock2.lock();
                    error.set(true);
                } catch (Throwable ignored) {
                }
            }
        });
View Full Code Here

    public void testLockInterruptibly() throws Exception {
        Config config = new Config();
        config.setProperty(GroupProperties.PROP_OPERATION_CALL_TIMEOUT_MILLIS, "5000");
        final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(1);
        final HazelcastInstance h1 = nodeFactory.newHazelcastInstance(config);
        final ILock lock = h1.getLock(randomString());
        final CountDownLatch latch = new CountDownLatch(1);
        lock.lock();
        Thread t = new Thread() {
            public void run() {
                try {
                    lock.lockInterruptibly();
                } catch (InterruptedException e) {
                    latch.countDown();
                }
            }
        };
View Full Code Here

    }

    @Test
    public void testInterruptionDuringBlockingOp2() throws InterruptedException {
        HazelcastInstance hz = createHazelcastInstance();
        final ILock lock = hz.getLock("lock");
        lock.lock();
        assertTrue(lock.isLockedByCurrentThread());

        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicBoolean interruptedFlag = new AtomicBoolean(false);

        final OpThread thread = new OpThread("Lock-Thread", latch, interruptedFlag) {
            protected void doOp() throws InterruptedException {
                assertTrue(lock.tryLock(1, TimeUnit.MINUTES));
            }
        };
        thread.start();

        Thread.sleep(5000);
        thread.interrupt();
        lock.unlock();

        assertTrue(latch.await(1, TimeUnit.MINUTES));

        if (thread.isInterruptionCaught()) {
            assertFalse("Thread interrupted flag should not be set!", interruptedFlag.get());
            assertFalse("Lock should not be in 'locked' state!", lock.isLocked());
        } else {
            assertTrue("Thread interrupted flag should be set! " + thread, interruptedFlag.get());
            assertTrue("Lock should be 'locked' state!", lock.isLocked());
        }
    }
View Full Code Here

    @Test
    public void testLock() throws Exception {
        String partitionKey = "hazelcast";
        HazelcastInstance hz = getHazelcastInstance(partitionKey);

        ILock lock = hz.getLock("lock@" + partitionKey);
        lock.lock();
        assertEquals("lock@" + partitionKey, lock.getName());
        assertEquals(partitionKey, lock.getPartitionKey());

        Node node = getNode(hz);
        LockServiceImpl lockService = node.nodeEngine.getService(LockServiceImpl.SERVICE_NAME);

        Partition partition = instances[0].getPartitionService().getPartition(partitionKey);
        LockStore lockStore = lockService.getLockStore(partition.getPartitionId(), new InternalLockNamespace(lock.getName()));
        assertTrue(lockStore.isLocked(node.getSerializationService().toData(lock.getName())));
    }
View Full Code Here

    @Test
    public void test() {
        HazelcastInstance[] instances = createHazelcastInstanceFactory(INSTANCE_COUNT).newInstances();
        HazelcastInstance hz = instances[0];
        //Hazelcast.newHazelcastInstance();
        ILock lock = hz.getLock(randomString());
        ICondition condition = lock.newCondition(randomString());

        ConsumerThread[] consumers = new ConsumerThread[CONSUMER_COUNT];
        for (int k = 0; k < consumers.length; k++) {
            ConsumerThread thread = new ConsumerThread(1, lock, condition);
            thread.start();
View Full Code Here

    public void concurrent_TryLock_WithTimeOutTest() throws InterruptedException {
        concurrent_LockTest(true);
    }

    private void concurrent_LockTest(boolean tryLockWithTimeOut) throws InterruptedException {
        final ILock lock = client.getLock(randomString());
        final AtomicInteger upTotal = new AtomicInteger(0);
        final AtomicInteger downTotal = new AtomicInteger(0);

        LockTestThread threads[] = new LockTestThread[8];
        for ( int i=0; i<threads.length; i++ ) {
View Full Code Here

    @Test
    public void testObtainLock_FromDiffClients() throws InterruptedException {

        HazelcastInstance clientA = HazelcastClient.newHazelcastClient();
        ILock lockA = clientA.getLock(name);
        lockA.lock();

        HazelcastInstance clientB = HazelcastClient.newHazelcastClient();
        ILock lockB = clientB.getLock(name);
        boolean lockObtained = lockB.tryLock();

        assertFalse("Lock obtained by 2 client ", lockObtained);
    }
View Full Code Here

        Hazelcast.shutdownAll();
    }

    @Test
    public void testLockOnClientCrash() throws InterruptedException {
        ILock lock = client1.getLock(keyOwnedByNode2);
        lock.lock();

        client1.getLifecycleService().terminate();

        lock = client2.getLock(keyOwnedByNode2);
        boolean lockObtained = lock.tryLock(120, TimeUnit.SECONDS);

        assertTrue("Lock was Not Obtained, lock should be released on client crash", lockObtained);
    }
View Full Code Here

        assertTrue("Lock was Not Obtained, lock should be released on client crash", lockObtained);
    }

    @Test
    public void testLockOnClient_withNodeCrash() throws InterruptedException {
        ILock lock = client1.getLock(keyOwnedByNode2);
        lock.lock();

        node2.getLifecycleService().terminate();

        lock = client2.getLock(keyOwnedByNode2);
        boolean lockObtained = lock.tryLock();

        assertFalse("Lock was obtained by 2 different clients ", lockObtained);
    }
View Full Code Here

TOP

Related Classes of com.hazelcast.core.ILock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.