Search in sources :

Example 1 with ObjectAlreadyExistsException

use of org.quartz.ObjectAlreadyExistsException in project wso2-synapse by wso2.

the class QuartzTaskManager method schedule.

@Override
public boolean schedule(TaskDescription taskDescription) {
    assertInitialized();
    assertStarted();
    if (taskDescription == null) {
        throw new SynapseTaskException("Task Description cannot be found", logger);
    }
    Trigger trigger;
    JobDetail jobDetail;
    synchronized (lock) {
        if (triggerFactory == null) {
            throw new SynapseTaskException("TriggerFactory cannot be found", logger);
        }
        if (jobDetailFactory == null) {
            throw new SynapseTaskException("JobDetailFactory cannot be found", logger);
        }
        trigger = triggerFactory.createTrigger(taskDescription);
        if (trigger == null) {
            throw new SynapseTaskException("Trigger cannot be created from : " + taskDescription, logger);
        }
        jobDetail = jobDetailFactory.createJobDetail(taskDescription, taskDescription.getResources(), SimpleQuartzJob.class);
        if (jobDetail == null) {
            throw new SynapseTaskException("JobDetail cannot be created from : " + taskDescription + " and job class " + taskDescription.getTaskImplClassName(), logger);
        }
    }
    Object clsInstance = taskDescription.getResource(TaskDescription.INSTANCE);
    if (clsInstance == null) {
        String className = (String) taskDescription.getProperty(TaskDescription.CLASSNAME);
        try {
            clsInstance = Class.forName(className).newInstance();
            if (clsInstance instanceof ManagedLifecycle) {
                Object se = properties.get(TaskConstants.SYNAPSE_ENV);
                if (!(se instanceof SynapseEnvironment)) {
                    return false;
                }
                ((ManagedLifecycle) clsInstance).init((SynapseEnvironment) se);
            }
            for (Object property : taskDescription.getXmlProperties()) {
                OMElement prop = (OMElement) property;
                logger.debug("Found Property : " + prop.toString());
                PropertyHelper.setStaticProperty(prop, clsInstance);
            }
        } catch (ClassNotFoundException e) {
            logger.error("Could not schedule task[" + name + "].", e);
            return false;
        } catch (InstantiationException e) {
            logger.error("Could not schedule task[" + name + "].", e);
            return false;
        } catch (IllegalAccessException e) {
            logger.error("Could not schedule task[" + name + "].", e);
            return false;
        }
    }
    if (!(clsInstance instanceof Task)) {
        logger.error("Could not schedule task[" + name + "]. Cannot load class " + "org.apache.synapse.startup.quartz.SimpleQuartzJob");
        return false;
    }
    jobDetail.getJobDataMap().put(TaskDescription.INSTANCE, clsInstance);
    jobDetail.getJobDataMap().put(TaskDescription.CLASSNAME, clsInstance.getClass().toString());
    jobDetail.getJobDataMap().put(TaskConstants.SYNAPSE_ENV, getProperty(TaskConstants.SYNAPSE_ENV));
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("scheduling job : " + jobDetail + " with trigger " + trigger);
        }
        if (taskDescription.getCount() != 0 && !isTaskRunning(jobDetail.getKey())) {
            try {
                synchronized (lock) {
                    scheduler.scheduleJob(jobDetail, trigger);
                }
            } catch (ObjectAlreadyExistsException e) {
                logger.warn("did not schedule the job : " + jobDetail + ". the job is already running.");
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("did not schedule the job : " + jobDetail + ". count is zero.");
            }
        }
    } catch (SchedulerException e) {
        throw new SynapseTaskException("Error scheduling job : " + jobDetail + " with trigger " + trigger);
    }
    logger.info("Scheduled task [" + taskDescription.getName() + "::" + taskDescription.getTaskGroup() + "]");
    return true;
}
Also used : Task(org.apache.synapse.task.Task) SchedulerException(org.quartz.SchedulerException) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) OMElement(org.apache.axiom.om.OMElement) SynapseTaskException(org.apache.synapse.task.SynapseTaskException) ManagedLifecycle(org.apache.synapse.ManagedLifecycle) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException)

Example 2 with ObjectAlreadyExistsException

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

the class SearchIndexUpdateHelper method processMessage.

/**
 * Processes a message by adding it to the database "queue" table to ultimately be placed on the real queue by a separate job.
 *
 * @param messageText the message text to place on the queue
 */
