Example usage for java.util.concurrent Semaphore release

List of usage examples for java.util.concurrent Semaphore release

Introduction

In this page you can find the example usage for java.util.concurrent Semaphore release.

Prototype

public void release() 

Source Link

Document

Releases a permit, returning it to the semaphore.

Usage

From source file:com.qwazr.search.index.IndexInstance.java

final Object postMappedDocument(final Map<String, Object> document) throws IOException, InterruptedException {
    if (document == null || document.isEmpty())
        return null;
    final Semaphore sem = schema.acquireWriteSemaphore();
    try {/*from   ww w . j  av  a2 s .c o m*/
        schema.checkSize(1);
        RecordsPoster.UpdateMapDocument poster = getDocumentPoster();
        poster.accept(document);
        Object id = poster.ids.isEmpty() ? null : poster.ids.iterator().next();
        nrtCommit();
        return id;
    } finally {
        if (sem != null)
            sem.release();
    }
}

From source file:com.bt.aloha.collections.memory.InMemoryCollectionImpl.java

public void remove(String infoId) {
    Semaphore semaphore = getSemaphores().get(infoId);
    if (semaphore == null)
        return;//from  w  w  w  . j a  v a  2s  .  co  m

    try {
        semaphore.acquire();
    } catch (InterruptedException e) {
        log.error(String.format(FAILED_TO_REMOVE_OBJECT_MESSAGE, infoId, this.getClass().getSimpleName(),
                e.getMessage()));
        throw new CollectionAccessInterruptedException(String.format(FAILED_TO_REMOVE_OBJECT_MESSAGE, infoId,
                this.getClass().getSimpleName(), e.getMessage()), e);
    }
    try {
        if (infos.remove(infoId) != null) {
            getSemaphores().remove(infoId);
            log.info(String.format("Removed info %s", infoId));
        } else
            log.warn(String.format("Failed to find info %s", infoId));
    } finally {
        semaphore.release();
    }
}

From source file:com.qwazr.search.index.IndexInstance.java

final <T> Object postDocument(final Map<String, Field> fields, final T document)
        throws IOException, InterruptedException {
    if (document == null)
        return null;
    final Semaphore sem = schema.acquireWriteSemaphore();
    try {//from  w ww .j  ava2 s .c om
        schema.checkSize(1);
        RecordsPoster.UpdateObjectDocument poster = getDocumentPoster(fields);
        poster.accept(document);
        Object id = poster.ids.isEmpty() ? null : poster.ids.iterator().next();
        nrtCommit();
        return id;
    } finally {
        if (sem != null)
            sem.release();
    }
}

From source file:com.qwazr.search.index.IndexInstance.java

final ResultDefinition search(final QueryDefinition queryDefinition,
        ResultDocumentBuilder.BuilderFactory<?> documentBuilderFactory) throws IOException,
        InterruptedException, ParseException, ReflectiveOperationException, QueryNodeException {
    final Semaphore sem = schema.acquireReadSemaphore();
    try {/*from w ww.  j a va  2 s.  c  o  m*/
        final IndexSearcher indexSearcher = searcherManager.acquire();
        try {
            return QueryUtils.search(buildQueryContext(indexSearcher, queryDefinition), documentBuilderFactory);
        } finally {
            searcherManager.release(indexSearcher);
        }
    } finally {
        if (sem != null)
            sem.release();
    }
}

From source file:com.parse.ParsePushTest.java

@Test
public void testSubscribeInBackgroundWithCallbackSuccess() throws Exception {
    final ParsePushChannelsController controller = mock(ParsePushChannelsController.class);
    when(controller.subscribeInBackground(anyString())).thenReturn(Task.<Void>forResult(null));
    ParseCorePlugins.getInstance().registerPushChannelsController(controller);

    ParsePush push = new ParsePush();
    final Semaphore done = new Semaphore(0);
    final Capture<Exception> exceptionCapture = new Capture<>();
    push.subscribeInBackground("test", new SaveCallback() {
        @Override/*from ww  w  . j  a  va2 s . co m*/
        public void done(ParseException e) {
            exceptionCapture.set(e);
            done.release();
        }
    });
    assertNull(exceptionCapture.get());
    assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
    verify(controller, times(1)).subscribeInBackground("test");
}

From source file:com.parse.ParsePushTest.java

@Test
public void testSubscribeInBackgroundWithCallbackFail() throws Exception {
    ParsePushChannelsController controller = mock(ParsePushChannelsController.class);
    final ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error");
    when(controller.subscribeInBackground(anyString())).thenReturn(Task.<Void>forError(exception));
    ParseCorePlugins.getInstance().registerPushChannelsController(controller);

    ParsePush push = new ParsePush();
    final Semaphore done = new Semaphore(0);
    final Capture<Exception> exceptionCapture = new Capture<>();
    push.subscribeInBackground("test", new SaveCallback() {
        @Override/*from   www.j a v a2  s. c o  m*/
        public void done(ParseException e) {
            exceptionCapture.set(e);
            done.release();
        }
    });
    assertSame(exception, exceptionCapture.get());
    assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
    verify(controller, times(1)).subscribeInBackground("test");
}

From source file:com.parse.ParsePushTest.java

