Search in sources :

Example 26 with JobExecutionException

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);
        }
    }
}
Also used : JobExecutionException(org.quartz.JobExecutionException) JobExecutionException(org.quartz.JobExecutionException)

Example 27 with JobExecutionException

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);
}
Also used : CamelContext(org.apache.camel.CamelContext) SchedulerException(org.quartz.SchedulerException) JobExecutionException(org.quartz.JobExecutionException) Trigger(org.quartz.Trigger) SchedulerContext(org.quartz.SchedulerContext)

Example 28 with JobExecutionException

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);
    }
}
Also used : Exchange(org.apache.camel.Exchange) ServiceSupport(org.apache.camel.support.ServiceSupport) JobExecutionException(org.quartz.JobExecutionException) LoadBalancer(org.apache.camel.processor.loadbalancer.LoadBalancer) RoundRobinLoadBalancer(org.apache.camel.processor.loadbalancer.RoundRobinLoadBalancer) JobExecutionException(org.quartz.JobExecutionException) SchedulerException(org.quartz.SchedulerException) CamelExchangeException(org.apache.camel.CamelExchangeException)

Example 29 with JobExecutionException

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;
}
Also used : TriggerKey(org.quartz.TriggerKey) JobDetail(org.quartz.JobDetail) JobKey(org.quartz.JobKey) JobExecutionException(org.quartz.JobExecutionException) Endpoint(org.apache.camel.Endpoint) DelegateEndpoint(org.apache.camel.DelegateEndpoint) DelegateEndpoint(org.apache.camel.DelegateEndpoint) Route(org.apache.camel.Route)

Example 30 with JobExecutionException

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);
    }
}
Also used : Exchange(org.apache.camel.Exchange) CamelContext(org.apache.camel.CamelContext) JobExecutionException(org.quartz.JobExecutionException) JobExecutionException(org.quartz.JobExecutionException) SchedulerException(org.quartz.SchedulerException) CamelExchangeException(org.apache.camel.CamelExchangeException)

Aggregations

JobExecutionException (org.quartz.JobExecutionException)33 SchedulerException (org.quartz.SchedulerException)9 EmailException (org.apache.commons.mail.EmailException)6 JobDataMap (org.quartz.JobDataMap)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 SchedulerContext (org.quartz.SchedulerContext)4 TimeSeriesResponse (com.linkedin.thirdeye.client.timeseries.TimeSeriesResponse)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ExecutionException (java.util.concurrent.ExecutionException)3 CamelContext (org.apache.camel.CamelContext)3 Route (org.apache.camel.Route)3 HtmlEmail (org.apache.commons.mail.HtmlEmail)3 ThirdEyeAnomalyConfiguration (com.linkedin.thirdeye.anomaly.ThirdEyeAnomalyConfiguration)2 MetricDimensionReport (com.linkedin.thirdeye.anomaly.alert.template.pojo.MetricDimensionReport)2 DataReportHelper (com.linkedin.thirdeye.anomaly.alert.util.DataReportHelper)2 DimensionKey (com.linkedin.thirdeye.api.DimensionKey)2 MetricTimeSeries (com.linkedin.thirdeye.api.MetricTimeSeries)2 TimeGranularity (com.linkedin.thirdeye.api.TimeGranularity)2 MetricExpression (com.linkedin.thirdeye.client.MetricExpression)2