private void processMessage(String messageText) {
    boolean isSearchIndexUpdateSqsNotificationEnabled = Boolean.valueOf(configurationHelper.getProperty(ConfigurationValue.SEARCH_INDEX_UPDATE_JMS_LISTENER_ENABLED));
    LOGGER.info("searchIndexUpdateSqsNotificationEnabled={} messageText={}", isSearchIndexUpdateSqsNotificationEnabled, messageText);
    // Only process messages if the service is enabled.
    if (isSearchIndexUpdateSqsNotificationEnabled) {
        // Add the message to the database queue if a message was configured. Otherwise, log a warning.
        if (StringUtils.isBlank(messageText)) {
            LOGGER.warn("Not sending search index update message because it is not configured.");
        } else {
            NotificationMessage notificationMessage = new NotificationMessage(MessageTypeEntity.MessageEventTypes.SQS.name(), getSqsQueueName(), messageText, null);
            // Add the notification message to the database JMS message queue to be processed.
            notificationMessagePublishingService.addNotificationMessageToDatabaseQueue(notificationMessage);
            // Schedule JMS publishing job.
            try {
                systemJobHelper.runSystemJob(JmsPublishingJob.JOB_NAME, null);
            } catch (ObjectAlreadyExistsException objectAlreadyExistsException) {
                // Ignore the error when job is already running.
                LOGGER.info("Failed to schedule JMS publishing job: ObjectAlreadyExistsException occurred");
            } catch (Exception e) {
                LOGGER.error("Failed to schedule JMS publishing job.", e);
            }
        }
    }
}
Also used : NotificationMessage(org.finra.herd.model.dto.NotificationMessage) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException)

Example 3 with ObjectAlreadyExistsException

use of org.quartz.ObjectAlreadyExistsException in project kylo by Teradata.

the class NifiStatsJmsReceiver method scheduleStatsCompaction.

/**
 * Schedule the compaction job in Quartz if the properties have this enabled with a Cron Expression
 */
private void scheduleStatsCompaction() {
    if (compactStatsEnabled && StringUtils.isNotBlank(compactStatsCronSchedule)) {
        QuartzScheduler scheduler = (QuartzScheduler) jobScheduler;
        JobIdentifier jobIdentifier = new DefaultJobIdentifier("Compact NiFi Processor Stats", "KYLO");
        TriggerIdentifier triggerIdentifier = new DefaultTriggerIdentifier(jobIdentifier.getName(), jobIdentifier.getGroup());
        try {
            scheduler.scheduleJob(jobIdentifier, triggerIdentifier, NiFiStatsCompactionQuartzJobBean.class, compactStatsCronSchedule, null);
        } catch (ObjectAlreadyExistsException e) {
            log.info("Unable to schedule the job to compact the NiFi processor stats.  It already exists.  Most likely another Kylo node has already schceduled this job. ");
        } catch (SchedulerException e) {
            throw new RuntimeException("Error scheduling job: Compact NiFi Processor Stats", e);
        }
    }
}
Also used : SchedulerException(org.quartz.SchedulerException) DefaultTriggerIdentifier(com.thinkbiganalytics.scheduler.model.DefaultTriggerIdentifier) QuartzScheduler(com.thinkbiganalytics.scheduler.QuartzScheduler) DefaultJobIdentifier(com.thinkbiganalytics.scheduler.model.DefaultJobIdentifier) JobIdentifier(com.thinkbiganalytics.scheduler.JobIdentifier) DefaultTriggerIdentifier(com.thinkbiganalytics.scheduler.model.DefaultTriggerIdentifier) TriggerIdentifier(com.thinkbiganalytics.scheduler.TriggerIdentifier) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException) DefaultJobIdentifier(com.thinkbiganalytics.scheduler.model.DefaultJobIdentifier)

Example 4 with ObjectAlreadyExistsException

use of org.quartz.ObjectAlreadyExistsException in project camel by apache.

the class QuartzEndpoint method addJobInScheduler.

