Search in sources :

Example 1 with JobLockService

use of org.alfresco.repo.lock.JobLockService 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)

Example 2 with JobLockService

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

the class AbstractScheduledLockedJob method executeInternal.

@Override
protected final synchronized void executeInternal(final JobExecutionContext jobContext) throws JobExecutionException {
    if (locker == null) {
        JobLockService jobLockServiceBean = (JobLockService) jobContext.getJobDetail().getJobDataMap().get("jobLockService");
        if (jobLockServiceBean == null)
            throw new JobExecutionException("Missing setting for bean jobLockService");
        String name = (String) jobContext.getJobDetail().getJobDataMap().get("name");
        String jobName = name == null ? this.getClass().getSimpleName() : name;
        locker = new ScheduledJobLockExecuter(jobLockServiceBean, jobName, this);
    }
    locker.execute(jobContext);
}
Also used : JobExecutionException(org.quartz.JobExecutionException) JobLockService(org.alfresco.repo.lock.JobLockService)

Example 3 with JobLockService

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

the class AbstractFeedCleanerTest method setUp.

@Before
public void setUp() throws Exception {
    ctx = ApplicationContextHelper.getApplicationContext();
    JobLockService jobLockService = (JobLockService) ctx.getBean("JobLockService");
    PolicyComponent policyComponent = (PolicyComponent) ctx.getBean("policyComponent");
    NodeService nodeService = (NodeService) ctx.getBean("NodeService");
    siteService = (SiteService) ctx.getBean("SiteService");
    personService = (PersonService) ctx.getBean("PersonService");
    feedDAO = (ActivityFeedDAO) ctx.getBean("feedDAO");
    transactionHelper = (RetryingTransactionHelper) ctx.getBean("retryingTransactionHelper");
    nodeArchiveService = (NodeArchiveService) ctx.getBean("nodeArchiveService");
    // Let's shut down the scheduler so that we aren't competing with the scheduled versions of jobs (ie. feed cleaner)
    Scheduler scheduler = (Scheduler) ctx.getBean("schedulerFactory");
    scheduler.shutdown();
    tearDown();
    AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
    for (int i = 1; i <= 7; i++) {
        siteService.createSite("myPreset", "testSite" + i, null, null, SiteVisibility.PUBLIC);
    }
    AuthenticationUtil.setRunAsUserSystem();
    // construct the test cleaner
    cleaner = new FeedCleaner();
    cleaner.setFeedDAO(feedDAO);
    cleaner.setPolicyComponent(policyComponent);
    cleaner.setJobLockService(jobLockService);
    cleaner.setNodeService(nodeService);
}
Also used : PolicyComponent(org.alfresco.repo.policy.PolicyComponent) JobLockService(org.alfresco.repo.lock.JobLockService) Scheduler(org.quartz.Scheduler) NodeService(org.alfresco.service.cmr.repository.NodeService) Before(org.junit.Before)

Aggregations

JobLockService (org.alfresco.repo.lock.JobLockService)3 HBBaseDataCollector (org.alfresco.heartbeat.HBBaseDataCollector)1 HBDataSenderService (org.alfresco.heartbeat.datasender.HBDataSenderService)1 LockAcquisitionException (org.alfresco.repo.lock.LockAcquisitionException)1 PolicyComponent (org.alfresco.repo.policy.PolicyComponent)1 NodeService (org.alfresco.service.cmr.repository.NodeService)1 QName (org.alfresco.service.namespace.QName)1 Before (org.junit.Before)1 JobDataMap (org.quartz.JobDataMap)1 JobExecutionException (org.quartz.JobExecutionException)1 Scheduler (org.quartz.Scheduler)1