Search in sources :

Example 21 with JobDataMap

use of org.quartz.JobDataMap in project OpenClinica by OpenClinica.

the class XalanTransformJob method executeInternal.

protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
    // need to generate a Locale so that user beans and other things will
    // generate normally
    // TODO make dynamic?
    Locale locale = new Locale("en-US");
    ResourceBundleProvider.updateLocale(locale);
    ResourceBundle pageMessages = ResourceBundleProvider.getPageMessagesBundle();
    JobDataMap dataMap = context.getMergedJobDataMap();
    // get the file information from the job
    String alertEmail = dataMap.getString(EMAIL);
    try {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        // Use the TransformerFactory to instantiate a Transformer that will work with  
        // the stylesheet you specify. This method call also processes the stylesheet
        // into a compiled Templates object.
        java.io.InputStream in = new java.io.FileInputStream(dataMap.getString(XSL_FILE_PATH));
        // tFactory.setAttribute("use-classpath", Boolean.TRUE);
        // tFactory.setErrorListener(new ListingErrorHandler());
        Transformer transformer = tFactory.newTransformer(new StreamSource(in));
        // Use the Transformer to apply the associated Templates object to an XML document
        // (foo.xml) and write the output to a file (foo.out).
        //  System.out.println("--> job starting: ");
        final long start = System.currentTimeMillis();
        transformer.transform(new StreamSource(dataMap.getString(XML_FILE_PATH)), new StreamResult(new FileOutputStream(dataMap.getString(SQL_FILE_PATH))));
        final long done = System.currentTimeMillis() - start;
    // System.out.println("--> job completed in " + done + " ms");
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : Locale(java.util.Locale) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) JobDataMap(org.quartz.JobDataMap) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamSource(javax.xml.transform.stream.StreamSource) FileNotFoundException(java.io.FileNotFoundException) FileOutputStream(java.io.FileOutputStream) ResourceBundle(java.util.ResourceBundle) TransformerException(javax.xml.transform.TransformerException)

Example 22 with JobDataMap

use of org.quartz.JobDataMap in project OpenClinica by OpenClinica.

the class XalanTriggerService method generateXalanTrigger.

public SimpleTrigger generateXalanTrigger(String xslFile, String xmlFile, String sqlFile, int datasetId) {
    Date startDateTime = new Date(System.currentTimeMillis());
    String jobName = xmlFile + datasetId;
    SimpleTrigger simpleTrigger = (SimpleTrigger) newTrigger().forJob(jobName, TRIGGER_GROUP_NAME).startAt(startDateTime).withSchedule(simpleSchedule().withRepeatCount(1).withIntervalInSeconds(100).withMisfireHandlingInstructionNextWithExistingCount());
    // set job data map
    JobDataMap jobDataMap = new JobDataMap();
    // jobDataMap.put(EMAIL, email);
    // jobDataMap.put(USER_ID, userAccount.getId());
    jobDataMap.put(XSL_FILE_PATH, xslFile);
    jobDataMap.put(XML_FILE_PATH, xmlFile);
    jobDataMap.put(SQL_FILE_PATH, sqlFile);
    // jobDataMap.put(DIRECTORY, directory);
    // jobDataMap.put(ExampleSpringJob.LOCALE, locale);
    simpleTrigger.getTriggerBuilder().usingJobData(jobDataMap);
    return simpleTrigger;
}
Also used : JobDataMap(org.quartz.JobDataMap) SimpleTrigger(org.quartz.SimpleTrigger) Date(java.util.Date)

Example 23 with JobDataMap

use of org.quartz.JobDataMap in project karaf by apache.

the class QuartzJobExecutor method execute.

/**
     * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
     */