@Test
public void testUnsubscribeInBackgroundWithCallbackSuccess() throws Exception {
    final ParsePushChannelsController controller = mock(ParsePushChannelsController.class);
    when(controller.unsubscribeInBackground(anyString())).thenReturn(Task.<Void>forResult(null));
    ParseCorePlugins.getInstance().registerPushChannelsController(controller);

    ParsePush push = new ParsePush();
    final Semaphore done = new Semaphore(0);
    final Capture<Exception> exceptionCapture = new Capture<>();
    push.unsubscribeInBackground("test", new SaveCallback() {
        @Override//from   www . j  a v  a  2s.  co m
        public void done(ParseException e) {
            exceptionCapture.set(e);
            done.release();
        }
    });
    assertNull(exceptionCapture.get());
    assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
    verify(controller, times(1)).unsubscribeInBackground("test");
}

From source file:com.parse.ParsePushTest.java

@Test
public void testUnsubscribeInBackgroundWithCallbackFail() throws Exception {
    ParsePushChannelsController controller = mock(ParsePushChannelsController.class);
    final ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error");
    when(controller.unsubscribeInBackground(anyString())).thenReturn(Task.<Void>forError(exception));
    ParseCorePlugins.getInstance().registerPushChannelsController(controller);

    ParsePush push = new ParsePush();
    final Semaphore done = new Semaphore(0);
    final Capture<Exception> exceptionCapture = new Capture<>();
    push.unsubscribeInBackground("test", new SaveCallback() {
        @Override/* w  ww  .ja  v  a2s  . c om*/
        public void done(ParseException e) {
            exceptionCapture.set(e);
            done.release();
        }
    });
    assertSame(exception, exceptionCapture.get());
    assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
    verify(controller, times(1)).unsubscribeInBackground("test");
}

From source file:com.thoughtworks.go.agent.bootstrapper.AgentBootstrapperTest.java

@Test
public void shouldNotDieWhenInvocationOfLauncherRaisesException_butCreationOfLauncherWentThrough()
        throws InterruptedException {
    final Semaphore waitForLauncherInvocation = new Semaphore(1);
    waitForLauncherInvocation.acquire();
    final AgentBootstrapper bootstrapper = new AgentBootstrapper() {
        @Override// w  ww  .jav a2  s  .c  om
        AgentLauncherCreator getLauncherCreator() {
            return new AgentLauncherCreator() {
                public AgentLauncher createLauncher() {
                    return new AgentLauncher() {
                        public int launch(AgentLaunchDescriptor descriptor) {
                            try {
                                throw new RuntimeException("fail!!! i say.");
                            } finally {
                                if (waitForLauncherInvocation.availablePermits() == 0) {
                                    waitForLauncherInvocation.release();
                                }
                            }
                        }

                    };
                }

                @Override
                public void close() {
                }
            };
        }
    };

    final AgentBootstrapper spyBootstrapper = stubJVMExit(bootstrapper);

    Thread stopLoopThd = new Thread(new Runnable() {
        public void run() {
            try {
                waitForLauncherInvocation.acquire();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            ReflectionUtil.setField(spyBootstrapper, "loop", false);
        }
    });
    stopLoopThd.start();
    try {
        spyBootstrapper.go(true,
                new AgentBootstrapperArgs(new URL("http://" + "ghost-name" + ":" + 3518 + "/go"), null,
                        AgentBootstrapperArgs.SslMode.NONE));
        stopLoopThd.join();
    } catch (Exception e) {
        fail("should not have propagated exception thrown while invoking the launcher");
    }
}

From source file:com.parse.ParsePushTest.java

@Test
public void testSendInBackgroundWithCallbackSuccess() throws Exception {
    // Mock controller
    ParsePushController controller = mock(ParsePushController.class);
    when(controller.sendInBackground(any(ParsePush.State.class), anyString()))
            .thenReturn(Task.<Void>forResult(null));
    ParseCorePlugins.getInstance().registerPushController(controller);

    // Make sample ParsePush data and call method
    ParsePush push = new ParsePush();
    JSONObject data = new JSONObject();
    data.put("key", "value");
    List<String> channels = new ArrayList<>();
    channels.add("test");
    channels.add("testAgain");
    push.builder.expirationTime((long) 1000).data(data).pushToIOS(true).channelSet(channels);
    final Semaphore done = new Semaphore(0);
    final Capture<Exception> exceptionCapture = new Capture<>();
    push.sendInBackground(new SendCallback() {
        @Override/*w w w .  j a va2s .c  om*/
        public void done(ParseException e) {
            exceptionCapture.set(e);
            done.release();
        }
    });

    // Make sure controller is executed and state parameter is correct
    assertNull(exceptionCapture.get());
    assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
    ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class);
    verify(controller, times(1)).sendInBackground(stateCaptor.capture(), anyString());
    ParsePush.State state = stateCaptor.getValue();
    assertTrue(state.pushToIOS());
    assertEquals(data, state.data(), JSONCompareMode.NON_EXTENSIBLE);
    assertEquals(2, state.channelSet().size());
    assertTrue(state.channelSet().contains("test"));
    assertTrue(state.channelSet().contains("testAgain"));
}