Search in sources :

Example 71 with TriggerKey

use of org.quartz.TriggerKey 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 72 with TriggerKey

use of org.quartz.TriggerKey in project camel by apache.

the class QuartzComponent method createEndpoint.

@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
    // Get couple of scheduler settings
    Integer startDelayedSeconds = getAndRemoveParameter(parameters, "startDelayedSeconds", Integer.class);
    if (startDelayedSeconds != null) {
        if (this.startDelayedSeconds != 0 && !(this.startDelayedSeconds == startDelayedSeconds)) {
            LOG.warn("A Quartz job is already configured with a different 'startDelayedSeconds' configuration! " + "All Quartz jobs must share the same 'startDelayedSeconds' configuration! Cannot apply the 'startDelayedSeconds' configuration!");
        } else {
            this.startDelayedSeconds = startDelayedSeconds;
        }
    }
    Boolean autoStartScheduler = getAndRemoveParameter(parameters, "autoStartScheduler", Boolean.class);
    if (autoStartScheduler != null) {
        this.autoStartScheduler = autoStartScheduler;
    }
    Boolean prefixJobNameWithEndpointId = getAndRemoveParameter(parameters, "prefixJobNameWithEndpointId", Boolean.class);
    if (prefixJobNameWithEndpointId != null) {
        this.prefixJobNameWithEndpointId = prefixJobNameWithEndpointId;
    }
    // Extract trigger.XXX and job.XXX properties to be set on endpoint below
    Map<String, Object> triggerParameters = IntrospectionSupport.extractProperties(parameters, "trigger.");
    Map<String, Object> jobParameters = IntrospectionSupport.extractProperties(parameters, "job.");
    // Create quartz endpoint
    QuartzEndpoint result = new QuartzEndpoint(uri, this);
    TriggerKey triggerKey = createTriggerKey(uri, remaining, result);
    result.setTriggerKey(triggerKey);
    result.setTriggerParameters(triggerParameters);
    result.setJobParameters(jobParameters);
    if (startDelayedSeconds != null) {
        result.setStartDelayedSeconds(startDelayedSeconds);
    }
    if (autoStartScheduler != null) {
        result.setAutoStartScheduler(autoStartScheduler);
    }
    if (prefixJobNameWithEndpointId != null) {
        result.setPrefixJobNameWithEndpointId(prefixJobNameWithEndpointId);
    }
    return result;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TriggerKey(org.quartz.TriggerKey)

Example 73 with TriggerKey

use of org.quartz.TriggerKey in project camel by apache.

the class QuartzComponent method createTriggerKey.

private TriggerKey createTriggerKey(String uri, String remaining, QuartzEndpoint endpoint) throws Exception {
    // Parse uri for trigger name and group
    URI u = new URI(uri);
    String path = ObjectHelper.after(u.getPath(), "/");
    String host = u.getHost();
    // host can be null if the uri did contain invalid host characters such as an underscore
    if (host == null) {
        host = ObjectHelper.before(remaining, "/");
        if (host == null) {
            host = remaining;
        }
    }
    // Trigger group can be optional, if so set it to this context's unique name
    String name;
    String group;
    if (ObjectHelper.isNotEmpty(path) && ObjectHelper.isNotEmpty(host)) {
        group = host;
        name = path;
    } else {
        String camelContextName = QuartzHelper.getQuartzContextName(getCamelContext());
        group = camelContextName == null ? "Camel" : "Camel_" + camelContextName;
        name = host;
    }
    if (prefixJobNameWithEndpointId) {
        name = endpoint.getId() + "_" + name;
    }
    return new TriggerKey(name, group);
}
Also used : TriggerKey(org.quartz.TriggerKey) URI(java.net.URI)

Example 74 with TriggerKey

use of org.quartz.TriggerKey in project camel by apache.

the class QuartzEndpoint method ensureNoDupTriggerKey.

private void ensureNoDupTriggerKey() {
    for (Route route : getCamelContext().getRoutes()) {
        if (route.getEndpoint() instanceof QuartzEndpoint) {
            QuartzEndpoint quartzEndpoint = (QuartzEndpoint) route.getEndpoint();
            TriggerKey checkTriggerKey = quartzEndpoint.getTriggerKey();
            if (triggerKey.equals(checkTriggerKey)) {
                throw new IllegalArgumentException("Trigger key " + triggerKey + " is already in use by " + quartzEndpoint);
            }
        }
    }
}
Also used : TriggerKey(org.quartz.TriggerKey) Route(org.apache.camel.Route)

Example 75 with TriggerKey

use of org.quartz.TriggerKey in project camel by apache.

the class QuartzEndpointConfigureTest method testConfigureName.

@Test
public void testConfigureName() throws Exception {
    QuartzEndpoint endpoint = resolveMandatoryEndpoint("quartz2://myName");
    Scheduler scheduler = endpoint.getComponent().getScheduler();
    TriggerKey triggerKey = endpoint.getTriggerKey();
    JobDetail jobDetail = scheduler.getJobDetail(JobKey.jobKey(triggerKey.getName(), triggerKey.getGroup()));
    assertEquals("getName()", "myName", triggerKey.getName());
    assertEquals("getGroup()", "Camel_" + context.getManagementName(), triggerKey.getGroup());
    assertEquals("getJobName", "myName", jobDetail.getKey().getName());
    assertEquals("getJobGroup", "Camel_" + context.getManagementName(), jobDetail.getKey().getGroup());
}
Also used : TriggerKey(org.quartz.TriggerKey) JobDetail(org.quartz.JobDetail) Scheduler(org.quartz.Scheduler) Test(org.junit.Test)

Aggregations

TriggerKey (org.quartz.TriggerKey)112 Trigger (org.quartz.Trigger)49 JobKey (org.quartz.JobKey)30 SchedulerException (org.quartz.SchedulerException)30 CronTrigger (org.quartz.CronTrigger)25 JobDetail (org.quartz.JobDetail)22 Test (org.junit.Test)18 Scheduler (org.quartz.Scheduler)18 SimpleTrigger (org.quartz.SimpleTrigger)15 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)11 ArrayList (java.util.ArrayList)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 HashMap (java.util.HashMap)9 Date (java.util.Date)6 List (java.util.List)5 CronScheduleBuilder (org.quartz.CronScheduleBuilder)5 BigDecimal (java.math.BigDecimal)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 ParseException (java.text.ParseException)4