public void execute(final JobExecutionContext context) throws JobExecutionException {
    final JobDataMap data = context.getJobDetail().getJobDataMap();
    final Object job = data.get(QuartzScheduler.DATA_MAP_OBJECT);
    final Logger logger = (Logger) data.get(QuartzScheduler.DATA_MAP_LOGGER);
    try {
        logger.debug("Executing job {} with name {}", job, data.get(QuartzScheduler.DATA_MAP_NAME));
        if (job instanceof org.apache.karaf.scheduler.Job) {
            @SuppressWarnings("unchecked") final InternalScheduleOptions options = (InternalScheduleOptions) data.get(QuartzScheduler.DATA_MAP_OPTIONS);
            final String name = (String) data.get(QuartzScheduler.DATA_MAP_NAME);
            final JobContext jobCtx = new JobContextImpl(name, options.configuration);
            ((org.apache.karaf.scheduler.Job) job).execute(jobCtx);
        } else if (job instanceof Runnable) {
            ((Runnable) job).run();
        } else {
            logger.error("Scheduled job {} is neither a job nor a runnable.", job);
        }
    } catch (final Throwable t) {
        // there is nothing we can do here, so we just log
        logger.error("Exception during job execution of " + job + " : " + t.getMessage(), t);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) Logger(org.slf4j.Logger) JobContext(org.apache.karaf.scheduler.JobContext) Job(org.quartz.Job)

Example 24 with JobDataMap

use of org.quartz.JobDataMap in project karaf by apache.

the class QuartzScheduler method schedule.

/**
     * Schedule a job
     * @see org.apache.karaf.scheduler.Scheduler#schedule(java.lang.Object, org.apache.karaf.scheduler.ScheduleOptions)
     * @throws SchedulerException if the job can't be scheduled
     * @throws IllegalArgumentException If the preconditions are not met
     */
public void schedule(final Object job, final ScheduleOptions options) throws IllegalArgumentException, SchedulerException {
    this.checkJob(job);
    if (!(options instanceof InternalScheduleOptions)) {
        throw new IllegalArgumentException("Options has not been created via schedule or is null.");
    }
    final InternalScheduleOptions opts = (InternalScheduleOptions) options;
    if (opts.argumentException != null) {
        throw opts.argumentException;
    }
    // as this method might be called from unbind and during
    // unbind a deactivate could happen, we check the scheduler first
    final org.quartz.Scheduler s = this.scheduler;
    if (s == null) {
        throw new IllegalStateException("Scheduler is not available anymore.");
    }
    final String name;
    if (opts.name != null) {
        // if there is already a job with the name, remove it first
        try {
            final JobKey key = JobKey.jobKey(opts.name);
            final JobDetail jobdetail = s.getJobDetail(key);
            if (jobdetail != null) {
                s.deleteJob(key);
                this.logger.debug("Unscheduling job with name {}", opts.name);
            }
        } catch (final SchedulerException ignored) {
        // ignore
        }
        name = opts.name;
    } else {
        name = job.getClass().getName() + ':' + UUID.randomUUID();
        opts.name = name;
    }
    final Trigger trigger = opts.trigger.withIdentity(name).build();
    // create the data map
    final JobDataMap jobDataMap = this.initDataMap(name, job, opts);
    final JobDetail detail = this.createJobDetail(name, jobDataMap, opts.canRunConcurrently);
    this.logger.debug("Scheduling job {} with name {} and trigger {}", job, name, trigger);
    s.scheduleJob(detail, trigger);
}
Also used : JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) JobDataMap(org.quartz.JobDataMap) SchedulerException(org.quartz.SchedulerException) Trigger(org.quartz.Trigger)

Example 25 with JobDataMap

use of org.quartz.JobDataMap in project sling by apache.

the class QuartzJobExecutor method execute.

/**
     * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
     */
@Override
public void execute(final JobExecutionContext context) throws JobExecutionException {
    final JobDataMap data = context.getJobDetail().getJobDataMap();
    final JobDesc desc = new JobDesc(data);
    final Logger logger = (Logger) data.get(QuartzScheduler.DATA_MAP_LOGGER);
    // check run on information
    if (!shouldRun(logger, desc)) {
        return;
    }
    String origThreadName = Thread.currentThread().getName();
    try {
        Thread.currentThread().setName(origThreadName + "-" + desc.name);
        logger.debug("Executing job {}", desc);
        if (desc.job instanceof org.apache.sling.commons.scheduler.Job) {
            @SuppressWarnings("unchecked") final Map<String, Serializable> configuration = (Map<String, Serializable>) data.get(QuartzScheduler.DATA_MAP_CONFIGURATION);
            final JobContext jobCtx = new JobContextImpl(desc.name, configuration);
            ((org.apache.sling.commons.scheduler.Job) desc.job).execute(jobCtx);
        } else if (desc.job instanceof Runnable) {
            ((Runnable) desc.job).run();
        } else {
            logger.error("Scheduled job {} is neither a job nor a runnable: {}", desc);
        }
    } catch (final Throwable t) {
        // if this is a quartz exception, rethrow it
        if (t instanceof JobExecutionException) {
            throw (JobExecutionException) t;
        }
        // there is nothing we can do here, so we just log
        logger.error("Exception during job execution of " + desc + " : " + t.getMessage(), t);
    } finally {
        Thread.currentThread().setName(origThreadName);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) Serializable(java.io.Serializable) Logger(org.slf4j.Logger) JobExecutionException(org.quartz.JobExecutionException) JobContext(org.apache.sling.commons.scheduler.JobContext) Job(org.quartz.Job) JobDataMap(org.quartz.JobDataMap) Map(java.util.Map)

Aggregations

JobDataMap (org.quartz.JobDataMap)56 Trigger (org.quartz.Trigger)21 SchedulerException (org.quartz.SchedulerException)20 JobDetail (org.quartz.JobDetail)18 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)11 Scheduler (org.quartz.Scheduler)10 JobExecutionException (org.quartz.JobExecutionException)9 ApplicationContext (org.springframework.context.ApplicationContext)9 ArrayList (java.util.ArrayList)6 Date (java.util.Date)6 HashMap (java.util.HashMap)6 TriggerBean (org.akaza.openclinica.bean.admin.TriggerBean)6 Command (org.openhab.core.types.Command)5 SchedulerContext (org.quartz.SchedulerContext)5 SimpleTrigger (org.quartz.SimpleTrigger)5 IOException (java.io.IOException)4 InetSocketAddress (java.net.InetSocketAddress)4 SocketChannel (java.nio.channels.SocketChannel)4 Locale (java.util.Locale)4 DatasetBean (org.akaza.openclinica.bean.extract.DatasetBean)4