Search in sources :

Example 16 with LockAcquisitionException

use of org.alfresco.repo.lock.LockAcquisitionException in project alfresco-repository by Alfresco.

the class LockingJob method execute.

@Override
public void execute(final JobExecutionContext jobexecutioncontext) throws JobExecutionException {
    final JobDataMap dataMap = jobexecutioncontext.getJobDetail().getJobDataMap();
    final HBBaseDataCollector collector = (HBBaseDataCollector) dataMap.get(COLLECTOR_KEY);
    final HBDataSenderService hbDataSenderService = (HBDataSenderService) dataMap.get(DATA_SENDER_SERVICE_KEY);
    final JobLockService jobLockService = (JobLockService) dataMap.get(JOB_LOCK_SERVICE_KEY);
    ParameterCheck.mandatory(COLLECTOR_KEY, collector);
    ParameterCheck.mandatory(DATA_SENDER_SERVICE_KEY, hbDataSenderService);
    ParameterCheck.mandatory(JOB_LOCK_SERVICE_KEY, jobLockService);
    QName lockQname = QName.createQName(NamespaceService.SYSTEM_MODEL_1_0_URI, collector.getCollectorId());
    LockCallback lockCallback = new LockCallback(lockQname);
    try {
        // Get lock
        String lockToken = jobLockService.getLock(lockQname, LOCK_TTL);
        // Register the refresh callback which will keep the lock alive.
        // The lock will not be released manually,
        // instead the job lock service will check the callback (running) flag every LOCK_TTL/2 ms from lock acquisition
        // and release the lock when the flag is set to false.
        jobLockService.refreshLock(lockToken, lockQname, LOCK_TTL, lockCallback);
        if (logger.isDebugEnabled()) {
            logger.debug("Lock acquired: " + lockQname + ": " + lockToken);
        }
        // Collect data and pass it to the data sender service
        collectAndSendDataLocked(collector, hbDataSenderService);
    } catch (LockAcquisitionException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Skipping collect and send data (could not get lock): " + e.getMessage());
        }
    } finally {
        if (logger.isDebugEnabled()) {
            logger.debug("Finished collector job. ID: " + collector.getCollectorId());
        }
        lockCallback.running.set(false);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) HBDataSenderService(org.alfresco.heartbeat.datasender.HBDataSenderService) JobLockService(org.alfresco.repo.lock.JobLockService) QName(org.alfresco.service.namespace.QName) HBBaseDataCollector(org.alfresco.heartbeat.HBBaseDataCollector) LockAcquisitionException(org.alfresco.repo.lock.LockAcquisitionException)

Aggregations

LockAcquisitionException (org.alfresco.repo.lock.LockAcquisitionException)16 JobLockRefreshCallback (org.alfresco.repo.lock.JobLockService.JobLockRefreshCallback)5 QName (org.alfresco.service.namespace.QName)5 List (java.util.List)4 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)4 JobExecutionException (org.quartz.JobExecutionException)4 ArrayList (java.util.ArrayList)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)3 NodeRef (org.alfresco.service.cmr.repository.NodeRef)3 JobDataMap (org.quartz.JobDataMap)3 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 HBBaseDataCollector (org.alfresco.heartbeat.HBBaseDataCollector)2 HBData (org.alfresco.heartbeat.datasender.HBData)2 LockingJob (org.alfresco.heartbeat.jobs.LockingJob)2 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)2 Test (org.junit.Test)2 JobDetail (org.quartz.JobDetail)2 JobExecutionContext (org.quartz.JobExecutionContext)2