Search in sources :

Example 1 with DelegateEndpoint

use of org.apache.camel.DelegateEndpoint in project camel by apache.

the class EndpointHelper method lookupEndpointRegistryId.

/**
     * 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;
}
Also used : Endpoint(org.apache.camel.Endpoint) DelegateEndpoint(org.apache.camel.DelegateEndpoint) BrowsableEndpoint(org.apache.camel.spi.BrowsableEndpoint) DelegateEndpoint(org.apache.camel.DelegateEndpoint) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with DelegateEndpoint

use of org.apache.camel.DelegateEndpoint in project camel by apache.

the class CamelJob method lookupQuartzEndpoint.

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

Example 3 with DelegateEndpoint

use of org.apache.camel.DelegateEndpoint 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)

Aggregations

DelegateEndpoint (org.apache.camel.DelegateEndpoint)3 Endpoint (org.apache.camel.Endpoint)3 Route (org.apache.camel.Route)2 JobExecutionException (org.quartz.JobExecutionException)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 BrowsableEndpoint (org.apache.camel.spi.BrowsableEndpoint)1 JobDetail (org.quartz.JobDetail)1 JobKey (org.quartz.JobKey)1 SchedulerException (org.quartz.SchedulerException)1 TriggerKey (org.quartz.TriggerKey)1