Example usage for java.util.concurrent.locks Lock lock

List of usage examples for java.util.concurrent.locks Lock lock

Introduction

In this page you can find the example usage for java.util.concurrent.locks Lock lock.

Prototype

lock

Source Link

Usage

From source file:com.github.jrh3k5.habitat4j.rest.CachingAccessTokenProvider.java

/**
 * Gets the current, if any, cached access token.
 * /* ww w .j a  va2s.  c o m*/
 * @return An {@link Optional} containing the access token that may be cached; this will be empty if no access token is currently cached.
 */
private Optional<AccessToken> getCurrentAccessToken() {
    final Lock readLock = storeLock.readLock();
    readLock.lock();
    try {
        return Optional.ofNullable(accessTokenStore.getValue());
    } finally {
        readLock.unlock();
    }
}

From source file:com.cip.crane.agent.deploy.UndeploymentThread.java

@Override
public void run() {
    Lock lock = LockHelper.getLock(taskID);
    try {//from www.j  av a2  s  .c  o  m
        lock.lock();
        DeploymentConf conf = (DeploymentConf) cs.getConf(localIp, taskID);
        DeploymentStatus status = (DeploymentStatus) cs.getStatus(localIp, taskID);
        if (status == null || status.getStatus() == DeploymentStatus.DELETE_SUCCESS) {
            return;
        }

        undeployTask(conf, status);
        cs.updateStatus(localIp, taskID, status);
    } catch (Exception e) {
        LOGGER.error(e, e);
    } finally {
        lock.unlock();
    }
}

From source file:org.oesf.eque.services.impl.QueueDispatcherImpl.java

@Override
public Integer allocateNumber() throws NoFreePlaceAvailableException {

    Lock atomic = this.lock;

    try {/*from   w ww .j  a  v  a  2 s  .c o  m*/
        atomic.lock();

        occupiedQueue.add(freeQueue.element());
        Integer allocatedNumber = freeQueue.remove();
        LOG.debug("QDS-34C5F87A3F23: The on-demand allocated number is: \"{}\"", allocatedNumber);

        notifyStateChange(allocatedNumber, LocalDateTime.now(), FREE, OCCUPIED);
        return allocatedNumber;

    } catch (IllegalStateException ex) { // occupiedQueue.add()

        LOG.error("QDS-0EAE6367CEEE: Can not occupy a number.");
        throw new ChangeStateException();

    } catch (NoSuchElementException ex) { // freeQueue .element() | .remove()

        LOG.debug("QDS-3A2DFE3EE673: Allocating a new place number.");
        throw new NoFreePlaceAvailableException();

    } finally {
        atomic.unlock();
    }
}

From source file:com.github.jrh3k5.habitat4j.rest.CachingAccessTokenProvider.java

/**
 * Gets a new access token./* w  w  w  .  j a  va 2s . co m*/
 * 
 * @return An {@link AccessToken} representing the new access token.
 */
private AccessToken getNewAccessToken() {
    final Lock writeLock = storeLock.writeLock();
    writeLock.lock();
    try {
        final AccessToken currentAccessToken = accessTokenStore.getValue();
        if (currentAccessToken != null && !needsRefresh(currentAccessToken)) {
            return currentAccessToken;
        }

        final AccessToken newAccessToken = sourceProvider.getAccessToken();
        accessTokenStore.setValue(newAccessToken);
        return newAccessToken;
    } finally {
        writeLock.unlock();
    }
}

From source file:com.dp.bigdata.taurus.agent.sheduler.KillTaskThread.java

@Override
public void run() {
    Lock lock = LockHelper.getLock(jobInstanceId);
    try {/*from   w ww.j a v  a 2  s . com*/
        lock.lock();
        ScheduleConf conf = (ScheduleConf) cs.getConf(localIp, jobInstanceId);
        ScheduleStatus status = (ScheduleStatus) cs.getStatus(localIp, jobInstanceId);
        killTask(localIp, conf, status);

    } catch (Exception e) {
        LOGGER.error(e, e);
    } finally {
        lock.unlock();
    }
}

From source file:com.cip.crane.agent.deploy.DeploymentThread.java

