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

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

Introduction

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

Prototype

void lockInterruptibly() throws InterruptedException;

Source Link

Document

Acquires the lock unless the current thread is Thread#interrupt interrupted .

Usage

From source file:com.civprod.util.concurrent.locks.CompositeLock.java

@Override
public void lockInterruptibly() throws InterruptedException {
    for (Lock curLock : interLocks) {
        curLock.lockInterruptibly();
    }//from  w w  w  .  java2s . com
}

From source file:be.fgov.kszbcss.rhq.websphere.config.CellConfiguration.java

<T> T execute(SessionAction<T> action) throws JMException, ConnectorException, InterruptedException {
    // Note: a read lock can't be upgraded to a write lock, so we need to acquire a write
    // lock first.
    Lock readLock = sessionLock.readLock();
    Lock writeLock = sessionLock.writeLock();
    writeLock.lockInterruptibly();
    try {//from  w  w  w.  j a v  a 2 s  . c o  m
        if (destroyed) {
            throw new IllegalStateException("Object already destroyed; not accepting any new requests");
        }
        if (session == null) {
            session = new Session("rhq-websphere-plugin", false);
            if (log.isDebugEnabled()) {
                log.debug("New session created: " + session);
            }
        }
        readLock.lockInterruptibly();
    } finally {
        writeLock.unlock();
    }
    if (log.isDebugEnabled()) {
        log.debug("Start executing action " + action + " on session " + session);
    }
    try {
        return action.execute(configService, appManagement, session);
    } finally {
        readLock.unlock();
        if (log.isDebugEnabled()) {
            log.debug("Finished executing action " + action + " on session " + session);
        }
    }
}

From source file:org.codehaus.wadi.core.contextualiser.SerialContextualiser.java

public boolean contextualise(Invocation invocation, Object id, Immoter immoter, boolean exclusiveOnly)
        throws InvocationException {
    Lock invocationLock = collapser.getLock(id);

    // the promotion begins here. allocate a lock and continue...
    try {/*from ww  w. ja va 2 s . c o  m*/
        invocationLock.lockInterruptibly();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new InvocationException(e);
    } catch (Exception e) {
        throw new InvocationException("Could not acquire serialization lock for session " + id, e);
    }

    try {
        // whilst we were waiting for the motionLock, the session in question may have been moved back into memory 
        // somehow. before we proceed, confirm that this has not happened.
        Motable context = lockHandler.acquire(invocation, id);
        if (null != context) {
            try {
                return immoter.contextualise(invocation, id, context);
            } finally {
                lockHandler.release(invocation, context);
            }
        }

        // session was not promoted whilst we were waiting for motionLock. Continue down Contextualiser stack.
        return next.contextualise(invocation, id, immoter, exclusiveOnly);
    } finally {
        invocationLock.unlock();
    }
}

From source file:org.codehaus.wadi.location.partitionmanager.local.BasicLocalPartition.java

public void waitForClientCompletion() {
    Lock writeLock = readWriteLock.writeLock();
    try {//from w w  w . j av  a2 s .  c o  m
        writeLock.lockInterruptibly();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new WADIRuntimeException(e);
    }
}

From source file:org.codehaus.wadi.location.partitionmanager.local.LocalPartitionEvacuateIMToPMAction.java

public void onMessage(Envelope message, EvacuateIMToPM request) {
    Peer newPeer = request.getPeer();/*from  w w  w  . j a  v  a 2s  .c  o  m*/
    Object key = request.getId();
    Location location;
    synchronized (nameToLocation) {
        location = (Location) nameToLocation.get(key);
    }
    boolean success = false;
    Peer oldPeer = null;
    if (location == null) {
        log.warn("evacuate [" + key + "]@[" + newPeer + "] failed; key not in use");
    } else {
        Lock lock = location.getExclusiveLock();
        try {
            lock.lockInterruptibly();
            try {
                oldPeer = location.getSMPeer();
                if (oldPeer == newPeer) {
                    log.warn("evacuate [" + key + "]@[" + newPeer + "] failed; evacuee is already there");
                } else {
                    location.setPeer(newPeer);
                    if (log.isDebugEnabled()) {
                        log.debug("evacuate [" + request.getId() + "] [" + oldPeer + "]->[" + newPeer + "]");
                    }
                    success = true;
                }
            } finally {
                lock.unlock();
            }
        } catch (InterruptedException e) {
            log.error("unexpected interruption waiting to perform relocation: " + key, e);
            Thread.currentThread().interrupt();
        }
    }

    SessionResponseMessage response = new EvacuatePMToIM(success);
    try {
        dispatcher.reply(message, response);
    } catch (MessageExchangeException e) {
        log.warn("See exception", e);
    }
}

From source file:org.codehaus.wadi.location.partitionmanager.local.LocalPartitionMoveIMToPMAction.java

protected void relocateSession(Envelope message, Location location, MoveIMToPM request, String imCorrelationId)
        throws MessageExchangeException {
    Object key = location.getKey();
    // session does exist - we need to ask SM to move it to IM
    Lock lock = location.getExclusiveLock();
    try {/*from   w w w .j  av a2  s.c o m*/
        // ensures that no-one else tries to relocate session whilst we are doing so...
        // wait til we have a lock on Location before retrieving the SM
        lock.lockInterruptibly();
        try {
            doRelocateSession(message, location, request, imCorrelationId);
        } finally {
            lock.unlock();
        }
    } catch (InterruptedException e) {
        log.error("unexpected interruption waiting to perform Session relocation: " + key, e);
        Thread.currentThread().interrupt();
    }
}

