Search in sources :

Example 61 with JobDetail

use of org.quartz.JobDetail in project Dempsy by Dempsy.

the class RelativeOutputSchedule method start.

/**
 * Container will invoke this method.
 */
@Override
public void start(final Infrastructure infra) {
    try {
        final OutputQuartzHelper outputQuartzHelper = new OutputQuartzHelper();
        final JobDetail jobDetail = outputQuartzHelper.getJobDetail(outputInvoker);
        final Trigger trigger = outputQuartzHelper.getSimpleTrigger(timeUnit, (int) interval);
        scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
    } catch (final SchedulerException se) {
        LOGGER.error("Error occurred while starting the relative scheduler : " + se.getMessage(), se);
    }
}
Also used : JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SchedulerException(org.quartz.SchedulerException)

Example 62 with JobDetail

use of org.quartz.JobDetail in project Dempsy by Dempsy.

the class CronOutputSchedule method start.

/*
     * (non-Javadoc)
     * 
     * @see com.nokia.dempsy.output.OutputExecuter#start()
     */
@Override
public void start(final Infrastructure infra) {
    try {
        final OutputQuartzHelper outputQuartzHelper = new OutputQuartzHelper();
        final JobDetail jobDetail = outputQuartzHelper.getJobDetail(outputInvoker);
        final Trigger trigger = outputQuartzHelper.getCronTrigger(cronExpression);
        scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
    } catch (final SchedulerException se) {
        LOGGER.error("Error occurred while starting the cron scheduler : " + se.getMessage(), se);
    }
}
Also used : JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) SchedulerException(org.quartz.SchedulerException)

Example 63 with JobDetail

use of org.quartz.JobDetail in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManager method createTask.