@Override
public void run() {
    LOGGER.debug("start deploy");
    Lock lock = LockHelper.getLock(task);
    try {//  www. ja  v  a  2s .c o  m
        lock.lock();
        DeploymentConf conf = (DeploymentConf) cs.getConf(localIp, task);
        DeploymentStatus status = (DeploymentStatus) cs.getStatus(localIp, task);
        if (status == null || status.getStatus() == DeploymentStatus.DEPLOY_SUCCESS) {
            return;
        }

        deployTask(conf, status);
        cs.updateStatus(localIp, task, status);
        cs.updateConf(localIp, task, conf);
    } catch (Exception e) {
        LOGGER.error(e, e);
    } finally {
        lock.unlock();
    }
}

From source file:org.nuxeo.nike.UpdateANikeDirectory.java

@OperationMethod
public void run() {
    Session directorySession = null;//from  w w w.  ja  v a2s  . c om
    CoreSession coreSession = null;

    Lock lock = new ReentrantLock();
    lock.lock();
    try {
        directorySession = directoryService.open(directoryName);

        // Delete all entries in the directory
        DocumentModelList entries = directorySession.getEntries();
        for (DocumentModel entry : entries) {
            directorySession.deleteEntry(entry);
        }

        // Query the documents and create unique entries
        coreSession = ctx.getCoreSession();
        String nxql = "SELECT * FROM " + docType;
        nxql += " WHERE ecm:currentLifeCycleState != 'deleted'";
        DocumentModelList allDocs = coreSession.query(nxql);

        List<String> titles = new ArrayList<String>();
        for (DocumentModel oneDoc : allDocs) {
            String title = oneDoc.getTitle();
            if (!titles.contains(title)) {
                titles.add(title);

                Map<String, Object> entry = new HashMap<String, Object>();
                entry.put("id", title);
                entry.put("label", title);
                entry.put("obsolete", 0);
                entry.put("ordering", 10000);

                directorySession.createEntry(entry);
            }
        }
        directorySession.close();

    } catch (Exception e) {
        log.error("Error creating the directory", e);
    } finally {
        lock.unlock();
    }
}

From source file:com.cip.crane.agent.sheduler.KillTaskThread.java

@Override
public void run() {
    Lock lock = LockHelper.getLock(jobInstanceId);
    try {//  w w  w.  j a va 2  s  .c om
        lock.lock();

        ScheduleConf conf = (ScheduleConf) cs.getConf(localIp, jobInstanceId);

        ScheduleStatus status = (ScheduleStatus) cs.getStatus(localIp, jobInstanceId);

        LOGGER.info("KILL TASK THREAD|jobInstanceId:" + jobInstanceId + "| ip:" + localIp + "  |conf:" + conf
                + "  |status:" + status);

        if (conf != null && status != null) {
            killTask(localIp, conf, status);
        }

    } catch (Exception e) {
        LOGGER.error(e, e);
    } finally {
        lock.unlock();
    }
}

From source file:org.springframework.integration.hazelcast.HazelcastLocalInstanceRegistrar.java

private void syncConfigurationMultiMap(HazelcastInstance hazelcastInstance) {
    Lock lock = hazelcastInstance.getLock(SPRING_INTEGRATION_INTERNAL_CLUSTER_LOCK);
    lock.lock();
    try {//from   w ww  . ja  v a  2 s .co m
        MultiMap<SocketAddress, SocketAddress> multiMap = hazelcastInstance
                .getMultiMap(SPRING_INTEGRATION_INTERNAL_CLUSTER_MULTIMAP);
        for (HazelcastInstance localInstance : Hazelcast.getAllHazelcastInstances()) {
            SocketAddress localInstanceSocketAddress = localInstance.getLocalEndpoint().getSocketAddress();
            if (multiMap.size() == 0) {
                multiMap.put(localInstanceSocketAddress, localInstanceSocketAddress);
            } else {
                multiMap.put(multiMap.keySet().iterator().next(), localInstanceSocketAddress);
            }
        }
    } finally {
        lock.unlock();
    }
}

From source file:info.pancancer.arch3.worker.WorkflowRunner.java

/**
 * Get the stdout of the running command.
 *
 * @return//  w ww  .j a va 2 s .co  m
 */
public String getStdOut() {
    String s;
    Lock lock = new ReentrantLock();
    lock.lock();
    try {
        this.outputStream.flush();
        s = this.outputStream.getAllLinesAsString();
    } finally {
        lock.unlock();
    }
    return s;
}