Search in sources :

Example 41 with JobKey

use of org.quartz.JobKey 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 42 with JobKey

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

the class ImportScheduler method removeCurrentJobsFromSchedule.

/**
 * Iterates of current job list and removes each job from the underlying schedule
 *
 * @throws org.quartz.SchedulerException if any.
 */
protected void removeCurrentJobsFromSchedule() throws SchedulerException {
    printCurrentSchedule();
    synchronized (m_lock) {
        for (JobKey key : m_scheduler.getJobKeys(GroupMatcher.<JobKey>groupEquals(JOB_GROUP))) {
            String jobName = key.getName();
            try {
                getScheduler().deleteJob(new JobKey(jobName, JOB_GROUP));
            } catch (SchedulerException e) {
                LOG.error("removeCurrentJobsFromSchedule: {}", e.getLocalizedMessage(), e);
            }
        }
    }
    printCurrentSchedule();
}
Also used : JobKey(org.quartz.JobKey) SchedulerException(org.quartz.SchedulerException)

Example 43 with JobKey

use of org.quartz.JobKey in project elastic-job by dangdangdotcom.

the class TransientProducerScheduler method register.

// TODO 并发优化
synchronized void register(final CloudJobConfiguration jobConfig) {
    String cron = jobConfig.getTypeConfig().getCoreConfig().getCron();
    JobKey jobKey = buildJobKey(cron);
    repository.put(jobKey, jobConfig.getJobName());
    try {
        if (!scheduler.checkExists(jobKey)) {
            scheduler.scheduleJob(buildJobDetail(jobKey), buildTrigger(jobKey.getName()));
        }
    } catch (final SchedulerException ex) {
        throw new JobSystemException(ex);
    }
}
Also used : JobKey(org.quartz.JobKey) SchedulerException(org.quartz.SchedulerException) JobSystemException(com.dangdang.ddframe.job.exception.JobSystemException)

Example 44 with JobKey

use of org.quartz.JobKey in project elastic-job by dangdangdotcom.

the class JobScheduleControllerTest method assertTriggerJobFailure.

@Test(expected = JobSystemException.class)
public void assertTriggerJobFailure() throws NoSuchFieldException, SchedulerException {
    JobKey jobKey = new JobKey("test_job");
    when(jobDetail.getKey()).thenReturn(jobKey);
    doThrow(SchedulerException.class).when(scheduler).triggerJob(jobKey);
    ReflectionUtils.setFieldValue(jobScheduleController, "scheduler", scheduler);
    ReflectionUtils.setFieldValue(jobScheduleController, "jobDetail", jobDetail);
    try {
        jobScheduleController.triggerJob();
    } finally {
        verify(jobDetail).getKey();
        verify(scheduler).triggerJob(jobKey);
    }
}
Also used : JobKey(org.quartz.JobKey) Test(org.junit.Test)

Example 45 with JobKey

use of org.quartz.JobKey in project elastic-job by dangdangdotcom.

the class JobScheduleControllerTest method assertTriggerJobIfShutdown.

@Test
public void assertTriggerJobIfShutdown() throws NoSuchFieldException, SchedulerException {
    JobKey jobKey = new JobKey("test_job");
    when(jobDetail.getKey()).thenReturn(jobKey);
    when(scheduler.isShutdown()).thenReturn(true);
    ReflectionUtils.setFieldValue(jobScheduleController, "scheduler", scheduler);
    ReflectionUtils.setFieldValue(jobScheduleController, "jobDetail", jobDetail);
    jobScheduleController.triggerJob();
    verify(jobDetail, times(0)).getKey();
    verify(scheduler, times(0)).triggerJob(jobKey);
}
Also used : JobKey(org.quartz.JobKey) Test(org.junit.Test)

Aggregations

JobKey (org.quartz.JobKey)66 SchedulerException (org.quartz.SchedulerException)33 JobDetail (org.quartz.JobDetail)22 Trigger (org.quartz.Trigger)19 Scheduler (org.quartz.Scheduler)17 TriggerKey (org.quartz.TriggerKey)15 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)6 List (java.util.List)6 CronTrigger (org.quartz.CronTrigger)6 JobDataMap (org.quartz.JobDataMap)6 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)5 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 Hashtable (java.util.Hashtable)2 Scheduled (org.apache.deltaspike.scheduler.api.Scheduled)2 CronScheduleBuilder (org.quartz.CronScheduleBuilder)2