Search in sources :

Example 11 with TriggerKey

use of org.quartz.TriggerKey in project opennms by OpenNMS.

the class ReportScheduler method printCurrentSchedule.

private void printCurrentSchedule() {
    try {
        LOG.info("calendarNames: {}", String.join(", ", getScheduler().getCalendarNames().toArray(new String[0])));
        LOG.info("current executing jobs: {}", StringUtils.arrayToCommaDelimitedString(getScheduler().getCurrentlyExecutingJobs().toArray()));
        LOG.info("current job names: {}", getScheduler().getJobKeys(GroupMatcher.<JobKey>groupEquals(JOB_GROUP)).stream().map(JobKey::getName).collect(Collectors.joining(", ")));
        LOG.info("scheduler metadata: {}", getScheduler().getMetaData());
        LOG.info("trigger names: {}", getScheduler().getTriggerKeys(GroupMatcher.<TriggerKey>groupEquals(JOB_GROUP)).stream().map(TriggerKey::getName).collect(Collectors.joining(", ")));
        for (TriggerKey key : getScheduler().getTriggerKeys(GroupMatcher.<TriggerKey>groupEquals(JOB_GROUP))) {
            CronTrigger t = (CronTrigger) getScheduler().getTrigger(key);
            final StringBuilder sb = new StringBuilder("trigger: ");
            sb.append(key.getName());
            sb.append(", calendar name: ");
            sb.append(t.getCalendarName());
            sb.append(", cron expression: ");
            sb.append(t.getCronExpression());
            sb.append(", URL: ");
            sb.append(t.getJobDataMap().get(ReportJob.KEY));
            sb.append(", next fire time: ");
            sb.append(t.getNextFireTime());
            sb.append(", previous fire time: ");
            sb.append(t.getPreviousFireTime());
            sb.append(", time zone: ");
            sb.append(t.getTimeZone());
            sb.append(", priority: ");
            sb.append(t.getPriority());
            LOG.info(sb.toString());
        }
    } catch (Throwable e) {
        LOG.error("printCurrentSchedule: {}", e.getMessage(), e);
    }
}
Also used : TriggerKey(org.quartz.TriggerKey) JobKey(org.quartz.JobKey) CronTrigger(org.quartz.CronTrigger)

Example 12 with TriggerKey

use of org.quartz.TriggerKey in project opennms by OpenNMS.

the class DefaultSchedulerService method getTriggerDescriptions.

/**
 * <p>getTriggerDescriptions</p>
 *
 * @return a {@link java.util.List} object.
 */
@Override
public List<TriggerDescription> getTriggerDescriptions() {
    List<TriggerDescription> triggerDescriptions = new ArrayList<>();
    try {
        Set<TriggerKey> triggerKeys = m_scheduler.getTriggerKeys(GroupMatcher.<TriggerKey>groupEquals(m_triggerGroup));
        for (TriggerKey triggerKey : triggerKeys) {
            TriggerDescription description = new TriggerDescription();
            Trigger trigger = m_scheduler.getTrigger(triggerKey);
            description.setNextFireTime(trigger.getNextFireTime());
            description.setTriggerName(triggerKey.getName());
            description.setReportId((String) trigger.getJobDataMap().get("reportId"));
            description.setDeliveryOptions((DeliveryOptions) trigger.getJobDataMap().get("deliveryOptions"));
            description.setReportParameters(((ReportParameters) trigger.getJobDataMap().get("criteria")).getReportParms());
            triggerDescriptions.add(description);
        }
    } catch (SchedulerException e) {
        LOG.error("exception lretrieving trigger descriptions", e);
    }
    return triggerDescriptions;
}
Also used : TriggerKey(org.quartz.TriggerKey) TriggerDescription(org.opennms.web.svclayer.model.TriggerDescription) Trigger(org.quartz.Trigger) SchedulerException(org.quartz.SchedulerException) ArrayList(java.util.ArrayList)

Example 13 with TriggerKey

use of org.quartz.TriggerKey in project BRFS by zhangnianli.

the class QuartzBaseSchedulers method killTask.

