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;
}
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;
}
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);
}
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);
}
}
}
}
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());
}
Aggregations