From source file:org.marketcetera.marketdata.core.manager.impl.MarketDataManagerImpl.java

@Override
public void setStatus(MarketDataProvider inProvider, ProviderStatus inStatus) {
    Messages.PROVIDER_REPORTS_STATUS.info(this, inProvider.getProviderName(), inStatus);
    Lock statusUpdateLock = requestLockObject.writeLock();
    try {/*w  ww . j a v  a 2 s  . c om*/
        ObjectName providerObjectName = getObjectNameFor(inProvider);
        statusUpdateLock.lockInterruptibly();
        if (inStatus == ProviderStatus.AVAILABLE) {
            // TODO check for duplicate provider name and warn
            addProvider(inProvider);
            if (!mbeanServer.isRegistered(providerObjectName)) {
                mbeanServer.registerMBean((MarketDataProviderMBean) inProvider, providerObjectName);
            }
        } else {
            removeProvider(inProvider);
        }
    } catch (InterruptedException e) {
        throw new MarketDataRequestFailed(e);
    } catch (JMException e) {
        Messages.JMX_REGISTRATION_ERROR.warn(this, e, inProvider.getProviderName());
    } finally {
        statusUpdateLock.unlock();
    }
}

From source file:org.marketcetera.marketdata.core.manager.impl.MarketDataManagerImpl.java

@Override
public long requestMarketData(MarketDataRequest inRequest, ISubscriber inSubscriber) {
    SLF4JLoggerProxy.debug(this, "Received: {}", inRequest);
    // route the request to available providers or to a particular provider
    Collection<MarketDataProvider> successfulProviders = new ArrayList<MarketDataProvider>();
    MarketDataRequestToken token = new Token(inSubscriber, inRequest);
    if (inRequest.getProvider() != null) {
        // a specific provider was requested - use that provider only
        MarketDataProvider provider = getMarketDataProviderForName(inRequest.getProvider());
        if (provider == null) {
            throw new MarketDataProviderNotAvailable();
        }/*from  www . j  a v a 2 s .c  o  m*/
        SLF4JLoggerProxy.debug(this, "Submitting {} to {}", token, provider);
        try {
            provider.requestMarketData(token);
        } catch (RuntimeException e) {
            throw new MarketDataException(e);
        }
        successfulProviders.add(provider);
    } else {
        boolean liveRequestSubmitted = false;
        for (MarketDataProvider provider : getActiveMarketDataProviders()) {
            if (liveRequestSubmitted && provider.getFeedType() != FeedType.LIVE) {
                SLF4JLoggerProxy.debug(this,
                        "Request has been submitted to all live feeds, no more requests will be issued");
                break;
            }
            try {
                SLF4JLoggerProxy.debug(this, "Submitting {} to {} [{}]", token, provider,
                        provider.getFeedType());
                provider.requestMarketData(token);
                successfulProviders.add(provider);
                if (provider.getFeedType() == FeedType.LIVE) {
                    liveRequestSubmitted = true;
                }
            } catch (RuntimeException e) {
                Messages.UNABLE_TO_REQUEST_MARKETDATA.warn(this, e, inRequest, provider.getProviderName());
                // continue to try from the next provider
            }
        }
        if (successfulProviders.isEmpty()) {
            throw new NoMarketDataProvidersAvailable();
        }
    }
    Lock requestLock = requestLockObject.writeLock();
    try {
        requestLock.lockInterruptibly();
        for (MarketDataProvider provider : successfulProviders) {
            providersByToken.put(token, provider);
        }
        tokensByTokenId.put(token.getId(), token);
    } catch (InterruptedException e) {
        Messages.MARKETDATA_REQUEST_INTERRUPTED.warn(this, inRequest);
        throw new MarketDataRequestTimedOut(e);
    } finally {
        requestLock.unlock();
    }
    return token.getId();
}

From source file:org.marketcetera.marketdata.core.manager.impl.MarketDataManagerImpl.java

@Override
public void cancelMarketDataRequest(long inRequestId) {
    SLF4JLoggerProxy.debug(this, "Canceling request {}", inRequestId);
    Lock cancelLock = requestLockObject.writeLock();
    try {//w w  w  . j  a v a2 s  . co  m
        cancelLock.lockInterruptibly();
        MarketDataRequestToken token = tokensByTokenId.remove(inRequestId);
        if (token != null) {
            for (MarketDataProvider provider : providersByToken.removeAll(token)) {
                SLF4JLoggerProxy.debug(this, "Canceling request {} with {}", inRequestId, provider);
                provider.cancelMarketDataRequest(token);
            }
        }
    } catch (InterruptedException ignored) {
    } finally {
        cancelLock.unlock();
    }
}

From source file:org.marketcetera.marketdata.core.provider.AbstractMarketDataProvider.java

@Override
public Event getSnapshot(Instrument inInstrument, Content inContent) {
    Lock snapshotLock = marketdataLock.readLock();
    try {//from w  w w  .ja  va 2  s. com
        snapshotLock.lockInterruptibly();
        MarketdataCacheElement cachedData = cachedMarketdata.get(inInstrument);
        if (cachedData != null) {
            return cachedData.getSnapshot(inContent);
        }
        return null;
    } catch (InterruptedException e) {
        org.marketcetera.marketdata.core.Messages.UNABLE_TO_ACQUIRE_LOCK.error(this);
        stop();
        throw new MarketDataRequestFailed(e);
    } finally {
        snapshotLock.unlock();
    }
}