Search in sources :

Example 1 with HBBaseDataCollector

use of org.alfresco.heartbeat.HBBaseDataCollector in project alfresco-repository by Alfresco.

the class QuartzJobSchedulerTest method testUnscheduling.

@Test
public void testUnscheduling() throws Exception {
    final String cron1 = "0 0/1 * * * ?";
    final String cron2 = "0 0/2 * * * ?";
    final String cron3 = "0 0/3 * * * ?";
    final HBBaseDataCollector c1 = new SimpleHBDataCollector("c1", cron1);
    final HBBaseDataCollector c2 = new SimpleHBDataCollector("c2", cron2);
    final HBBaseDataCollector c3 = new SimpleHBDataCollector("c3", cron3);
    // Register 3 collectors with 3 different cron expressions
    hbJobScheduler.scheduleJob(c1);
    hbJobScheduler.scheduleJob(c2);
    hbJobScheduler.scheduleJob(c3);
    // Unschedule 2
    hbJobScheduler.unscheduleJob(c1);
    hbJobScheduler.unscheduleJob(c2);
    // 1 & 2 gone, 3 is still there
    assertFalse(isJobScheduledForCollector(c1.getCollectorId(), scheduler));
    assertFalse(isJobScheduledForCollector(c2.getCollectorId(), scheduler));
    assertTrue(isJobScheduledForCollector(c3.getCollectorId(), scheduler));
}
Also used : HBBaseDataCollector(org.alfresco.heartbeat.HBBaseDataCollector) Test(org.junit.Test)

Example 2 with HBBaseDataCollector

use of org.alfresco.heartbeat.HBBaseDataCollector 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 3 with HBBaseDataCollector

use of org.alfresco.heartbeat.HBBaseDataCollector in project alfresco-repository by Alfresco.

the class NonLockingJob 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);
    ParameterCheck.mandatory(COLLECTOR_KEY, collector);
    ParameterCheck.mandatory(DATA_SENDER_SERVICE_KEY, hbDataSenderService);
    try {
        List<HBData> data = collector.collectData();
        hbDataSenderService.sendData(data);
        if (logger.isDebugEnabled()) {
            logger.debug("Finished collector job. ID:" + collector.getCollectorId());
        }
    } catch (final Exception e) {
        // Log the error but don't rethrow, collector errors are non fatal
        logger.error("Heartbeat failed to collect data for collector ID: " + collector.getCollectorId(), e);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) HBDataSenderService(org.alfresco.heartbeat.datasender.HBDataSenderService) HBBaseDataCollector(org.alfresco.heartbeat.HBBaseDataCollector) HBData(org.alfresco.heartbeat.datasender.HBData) JobExecutionException(org.quartz.JobExecutionException)

Example 4 with HBBaseDataCollector

use of org.alfresco.heartbeat.HBBaseDataCollector in project alfresco-repository by Alfresco.

the class QuartzJobSchedulerTest method testJobsScheduledWithDifferentCronExpressions.

/**
 * Jobs are scheduled with cron expressions provided by collectors
 */
@Test
public void testJobsScheduledWithDifferentCronExpressions() throws Exception {
    final String cron1 = "0 0/1 * * * ?";
    final String cron2 = "0 0/2 * * * ?";
    final String cron3 = "0 0/3 * * * ?";
    final HBBaseDataCollector c1 = new SimpleHBDataCollector("c1", cron1);
    final HBBaseDataCollector c2 = new SimpleHBDataCollector("c2", cron2);
    final HBBaseDataCollector c3 = new SimpleHBDataCollector("c3", cron3);
    final String triggerName1 = "heartbeat-" + c1.getCollectorId() + "-Trigger";
    final String triggerName2 = "heartbeat-" + c2.getCollectorId() + "-Trigger";
    final String triggerName3 = "heartbeat-" + c3.getCollectorId() + "-Trigger";
    // Register 3 collectors with 3 different cron expressions
    hbJobScheduler.scheduleJob(c1);
    hbJobScheduler.scheduleJob(c2);
    hbJobScheduler.scheduleJob(c3);
    String testCron1 = ((CronTrigger) scheduler.getTrigger(new TriggerKey(triggerName1, Scheduler.DEFAULT_GROUP))).getCronExpression();
    String testCron2 = ((CronTrigger) scheduler.getTrigger(new TriggerKey(triggerName2, Scheduler.DEFAULT_GROUP))).getCronExpression();
    String testCron3 = ((CronTrigger) scheduler.getTrigger(new TriggerKey(triggerName3, Scheduler.DEFAULT_GROUP))).getCronExpression();
    assertEquals("Cron expression doesn't match", cron1, testCron1);
    assertEquals("Cron expression doesn't match", cron2, testCron2);
    assertEquals("Cron expression doesn't match", cron3, testCron3);
}
Also used : HBBaseDataCollector(org.alfresco.heartbeat.HBBaseDataCollector) Test(org.junit.Test)