@Override
public boolean killTask(T task) throws Exception {
    TriggerKey triggerKey = TriggerKey.triggerKey(task.getTaskName(), task.getTaskGroupName());
    JobKey jobKey = new JobKey(task.getTaskName(), task.getTaskGroupName());
    Scheduler scheduler = this.ssf.getScheduler(this.instanceName);
    if (!scheduler.isShutdown()) {
        // 1.停止触发器
        scheduler.pauseTrigger(triggerKey);
        // 2.移除触发器
        scheduler.unscheduleJob(triggerKey);
        // 3.删除任务
        // 删除任务
        scheduler.deleteJob(jobKey);
    }
    return true;
}
Also used : TriggerKey(org.quartz.TriggerKey) JobKey(org.quartz.JobKey) Scheduler(org.quartz.Scheduler)

Example 14 with TriggerKey

use of org.quartz.TriggerKey in project herd by FINRAOS.

the class ServiceSpringModuleConfig method quartzScheduler.

/**
 * Gets a Quartz scheduler factory bean that can return a Quartz scheduler.
 *
 * @return the Quartz scheduler factory bean.
 * @throws Exception if the bean couldn't be created.
 */
@Bean
public SchedulerFactoryBean quartzScheduler() throws Exception {
    SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();
    AutowiringQuartzSpringBeanJobFactory jobFactory = new AutowiringQuartzSpringBeanJobFactory();
    jobFactory.setApplicationContext(applicationContext);
    quartzScheduler.setJobFactory(jobFactory);
    // Name our scheduler.
    quartzScheduler.setSchedulerName("herdScheduler");
    // Setup the scheduler to use Spring’s dataSource and transactionManager.
    quartzScheduler.setDataSource(herdDataSource);
    quartzScheduler.setTransactionManager(herdTransactionManager);
    // Create the Quartz tables for JUnits.
    if (shouldCreateQuartzTables()) {
        ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
        resourceDatabasePopulator.addScript(new ClassPathResource("createQuartzTables.sql"));
        // This is what the DataSourceInitializer does.
        DatabasePopulatorUtils.execute(resourceDatabasePopulator, herdDataSource);
    }
    // Set quartz properties. Please note that Spring uses LocalDataSourceJobStore extension of JobStoreCMT.
    Properties quartzProperties = new Properties();
    quartzScheduler.setQuartzProperties(quartzProperties);
    // Configure Main Main Scheduler Properties. The "instance" parameters are needed to manage cluster instances.
    quartzProperties.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, "herdSystemJobScheduler");
    quartzProperties.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_ID, StdSchedulerFactory.AUTO_GENERATE_INSTANCE_ID);
    // Configure ThreadPool.
    quartzProperties.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, "org.quartz.simpl.SimpleThreadPool");
    quartzProperties.setProperty("org.quartz.threadPool.threadCount", configurationHelper.getProperty(ConfigurationValue.SYSTEM_JOBS_THREAD_POOL_THREAD_COUNT));
    quartzProperties.setProperty("org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread", "true");
    // Configure JobStore.
    quartzProperties.setProperty("org.quartz.jobStore.tablePrefix", "QRTZ_");
    quartzProperties.setProperty("org.quartz.jobStore.isClustered", "true");
    quartzProperties.setProperty(ConfigurationValue.QUARTZ_JOBSTORE_DRIVER_DELEGATE_CLASS.getKey(), getQuartzDatabaseDelegateClass());
    // Build a list of system jobs to be scheduled.
    Map<String, AbstractSystemJob> systemJobs = applicationContext.getBeansOfType(AbstractSystemJob.class);
    List<JobDetail> jobDetails = new ArrayList<>();
    List<CronTrigger> triggers = new ArrayList<>();
    for (Map.Entry<String, AbstractSystemJob> entry : systemJobs.entrySet()) {
        // Prepare job detail and trigger for the system job.
        String jobName = entry.getKey();
        AbstractSystemJob systemJob = entry.getValue();
        JobDetail jobDetail = newJob(systemJob.getClass()).withIdentity(jobName).storeDurably().requestRecovery().build();
        TriggerKey jobTriggerKey = TriggerKey.triggerKey(jobName + AbstractSystemJob.CRON_TRIGGER_SUFFIX);
        CronTrigger trigger = newTrigger().withIdentity(jobTriggerKey).forJob(jobName).usingJobData(systemJob.getJobDataMap()).withSchedule(cronSchedule(systemJob.getCronExpression())).build();
        // Add this system job to the list of jobs/triggers to be scheduled.
        jobDetails.add(jobDetail);
        triggers.add(trigger);
    }
    // Schedule the system jobs and set a flag to overwrite already registered jobs that would be read in from the database (persistent job store).
    quartzScheduler.setOverwriteExistingJobs(true);
    quartzScheduler.setJobDetails(jobDetails.toArray(new JobDetail[jobDetails.size()]));
    quartzScheduler.setTriggers(triggers.toArray(new CronTrigger[triggers.size()]));
    return quartzScheduler;
}
Also used : CronTrigger(org.quartz.CronTrigger) AbstractSystemJob(org.finra.herd.service.systemjobs.AbstractSystemJob) ResourceDatabasePopulator(org.springframework.jdbc.datasource.init.ResourceDatabasePopulator) ArrayList(java.util.ArrayList) AutowiringQuartzSpringBeanJobFactory(org.finra.herd.core.AutowiringQuartzSpringBeanJobFactory) Properties(java.util.Properties) ClassPathResource(org.springframework.core.io.ClassPathResource) TriggerKey(org.quartz.TriggerKey) JobDetail(org.quartz.JobDetail) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) Map(java.util.Map) HashMap(java.util.HashMap) ProcessEngineFactoryBean(org.activiti.spring.ProcessEngineFactoryBean) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 15 with TriggerKey

