Search in sources :

Example 1 with IsSingleton

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

the class DefaultCamelContext method resolveLanguage.

// Helper methods
// -----------------------------------------------------------------------
public Language resolveLanguage(String language) {
    Language answer;
    synchronized (languages) {
        answer = languages.get(language);
        // check if the language is singleton, if so return the shared instance
        if (answer instanceof IsSingleton) {
            boolean singleton = ((IsSingleton) answer).isSingleton();
            if (singleton) {
                return answer;
            }
        }
        // language not known or not singleton, then use resolver
        answer = getLanguageResolver().resolveLanguage(language, this);
        // inject CamelContext if aware
        if (answer != null) {
            if (answer instanceof CamelContextAware) {
                ((CamelContextAware) answer).setCamelContext(this);
            }
            if (answer instanceof Service) {
                try {
                    startService((Service) answer);
                } catch (Exception e) {
                    throw ObjectHelper.wrapRuntimeCamelException(e);
                }
            }
            languages.put(language, answer);
        }
    }
    return answer;
}
Also used : IsSingleton(org.apache.camel.IsSingleton) CamelContextAware(org.apache.camel.CamelContextAware) Language(org.apache.camel.spi.Language) Service(org.apache.camel.Service) StatefulService(org.apache.camel.StatefulService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SuspendableService(org.apache.camel.SuspendableService) RuntimeCamelException(org.apache.camel.RuntimeCamelException) MalformedObjectNameException(javax.management.MalformedObjectNameException) VetoCamelContextStartException(org.apache.camel.VetoCamelContextStartException) IOException(java.io.IOException) LoadPropertiesException(org.apache.camel.util.LoadPropertiesException) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) ResolveEndpointFailedException(org.apache.camel.ResolveEndpointFailedException) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) FailedToStartRouteException(org.apache.camel.FailedToStartRouteException)

Example 2 with IsSingleton

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

the class ConsumerCache method doGetPollingConsumer.

protected synchronized PollingConsumer doGetPollingConsumer(Endpoint endpoint, boolean pooled) {
    String key = endpoint.getEndpointUri();
    PollingConsumer answer = consumers.get(key);
    if (pooled && answer == null) {
        pool.acquire(endpoint);
    }
    if (answer == null) {
        try {
            answer = endpoint.createPollingConsumer();
            answer.start();
        } catch (Exception e) {
            throw new FailedToCreateConsumerException(endpoint, e);
        }
        if (pooled && answer instanceof ServicePoolAware) {
            LOG.debug("Adding to producer service pool with key: {} for producer: {}", endpoint, answer);
            answer = pool.addAndAcquire(endpoint, answer);
        } else {
            boolean singleton = false;
            if (answer instanceof IsSingleton) {
                singleton = ((IsSingleton) answer).isSingleton();
            }
            if (singleton) {
                LOG.debug("Adding to consumer cache with key: {} for consumer: {}", endpoint, answer);
                consumers.put(key, answer);
            } else {
                LOG.debug("Consumer for endpoint: {} is not singleton and thus not added to consumer cache", key);
            }
        }
    }
    if (answer != null) {
        // record statistics
        if (extendedStatistics) {
            statistics.onHit(key);
        }
    }
    return answer;
}
Also used : PollingConsumer(org.apache.camel.PollingConsumer) FailedToCreateConsumerException(org.apache.camel.FailedToCreateConsumerException) IsSingleton(org.apache.camel.IsSingleton) ServicePoolAware(org.apache.camel.ServicePoolAware) RuntimeCamelException(org.apache.camel.RuntimeCamelException) FailedToCreateConsumerException(org.apache.camel.FailedToCreateConsumerException)

Example 3 with IsSingleton

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

the class DefaultCamelContext method deferStartService.

