use of org.quartz.JobExecutionException in project openhab1-addons by openhab.
the class EventJob method execute.
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
final String config = context.getJobDetail().getJobDataMap().getString(KEY_CONFIG);
final String eventId = context.getJobDetail().getJobDataMap().getString(KEY_EVENT);
final int recIndex = context.getJobDetail().getJobDataMap().getInt(KEY_REC_INDEX);
final EventTrigger eventTrigger = EventTrigger.valueOf(context.getJobDetail().getJobDataMap().getString(KEY_EVENT_TRIGGER));
CalendarRuntime calendarRuntime = EventStorage.getInstance().getEventCache().get(config);
if (calendarRuntime == null) {
throw new JobExecutionException("cannot get runtime for config: " + config, false);
}
EventContainer eventContainer = calendarRuntime.getEventMap().get(eventId);
if (eventContainer == null) {
throw new JobExecutionException("cannot get event-container for config: " + config + " and eventId: " + eventId, false);
}
if (eventContainer.getEventList().size() <= recIndex) {
throw new JobExecutionException("cannot get recurence-event for config: " + config + " and eventId: " + eventId + " and occurence: " + recIndex, false);
}
CalDavEvent event = eventContainer.getEventList().get(recIndex);
log.info("event {} for: {}", eventTrigger, event.getShortName());
for (EventNotifier notifier : CalDavLoaderImpl.instance.getEventListenerList()) {
try {
if (eventTrigger == EventTrigger.BEGIN) {
notifier.eventBegins(event);
} else if (eventTrigger == EventTrigger.END) {
notifier.eventEnds(event);
} else {
throw new IllegalStateException("not implemented event trigger: " + eventTrigger);
}
} catch (Exception e) {
log.error("error while invoking listener", e);
}
}
if (eventTrigger == EventTrigger.END) {
// if event is ended, remove it from the map
calendarRuntime.getEventMap().remove(eventContainer.getEventId());
}
} catch (Exception e) {
log.error("error executing event job", e);
throw new JobExecutionException("error executing event job", e, false);
}
}
use of org.quartz.JobExecutionException in project openhab1-addons by openhab.
the class ChangeStateJob method execute.
/*
* (non-Javadoc)
*
* @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
*/
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
String content = (String) context.getJobDetail().getJobDataMap().get(JOB_DATA_CONTENT_KEY);
if (StringUtils.isNotBlank(content)) {
String deviceId = content.split(";")[0];
String state = content.split(";")[1];
boolean setToOn = state == "on";
logger.debug("About to execute ChangeStateJob with arguments {} {}", deviceId, state);
try {
MystromClient.ChangeState(deviceId, setToOn);
} catch (Exception e) {
throw new JobExecutionException("Executing command '" + deviceId + " " + state + "' throws an Exception. Job will be refired immediately.", e, true);
}
}
}
use of org.quartz.JobExecutionException in project camel by apache.
the class CamelJob method execute.
public void execute(JobExecutionContext context) throws JobExecutionException {
String camelContextName = (String) context.getJobDetail().getJobDataMap().get(QuartzConstants.QUARTZ_CAMEL_CONTEXT_NAME);
String endpointUri = (String) context.getJobDetail().getJobDataMap().get(QuartzConstants.QUARTZ_ENDPOINT_URI);
SchedulerContext schedulerContext;
try {
schedulerContext = context.getScheduler().getContext();
} catch (SchedulerException e) {
throw new JobExecutionException("Failed to obtain scheduler context for job " + context.getJobDetail().getName());
}
CamelContext camelContext = (CamelContext) schedulerContext.get(QuartzConstants.QUARTZ_CAMEL_CONTEXT + "-" + camelContextName);
if (camelContext == null) {
throw new JobExecutionException("No CamelContext could be found with name: " + camelContextName);
}
Trigger trigger = context.getTrigger();
QuartzEndpoint endpoint = lookupQuartzEndpoint(camelContext, endpointUri, trigger);
if (endpoint == null) {
throw new JobExecutionException("No QuartzEndpoint could be found with endpointUri: " + endpointUri);
}
endpoint.onJobExecute(context);
}
use of org.quartz.JobExecutionException in project camel by apache.
the class QuartzEndpoint method onJobExecute.
/**
* This method is invoked when a Quartz job is fired.
*
* @param jobExecutionContext the Quartz Job context
*/
public void onJobExecute(final JobExecutionContext jobExecutionContext) throws JobExecutionException {
boolean run = true;
LoadBalancer balancer = getLoadBalancer();
if (balancer instanceof ServiceSupport) {
run = ((ServiceSupport) balancer).isRunAllowed();
}
if (!run) {
// quartz scheduler could potential trigger during a route has been shutdown
LOG.warn("Cannot execute Quartz Job with context: " + jobExecutionContext + " because processor is not started: " + balancer);
return;
}
LOG.debug("Firing Quartz Job with context: {}", jobExecutionContext);
Exchange exchange = createExchange(jobExecutionContext);
try {
balancer.process(exchange);
if (exchange.getException() != null) {
// propagate the exception back to Quartz
throw new JobExecutionException(exchange.getException());
}
} catch (Exception e) {
// log the error
LOG.error(CamelExchangeException.createExceptionMessage("Error processing exchange", exchange, e));
// and rethrow to let quartz handle it
if (e instanceof JobExecutionException) {
throw (JobExecutionException) e;
}
throw new JobExecutionException(e);
}
}
use of org.quartz.JobExecutionException in project camel by apache.
the class CamelJob method lookupQuartzEndpoint.
protected QuartzEndpoint lookupQuartzEndpoint(CamelContext camelContext, JobExecutionContext quartzContext) throws JobExecutionException {
TriggerKey triggerKey = quartzContext.getTrigger().getKey();
JobDetail jobDetail = quartzContext.getJobDetail();
JobKey jobKey = jobDetail.getKey();
if (LOG.isDebugEnabled()) {
LOG.debug("Looking up existing QuartzEndpoint with triggerKey={}", triggerKey);
}
// as we prefer to use the existing endpoint from the routes
for (Route route : camelContext.getRoutes()) {
Endpoint endpoint = route.getEndpoint();
if (endpoint instanceof DelegateEndpoint) {
endpoint = ((DelegateEndpoint) endpoint).getEndpoint();
}
if (endpoint instanceof QuartzEndpoint) {
QuartzEndpoint quartzEndpoint = (QuartzEndpoint) endpoint;
TriggerKey checkTriggerKey = quartzEndpoint.getTriggerKey();
if (LOG.isTraceEnabled()) {
LOG.trace("Checking route endpoint={} with checkTriggerKey={}", quartzEndpoint, checkTriggerKey);
}
if (triggerKey.equals(checkTriggerKey) || (jobDetail.requestsRecovery() && jobKey.getGroup().equals(checkTriggerKey.getGroup()) && jobKey.getName().equals(checkTriggerKey.getName()))) {
return quartzEndpoint;
}
}
}
// fallback and lookup existing from registry (eg maybe a @Consume POJO with a quartz endpoint, and thus not from a route)
String endpointUri = quartzContext.getMergedJobDataMap().getString(QuartzConstants.QUARTZ_ENDPOINT_URI);
QuartzEndpoint result = null;
// Even though the same camelContext.getEndpoint call, but if/else display different log.
if (camelContext.hasEndpoint(endpointUri) != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Getting Endpoint from camelContext.");
}
result = camelContext.getEndpoint(endpointUri, QuartzEndpoint.class);
} else if ((result = searchForEndpointMatch(camelContext, endpointUri)) != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Found match for endpoint URI = " + endpointUri + " by searching endpoint list.");
}
} else {
LOG.warn("Cannot find existing QuartzEndpoint with uri: {}. Creating new endpoint instance.", endpointUri);
result = camelContext.getEndpoint(endpointUri, QuartzEndpoint.class);
}
if (result == null) {
throw new JobExecutionException("No QuartzEndpoint could be found with endpointUri: " + endpointUri);
}
return result;
}
Aggregations