Search in sources :

Example 1 with RequisitionDef

use of org.opennms.netmgt.config.provisiond.RequisitionDef in project opennms by OpenNMS.

the class ImportSchedulerIT method createJobAndVerifyImportJobFactoryIsRegistered.

@Test
public void createJobAndVerifyImportJobFactoryIsRegistered() throws SchedulerException, InterruptedException {
    RequisitionDef def = m_dao.getDefs().get(0);
    JobDetail detail = new JobDetailImpl("test", ImportScheduler.JOB_GROUP, ImportJob.class, false, false);
    detail.getJobDataMap().put(ImportJob.URL, def.getImportUrlResource());
    detail.getJobDataMap().put(ImportJob.RESCAN_EXISTING, def.getRescanExisting());
    class MyBoolWrapper {

        volatile Boolean m_called = false;

        public Boolean getCalled() {
            return m_called;
        }

        public void setCalled(Boolean called) {
            m_called = called;
        }
    }
    final MyBoolWrapper callTracker = new MyBoolWrapper();
    m_importScheduler.getScheduler().getListenerManager().addTriggerListener(new TriggerListener() {

        @Override
        public String getName() {
            return "TestTriggerListener";
        }

        @Override
        public void triggerComplete(Trigger trigger, JobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode) {
            LOG.info("triggerComplete called on trigger listener");
            callTracker.setCalled(true);
        }

        @Override
        public void triggerFired(Trigger trigger, JobExecutionContext context) {
            LOG.info("triggerFired called on trigger listener");
            Job jobInstance = context.getJobInstance();
            if (jobInstance instanceof ImportJob) {
                Assert.assertNotNull(((ImportJob) jobInstance).getProvisioner());
                Assert.assertTrue(context.getJobDetail().getJobDataMap().containsKey(ImportJob.URL));
                Assert.assertEquals("dns://localhost/localhost", context.getJobDetail().getJobDataMap().get(ImportJob.URL));
                Assert.assertTrue(context.getJobDetail().getJobDataMap().containsKey(ImportJob.RESCAN_EXISTING));
                Assert.assertEquals("dbonly", context.getJobDetail().getJobDataMap().get(ImportJob.RESCAN_EXISTING));
            }
            callTracker.setCalled(true);
        }

        @Override
        public void triggerMisfired(Trigger trigger) {
            LOG.info("triggerMisFired called on trigger listener");
            callTracker.setCalled(true);
        }

        @Override
        public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
            LOG.info("vetoJobExecution called on trigger listener");
            callTracker.setCalled(true);
            return false;
        }
    });
    Calendar testCal = Calendar.getInstance();
    testCal.add(Calendar.SECOND, 5);
    SimpleTriggerImpl trigger = new SimpleTriggerImpl("test", ImportScheduler.JOB_GROUP, testCal.getTime());
    m_importScheduler.getScheduler().scheduleJob(detail, trigger);
    m_importScheduler.start();
    int callCheck = 0;
    while (!callTracker.getCalled() && callCheck++ < 2) {
        Thread.sleep(5000);
    }
//TODO: need to fix the interrupted exception that occurs in the provisioner
}
Also used : SimpleTriggerImpl(org.quartz.impl.triggers.SimpleTriggerImpl) Calendar(java.util.Calendar) TriggerListener(org.quartz.TriggerListener) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) JobDetailImpl(org.quartz.impl.JobDetailImpl) JobExecutionContext(org.quartz.JobExecutionContext) RequisitionDef(org.opennms.netmgt.config.provisiond.RequisitionDef) Job(org.quartz.Job) Test(org.junit.Test)

Example 2 with RequisitionDef

use of org.opennms.netmgt.config.provisiond.RequisitionDef in project opennms by OpenNMS.

the class ImportScheduler method buildImportSchedule.

/**
     * <p>buildImportSchedule</p>
     */
protected void buildImportSchedule() {
    synchronized (m_lock) {
        Iterator<RequisitionDef> it = m_configDao.getDefs().iterator();
        while (it.hasNext()) {
            RequisitionDef def = it.next();
            JobDetail detail = null;
            CronTriggerImpl trigger = null;
            try {
                detail = new JobDetailImpl(def.getImportName().orElse(null), JOB_GROUP, ImportJob.class, false, false);
                detail.getJobDataMap().put(ImportJob.URL, def.getImportUrlResource());
                detail.getJobDataMap().put(ImportJob.RESCAN_EXISTING, def.getRescanExisting());
                trigger = new CronTriggerImpl(def.getImportName().orElse(null), JOB_GROUP, def.getCronSchedule().orElse(null));
                trigger.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING);
                getScheduler().scheduleJob(detail, trigger);
            } catch (ParseException e) {
                LOG.error("buildImportSchedule: {}", e.getLocalizedMessage(), e);
            } catch (SchedulerException e) {
                LOG.error("buildImportSchedule: {}", e.getLocalizedMessage(), e);
            }
        }
    }
    printCurrentSchedule();
}
Also used : JobDetail(org.quartz.JobDetail) SchedulerException(org.quartz.SchedulerException) JobDetailImpl(org.quartz.impl.JobDetailImpl) CronTriggerImpl(org.quartz.impl.triggers.CronTriggerImpl) RequisitionDef(org.opennms.netmgt.config.provisiond.RequisitionDef) ParseException(java.text.ParseException)

Aggregations

RequisitionDef (org.opennms.netmgt.config.provisiond.RequisitionDef)2 JobDetail (org.quartz.JobDetail)2 JobDetailImpl (org.quartz.impl.JobDetailImpl)2 ParseException (java.text.ParseException)1 Calendar (java.util.Calendar)1 Test (org.junit.Test)1 Job (org.quartz.Job)1 JobExecutionContext (org.quartz.JobExecutionContext)1 SchedulerException (org.quartz.SchedulerException)1 Trigger (org.quartz.Trigger)1 TriggerListener (org.quartz.TriggerListener)1 CronTriggerImpl (org.quartz.impl.triggers.CronTriggerImpl)1 SimpleTriggerImpl (org.quartz.impl.triggers.SimpleTriggerImpl)1