Java 类com.hazelcast.config.SemaphoreConfig 实例源码

项目:hazelcast-archive    文件:ConcurrentMapManager.java   
synchronized void initSemaphore(SemaphoreConfig sc, Request request, String name) throws Exception {
    if (request.record.getValue() == null) {
        final int configInitialPermits = sc.getInitialPermits();
        SemaphoreFactory factory = sc.getFactoryImplementation();
        if (factory == null) {
            String factoryClassName = sc.getFactoryClassName();
            if (factoryClassName != null && factoryClassName.length() != 0) {
                ClassLoader cl = node.getConfig().getClassLoader();
                Class factoryClass = Serializer.loadClass(cl, factoryClassName);
                factory = (SemaphoreFactory) factoryClass.newInstance();
            }
        }
        if (factory != null) {
            int initialPermits = factory.getInitialPermits(name, configInitialPermits);
            request.record.setValue(new DistributedSemaphore(initialPermits));
        }
    }
}
项目:hazelcast-archive    文件:SemaphoreTest.java   
@Test
public void testSemaphoreWithTimeout() {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 10);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    ISemaphore semaphore = instance.getSemaphore("test");
    //Test acquire and timeout.
    try {
        assertEquals(10, semaphore.availablePermits());
        semaphore.tryAcquire();
        assertEquals(9, semaphore.availablePermits());
        assertEquals(false, semaphore.tryAcquire(10, 10, TimeUnit.MILLISECONDS));
        assertEquals(9, semaphore.availablePermits());
        semaphore.release();
        //Test acquire and timeout and check for partial acquisitions.
        assertEquals(10, semaphore.availablePermits());
        assertEquals(false, semaphore.tryAcquire(20, 10, TimeUnit.MILLISECONDS));
        assertEquals(10, semaphore.availablePermits());
    } catch (Throwable e) {
        fail(e.getMessage());
    }
}
项目:hazelcast-archive    文件:SemaphoreTest.java   
@Test
public void testSimpleSemaphore() {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 1);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    ISemaphore semaphore = instance.getSemaphore("test");
    assertEquals(1, semaphore.availablePermits());
    semaphore.tryAcquire();
    assertEquals(0, semaphore.availablePermits());
    semaphore.release();
    assertEquals(1, semaphore.availablePermits());
    semaphore.tryAcquire();
    assertEquals(0, semaphore.availablePermits());
    semaphore.release();
    assertEquals(1, semaphore.availablePermits());
    semaphore.tryAcquire();
    assertEquals(0, semaphore.availablePermits());
    semaphore.release();
    assertEquals(1, semaphore.availablePermits());
    semaphore.tryAcquire();
    assertEquals(0, semaphore.availablePermits());
    semaphore.release();
    assertEquals(1, semaphore.availablePermits());
}
项目:hazelcast-archive    文件:SemaphoreTest.java   
@Test
public void testSemaphoreReducePermits() {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 10);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    ISemaphore semaphore = instance.getSemaphore("test");
    assertEquals(10, semaphore.availablePermits());
    semaphore.reducePermits(1);
    assertEquals(9, semaphore.availablePermits());
    semaphore.tryAcquire(9);
    assertEquals(0, semaphore.availablePermits());
    semaphore.reducePermits(8);
    assertEquals(-8, semaphore.availablePermits());
    semaphore.release();
    assertEquals(-7, semaphore.availablePermits());
    semaphore.release(8);
    assertEquals(1, semaphore.availablePermits());
}
项目:hazelcast-archive    文件:SemaphoreTest.java   
@Test
public void testSemaphoreDisconnect() throws InterruptedException {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 10);
    Config config = new Config();
    config.setProperty(GroupProperties.PROP_CONNECTION_MONITOR_INTERVAL, "1");
    config.setProperty(GroupProperties.PROP_CONNECTION_MONITOR_MAX_FAULTS, "1");
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config);
    HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config);
    ISemaphore semaphore1 = instance1.getSemaphore("test");
    ISemaphore semaphore2 = instance2.getSemaphore("test");
    assertEquals(10, semaphore1.availablePermits());
    semaphore1.tryAcquireAttach(5);
    semaphore1.reducePermits(1);
    instance1.getLifecycleService().kill();
    Thread.sleep(500);
    assertEquals(9, semaphore2.availablePermits());
}
项目:hazelcast-archive    文件:HazelcastClientSemaphoreTest.java   
@Test
public void testSemaphoreReducePermits() {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("test", 10);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    HazelcastClient client = newHazelcastClient(instance);
    ISemaphore semaphore = client.getSemaphore("test");
    assertEquals(10, semaphore.availablePermits());
    semaphore.reducePermits(1);
    assertEquals(9, semaphore.availablePermits());
    semaphore.tryAcquire(9);
    assertEquals(0, semaphore.availablePermits());
    semaphore.reducePermits(8);
    assertEquals(-8, semaphore.availablePermits());
    semaphore.release();
    assertEquals(-7, semaphore.availablePermits());
    semaphore.release(8);
    assertEquals(1, semaphore.availablePermits());
}
项目:hazelcast-archive    文件:HazelcastClientSemaphoreTest.java   
@Test
public void testSemaphoreDisconnect() throws InterruptedException {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 10);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    HazelcastClient client1 = newHazelcastClient(instance);
    HazelcastClient client2 = newHazelcastClient(instance);
    ISemaphore semaphore1 = client1.getSemaphore("test");
    ISemaphore semaphore2 = client2.getSemaphore("test");
    assertEquals(10, semaphore1.availablePermits());
    semaphore1.tryAcquireAttach(5);
    semaphore2.tryAcquire(3);
    semaphore1.reducePermits(1);
    assertEquals(1, semaphore2.availablePermits());
    client1.shutdown();
    assertEquals(6, semaphore2.availablePermits());
}
项目:hazelcast-archive    文件:HazelcastClientSemaphoreTest.java   
@Test
public void testSemaphorePeerDisconnect() {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 10);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    HazelcastClient client1 = newHazelcastClient(instance);
    HazelcastClient client2 = newHazelcastClient(instance);
    ISemaphore semaphore1 = client1.getSemaphore("test");
    ISemaphore semaphore2 = client2.getSemaphore("test");
    semaphore2.tryAcquireAttach(5);
    semaphore2.tryAcquire(3);
    semaphore2.detach(2);
    assertEquals(2, semaphore1.availablePermits());
    client2.shutdown();
    assertEquals(5, semaphore1.availablePermits());
}
项目:health-and-care-developer-network    文件:ConcurrentMapManager.java   
synchronized void initSemaphore(SemaphoreConfig sc, Request request, String name) throws Exception {
    if (request.record.getValue() == null) {
        final int configInitialPermits = sc.getInitialPermits();
        SemaphoreFactory factory = sc.getFactoryImplementation();
        if (factory == null) {
            String factoryClassName = sc.getFactoryClassName();
            if (factoryClassName != null && factoryClassName.length() != 0) {
                ClassLoader cl = node.getConfig().getClassLoader();
                Class factoryClass = Serializer.loadClass(cl, factoryClassName);
                factory = (SemaphoreFactory) factoryClass.newInstance();
            }
        }
        if (factory != null) {
            int initialPermits = factory.getInitialPermits(name, configInitialPermits);
            request.record.setValue(new DistributedSemaphore(initialPermits));
        }
    }
}
项目:health-and-care-developer-network    文件:SemaphoreTest.java   
@Test
public void testSemaphoreWithTimeout() {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 10);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    ISemaphore semaphore = instance.getSemaphore("test");
    //Test acquire and timeout.
    try {
        assertEquals(10, semaphore.availablePermits());
        semaphore.tryAcquire();
        assertEquals(9, semaphore.availablePermits());
        assertEquals(false, semaphore.tryAcquire(10, 10, TimeUnit.MILLISECONDS));
        assertEquals(9, semaphore.availablePermits());
        semaphore.release();
        //Test acquire and timeout and check for partial acquisitions.
        assertEquals(10, semaphore.availablePermits());
        assertEquals(false, semaphore.tryAcquire(20, 10, TimeUnit.MILLISECONDS));
        assertEquals(10, semaphore.availablePermits());
    } catch (Throwable e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
项目:health-and-care-developer-network    文件:SemaphoreTest.java   
@Test
public void testSimpleSemaphore() {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 1);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    ISemaphore semaphore = instance.getSemaphore("test");
    assertEquals(1, semaphore.availablePermits());
    semaphore.tryAcquire();
    assertEquals(0, semaphore.availablePermits());
    semaphore.release();
    assertEquals(1, semaphore.availablePermits());
    semaphore.tryAcquire();
    assertEquals(0, semaphore.availablePermits());
    semaphore.release();
    assertEquals(1, semaphore.availablePermits());
    semaphore.tryAcquire();
    assertEquals(0, semaphore.availablePermits());
    semaphore.release();
    assertEquals(1, semaphore.availablePermits());
    semaphore.tryAcquire();
    assertEquals(0, semaphore.availablePermits());
    semaphore.release();
    assertEquals(1, semaphore.availablePermits());
}
项目:health-and-care-developer-network    文件:SemaphoreTest.java   
@Test
public void testSemaphoreReducePermits() {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 10);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    ISemaphore semaphore = instance.getSemaphore("test");
    assertEquals(10, semaphore.availablePermits());
    semaphore.reducePermits(1);
    assertEquals(9, semaphore.availablePermits());
    semaphore.tryAcquire(9);
    assertEquals(0, semaphore.availablePermits());
    semaphore.reducePermits(8);
    assertEquals(-8, semaphore.availablePermits());
    semaphore.release();
    assertEquals(-7, semaphore.availablePermits());
    semaphore.release(8);
    assertEquals(1, semaphore.availablePermits());
}
项目:health-and-care-developer-network    文件:SemaphoreTest.java   
@Test
public void testSemaphoreDisconnect() throws InterruptedException {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 10);
    Config config = new Config();
    config.setProperty(GroupProperties.PROP_CONNECTION_MONITOR_INTERVAL, "1");
    config.setProperty(GroupProperties.PROP_CONNECTION_MONITOR_MAX_FAULTS, "1");
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config);
    HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config);
    ISemaphore semaphore1 = instance1.getSemaphore("test");
    ISemaphore semaphore2 = instance2.getSemaphore("test");
    assertEquals(10, semaphore1.availablePermits());
    semaphore1.tryAcquireAttach(5);
    semaphore1.reducePermits(1);
    instance1.getLifecycleService().kill();
    Thread.sleep(500);
    assertEquals(9, semaphore2.availablePermits());
}
项目:health-and-care-developer-network    文件:HazelcastClientSemaphoreTest.java   
@Test
public void testSemaphoreReducePermits() {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("test", 10);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    HazelcastClient client = newHazelcastClient(instance);
    ISemaphore semaphore = client.getSemaphore("test");
    assertEquals(10, semaphore.availablePermits());
    semaphore.reducePermits(1);
    assertEquals(9, semaphore.availablePermits());
    semaphore.tryAcquire(9);
    assertEquals(0, semaphore.availablePermits());
    semaphore.reducePermits(8);
    assertEquals(-8, semaphore.availablePermits());
    semaphore.release();
    assertEquals(-7, semaphore.availablePermits());
    semaphore.release(8);
    assertEquals(1, semaphore.availablePermits());
}
项目:health-and-care-developer-network    文件:HazelcastClientSemaphoreTest.java   
@Test
public void testSemaphoreDisconnect() throws InterruptedException {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 10);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    HazelcastClient client1 = newHazelcastClient(instance);
    HazelcastClient client2 = newHazelcastClient(instance);
    ISemaphore semaphore1 = client1.getSemaphore("test");
    ISemaphore semaphore2 = client2.getSemaphore("test");
    assertEquals(10, semaphore1.availablePermits());
    semaphore1.tryAcquireAttach(5);
    semaphore2.tryAcquire(3);
    semaphore1.reducePermits(1);
    assertEquals(1, semaphore2.availablePermits());
    client1.shutdown();
    assertEquals(6, semaphore2.availablePermits());
}
项目:health-and-care-developer-network    文件:HazelcastClientSemaphoreTest.java   
@Test
public void testSemaphorePeerDisconnect() {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 10);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    HazelcastClient client1 = newHazelcastClient(instance);
    HazelcastClient client2 = newHazelcastClient(instance);
    ISemaphore semaphore1 = client1.getSemaphore("test");
    ISemaphore semaphore2 = client2.getSemaphore("test");
    semaphore2.tryAcquireAttach(5);
    semaphore2.tryAcquire(3);
    semaphore2.detach(2);
    assertEquals(2, semaphore1.availablePermits());
    client2.shutdown();
    assertEquals(5, semaphore1.availablePermits());
}
项目:hazelcast-archive    文件:ConcurrentMapManager.java   
@Override
public void handle(final Request request) {
    request.record = ensureRecord(request, null);
    if (request.record.getValue() == null) {
        final String name = (String) toObject(request.key);
        final SemaphoreConfig sc = node.getConfig().getSemaphoreConfig(name);
        final int configInitialPermits = sc.getInitialPermits();
        if (sc.isFactoryEnabled()) {
            node.executorManager.executeNow(new Runnable() {
                public void run() {
                    try {
                        initSemaphore(sc, request, name);
                    } catch (Exception e) {
                        logger.log(Level.SEVERE, e.getMessage(), e);
                    } finally {
                        enqueueAndReturn(new Processable() {
                            public void process() {
                                SemaphoreOperationHandler.this.handle(request);
                            }
                        });
                    }
                }
            });
            return;
        } else {
            request.record.setValue(new DistributedSemaphore(configInitialPermits));
        }
    }
    doOperation(request);
}
项目:hazelcast-archive    文件:SemaphoreTest.java   
@Test
public void testSemaphoreIncreasePermits() {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 1);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    ISemaphore semaphore = instance.getSemaphore("test");
    assertEquals(1, semaphore.availablePermits());
    semaphore.release();
    assertEquals(2, semaphore.availablePermits());
}
项目:hazelcast-archive    文件:HazelcastClientSemaphoreTest.java   
@Test
public void testSimpleSemaphore() {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("test", 1);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    HazelcastClient client = newHazelcastClient(instance);
    ISemaphore semaphore = client.getSemaphore("test");
    assertEquals(1, semaphore.availablePermits());
    semaphore.tryAcquire();
    assertEquals(0, semaphore.availablePermits());
    semaphore.release();
    assertEquals(1, semaphore.availablePermits());
}
项目:hazelcast-archive    文件:HazelcastClientSemaphoreTest.java   
@Test
public void testSemaphoreIncreasePermits() {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 1);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    HazelcastClient client = newHazelcastClient(instance);
    ISemaphore semaphore = client.getSemaphore("test");
    assertEquals(1, semaphore.availablePermits());
    semaphore.release();
    assertEquals(2, semaphore.availablePermits());
}
项目:health-and-care-developer-network    文件:ConcurrentMapManager.java   
@Override
public void handle(final Request request) {
    request.record = ensureRecord(request, null);
    if (request.record.getValue() == null) {
        final String name = (String) toObject(request.key);
        final SemaphoreConfig sc = node.getConfig().getSemaphoreConfig(name);
        final int configInitialPermits = sc.getInitialPermits();
        if (sc.isFactoryEnabled()) {
            node.executorManager.executeNow(new Runnable() {
                public void run() {
                    try {
                        initSemaphore(sc, request, name);
                    } catch (Exception e) {
                        logger.log(Level.SEVERE, e.getMessage(), e);
                    } finally {
                        enqueueAndReturn(new Processable() {
                            public void process() {
                                SemaphoreOperationHandler.this.handle(request);
                            }
                        });
                    }
                }
            });
            return;
        } else {
            request.record.setValue(new DistributedSemaphore(configInitialPermits));
        }
    }
    doOperation(request);
}
项目:health-and-care-developer-network    文件:SemaphoreTest.java   
@Test
public void testSemaphoreIncreasePermits() {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 1);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    ISemaphore semaphore = instance.getSemaphore("test");
    assertEquals(1, semaphore.availablePermits());
    semaphore.release();
    assertEquals(2, semaphore.availablePermits());
}
项目:health-and-care-developer-network    文件:HazelcastClientSemaphoreTest.java   
@Test
public void testSimpleSemaphore() {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("test", 1);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    HazelcastClient client = newHazelcastClient(instance);
    ISemaphore semaphore = client.getSemaphore("test");
    assertEquals(1, semaphore.availablePermits());
    semaphore.tryAcquire();
    assertEquals(0, semaphore.availablePermits());
    semaphore.release();
    assertEquals(1, semaphore.availablePermits());
}
项目:health-and-care-developer-network    文件:HazelcastClientSemaphoreTest.java   
@Test
public void testSemaphoreIncreasePermits() {
    SemaphoreConfig semaphoreConfig = new SemaphoreConfig("default", 1);
    Config config = new Config();
    config.addSemaphoreConfig(semaphoreConfig);
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
    HazelcastClient client = newHazelcastClient(instance);
    ISemaphore semaphore = client.getSemaphore("test");
    assertEquals(1, semaphore.availablePermits());
    semaphore.release();
    assertEquals(2, semaphore.availablePermits());
}