use of org.quartz.TriggerKey in project herd by FINRAOS.

the class SystemJobHelper method runSystemJob.

/**
 * Starts a system job asynchronously.
 *
 * @param jobName the system job name (case-sensitive)
 * @param parameters the list of parameters
 *
 * @throws org.quartz.SchedulerException if fails to schedule the system job
 */
public void runSystemJob(String jobName, List<Parameter> parameters) throws SchedulerException {
    // Validate the system job name.
    AbstractSystemJob systemJob;
    try {
        systemJob = (AbstractSystemJob) applicationContext.getBean(jobName);
    } catch (Exception e) {
        throw new ObjectNotFoundException(String.format("System job with name \"%s\" doesn't exist.", jobName), e);
    }
    // Validate parameters per relative system job.
    systemJob.validateParameters(parameters);
    // Prepare a trigger to run the system job only once.
    TriggerKey triggerKey = TriggerKey.triggerKey(jobName + AbstractSystemJob.RUN_ONCE_TRIGGER_SUFFIX);
    Trigger trigger = newTrigger().withIdentity(triggerKey).forJob(jobName).usingJobData(systemJob.getJobDataMap(parameters)).startNow().build();
    LOGGER.debug(String.format("schedule job with trigger: calendarName: %s, description: %s, endTime: %s, finalFireTime: %s, jobKey: %s, key: %s, " + "misfireInstruction: %s, nextFireTime: %s, previousFireTime: %s, priority: %s, startTime: %s", trigger.getCalendarName(), trigger.getDescription(), trigger.getEndTime(), trigger.getFinalFireTime(), trigger.getJobKey(), trigger.getKey(), trigger.getMisfireInstruction(), trigger.getNextFireTime(), trigger.getPreviousFireTime(), trigger.getPriority(), trigger.getStartTime()));
    // Schedule the system job.
    schedulerFactory.getScheduler().scheduleJob(trigger);
}
Also used : TriggerKey(org.quartz.TriggerKey) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) Trigger(org.quartz.Trigger) AbstractSystemJob(org.finra.herd.service.systemjobs.AbstractSystemJob) ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) SchedulerException(org.quartz.SchedulerException)

Aggregations

TriggerKey (org.quartz.TriggerKey)116 Trigger (org.quartz.Trigger)49 SchedulerException (org.quartz.SchedulerException)34 JobKey (org.quartz.JobKey)33 CronTrigger (org.quartz.CronTrigger)25 JobDetail (org.quartz.JobDetail)22 Scheduler (org.quartz.Scheduler)22 Test (org.junit.Test)18 SimpleTrigger (org.quartz.SimpleTrigger)15 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)11 ArrayList (java.util.ArrayList)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 HashMap (java.util.HashMap)9 Date (java.util.Date)6 List (java.util.List)5 CronScheduleBuilder (org.quartz.CronScheduleBuilder)5 BigDecimal (java.math.BigDecimal)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 ParseException (java.text.ParseException)4