/** * Lookup the id the given endpoint has been enlisted with in the {@link org.apache.camel.spi.Registry}. * * @param endpoint the endpoint * @return the endpoint id, or <tt>null</tt> if not found */ public static String lookupEndpointRegistryId(Endpoint endpoint) { if (endpoint == null || endpoint.getCamelContext() == null) { return null; } // it may be a delegate endpoint, which we need to match as well Endpoint delegate = null; if (endpoint instanceof DelegateEndpoint) { delegate = ((DelegateEndpoint) endpoint).getEndpoint(); } Map<String, Endpoint> map = endpoint.getCamelContext().getRegistry().findByTypeWithName(Endpoint.class); for (Map.Entry<String, Endpoint> entry : map.entrySet()) { if (entry.getValue().equals(endpoint) || entry.getValue().equals(delegate)) { return entry.getKey(); } } // not found return null; }
private QuartzEndpoint lookupQuartzEndpoint(CamelContext camelContext, String endpointUri, Trigger trigger) throws JobExecutionException { String targetTriggerName = trigger.getName(); String targetTriggerGroup = trigger.getGroup(); LOG.debug("Looking up existing QuartzEndpoint with trigger {}.{}", targetTriggerName, targetTriggerGroup); try { // check all active routes for the quartz endpoint this task matches // 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; String triggerName = quartzEndpoint.getTrigger().getName(); String triggerGroup = quartzEndpoint.getTrigger().getGroup(); LOG.trace("Checking route trigger {}.{}", triggerName, triggerGroup); if (triggerName.equals(targetTriggerName) && triggerGroup.equals(targetTriggerGroup)) { return (QuartzEndpoint) endpoint; } } } } catch (Exception e) { throw new JobExecutionException("Error lookup up existing QuartzEndpoint with trigger: " + trigger, e); } // fallback and lookup existing from registry (eg maybe a @Consume POJO with a quartz endpoint, and thus not from a route) if (camelContext.hasEndpoint(endpointUri) != null) { return camelContext.getEndpoint(endpointUri, QuartzEndpoint.class); } else { LOG.warn("Cannot find existing QuartzEndpoint with uri: {}. Creating new endpoint instance.", endpointUri); return camelContext.getEndpoint(endpointUri, QuartzEndpoint.class); } }
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); } // check all active routes for the quartz endpoint this task matches // 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; }