public void deferStartService(Object object, boolean stopOnShutdown) throws Exception {
    if (object instanceof Service) {
        Service service = (Service) object;
        // only add to services to close if its a singleton
        // otherwise we could for example end up with a lot of prototype scope endpoints
        // assume singleton by default
        boolean singleton = true;
        if (object instanceof IsSingleton) {
            singleton = ((IsSingleton) service).isSingleton();
        }
        // do not add endpoints as they have their own list
        if (singleton && !(service instanceof Endpoint)) {
            // only add to list of services to stop if its not already there
            if (stopOnShutdown && !hasService(service)) {
                servicesToStop.add(service);
            }
        }
        // are we already started?
        if (isStarted()) {
            ServiceHelper.startService(service);
        } else {
            deferStartupListener.addService(service);
        }
    }
}
Also used : IsSingleton(org.apache.camel.IsSingleton) Endpoint(org.apache.camel.Endpoint) Service(org.apache.camel.Service) StatefulService(org.apache.camel.StatefulService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SuspendableService(org.apache.camel.SuspendableService)

Example 4 with IsSingleton

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

the class DefaultCamelContext method doAddService.

private void doAddService(Object object, boolean stopOnShutdown, boolean forceStart) throws Exception {
    // inject CamelContext
    if (object instanceof CamelContextAware) {
        CamelContextAware aware = (CamelContextAware) object;
        aware.setCamelContext(this);
    }
    if (object instanceof Service) {
        Service service = (Service) object;
        for (LifecycleStrategy strategy : lifecycleStrategies) {
            if (service instanceof Endpoint) {
                // use specialized endpoint add
                strategy.onEndpointAdd((Endpoint) service);
            } else {
                strategy.onServiceAdd(this, service, null);
            }
        }
        if (!forceStart) {
            // now start the service (and defer starting if CamelContext is starting up itself)
            deferStartService(object, stopOnShutdown);
        } else {
            // only add to services to close if its a singleton
            // otherwise we could for example end up with a lot of prototype scope endpoints
            // assume singleton by default
            boolean singleton = true;
            if (object instanceof IsSingleton) {
                singleton = ((IsSingleton) service).isSingleton();
            }
            // do not add endpoints as they have their own list
            if (singleton && !(service instanceof Endpoint)) {
                // only add to list of services to stop if its not already there
                if (stopOnShutdown && !hasService(service)) {
                    servicesToStop.add(service);
                }
            }
            ServiceHelper.startService(service);
        }
    }
}
Also used : CamelContextAware(org.apache.camel.CamelContextAware) IsSingleton(org.apache.camel.IsSingleton) Endpoint(org.apache.camel.Endpoint) LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) Service(org.apache.camel.Service) StatefulService(org.apache.camel.StatefulService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SuspendableService(org.apache.camel.SuspendableService)

Example 5 with IsSingleton

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

the class EmptyConsumerCache method acquirePollingConsumer.

@Override
public PollingConsumer acquirePollingConsumer(Endpoint endpoint) {
    // always create a new consumer
    PollingConsumer answer;
    try {
        answer = endpoint.createPollingConsumer();
        boolean singleton = true;
        if (answer instanceof IsSingleton) {
            singleton = ((IsSingleton) answer).isSingleton();
        }
        if (getCamelContext().isStartingRoutes() && singleton) {
            // if we are currently starting a route, then add as service and enlist in JMX
            // - but do not enlist non-singletons in JMX
            // - note addService will also start the service
            getCamelContext().addService(answer);
        } else {
            // must then start service so producer is ready to be used
            ServiceHelper.startService(answer);
        }
    } catch (Exception e) {
        throw new FailedToCreateConsumerException(endpoint, e);
    }
    return answer;
}
Also used : PollingConsumer(org.apache.camel.PollingConsumer) FailedToCreateConsumerException(org.apache.camel.FailedToCreateConsumerException) IsSingleton(org.apache.camel.IsSingleton) FailedToCreateConsumerException(org.apache.camel.FailedToCreateConsumerException)

Aggregations

IsSingleton (org.apache.camel.IsSingleton)5 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 Service (org.apache.camel.Service)3 StatefulService (org.apache.camel.StatefulService)3 SuspendableService (org.apache.camel.SuspendableService)3 CamelContextAware (org.apache.camel.CamelContextAware)2 Endpoint (org.apache.camel.Endpoint)2 FailedToCreateConsumerException (org.apache.camel.FailedToCreateConsumerException)2 PollingConsumer (org.apache.camel.PollingConsumer)2 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2 IOException (java.io.IOException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 FailedToStartRouteException (org.apache.camel.FailedToStartRouteException)1 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)1 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)1 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)1 ServicePoolAware (org.apache.camel.ServicePoolAware)1 VetoCamelContextStartException (org.apache.camel.VetoCamelContextStartException)1 Language (org.apache.camel.spi.Language)1 LifecycleStrategy (org.apache.camel.spi.LifecycleStrategy)1