Example 5 with HBBaseDataCollector

use of org.alfresco.heartbeat.HBBaseDataCollector in project alfresco-repository by Alfresco.

the class LockingJobTest method testJobLocking.

@Test
public void testJobLocking() throws Exception {
    HBBaseDataCollector simpleCollector = mock(HBBaseDataCollector.class);
    when(simpleCollector.getCollectorId()).thenReturn("c1");
    when(simpleCollector.getCronExpression()).thenReturn("0 0 0 ? * *");
    // mock the job context
    JobExecutionContext mockJobExecutionContext = mock(JobExecutionContext.class);
    JobDataMap jobDataMap = new JobDataMap();
    jobDataMap.put("collector", simpleCollector);
    jobDataMap.put("hbDataSenderService", mockDataSenderService);
    jobDataMap.put("jobLockService", mockJobLockService);
    JobDetail jobDetail = JobBuilder.newJob().setJobData(jobDataMap).ofType(LockingJob.class).build();
    when(mockJobExecutionContext.getJobDetail()).thenReturn(jobDetail);
    // Simulate job lock service
    String lockToken = "token";
    when(mockJobLockService.getLock(isA(QName.class), anyLong())).thenReturn(// first job gets the lock
    lockToken).thenThrow(// second job doesn't get the lock
    new LockAcquisitionException("", ""));
    // Run two heart beat jobs
    new LockingJob().execute(mockJobExecutionContext);
    new LockingJob().execute(mockJobExecutionContext);
    // Verify that the collector only collects data once, since only one job got the lock
    verify(simpleCollector, Mockito.times(1)).collectData();
    // Verify that data was passed to data sender
    verify(mockDataSenderService, Mockito.times(1)).sendData(any(List.class));
    verify(mockDataSenderService, Mockito.times(0)).sendData(any(HBData.class));
    // Verify that both jobs tried to get the lock
    verify(mockJobLockService, Mockito.times(2)).getLock(any(QName.class), anyLong());
    // Verify that a callback was registered once
    verify(mockJobLockService, Mockito.times(1)).refreshLock(eq(lockToken), any(QName.class), anyLong(), any(JobLockService.JobLockRefreshCallback.class));
}
Also used : JobDataMap(org.quartz.JobDataMap) JobDetail(org.quartz.JobDetail) LockingJob(org.alfresco.heartbeat.jobs.LockingJob) QName(org.alfresco.service.namespace.QName) HBBaseDataCollector(org.alfresco.heartbeat.HBBaseDataCollector) JobExecutionContext(org.quartz.JobExecutionContext) LinkedList(java.util.LinkedList) List(java.util.List) HBData(org.alfresco.heartbeat.datasender.HBData) LockAcquisitionException(org.alfresco.repo.lock.LockAcquisitionException) Test(org.junit.Test)

Aggregations

HBBaseDataCollector (org.alfresco.heartbeat.HBBaseDataCollector)5 Test (org.junit.Test)3 JobDataMap (org.quartz.JobDataMap)3 HBData (org.alfresco.heartbeat.datasender.HBData)2 HBDataSenderService (org.alfresco.heartbeat.datasender.HBDataSenderService)2 LockAcquisitionException (org.alfresco.repo.lock.LockAcquisitionException)2 QName (org.alfresco.service.namespace.QName)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 LockingJob (org.alfresco.heartbeat.jobs.LockingJob)1 JobLockService (org.alfresco.repo.lock.JobLockService)1 JobDetail (org.quartz.JobDetail)1 JobExecutionContext (org.quartz.JobExecutionContext)1 JobExecutionException (org.quartz.JobExecutionException)1