@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()); }
@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()); }
@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()); }
@Override public void getLockWithTimeout(String name, long timeout, Handler<AsyncResult<Lock>> resultHandler) { ContextImpl context = (ContextImpl) vertx.getOrCreateContext(); // Ordered on the internal blocking executor context.executeBlocking(() -> { ISemaphore iSemaphore = hazelcast.getSemaphore(LOCK_SEMAPHORE_PREFIX + name); boolean locked = false; long remaining = timeout; do { long start = System.nanoTime(); try { locked = iSemaphore.tryAcquire(remaining, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { // OK continue } remaining = remaining - MILLISECONDS.convert(System.nanoTime() - start, NANOSECONDS); } while (!locked && remaining > 0); if (locked) { return new HazelcastLock(iSemaphore); } else { throw new VertxException("Timed out waiting to get lock " + name); } }, resultHandler); }
@Override public ISemaphore getISemaphore(String name) { name = Objects.requireNonNull(name); final ISemaphore valu = getBeanSafely(name, ISemaphore.class); if (null != valu) { return valu; } return hz().getSemaphore(name); }
@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()); }
@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()); }
@Override public ISemaphore getSemaphore(String s) { return null; }
public HazelcastSemaphore(ISemaphore delegate) { this.delegate = delegate; }
@Override public ISemaphore getSemaphore(final String name) { return getHazelcastInstance().getSemaphore(name); }
public SemaphoreMBean(ISemaphore managedObject, ManagementService managementService) { super(managedObject, managementService); }
private HazelcastLock(ISemaphore semaphore) { this.semaphore = semaphore; }
public ISemaphore getISemaphore(String name);