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