private void addJobInScheduler() throws Exception {
    // Add or use existing trigger to/from scheduler
    Scheduler scheduler = getComponent().getScheduler();
    JobDetail jobDetail;
    Trigger oldTrigger = scheduler.getTrigger(triggerKey);
    boolean triggerExisted = oldTrigger != null;
    if (triggerExisted && !isRecoverableJob()) {
        ensureNoDupTriggerKey();
    }
    jobDetail = createJobDetail();
    Trigger trigger = createTrigger(jobDetail);
    QuartzHelper.updateJobDataMap(getCamelContext(), jobDetail, getEndpointUri(), isUsingFixedCamelContextName());
    if (triggerExisted) {
        // Reschedule job if trigger settings were changed
        if (hasTriggerChanged(oldTrigger, trigger)) {
            scheduler.rescheduleJob(triggerKey, trigger);
        }
    } else {
        try {
            // Schedule it now. Remember that scheduler might not be started it, but we can schedule now.
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (ObjectAlreadyExistsException ex) {
            // some other VM might may have stored the job & trigger in DB in clustered mode, in the mean time
            if (!(getComponent().isClustered())) {
                throw ex;
            } else {
                trigger = scheduler.getTrigger(triggerKey);
                if (trigger == null) {
                    throw new SchedulerException("Trigger could not be found in quartz scheduler.");
                }
            }
        }
    }
    if (LOG.isInfoEnabled()) {
        LOG.info("Job {} (triggerType={}, jobClass={}) is scheduled. Next fire date is {}", new Object[] { trigger.getKey(), trigger.getClass().getSimpleName(), jobDetail.getJobClass().getSimpleName(), trigger.getNextFireTime() });
    }
    // Increase camel job count for this endpoint
    AtomicInteger number = (AtomicInteger) scheduler.getContext().get(QuartzConstants.QUARTZ_CAMEL_JOBS_COUNT);
    if (number != null) {
        number.incrementAndGet();
    }
    jobAdded.set(true);
}
Also used : JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SimpleTrigger(org.quartz.SimpleTrigger) CronTrigger(org.quartz.CronTrigger) SchedulerException(org.quartz.SchedulerException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Scheduler(org.quartz.Scheduler) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException)

Example 5 with ObjectAlreadyExistsException

use of org.quartz.ObjectAlreadyExistsException in project jbpm by kiegroup.

the class QuartzSchedulerService method internalSchedule.

@Override
public void internalSchedule(TimerJobInstance timerJobInstance) {
    GlobalQuartzJobHandle quartzJobHandle = (GlobalQuartzJobHandle) timerJobInstance.getJobHandle();
    // Define job instance
    Map<String, Object> dataMap = new HashMap<>();
    dataMap.put("timerJobInstance", timerJobInstance);
    JobDetail jobq = newJob(QuartzJob.class).withIdentity(quartzJobHandle.getJobName(), quartzJobHandle.getJobGroup()).requestRecovery().usingJobData(new JobDataMap(dataMap)).build();
    // Amend nextFireTime not to schedule older than now + RESCHEDULE_DELAY
    Date nextFireTime = timerJobInstance.getTrigger().hasNextFireTime();
    Date threshold = new Date(System.currentTimeMillis() + RESCHEDULE_DELAY);
    if (nextFireTime.before(threshold)) {
        logger.debug("nextFireTime [" + nextFireTime + "] is older than (now + RESCHEDULE_DELAY). Amending it to [" + threshold + "]");
        nextFireTime = threshold;
    }
    // Define a Trigger that will fire "now"
    org.quartz.Trigger triggerq = newTrigger().withIdentity(quartzJobHandle.getJobName() + "_trigger", quartzJobHandle.getJobGroup()).startAt(nextFireTime).build();
    // nextFireTime is mapped to startTime
    logger.debug("triggerq.name = {}, triggerq.startTime = {}", triggerq.getKey().getName(), triggerq.getStartTime());
    // Schedule the job with the trigger
    try {
        if (scheduler.isShutdown()) {
            return;
        }
        globalTimerService.getTimerJobFactoryManager().addTimerJobInstance(timerJobInstance);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey(quartzJobHandle.getJobName(), quartzJobHandle.getJobGroup()));
        if (jobDetail == null) {
            scheduler.scheduleJob(jobq, triggerq);
        } else {
            // need to add the job again to replace existing especially important if jobs are persisted in db
            scheduler.addJob(jobq, true, true);
            scheduler.rescheduleJob(triggerq.getKey(), triggerq);
        }
    } catch (ObjectAlreadyExistsException e) {
        // in general this should not happen even in clustered environment but just in case
        // already registered jobs should be caught in scheduleJob but due to race conditions it might not
        // catch it in time - clustered deployments only
        logger.warn("Job has already been scheduled, most likely running in cluster: {}", e.getMessage());
    } catch (JobPersistenceException e) {
        if (e.getCause() instanceof NotSerializableException) {
            // in case job cannot be persisted, like rule timer then make it in memory
            internalSchedule(new InmemoryTimerJobInstanceDelegate(quartzJobHandle.getJobName(), ((GlobalTimerService) globalTimerService).getTimerServiceId()));
        } else {
            globalTimerService.getTimerJobFactoryManager().removeTimerJobInstance(timerJobInstance);
            throw new RuntimeException(e);
        }
    } catch (SchedulerException e) {
        globalTimerService.getTimerJobFactoryManager().removeTimerJobInstance(timerJobInstance);
        throw new RuntimeException("Exception while scheduling job", e);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) SchedulerException(org.quartz.SchedulerException) HashMap(java.util.HashMap) JobPersistenceException(org.quartz.JobPersistenceException) Date(java.util.Date) JobDetail(org.quartz.JobDetail) NotSerializableException(java.io.NotSerializableException) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException)

Aggregations

ObjectAlreadyExistsException (org.quartz.ObjectAlreadyExistsException)6 JobDetail (org.quartz.JobDetail)4 SchedulerException (org.quartz.SchedulerException)4 Trigger (org.quartz.Trigger)2 JobIdentifier (com.thinkbiganalytics.scheduler.JobIdentifier)1 QuartzScheduler (com.thinkbiganalytics.scheduler.QuartzScheduler)1 TriggerIdentifier (com.thinkbiganalytics.scheduler.TriggerIdentifier)1 DefaultJobIdentifier (com.thinkbiganalytics.scheduler.model.DefaultJobIdentifier)1 DefaultTriggerIdentifier (com.thinkbiganalytics.scheduler.model.DefaultTriggerIdentifier)1 NotSerializableException (java.io.NotSerializableException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 OMElement (org.apache.axiom.om.OMElement)1 ManagedLifecycle (org.apache.synapse.ManagedLifecycle)1 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)1 SynapseTaskException (org.apache.synapse.task.SynapseTaskException)1 Task (org.apache.synapse.task.Task)1 NotificationMessage (org.finra.herd.model.dto.NotificationMessage)1 CronTrigger (org.quartz.CronTrigger)1