List of usage examples for java.util.concurrent.locks Lock unlock
void unlock();
From source file:org.soaplab.services.storage.FileStorage.java
/************************************************************************** * **************************************************************************/ protected void saveJobProperties(File jobDir, Properties jobProps) { File propsFile = new File(jobDir, FILE_JOB_PROPS); Lock writelock = getLock(jobDir.getName(), false); writelock.lock();//from w w w. java2 s. c om jobDir.mkdirs(); try { FileOutputStream fos = new FileOutputStream(propsFile); jobProps.store(fos, null); fos.close(); } catch (IOException e) { log.error("Cannot store job properties into " + propsFile.getAbsolutePath() + ": " + e.toString()); } finally { writelock.unlock(); } }
From source file:org.marketcetera.marketdata.core.provider.AbstractMarketDataProvider.java
@Override public int getActiveRequests() { Lock marketdataQueryLock = marketdataLock.readLock(); try {/* w ww . j av a 2s . c o m*/ marketdataQueryLock.lockInterruptibly(); } catch (InterruptedException e) { org.marketcetera.marketdata.core.Messages.UNABLE_TO_ACQUIRE_LOCK.error(this); throw new MarketDataRequestFailed(e); } try { return requestsByAtom.size(); } finally { marketdataQueryLock.unlock(); } }
From source file:com.cloudera.oryx.als.serving.ServerRecommender.java
@Override public float estimateForAnonymous(String toItemID, String[] itemIDs, float[] values) throws NotReadyException, NoSuchItemException { Generation generation = getCurrentGeneration(); LongObjectMap<float[]> Y = generation.getY(); Lock yLock = generation.getYLock().readLock(); float[] toItemFeatures; yLock.lock();//w ww. jav a2 s . c o m try { toItemFeatures = Y.get(StringLongMapping.toLong(toItemID)); } finally { yLock.unlock(); } if (toItemFeatures == null) { throw new NoSuchItemException(toItemID); } float[] anonymousUserFeatures = buildAnonymousUserFeatures(itemIDs, values); return (float) SimpleVectorMath.dot(anonymousUserFeatures, toItemFeatures); }
From source file:org.geotools.gce.imagemosaic.catalog.GTDataStoreGranuleCatalog.java
@Override public BoundingBox getBounds(final String typeName) { final Lock lock = rwLock.readLock(); ReferencedEnvelope bound = null;/* w w w. ja v a 2 s . c o m*/ try { lock.lock(); checkStore(); if (bounds.containsKey(typeName)) { bound = bounds.get(typeName); } else { bound = this.tileIndexStore.getFeatureSource(typeName).getBounds(); bounds.put(typeName, bound); } } catch (IOException e) { LOGGER.log(Level.FINER, e.getMessage(), e); bounds.remove(typeName); } finally { lock.unlock(); } // return bounds; return bound; }
From source file:de.hoegertn.demo.cxfsimple.SpringStarter.java
public final void doStart() throws Exception { try {//from ww w . j a va 2 s .c om this.doBeforeSpringStart(); } catch (Exception e) { throw new RuntimeException("Before spring failed", e); } Lock writeLock = this.rwLock.writeLock(); AbstractXmlApplicationContext ctx = null; try { writeLock.lock(); if (this.context.get() != null) { throw new RuntimeException("Already started"); } ctx = this.createSpringContext(); final PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); configurer.setProperties(System.getProperties()); ctx.addBeanFactoryPostProcessor(configurer); ctx.setConfigLocation(this.getSpringResource()); ctx.refresh(); } catch (Exception e) { if (ctx != null) { try { ctx.close(); } catch (Exception e1) { this.logger.warn("Failed to close context", e1); } ctx = null; } throw new RuntimeException("Spring context failed", e); } finally { if (ctx != null) { this.context.set(ctx); } writeLock.unlock(); } try { this.doAfterSpringStart(); } catch (Exception e) { throw new RuntimeException("After spring failed", e); } }
From source file:com.cloudera.oryx.als.serving.ServerRecommender.java
@Override public void setPreference(String userID, String itemID, float value) { // Record datum try {/*from w w w. jav a 2 s.c o m*/ generationManager.append(userID, itemID, value); } catch (IOException ioe) { log.warn("Could not append datum; continuing", ioe); } Generation generation; try { generation = getCurrentGeneration(); } catch (NotReadyException nre) { // Corner case -- no model ready so all we can do is record (above). Don't fail the request. return; } long longUserID = StringLongMapping.toLong(userID); long longItemID = StringLongMapping.toLong(itemID); float[] userFeatures = getFeatures(longUserID, generation.getX(), generation.getXLock()); boolean newItem; Lock yReadLock = generation.getYLock().readLock(); yReadLock.lock(); try { newItem = generation.getY().get(longItemID) == null; } finally { yReadLock.unlock(); } if (newItem) { generation.getCandidateFilter().addItem(itemID); } float[] itemFeatures = getFeatures(longItemID, generation.getY(), generation.getYLock()); updateFeatures(userFeatures, itemFeatures, value, generation); LongObjectMap<LongSet> knownItemIDs = generation.getKnownItemIDs(); if (knownItemIDs != null) { LongSet userKnownItemIDs; ReadWriteLock knownItemLock = generation.getKnownItemLock(); Lock knownItemReadLock = knownItemLock.readLock(); knownItemReadLock.lock(); try { userKnownItemIDs = knownItemIDs.get(longUserID); if (userKnownItemIDs == null) { userKnownItemIDs = new LongSet(); Lock knownItemWriteLock = knownItemLock.writeLock(); knownItemReadLock.unlock(); knownItemWriteLock.lock(); try { knownItemIDs.put(longUserID, userKnownItemIDs); } finally { knownItemReadLock.lock(); knownItemWriteLock.unlock(); } } } finally { knownItemReadLock.unlock(); } synchronized (userKnownItemIDs) { userKnownItemIDs.add(longItemID); } } }
From source file:com.mg.framework.service.DatabaseAuditServiceImpl.java
public void auditModify(PostUpdateEvent modifyEvent) { if (!isAuditActivated) return;/*from www.j av a2 s. c om*/ try { List<Integer> auditedList; String[] names; Lock lock = entityAuditSetupLock.readLock(); lock.lock(); try { EntityAuditSetup auditSetup = entityAuditSetup.get(modifyEvent.getPersister().getEntityName()); if (auditSetup == null || !auditSetup.isAuditModify()) return; auditedList = new ArrayList<Integer>(); names = modifyEvent.getPersister().getPropertyNames(); if (!auditSetup.isAuditModifyAllProperties()) { for (int i = 0; i < names.length; i++) if (auditSetup.isAuditModifyProperty(names[i])) auditedList.add(i); } else { for (int i = 0; i < names.length; i++) auditedList.add(i); } } finally { lock.unlock(); } if (auditedList.isEmpty()) return; String[] stateStr = new String[auditedList.size()]; String[] oldStateStr = new String[auditedList.size()]; String[] auditedNames = new String[auditedList.size()]; int j = 0; for (int i = 0; i < names.length; i++) { if (!auditedList.contains(i)) continue; Type type = modifyEvent.getPersister().getPropertyType(names[i]); if (type.isCollectionType()) continue; auditedNames[j] = names[i]; if (type.isEntityType()) { ClassMetadata metadata = modifyEvent.getPersister().getFactory() .getClassMetadata(type.getName()); stateStr[j] = entityPropertyToString(modifyEvent.getState()[i], metadata); oldStateStr[j] = entityPropertyToString(modifyEvent.getOldState()[i], metadata); } else { stateStr[j] = modifyEvent.getState()[i] == null ? null : modifyEvent.getState()[i].toString(); oldStateStr[j] = modifyEvent.getOldState()[i] == null ? null : modifyEvent.getOldState()[i].toString(); } j++; } // ?? ? if (j == 0) return; //?? ? ? if (names.length > j) { auditedNames = (String[]) ArrayUtils.subarray(auditedNames, 0, j); stateStr = (String[]) ArrayUtils.subarray(stateStr, 0, j); oldStateStr = (String[]) ArrayUtils.subarray(oldStateStr, 0, j); } sendAuditMessage(new EntityAuditEvent(modifyEvent.getPersister().getEntityName(), DatabaseAuditType.MODIFY, modifyEvent.getId().toString(), modifyEvent.getPersister().getIdentifierPropertyName(), auditedNames, stateStr, oldStateStr)); } catch (Exception e) { logger.error("audit modify failed", e); } }
From source file:net.gbmb.collector.RecordController.java
private Collection registerCollection(Application application, String cid, String mode) throws CollectionStateException { boolean isTest = TEST_KEY.equals(mode); Lock lock = hazelcast.getLock(cid); lock.lock();// w w w .j av a 2 s . c o m try { if (collectionRepository.findByCollectionId(cid) != null) { // collection already existing throw new CollectionStateException(String.format("Collection already existing: %s", cid)); } Collection collection = new Collection(cid); collection.setCreationDate(new Date()); collection.setState(CollectionState.COLLECTING); collection.setCancelable(application.getCancelableCollection()); collection.setTestMode(isTest); if (collectionMap.putIfAbsent(cid, collection) == null) { // no previous record return collection; } else { // already existing throw new CollectionStateException("Already existising: " + cid); } } finally { lock.unlock(); } }
From source file:net.sf.xfd.provider.ProviderBase.java
private boolean isPossiblySpecial(Stat s) { if (s == null) return true; final Lock lock = mounts.getLock(); lock.lock();/*from w ww.jav a 2 s . com*/ try { final MountInfo.Mount mount = mounts.mountMap.get(s.st_dev); if (mount == null || mounts.isVolatile(mount)) { return true; } } finally { lock.unlock(); } return false; }
From source file:org.mule.modules.jmsbatchmessaging.JmsBatchMessagingConnector.java
/** * Consume messages in batches from a JMS destination. * <p/>/*from w w w . j a v a 2 s .c om*/ * {@sample.xml ../../../doc/jms-batch-messaging-connector.xml.sample * jms-batch-messaging:consume} * * @param destinationName The JMS destination to consume messages from * @param amountOfThreads The amount of threads used to consume messages in parallel batches * @param batchSize The size of each batch * @param timeout The timeout, in milliseconds, to wait before releasing a batch that hasn't received its full batch of messages * @param isTopic Whether or not the JMS destination is a topic. * @throws Exception */ @Source public void consume(String destinationName, int amountOfThreads, Boolean isTopic, int batchSize, long timeout, final SourceCallback callback) throws Exception { Lock lock = null; try { lock = muleContext.getLockFactory().createLock("JMS_BATCH_MESSAGING_CONSUMER_LOCK"); lock.lock(); logger.debug(String.format("Starting batch (size=%s) processing with %s threads", batchSize, amountOfThreads)); ExecutorService executorService = Executors.newFixedThreadPool(amountOfThreads); for (int i = 0; i < amountOfThreads; i++) { executorService.execute(new DestinationMessageConsumer(muleContext, batchSize, timeout, callback, connector, destinationName, isTopic, isTransactional)); } executorService.shutdown(); try { executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { logger.debug("Thread interrupted"); } } finally { if (lock != null) { lock.unlock(); } } }