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;
}
use of org.quartz.JobExecutionException in project camel by apache.
the class CamelJob method execute.
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
Exchange exchange = null;
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Running CamelJob jobExecutionContext={}", context);
}
CamelContext camelContext = getCamelContext(context);
QuartzEndpoint endpoint = lookupQuartzEndpoint(camelContext, context);
exchange = endpoint.createExchange();
exchange.setIn(new QuartzMessage(exchange, context));
endpoint.getConsumerLoadBalancer().process(exchange);
if (exchange.getException() != null) {
throw new JobExecutionException(exchange.getException());
}
} catch (Exception e) {
if (exchange != null) {
LOG.error(CamelExchangeException.createExceptionMessage("Error processing exchange", exchange, e));
} else {
LOG.error("Failed to execute CamelJob.", e);
}
// and rethrow to let quartz handle it
if (e instanceof JobExecutionException) {
throw (JobExecutionException) e;
}
throw new JobExecutionException(e);
}
}
Aggregations