@Override
public Task createTask(Task task) {
    Assert.notNull(task);
    Assert.notNull(task.getInstanceId());
    Assert.notNull(task.getTaskType());
    // 
    try {
        // task id
        String taskId = Key.createUniqueName(DEFAULT_GROUP_NAME);
        // description
        String description = task.getDescription();
        if (StringUtils.isEmpty(description)) {
            description = AutowireHelper.getBeanDescription(task.getTaskType());
        }
        // job properties
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put(SchedulableTaskExecutor.PARAMETER_INSTANCE_ID, task.getInstanceId());
        task.getParameters().entrySet().forEach(entry -> {
            jobDataMap.put(entry.getKey(), entry.getValue());
        });
        // validate init method
        try {
            LongRunningTaskExecutor<?> taskExecutor = AutowireHelper.createBean(task.getTaskType());
            taskExecutor.init(jobDataMap);
        } catch (ResultCodeException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new ResultCodeException(CoreResultCode.LONG_RUNNING_TASK_INIT_FAILED, ImmutableMap.of("taskId", taskId, "taskType", task.getTaskType().getSimpleName(), "instanceId", task.getInstanceId()), ex);
        }
        // create job detail - job definition
        JobDetail jobDetail = JobBuilder.newJob().withIdentity(taskId, DEFAULT_GROUP_NAME).withDescription(description).ofType(task.getTaskType()).usingJobData(jobDataMap).storeDurably().build();
        // add job
        scheduler.addJob(jobDetail, false);
        // 
        LOG.debug("Job '{}' ({}) was created and registered", taskId, task.getTaskType());
        // 
        return getTask(taskId);
    } catch (org.quartz.SchedulerException ex) {
        throw new SchedulerException(CoreResultCode.SCHEDULER_CREATE_TASK_FAILED, ex);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) JobDetail(org.quartz.JobDetail) SchedulerException(eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) InvalidCronExpressionException(eu.bcvsolutions.idm.core.scheduler.exception.InvalidCronExpressionException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) SchedulerException(eu.bcvsolutions.idm.core.scheduler.exception.SchedulerException) DryRunNotSupportedException(eu.bcvsolutions.idm.core.scheduler.api.exception.DryRunNotSupportedException)

Example 64 with JobDetail

use of org.quartz.JobDetail in project openhab1-addons by openhab.

the class SonosBinding method execute.

@Override
protected void execute() {
    if (isProperlyConfigured()) {
        if (!bindingStarted) {
            // This will create necessary network resources for UPnP right away
            upnpService = new UpnpServiceImpl(new SonosUpnpServiceConfiguration(), listener);
            try {
                Iterator<SonosZonePlayer> it = sonosZonePlayerCache.iterator();
                while (it.hasNext()) {
                    SonosZonePlayer aPlayer = it.next();
                    if (aPlayer.getDevice() == null) {
                        logger.info("Querying the network for a predefined Sonos device with UDN {}", aPlayer.getUdn());
                        upnpService.getControlPoint().search(new UDNHeader(aPlayer.getUdn()));
                    }
                }
                logger.info("Querying the network for any other Sonos device");
                final UDAServiceType udaType = new UDAServiceType("AVTransport");
                upnpService.getControlPoint().search(new UDAServiceTypeHeader(udaType));
            } catch (Exception e) {
                logger.warn("An exception occurred while searching the network for Sonos devices: ", e.getMessage());
            }
            bindingStarted = true;
        }
        Scheduler sched = null;
        try {
            sched = StdSchedulerFactory.getDefaultScheduler();
        } catch (SchedulerException e) {
            logger.error("An exception occurred while getting a reference to the Quartz Scheduler");
        }
        // Cycle through the Items and setup sonos zone players if required
        for (SonosBindingProvider provider : providers) {
            for (String itemName : provider.getItemNames()) {
                for (String sonosID : provider.getSonosID(itemName)) {
                    if (!sonosZonePlayerCache.contains(sonosID)) {
                        // the device is not yet discovered on the network or not defined in the .cfg
                        // Verify that the sonosID has the format of a valid UDN
                        Pattern SONOS_UDN_PATTERN = Pattern.compile("RINCON_(\\w{17})");
                        Matcher matcher = SONOS_UDN_PATTERN.matcher(sonosID);
                        if (matcher.matches()) {
                            // Add device to the cached Configs
                            SonosZonePlayer thePlayer = new SonosZonePlayer(sonosID, self);
                            thePlayer.setUdn(new UDN(sonosID));
                            sonosZonePlayerCache.add(thePlayer);
                            // Query the network for this device
                            logger.info("Querying the network for a predefined Sonos device with UDN '{}'", thePlayer.getUdn());
                            upnpService.getControlPoint().search(new UDNHeader(thePlayer.getUdn()));
                        }
                    }
                }
            }
        }
        // Cycle through the item binding configuration that define polling criteria
        for (SonosCommandType sonosCommandType : SonosCommandType.getPolling()) {
            for (SonosBindingProvider provider : providers) {
                for (String itemName : provider.getItemNames(sonosCommandType.getSonosCommand())) {
                    for (Command aCommand : provider.getCommands(itemName, sonosCommandType.getSonosCommand())) {
                        // We are dealing with a valid device
                        SonosZonePlayer thePlayer = sonosZonePlayerCache.getById(provider.getSonosID(itemName, aCommand));
                        if (thePlayer != null) {
                            RemoteDevice theDevice = thePlayer.getDevice();
                            // Not all Sonos devices have the same capabilities
                            if (theDevice != null) {
                                if (theDevice.findService(new UDAServiceId(sonosCommandType.getService())) != null) {
                                    boolean jobExists = false;
                                    // enumerate each job group
                                    try {
                                        for (String group : sched.getJobGroupNames()) {
                                            // enumerate each job in group
                                            for (JobKey jobKey : sched.getJobKeys(jobGroupEquals(group))) {
                                                if (jobKey.getName().equals(provider.getSonosID(itemName, aCommand) + "-" + sonosCommandType.getJobClass().toString())) {
                                                    jobExists = true;
                                                    break;
                                                }
                                            }
                                        }
                                    } catch (SchedulerException e1) {
                                        logger.error("An exception occurred while quering the Quartz Scheduler ({})", e1.getMessage());
                                    }
                                    if (!jobExists) {
                                        // set up the Quartz jobs
                                        JobDataMap map = new JobDataMap();
                                        map.put("Player", thePlayer);
                                        JobDetail job = newJob(sonosCommandType.getJobClass()).withIdentity(provider.getSonosID(itemName, aCommand) + "-" + sonosCommandType.getJobClass().toString(), "Sonos-" + provider.toString()).usingJobData(map).build();
                                        Trigger trigger = newTrigger().withIdentity(provider.getSonosID(itemName, aCommand) + "-" + sonosCommandType.getJobClass().toString(), "Sonos-" + provider.toString()).startNow().withSchedule(simpleSchedule().repeatForever().withIntervalInMilliseconds(pollingPeriod)).build();
                                        try {
                                            sched.scheduleJob(job, trigger);
                                        } catch (SchedulerException e) {
                                            logger.error("An exception occurred while scheduling a Quartz Job ({})", e.getMessage());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) JobDataMap(org.quartz.JobDataMap) UpnpServiceImpl(org.teleal.cling.UpnpServiceImpl) SchedulerException(org.quartz.SchedulerException) UDNHeader(org.teleal.cling.model.message.header.UDNHeader) Matcher(java.util.regex.Matcher) Scheduler(org.quartz.Scheduler) SonosCommandType(org.openhab.binding.sonos.SonosCommandType) UDAServiceTypeHeader(org.teleal.cling.model.message.header.UDAServiceTypeHeader) ConfigurationException(org.osgi.service.cm.ConfigurationException) IllegalClassException(org.apache.commons.lang.IllegalClassException) JobExecutionException(org.quartz.JobExecutionException) SAXException(org.xml.sax.SAXException) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) SchedulerException(org.quartz.SchedulerException) JobKey(org.quartz.JobKey) JobDetail(org.quartz.JobDetail) SonosBindingProvider(org.openhab.binding.sonos.SonosBindingProvider) Trigger(org.quartz.Trigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) Command(org.openhab.core.types.Command) UDAServiceType(org.teleal.cling.model.types.UDAServiceType) RemoteDevice(org.teleal.cling.model.meta.RemoteDevice) UDN(org.teleal.cling.model.types.UDN) UDAServiceId(org.teleal.cling.model.types.UDAServiceId)

Example 65 with JobDetail

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

the class XsltTransformJob method postSuccessMessage.

private void postSuccessMessage(String message, JobExecutionContext context) {
    String SCHEDULER = "schedulerFactoryBean";
    try {
        ApplicationContext appContext = (ApplicationContext) context.getScheduler().getContext().get("applicationContext");
        StdScheduler scheduler = (StdScheduler) appContext.getBean(SCHEDULER);
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap dataMap = jobDetail.getJobDataMap();
        dataMap.put("successMsg", message);
        jobDetail.getJobBuilder().usingJobData(dataMap);
        // replace the job with the extra data
        scheduler.addJob(jobDetail, true);
    } catch (SchedulerException e) {
        throw new IllegalStateException("Error processing post success message", e);
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) JobDetail(org.quartz.JobDetail) JobDataMap(org.quartz.JobDataMap) SchedulerException(org.quartz.SchedulerException) StdScheduler(org.quartz.impl.StdScheduler)

Aggregations

JobDetail (org.quartz.JobDetail)131 SchedulerException (org.quartz.SchedulerException)59 Trigger (org.quartz.Trigger)51 Scheduler (org.quartz.Scheduler)34 Test (org.junit.Test)30 CronTrigger (org.quartz.CronTrigger)26 JobKey (org.quartz.JobKey)22 SimpleTrigger (org.quartz.SimpleTrigger)22 JobDataMap (org.quartz.JobDataMap)20 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 TriggerKey (org.quartz.TriggerKey)10 HashMap (java.util.HashMap)8 Date (java.util.Date)7 ArrayList (java.util.ArrayList)5 Command (org.openhab.core.types.Command)5 IOException (java.io.IOException)4 Serializable (java.io.Serializable)4 InetSocketAddress (java.net.InetSocketAddress)4 SocketChannel (java.nio.channels.SocketChannel)4