You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/01/07 09:01:07 UTC
[camel] branch bean-scope created (now 123f92f)
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a change to branch bean-scope
in repository https://gitbox.apache.org/repos/asf/camel.git.
at 123f92f CAMEL-14359: camel-bean - Adding bean scope option.
This branch includes the following new commits:
new 33e7fde Polished
new 000fbb0 CAMEL-14359: camel-bean is now singleton scoped by default (cache=true).
new bc61558 CAMEL-14359: camel-bean is now singleton scoped by default (cache=true).
new 7e06e5f CAMEL-14359: camel-bean is now singleton scoped by default (cache=true).
new 2745841 CAMEL-14359: camel-bean - Adding bean scope option.
new 5e1bfe2 CAMEL-14359: camel-bean - Adding bean scope option.
new 22f4021 CAMEL-14359: camel-bean - Adding bean scope option.
new 123f92f CAMEL-14359: camel-bean - Adding bean scope option.
The 8 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[camel] 01/08: Polished
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch bean-scope
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 33e7fde6c9f8fa7ed0132647cb6050e90b43a52d
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jan 6 09:41:19 2020 +0100
Polished
---
.../java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java | 1 -
1 file changed, 1 deletion(-)
diff --git a/core/camel-base/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java b/core/camel-base/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
index 48a0ff9..37fa497 100644
--- a/core/camel-base/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
+++ b/core/camel-base/src/main/java/org/apache/camel/impl/converter/BaseTypeConverterRegistry.java
@@ -17,7 +17,6 @@
package org.apache.camel.impl.converter;
import java.io.BufferedReader;
-import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
[camel] 04/08: CAMEL-14359: camel-bean is now singleton scoped by
default (cache=true).
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch bean-scope
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 7e06e5fc4be0129c93b077aa08994375907c2ca3
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jan 6 20:21:08 2020 +0100
CAMEL-14359: camel-bean is now singleton scoped by default (cache=true).
---
.../camel/component/bean/DefaultBeanProcessorFactory.java | 14 ++++++--------
.../java/org/apache/camel/language/bean/BeanLanguage.java | 10 ----------
2 files changed, 6 insertions(+), 18 deletions(-)
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java
index a9b6feb..95c3bba 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java
@@ -42,15 +42,14 @@ public final class DefaultBeanProcessorFactory implements BeanProcessorFactory {
@Override
public Processor createBeanProcessor(CamelContext camelContext, Object bean, String beanType, Class<?> beanClass, String ref,
- String method, boolean cacheBean) throws Exception {
+ String method, boolean singleton) throws Exception {
BeanProcessor answer;
Class<?> clazz = bean != null ? bean.getClass() : null;
BeanHolder beanHolder;
if (ObjectHelper.isNotEmpty(ref)) {
- // lets cache by default
- if (cacheBean) {
+ if (singleton) {
// cache the registry lookup which avoids repeat lookup in the registry
beanHolder = new RegistryBean(camelContext, ref).createCacheHolder();
// bean holder will check if the bean exists
@@ -62,7 +61,6 @@ public final class DefaultBeanProcessorFactory implements BeanProcessorFactory {
answer = new BeanProcessor(beanHolder);
} else {
if (bean == null) {
-
if (beanType == null && beanClass == null) {
throw new IllegalArgumentException("bean, ref or beanType must be provided");
}
@@ -79,7 +77,7 @@ public final class DefaultBeanProcessorFactory implements BeanProcessorFactory {
}
// attempt to create bean using injector which supports auto-wiring
- if (cacheBean && camelContext.getInjector().supportsAutoWiring()) {
+ if (singleton && camelContext.getInjector().supportsAutoWiring()) {
try {
LOG.debug("Attempting to create new bean instance from class: {} via auto-wiring enabled", clazz);
bean = CamelContextHelper.newInstance(camelContext, clazz);
@@ -89,7 +87,7 @@ public final class DefaultBeanProcessorFactory implements BeanProcessorFactory {
}
// create a bean if there is a default public no-arg constructor
- if (bean == null && cacheBean && ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
+ if (bean == null && singleton && ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
LOG.debug("Class has default no-arg constructor so creating a new bean instance: {}", clazz);
bean = CamelContextHelper.newInstance(camelContext, clazz);
ObjectHelper.notNull(bean, "bean", this);
@@ -107,7 +105,7 @@ public final class DefaultBeanProcessorFactory implements BeanProcessorFactory {
if (bean != null) {
beanHolder = new ConstantBeanHolder(bean, camelContext);
} else {
- if (cacheBean && ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
+ if (singleton && ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
// we can only cache if we can create an instance of the bean, and for that we need a public constructor
beanHolder = new ConstantTypeBeanHolder(clazz, camelContext).createCacheHolder();
} else {
@@ -129,7 +127,7 @@ public final class DefaultBeanProcessorFactory implements BeanProcessorFactory {
// check there is a method with the given name, and leverage BeanInfo for that
// which we only do if we are caching the bean as otherwise we will create a bean instance for this check
// which we only want to do if we cache the bean
- if (cacheBean) {
+ if (singleton) {
BeanInfo beanInfo = beanHolder.getBeanInfo();
if (bean != null) {
// there is a bean instance, so check for any methods
diff --git a/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanLanguage.java b/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanLanguage.java
index 18b3f18..a230806 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanLanguage.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanLanguage.java
@@ -122,16 +122,6 @@ public class BeanLanguage extends LanguageSupport {
return new BeanExpression(beanName, method);
}
- public Expression createExpression(Object bean, String method) {
- org.apache.camel.util.ObjectHelper.notNull(bean, "bean");
- if (bean instanceof String) {
- String beanName = (String) bean;
- return new BeanExpression(beanName, method);
- } else {
- return new BeanExpression(bean, method);
- }
- }
-
@Override
public boolean isSingleton() {
return false;
[camel] 02/08: CAMEL-14359: camel-bean is now singleton scoped by
default (cache=true).
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch bean-scope
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 000fbb01af6676b6f21cc5187cdf296f5ea09965
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jan 6 11:50:52 2020 +0100
CAMEL-14359: camel-bean is now singleton scoped by default (cache=true).
---
.../camel-bean/src/main/docs/bean-component.adoc | 10 ++--
.../camel-bean/src/main/docs/class-component.adoc | 10 ++--
.../component/bean/AbstractBeanProcessor.java | 12 ++---
.../apache/camel/component/bean/BeanComponent.java | 35 ++++++++++----
.../apache/camel/component/bean/BeanEndpoint.java | 49 ++++++++++++--------
.../apache/camel/component/bean/BeanProcessor.java | 8 ++--
.../apache/camel/component/bean/RegistryBean.java | 4 +-
.../camel/component/beanclass/ClassEndpoint.java | 2 +-
.../apache/camel/spring/config/errorHandler.xml | 2 +-
.../org/apache/camel/spi/BeanProcessorFactory.java | 4 +-
.../org/apache/camel/model/BeanDefinition.java | 25 ++++++++--
.../apache/camel/model/ProcessorDefinition.java | 54 ++++++++--------------
.../java/org/apache/camel/reifier/BeanReifier.java | 6 +--
.../org/apache/camel/reifier/ProcessorReifier.java | 1 -
.../camel/component/bean/BeanEndpointTest.java | 12 ++---
.../camel/component/bean/BeanLifecycleTest.java | 4 +-
.../org/apache/camel/processor/BeanCachedTest.java | 1 +
...{BeanCachedTest.java => BeanSingletonTest.java} | 9 ++--
.../runtimecatalog/impl/JSonSchemaHelperTest.java | 9 ++--
.../endpoint/dsl/BeanEndpointBuilderFactory.java | 47 ++++++++++++++++---
.../endpoint/dsl/ClassEndpointBuilderFactory.java | 51 ++++++++++++++++----
.../modules/ROOT/pages/camel-3x-upgrade-guide.adoc | 9 ++++
22 files changed, 236 insertions(+), 128 deletions(-)
diff --git a/components/camel-bean/src/main/docs/bean-component.adoc b/components/camel-bean/src/main/docs/bean-component.adoc
index dbf6d3b..389a924 100644
--- a/components/camel-bean/src/main/docs/bean-component.adoc
+++ b/components/camel-bean/src/main/docs/bean-component.adoc
@@ -23,14 +23,15 @@ the Registry
// component options: START
-The Bean component supports 4 options, which are listed below.
+The Bean component supports 5 options, which are listed below.
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
-| *cache* (advanced) | If enabled, Camel will cache the result of the first Registry look-up. Cache can be enabled if the bean in the Registry is defined as a singleton scope. | | Boolean
+| *cache* (producer) | *Deprecated* Use singleton option instead. | true | Boolean
+| *singleton* (producer) | Whether to use singleton scoped beans. If enabled then the bean is created or looked up once and reused (the bean should be thread-safe). Setting this to false will let Camel create/lookup a new bean instance, per use; which acts as prototype scoped. However beware that if you lookup the bean, then the registry that holds the bean, would return a bean accordingly to its configuration, which can be singleton or prototype scoped. For example if you use Spring, or [...]
| *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean
@@ -58,15 +59,16 @@ with the following path and query parameters:
|===
-=== Query Parameters (6 parameters):
+=== Query Parameters (7 parameters):
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
-| *cache* (common) | If enabled, Camel will cache the result of the first Registry look-up. Cache can be enabled if the bean in the Registry is defined as a singleton scope. | | Boolean
+| *cache* (common) | *Deprecated* Use singleton option instead. | | Boolean
| *method* (common) | Sets the name of the method to invoke on the bean | | String
| *parameters* (common) | Used for configuring additional properties on the bean | | Map
+| *singleton* (common) | Whether to use singleton scoped beans. If enabled then the bean is created or looked up once and reused (the bean should be thread-safe). Setting this to false will let Camel create/lookup a new bean instance, per use; which acts as prototype scoped. However beware that if you lookup the bean, then the registry that holds the bean, would return a bean accordingly to its configuration, which can be singleton or prototype scoped. For example if you use Spring, or C [...]
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *basicPropertyBinding* (advanced) | Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *synchronous* (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean
diff --git a/components/camel-bean/src/main/docs/class-component.adoc b/components/camel-bean/src/main/docs/class-component.adoc
index ba2fb48..0e3fa66 100644
--- a/components/camel-bean/src/main/docs/class-component.adoc
+++ b/components/camel-bean/src/main/docs/class-component.adoc
@@ -26,14 +26,15 @@ bean.
// component options: START
-The Class component supports 4 options, which are listed below.
+The Class component supports 5 options, which are listed below.
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
-| *cache* (advanced) | If enabled, Camel will cache the result of the first Registry look-up. Cache can be enabled if the bean in the Registry is defined as a singleton scope. | | Boolean
+| *cache* (producer) | *Deprecated* Use singleton option instead. | true | Boolean
+| *singleton* (producer) | Whether to use singleton scoped beans. If enabled then the bean is created or looked up once and reused (the bean should be thread-safe). Setting this to false will let Camel create/lookup a new bean instance, per use; which acts as prototype scoped. However beware that if you lookup the bean, then the registry that holds the bean, would return a bean accordingly to its configuration, which can be singleton or prototype scoped. For example if you use Spring, or [...]
| *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean
@@ -61,15 +62,16 @@ with the following path and query parameters:
|===
-=== Query Parameters (6 parameters):
+=== Query Parameters (7 parameters):
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
-| *cache* (common) | If enabled, Camel will cache the result of the first Registry look-up. Cache can be enabled if the bean in the Registry is defined as a singleton scope. | | Boolean
+| *cache* (common) | *Deprecated* Use singleton option instead. | | Boolean
| *method* (common) | Sets the name of the method to invoke on the bean | | String
| *parameters* (common) | Used for configuring additional properties on the bean | | Map
+| *singleton* (common) | Whether to use singleton scoped beans. If enabled then the bean is created or looked up once and reused (the bean should be thread-safe). Setting this to false will let Camel create/lookup a new bean instance, per use; which acts as prototype scoped. However beware that if you lookup the bean, then the registry that holds the bean, would return a bean accordingly to its configuration, which can be singleton or prototype scoped. For example if you use Spring, or C [...]
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *basicPropertyBinding* (advanced) | Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *synchronous* (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java
index 7ac80a8..25b8ac7 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java
@@ -35,7 +35,7 @@ public abstract class AbstractBeanProcessor extends AsyncProcessorSupport {
private transient Processor processor;
private transient boolean lookupProcessorDone;
private final Object lock = new Object();
- private Boolean cache;
+ private Boolean singleton;
private String method;
private boolean shorthandMethod;
@@ -88,7 +88,7 @@ public abstract class AbstractBeanProcessor extends AsyncProcessorSupport {
Processor target = getProcessor();
if (target == null) {
// only attempt to lookup the processor once or nearly once
- boolean allowCache = cache == null || cache; // allow cache by default
+ boolean allowCache = singleton == null || singleton; // allow cache by default
if (allowCache) {
if (!lookupProcessorDone) {
synchronized (lock) {
@@ -167,12 +167,12 @@ public abstract class AbstractBeanProcessor extends AsyncProcessorSupport {
return method;
}
- public Boolean getCache() {
- return cache;
+ public Boolean getSingleton() {
+ return singleton;
}
- public void setCache(Boolean cache) {
- this.cache = cache;
+ public void setSingleton(Boolean singleton) {
+ this.singleton = singleton;
}
/**
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java
index 5b27e1b..0d0a403 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java
@@ -26,7 +26,7 @@ import org.apache.camel.support.LRUCacheFactory;
import org.apache.camel.util.PropertiesHelper;
/**
- * The <a href="http://camel.apache.org/bean.html">Bean Component</a> is for invoking Java beans from Camel.
+ * The Bean component is for invoking Java beans from Camel.
*/
@org.apache.camel.spi.annotations.Component("bean")
public class BeanComponent extends DefaultComponent {
@@ -36,9 +36,15 @@ public class BeanComponent extends DefaultComponent {
@SuppressWarnings("unchecked")
private final Map<BeanInfoCacheKey, BeanInfo> beanInfoCache = LRUCacheFactory.newLRUSoftCache(1000);
- @Metadata(label = "advanced", description = "If enabled, Camel will cache the result of the first Registry look-up."
- + " Cache can be enabled if the bean in the Registry is defined as a singleton scope.")
+ @Deprecated
+ @Metadata(defaultValue = "true", description = "Use singleton option instead.")
private Boolean cache;
+ @Metadata(defaultValue = "true", description = "Whether to use singleton scoped beans. If enabled then the bean"
+ + " is created or looked up once and reused (the bean should be thread-safe). Setting this to false will let Camel create/lookup"
+ + " a new bean instance, per use; which acts as prototype scoped. However beware that if you lookup the bean, then the registry that holds the bean, would return "
+ + " a bean accordingly to its configuration, which can be singleton or prototype scoped. For example if you use Spring, or CDI, which"
+ + " has their own settings for setting bean scopes.")
+ private Boolean singleton = Boolean.TRUE;
public BeanComponent() {
}
@@ -49,7 +55,10 @@ public class BeanComponent extends DefaultComponent {
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
BeanEndpoint endpoint = new BeanEndpoint(uri, this);
endpoint.setBeanName(remaining);
- endpoint.setCache(cache);
+ if (cache != null) {
+ endpoint.setCache(cache);
+ }
+ endpoint.setSingleton(singleton);
setProperties(endpoint, parameters);
// the bean.xxx options is for the bean
@@ -75,15 +84,21 @@ public class BeanComponent extends DefaultComponent {
beanInfoCache.clear();
}
+ @Deprecated
public Boolean getCache() {
- return cache;
+ return singleton;
}
- /**
- * If enabled, Camel will cache the result of the first Registry look-up.
- * Cache can be enabled if the bean in the Registry is defined as a singleton scope.
- */
+ @Deprecated
public void setCache(Boolean cache) {
- this.cache = cache;
+ this.singleton = cache;
+ }
+
+ public Boolean getSingleton() {
+ return singleton;
+ }
+
+ public void setSingleton(Boolean singleton) {
+ this.singleton = singleton;
}
}
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
index 8ceac11..0e3b954 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
@@ -40,9 +40,15 @@ public class BeanEndpoint extends DefaultEndpoint {
private String beanName;
@UriParam(label = "common", description = "Sets the name of the method to invoke on the bean")
private String method;
- @UriParam(label = "common", description = "If enabled, Camel will cache the result of the first Registry look-up."
- + " Cache can be enabled if the bean in the Registry is defined as a singleton scope.")
+ @Deprecated
+ @UriParam(label = "common", description = "Use singleton option instead.")
private Boolean cache;
+ @UriParam(label = "common", defaultValue = "true", description = "Whether to use singleton scoped beans. If enabled then the bean"
+ + " is created or looked up once and reused (the bean should be thread-safe). Setting this to false will let Camel create/lookup"
+ + " a new bean instance, per use; which acts as prototype scoped. However beware that if you lookup the bean, then the registry that holds the bean, would return "
+ + " a bean accordingly to its configuration, which can be singleton or prototype scoped. For example if you use Spring, or CDI, which"
+ + " has their own settings for setting bean scopes.")
+ private Boolean singleton = Boolean.TRUE;
@UriParam(prefix = "bean.", label = "common", description = "Used for configuring additional properties on the bean", multiValue = true)
private Map<String, Object> parameters;
@@ -83,7 +89,8 @@ public class BeanEndpoint extends DefaultEndpoint {
BeanHolder holder = getBeanHolder();
if (holder == null) {
RegistryBean registryBean = new RegistryBean(getCamelContext(), beanName);
- if (isCache()) {
+ if (isSingleton()) {
+ // if singleton then create a cached holder that use the same singleton instance
holder = registryBean.createCacheHolder();
} else {
holder = registryBean;
@@ -93,7 +100,7 @@ public class BeanEndpoint extends DefaultEndpoint {
if (method != null) {
processor.setMethod(method);
}
- processor.setCache(cache);
+ processor.setSingleton(singleton);
if (parameters != null) {
setProperties(processor, parameters);
}
@@ -113,36 +120,41 @@ public class BeanEndpoint extends DefaultEndpoint {
return beanName;
}
- /**
- * Sets the name of the bean to invoke
- */
public void setBeanName(String beanName) {
this.beanName = beanName;
}
+ @Deprecated
public boolean isCache() {
- return cache != null ? cache : false;
+ return isSingleton();
}
+ @Deprecated
public Boolean getCache() {
- return cache;
+ return getSingleton();
}
- /**
- * If enabled, Camel will cache the result of the first Registry look-up.
- * Cache can be enabled if the bean in the Registry is defined as a singleton scope.
- */
+ @Deprecated
public void setCache(Boolean cache) {
- this.cache = cache;
+ setSingleton(cache);
+ }
+
+ public boolean isSingleton() {
+ return singleton != null ? singleton : false;
+ }
+
+ public Boolean getSingleton() {
+ return singleton;
+ }
+
+ public void setSingleton(Boolean singleton) {
+ this.singleton = singleton;
}
public String getMethod() {
return method;
}
- /**
- * Sets the name of the method to invoke on the bean
- */
public void setMethod(String method) {
this.method = method;
}
@@ -159,9 +171,6 @@ public class BeanEndpoint extends DefaultEndpoint {
return parameters;
}
- /**
- * Used for configuring additional properties on the bean
- */
public void setParameters(Map<String, Object> parameters) {
this.parameters = parameters;
}
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanProcessor.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
index b545deb..b9ad238 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
@@ -76,12 +76,12 @@ public class BeanProcessor extends ServiceSupport implements AsyncProcessor {
return delegate.getMethod();
}
- public Boolean getCache() {
- return delegate.getCache();
+ public Boolean getSingleton() {
+ return delegate.getSingleton();
}
- public void setCache(Boolean cache) {
- delegate.setCache(cache);
+ public void setSingleton(Boolean singleton) {
+ delegate.setSingleton(singleton);
}
public void setMethod(String method) {
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/RegistryBean.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/RegistryBean.java
index b871c8f..e4f29cf 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/RegistryBean.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/RegistryBean.java
@@ -59,9 +59,7 @@ public class RegistryBean implements BeanHolder {
}
/**
- * Creates a cached and constant {@link org.apache.camel.component.bean.BeanHolder} from this holder.
- *
- * @return a new {@link org.apache.camel.component.bean.BeanHolder} that has cached the lookup of the bean.
+ * Creates a singleton (cached and constant) {@link org.apache.camel.component.bean.BeanHolder} from this holder.
*/
public ConstantBeanHolder createCacheHolder() {
Object bean = getBean();
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/beanclass/ClassEndpoint.java b/components/camel-bean/src/main/java/org/apache/camel/component/beanclass/ClassEndpoint.java
index 508d61a..5f85a65 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/beanclass/ClassEndpoint.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/beanclass/ClassEndpoint.java
@@ -21,7 +21,7 @@ import org.apache.camel.component.bean.BeanEndpoint;
import org.apache.camel.spi.UriEndpoint;
/**
- * The <a href="http://camel.apache.org/class.html">class component</a> is for invoking Java classes (Java beans) from Camel.
+ * The Class component is for invoking Java classes (Java beans) from Camel.
*/
@UriEndpoint(firstVersion = "2.4.0", scheme = "class", title = "Class", syntax = "class:beanName", producerOnly = true, label = "core,java")
public class ClassEndpoint extends BeanEndpoint {
diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/config/errorHandler.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/config/errorHandler.xml
index 6fef7a0..17d9b80 100644
--- a/components/camel-spring/src/test/resources/org/apache/camel/spring/config/errorHandler.xml
+++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/config/errorHandler.xml
@@ -38,7 +38,7 @@
</camelContext>
<bean id="deadLetterErrorHandler" class="org.apache.camel.builder.DeadLetterChannelBuilder">
- <property name="deadLetterUri" value="bean:exceptionProcessor"/>
+ <property name="deadLetterUri" value="mock:exceptionProcessor"/>
<property name="redeliveryPolicy" ref="rsRedeliveryPolicyConfig"/>
</bean>
diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/BeanProcessorFactory.java b/core/camel-api/src/main/java/org/apache/camel/spi/BeanProcessorFactory.java
index aa6e7f0..1de2b82 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/BeanProcessorFactory.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/BeanProcessorFactory.java
@@ -50,10 +50,10 @@ public interface BeanProcessorFactory {
* @param beanClass or the bean class
* @param ref or bean reference to lookup the bean from the registry
* @param method optional name of method to invoke
- * @param cacheBean whether to cache lookup up the bean
+ * @param singleton whether to use singleton scoped bean
* @return the created processor
* @throws Exception is thrown if error creating the processor
*/
Processor createBeanProcessor(CamelContext camelContext, Object bean, String beanType, Class<?> beanClass, String ref,
- String method, boolean cacheBean) throws Exception;
+ String method, boolean singleton) throws Exception;
}
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/model/BeanDefinition.java b/core/camel-core-engine/src/main/java/org/apache/camel/model/BeanDefinition.java
index 701497c..00b3c20 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/model/BeanDefinition.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/model/BeanDefinition.java
@@ -40,7 +40,11 @@ public class BeanDefinition extends NoOutputDefinition<BeanDefinition> {
private String beanType;
@XmlAttribute
@Metadata(defaultValue = "true", javaType = "java.lang.Boolean")
+ @Deprecated
private String cache;
+ @XmlAttribute
+ @Metadata(defaultValue = "true", javaType = "java.lang.Boolean")
+ private String singleton;
@XmlTransient
private Class<?> beanClass;
@XmlTransient
@@ -147,14 +151,29 @@ public class BeanDefinition extends NoOutputDefinition<BeanDefinition> {
}
public String getCache() {
- return cache;
+ return getSingleton();
}
/**
- * Caches the bean lookup, to avoid lookup up bean on every usage.
+ * Use singleton option instead
*/
public void setCache(String cache) {
- this.cache = cache;
+ setSingleton(cache);
+ }
+
+ public String getSingleton() {
+ return singleton;
+ }
+
+ /**
+ * Whether to use singleton scoped beans. If enabled then the bean is created or looked up once and reused (the bean should be thread-safe).
+ * Setting this to false will let Camel create/lookup a new bean instance, per use; which acts as prototype scoped.
+ * However beware that if you lookup the bean, then the registry that holds the bean, would return a bean
+ * accordingly to its configuration, which can be singleton or prototype scoped.
+ * For example if you use Spring, or CDI, which has their own settings for setting bean scopes.
+ */
+ public void setSingleton(String singleton) {
+ this.singleton = singleton;
}
// Fluent API
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/model/ProcessorDefinition.java b/core/camel-core-engine/src/main/java/org/apache/camel/model/ProcessorDefinition.java
index 5535384..7c67196 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/model/ProcessorDefinition.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/model/ProcessorDefinition.java
@@ -2499,10 +2499,8 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>
* Translator EIP:</a> Adds a bean which is invoked which could be a final
* destination, or could be a transformation in a pipeline
*
- * @param bean the bean to invoke, or a reference to a bean if the type is a
- * String
- * @param method the method name to invoke on the bean (can be used to avoid
- * ambiguity)
+ * @param bean the bean to invoke, or a reference to a bean if the type is a String
+ * @param method the method name to invoke on the bean (can be used to avoid ambiguity)
* @return the builder
*/
public Type bean(Supplier<Object> bean, String method) {
@@ -2514,21 +2512,18 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>
* Translator EIP:</a> Adds a bean which is invoked which could be a final
* destination, or could be a transformation in a pipeline
*
- * @param bean the bean to invoke, or a reference to a bean if the type is a
- * String
- * @param cache if enabled, Camel will cache the result of the first
- * Registry look-up. Cache can be enabled if the bean in the
- * Registry is defined as a singleton scope. the multi parameter
+ * @param bean the bean to invoke, or a reference to a bean if the type is a String
+ * @param singleton whether to use singleton scoped beans, or not
* @return the builder
*/
- public Type bean(Object bean, boolean cache) {
+ public Type bean(Object bean, boolean singleton) {
BeanDefinition answer = new BeanDefinition();
if (bean instanceof String) {
answer.setRef((String)bean);
} else {
answer.setBean(bean);
}
- answer.setCache(Boolean.toString(cache));
+ answer.setSingleton(Boolean.toString(singleton));
addOutput(answer);
return asType();
}
@@ -2538,16 +2533,12 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>
* Translator EIP:</a> Adds a bean which is invoked which could be a final
* destination, or could be a transformation in a pipeline
*
- * @param bean the bean to invoke, or a reference to a bean if the type is a
- * String
- * @param method the method name to invoke on the bean (can be used to avoid
- * ambiguity)
- * @param cache if enabled, Camel will cache the result of the first
- * Registry look-up. Cache can be enabled if the bean in the
- * Registry is defined as a singleton scope. the multi parameter
+ * @param bean the bean to invoke, or a reference to a bean if the type is a String
+ * @param method the method name to invoke on the bean (can be used to avoid ambiguity)
+ * @param singleton whether to use singleton scoped beans, or not
* @return the builder
*/
- public Type bean(Object bean, String method, boolean cache) {
+ public Type bean(Object bean, String method, boolean singleton) {
BeanDefinition answer = new BeanDefinition();
if (bean instanceof String) {
answer.setRef((String)bean);
@@ -2555,7 +2546,7 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>
answer.setBean(bean);
}
answer.setMethod(method);
- answer.setCache(Boolean.toString(cache));
+ answer.setSingleton(Boolean.toString(singleton));
addOutput(answer);
return asType();
}
@@ -2565,8 +2556,7 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>
* Translator EIP:</a> Adds a bean which is invoked which could be a final
* destination, or could be a transformation in a pipeline
*
- * @param beanType the bean class, Camel will instantiate an object at
- * runtime
+ * @param beanType the bean class, Camel will instantiate an object at runtime
* @return the builder
*/
public Type bean(Class<?> beanType) {
@@ -2581,10 +2571,8 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>
* Translator EIP:</a> Adds a bean which is invoked which could be a final
* destination, or could be a transformation in a pipeline
*
- * @param beanType the bean class, Camel will instantiate an object at
- * runtime
- * @param method the method name to invoke on the bean (can be used to avoid
- * ambiguity)
+ * @param beanType the bean class, Camel will instantiate an object at runtime
+ * @param method the method name to invoke on the bean (can be used to avoid ambiguity)
* @return the builder
*/
public Type bean(Class<?> beanType, String method) {
@@ -2600,20 +2588,16 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>
* Translator EIP:</a> Adds a bean which is invoked which could be a final
* destination, or could be a transformation in a pipeline
*
- * @param beanType the bean class, Camel will instantiate an object at
- * runtime
- * @param method the method name to invoke on the bean (can be used to avoid
- * ambiguity)
- * @param cache if enabled, Camel will cache the result of the first
- * Registry look-up. Cache can be enabled if the bean in the
- * Registry is defined as a singleton scope. the multi parameter
+ * @param beanType the bean class, Camel will instantiate an object at runtime
+ * @param method the method name to invoke on the bean (can be used to avoid ambiguity)
+ * @param singleton whether to use singleton scoped beans, or not
* @return the builder
*/
- public Type bean(Class<?> beanType, String method, boolean cache) {
+ public Type bean(Class<?> beanType, String method, boolean singleton) {
BeanDefinition answer = new BeanDefinition();
answer.setBeanType(beanType);
answer.setMethod(method);
- answer.setCache(Boolean.toString(cache));
+ answer.setSingleton(Boolean.toString(singleton));
addOutput(answer);
return asType();
}
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/BeanReifier.java b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/BeanReifier.java
index e7db558..7fac849 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/BeanReifier.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/BeanReifier.java
@@ -44,11 +44,11 @@ public class BeanReifier extends ProcessorReifier<BeanDefinition> {
if (fac == null) {
throw new IllegalStateException("Cannot find BeanProcessorFactory. Make sure camel-bean is on the classpath.");
}
- return fac.createBeanProcessor(camelContext, bean, beanType, beanClass, ref, method, isCacheBean());
+ return fac.createBeanProcessor(camelContext, bean, beanType, beanClass, ref, method, isSingletonBean());
}
- private boolean isCacheBean() {
- return definition.getCache() == null || Boolean.parseBoolean(definition.getCache());
+ private boolean isSingletonBean() {
+ return definition.getSingleton() == null || Boolean.parseBoolean(definition.getSingleton());
}
}
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/ProcessorReifier.java b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/ProcessorReifier.java
index d831188..a2b7cd3 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/ProcessorReifier.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/ProcessorReifier.java
@@ -27,7 +27,6 @@ import org.apache.camel.Channel;
import org.apache.camel.ErrorHandlerFactory;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.Processor;
-import org.apache.camel.RouteAware;
import org.apache.camel.model.AggregateDefinition;
import org.apache.camel.model.BeanDefinition;
import org.apache.camel.model.CatchDefinition;
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanEndpointTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanEndpointTest.java
index bb4c44e..06750b7 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanEndpointTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanEndpointTest.java
@@ -44,7 +44,7 @@ public class BeanEndpointTest extends ContextTestSupport {
endpoint.setBeanName("foo");
assertEquals("foo", endpoint.getBeanName());
- assertEquals(false, endpoint.isCache());
+ assertEquals(true, endpoint.isSingleton());
assertNull(endpoint.getBeanHolder());
assertNull(endpoint.getMethod());
assertEquals("bean:foo", endpoint.getEndpointUri());
@@ -70,7 +70,7 @@ public class BeanEndpointTest extends ContextTestSupport {
endpoint.setBeanName("foo");
assertEquals("foo", endpoint.getBeanName());
- assertEquals(false, endpoint.isCache());
+ assertEquals(true, endpoint.isSingleton());
assertNull(endpoint.getBeanHolder());
assertNull(endpoint.getMethod());
assertEquals("bean:foo", endpoint.getEndpointUri());
@@ -98,7 +98,7 @@ public class BeanEndpointTest extends ContextTestSupport {
endpoint.setBeanName("foo");
assertEquals("foo", endpoint.getBeanName());
- assertEquals(false, endpoint.isCache());
+ assertEquals(true, endpoint.isSingleton());
assertNull(endpoint.getBeanHolder());
assertNull(endpoint.getMethod());
assertEquals("bean:foo", endpoint.getEndpointUri());
@@ -124,7 +124,7 @@ public class BeanEndpointTest extends ContextTestSupport {
endpoint.setMethod("hello");
assertEquals("foo", endpoint.getBeanName());
- assertEquals(false, endpoint.isCache());
+ assertEquals(true, endpoint.isSingleton());
assertNull(endpoint.getBeanHolder());
assertEquals("hello", endpoint.getMethod());
assertEquals("bean:foo?method=hello", endpoint.getEndpointUri());
@@ -151,7 +151,7 @@ public class BeanEndpointTest extends ContextTestSupport {
endpoint.setMethod("hello");
assertEquals("foo", endpoint.getBeanName());
- assertEquals(true, endpoint.isCache());
+ assertEquals(true, endpoint.isSingleton());
assertNull(endpoint.getBeanHolder());
assertEquals("hello", endpoint.getMethod());
assertEquals("bean:foo?method=hello", endpoint.getEndpointUri());
@@ -179,7 +179,7 @@ public class BeanEndpointTest extends ContextTestSupport {
BeanHolder holder = new RegistryBean(context, "foo");
endpoint.setBeanHolder(holder);
- assertEquals(false, endpoint.isCache());
+ assertEquals(true, endpoint.isSingleton());
assertEquals(holder, endpoint.getBeanHolder());
context.addRoutes(new RouteBuilder() {
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanLifecycleTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanLifecycleTest.java
index f7674cb..6154056 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanLifecycleTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanLifecycleTest.java
@@ -83,9 +83,9 @@ public class BeanLifecycleTest extends ContextTestSupport {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
- from("direct:foo").routeId("foo").bean(statefulInstance, "doSomething").bean(MyStatefulBean.class, "doSomething")
+ from("direct:foo").routeId("foo").bean(statefulInstance, "doSomething", false).bean(MyStatefulBean.class, "doSomething")
.bean(MyStatefulBean.class.getName(), "doSomething", true).bean(MyStatelessBean.class.getName(), "doSomething", false)
- .to("bean:statefulInstanceInRegistry?method=doSomething&cache=true").to("bean:statefulInstanceInRegistryNoCache?method=doSomething&cache=false")
+ .to("bean:statefulInstanceInRegistry?method=doSomething&singleton=true").to("bean:statefulInstanceInRegistryNoCache?method=doSomething&singleton=false")
.to("mock:result");
}
};
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/BeanCachedTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/BeanCachedTest.java
index 69ef1e6..9f1e053 100644
--- a/core/camel-core/src/test/java/org/apache/camel/processor/BeanCachedTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/BeanCachedTest.java
@@ -25,6 +25,7 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.JndiRegistry;
import org.junit.Test;
+@Deprecated
public class BeanCachedTest extends ContextTestSupport {
private Context context;
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/BeanCachedTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/BeanSingletonTest.java
similarity index 92%
copy from core/camel-core/src/test/java/org/apache/camel/processor/BeanCachedTest.java
copy to core/camel-core/src/test/java/org/apache/camel/processor/BeanSingletonTest.java
index 69ef1e6..b5104be 100644
--- a/core/camel-core/src/test/java/org/apache/camel/processor/BeanCachedTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/BeanSingletonTest.java
@@ -25,7 +25,7 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.JndiRegistry;
import org.junit.Test;
-public class BeanCachedTest extends ContextTestSupport {
+public class BeanSingletonTest extends ContextTestSupport {
private Context context;
@@ -36,9 +36,8 @@ public class BeanCachedTest extends ContextTestSupport {
return new RouteBuilder() {
@Override
public void configure() {
- from("direct:noCache").to("bean:something?cache=false");
- from("direct:cached").to("bean:something?cache=true");
-
+ from("direct:noCache").to("bean:something?singleton=false");
+ from("direct:cached").to("bean:something?singleton=true");
}
};
}
@@ -65,7 +64,7 @@ public class BeanCachedTest extends ContextTestSupport {
}
@Test
- public void testBeanWithCached() throws Exception {
+ public void testBeanWithSingleton() throws Exception {
// Just make sure the bean processor doesn't work if the cached is false
MyBean originalInstance = registry.lookupByNameAndType("something", MyBean.class);
template.sendBody("direct:cached", null);
diff --git a/core/camel-core/src/test/java/org/apache/camel/runtimecatalog/impl/JSonSchemaHelperTest.java b/core/camel-core/src/test/java/org/apache/camel/runtimecatalog/impl/JSonSchemaHelperTest.java
index a9c3c44..b0d113f 100644
--- a/core/camel-core/src/test/java/org/apache/camel/runtimecatalog/impl/JSonSchemaHelperTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/runtimecatalog/impl/JSonSchemaHelperTest.java
@@ -54,14 +54,15 @@ public class JSonSchemaHelperTest extends ContextTestSupport {
assertEquals("version", rows.get(16).keySet().iterator().next());
rows = JSonSchemaHelper.parseJsonSchema("properties", json, true);
- assertEquals(7, rows.size());
+ assertEquals(8, rows.size());
assertEquals("beanName", rows.get(0).values().iterator().next());
assertEquals("cache", rows.get(1).values().iterator().next());
assertEquals("method", rows.get(2).values().iterator().next());
assertEquals("parameters", rows.get(3).values().iterator().next());
- assertEquals("lazyStartProducer", rows.get(4).values().iterator().next());
- assertEquals("basicPropertyBinding", rows.get(5).values().iterator().next());
- assertEquals("synchronous", rows.get(6).values().iterator().next());
+ assertEquals("singleton", rows.get(4).values().iterator().next());
+ assertEquals("lazyStartProducer", rows.get(5).values().iterator().next());
+ assertEquals("basicPropertyBinding", rows.get(6).values().iterator().next());
+ assertEquals("synchronous", rows.get(7).values().iterator().next());
}
@Test
diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BeanEndpointBuilderFactory.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BeanEndpointBuilderFactory.java
index 2b2654b..471c756 100644
--- a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BeanEndpointBuilderFactory.java
+++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BeanEndpointBuilderFactory.java
@@ -39,28 +39,26 @@ public interface BeanEndpointBuilderFactory {
return (AdvancedBeanEndpointBuilder) this;
}
/**
- * If enabled, Camel will cache the result of the first Registry
- * look-up. Cache can be enabled if the bean in the Registry is defined
- * as a singleton scope.
+ * Use singleton option instead.
*
* The option is a: <code>java.lang.Boolean</code> type.
*
* Group: common
*/
+ @Deprecated
default BeanEndpointBuilder cache(Boolean cache) {
doSetProperty("cache", cache);
return this;
}
/**
- * If enabled, Camel will cache the result of the first Registry
- * look-up. Cache can be enabled if the bean in the Registry is defined
- * as a singleton scope.
+ * Use singleton option instead.
*
* The option will be converted to a <code>java.lang.Boolean</code>
* type.
*
* Group: common
*/
+ @Deprecated
default BeanEndpointBuilder cache(String cache) {
doSetProperty("cache", cache);
return this;
@@ -102,6 +100,43 @@ public interface BeanEndpointBuilderFactory {
return this;
}
/**
+ * Whether to use singleton scoped beans. If enabled then the bean is
+ * created or looked up once and reused (the bean should be
+ * thread-safe). Setting this to false will let Camel create/lookup a
+ * new bean instance, per use; which acts as prototype scoped. However
+ * beware that if you lookup the bean, then the registry that holds the
+ * bean, would return a bean accordingly to its configuration, which can
+ * be singleton or prototype scoped. For example if you use Spring, or
+ * CDI, which has their own settings for setting bean scopes.
+ *
+ * The option is a: <code>java.lang.Boolean</code> type.
+ *
+ * Group: common
+ */
+ default BeanEndpointBuilder singleton(Boolean singleton) {
+ doSetProperty("singleton", singleton);
+ return this;
+ }
+ /**
+ * Whether to use singleton scoped beans. If enabled then the bean is
+ * created or looked up once and reused (the bean should be
+ * thread-safe). Setting this to false will let Camel create/lookup a
+ * new bean instance, per use; which acts as prototype scoped. However
+ * beware that if you lookup the bean, then the registry that holds the
+ * bean, would return a bean accordingly to its configuration, which can
+ * be singleton or prototype scoped. For example if you use Spring, or
+ * CDI, which has their own settings for setting bean scopes.
+ *
+ * The option will be converted to a <code>java.lang.Boolean</code>
+ * type.
+ *
+ * Group: common
+ */
+ default BeanEndpointBuilder singleton(String singleton) {
+ doSetProperty("singleton", singleton);
+ return this;
+ }
+ /**
* Whether the producer should be started lazy (on the first message).
* By starting lazy you can use this to allow CamelContext and routes to
* startup in situations where a producer may otherwise fail during
diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/ClassEndpointBuilderFactory.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/ClassEndpointBuilderFactory.java
index d8ed2bd..2718f10 100644
--- a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/ClassEndpointBuilderFactory.java
+++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/ClassEndpointBuilderFactory.java
@@ -23,7 +23,7 @@ import org.apache.camel.builder.EndpointProducerBuilder;
import org.apache.camel.builder.endpoint.AbstractEndpointBuilder;
/**
- * The class component is for invoking Java classes (Java beans) from Camel.
+ * The Class component is for invoking Java classes (Java beans) from Camel.
*
* Generated by camel-package-maven-plugin - do not edit this file!
*/
@@ -39,28 +39,26 @@ public interface ClassEndpointBuilderFactory {
return (AdvancedClassEndpointBuilder) this;
}
/**
- * If enabled, Camel will cache the result of the first Registry
- * look-up. Cache can be enabled if the bean in the Registry is defined
- * as a singleton scope.
+ * Use singleton option instead.
*
* The option is a: <code>java.lang.Boolean</code> type.
*
* Group: common
*/
+ @Deprecated
default ClassEndpointBuilder cache(Boolean cache) {
doSetProperty("cache", cache);
return this;
}
/**
- * If enabled, Camel will cache the result of the first Registry
- * look-up. Cache can be enabled if the bean in the Registry is defined
- * as a singleton scope.
+ * Use singleton option instead.
*
* The option will be converted to a <code>java.lang.Boolean</code>
* type.
*
* Group: common
*/
+ @Deprecated
default ClassEndpointBuilder cache(String cache) {
doSetProperty("cache", cache);
return this;
@@ -102,6 +100,43 @@ public interface ClassEndpointBuilderFactory {
return this;
}
/**
+ * Whether to use singleton scoped beans. If enabled then the bean is
+ * created or looked up once and reused (the bean should be
+ * thread-safe). Setting this to false will let Camel create/lookup a
+ * new bean instance, per use; which acts as prototype scoped. However
+ * beware that if you lookup the bean, then the registry that holds the
+ * bean, would return a bean accordingly to its configuration, which can
+ * be singleton or prototype scoped. For example if you use Spring, or
+ * CDI, which has their own settings for setting bean scopes.
+ *
+ * The option is a: <code>java.lang.Boolean</code> type.
+ *
+ * Group: common
+ */
+ default ClassEndpointBuilder singleton(Boolean singleton) {
+ doSetProperty("singleton", singleton);
+ return this;
+ }
+ /**
+ * Whether to use singleton scoped beans. If enabled then the bean is
+ * created or looked up once and reused (the bean should be
+ * thread-safe). Setting this to false will let Camel create/lookup a
+ * new bean instance, per use; which acts as prototype scoped. However
+ * beware that if you lookup the bean, then the registry that holds the
+ * bean, would return a bean accordingly to its configuration, which can
+ * be singleton or prototype scoped. For example if you use Spring, or
+ * CDI, which has their own settings for setting bean scopes.
+ *
+ * The option will be converted to a <code>java.lang.Boolean</code>
+ * type.
+ *
+ * Group: common
+ */
+ default ClassEndpointBuilder singleton(String singleton) {
+ doSetProperty("singleton", singleton);
+ return this;
+ }
+ /**
* Whether the producer should be started lazy (on the first message).
* By starting lazy you can use this to allow CamelContext and routes to
* startup in situations where a producer may otherwise fail during
@@ -203,7 +238,7 @@ public interface ClassEndpointBuilderFactory {
}
/**
* Class (camel-bean)
- * The class component is for invoking Java classes (Java beans) from Camel.
+ * The Class component is for invoking Java classes (Java beans) from Camel.
*
* Category: core,java
* Since: 2.4
diff --git a/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide.adoc b/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide.adoc
index 00599a2..448c138 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide.adoc
@@ -11,6 +11,15 @@ from both 3.0 to 3.1 and 3.1 to 3.2.
== Upgrading Camel 3.0 to 3.1
+=== came-bean
+
+The bean component has been changed to behave as singleton scoped by default.
+This means that the bean is created or looked up once and reused.
+
+The option `cache` has been deprecated in favour of the new `singleton` option that by default is `true`. You can set this to `false` to use the old behaviour.
+
+NOTE: Setting this to false will let Camel create/lookup a new bean instance, per use; which acts as prototype scoped. However beware that if you lookup the bean, then the registry that holds the bean, would return a bean accordingly to its configuration, which can be singleton or prototype scoped. For example if you use Spring, or CDI, which has their own settings for setting bean scopes.
+
=== camel-nats
The `camel-nats` component has changed its endpoint syntax from `nats:servers` to `nats:topic`.
[camel] 06/08: CAMEL-14359: camel-bean - Adding bean scope option.
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch bean-scope
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 5e1bfe28dca3c33be93407d66c4ee4df3aba5796
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Jan 7 07:17:47 2020 +0100
CAMEL-14359: camel-bean - Adding bean scope option.
---
.../component/bean/AbstractBeanProcessor.java | 1 +
.../apache/camel/component/bean/BeanComponent.java | 3 +-
.../apache/camel/component/bean/BeanEndpoint.java | 5 +-
.../apache/camel/component/bean/BeanProcessor.java | 1 +
.../bean/DefaultBeanProcessorFactory.java | 13 ++---
.../src/main/java/org/apache/camel}/BeanScope.java | 13 +++--
.../org/apache/camel/spi/BeanProcessorFactory.java | 5 +-
.../org/apache/camel/model/BeanDefinition.java | 37 +++++++-------
.../java/org/apache/camel/model/LogDefinition.java | 2 +-
.../apache/camel/model/ProcessorDefinition.java | 56 +++++++++++++++++-----
.../java/org/apache/camel/reifier/BeanReifier.java | 13 +++--
.../bean/BeanExplicitMethodAmbiguousTest.java | 7 +--
.../camel/component/bean/BeanLifecycleTest.java | 7 +--
.../camel/component/bean/BeanNoCacheTest.java | 3 +-
.../camel/component/bean/BeanRefNoCacheTest.java | 3 +-
.../camel/component/bean/NewInstanceTest.java | 3 +-
.../bean/issues/BeanThisAdviceWithIssueTest.java | 3 +-
.../apache/camel/processor/BeanSingletonTest.java | 2 +-
.../runtimecatalog/impl/JSonSchemaHelperTest.java | 8 ++--
.../endpoint/dsl/BeanEndpointBuilderFactory.java | 16 ++-----
.../endpoint/dsl/ClassEndpointBuilderFactory.java | 16 ++-----
.../modules/ROOT/pages/bean-component.adoc | 12 +++--
.../modules/ROOT/pages/class-component.adoc | 12 +++--
docs/components/modules/ROOT/pages/index.adoc | 2 +-
.../modules/ROOT/pages/camel-3x-upgrade-guide.adoc | 4 +-
25 files changed, 139 insertions(+), 108 deletions(-)
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java
index 62deec6..c9492fd 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.bean;
import org.apache.camel.AsyncCallback;
+import org.apache.camel.BeanScope;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java
index 9bd470e..5d2d74e 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.bean;
import java.util.Map;
+import org.apache.camel.BeanScope;
import org.apache.camel.Endpoint;
import org.apache.camel.spi.Metadata;
import org.apache.camel.support.DefaultComponent;
@@ -98,7 +99,7 @@ public class BeanComponent extends DefaultComponent {
if (cache) {
scope = BeanScope.Singleton;
} else {
- scope = BeanScope.Delegate;
+ scope = BeanScope.Prototype;
}
}
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
index 1c6dd74..c733b29 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.bean;
import java.util.Map;
+import org.apache.camel.BeanScope;
import org.apache.camel.Component;
import org.apache.camel.Consumer;
import org.apache.camel.ExchangePattern;
@@ -93,7 +94,7 @@ public class BeanEndpoint extends DefaultEndpoint {
BeanHolder holder = getBeanHolder();
if (holder == null) {
RegistryBean registryBean = new RegistryBean(getCamelContext(), beanName);
- if (isSingleton()) {
+ if (scope == BeanScope.Singleton) {
// if singleton then create a cached holder that use the same singleton instance
holder = registryBean.createCacheHolder();
} else {
@@ -143,7 +144,7 @@ public class BeanEndpoint extends DefaultEndpoint {
if (cache) {
scope = BeanScope.Singleton;
} else {
- scope = BeanScope.Delegate;
+ scope = BeanScope.Prototype;
}
}
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanProcessor.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
index 72342e8..a95cd4b 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
@@ -20,6 +20,7 @@ import java.util.concurrent.CompletableFuture;
import org.apache.camel.AsyncCallback;
import org.apache.camel.AsyncProcessor;
+import org.apache.camel.BeanScope;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java
index 95c3bba..fadb831 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.bean;
import java.lang.reflect.Method;
+import org.apache.camel.BeanScope;
import org.apache.camel.CamelContext;
import org.apache.camel.Processor;
import org.apache.camel.RuntimeCamelException;
@@ -42,14 +43,14 @@ public final class DefaultBeanProcessorFactory implements BeanProcessorFactory {
@Override
public Processor createBeanProcessor(CamelContext camelContext, Object bean, String beanType, Class<?> beanClass, String ref,
- String method, boolean singleton) throws Exception {
+ String method, BeanScope scope) throws Exception {
BeanProcessor answer;
Class<?> clazz = bean != null ? bean.getClass() : null;
BeanHolder beanHolder;
if (ObjectHelper.isNotEmpty(ref)) {
- if (singleton) {
+ if (scope == BeanScope.Singleton) {
// cache the registry lookup which avoids repeat lookup in the registry
beanHolder = new RegistryBean(camelContext, ref).createCacheHolder();
// bean holder will check if the bean exists
@@ -77,7 +78,7 @@ public final class DefaultBeanProcessorFactory implements BeanProcessorFactory {
}
// attempt to create bean using injector which supports auto-wiring
- if (singleton && camelContext.getInjector().supportsAutoWiring()) {
+ if (scope == BeanScope.Singleton && camelContext.getInjector().supportsAutoWiring()) {
try {
LOG.debug("Attempting to create new bean instance from class: {} via auto-wiring enabled", clazz);
bean = CamelContextHelper.newInstance(camelContext, clazz);
@@ -87,7 +88,7 @@ public final class DefaultBeanProcessorFactory implements BeanProcessorFactory {
}
// create a bean if there is a default public no-arg constructor
- if (bean == null && singleton && ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
+ if (bean == null && scope == BeanScope.Singleton && ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
LOG.debug("Class has default no-arg constructor so creating a new bean instance: {}", clazz);
bean = CamelContextHelper.newInstance(camelContext, clazz);
ObjectHelper.notNull(bean, "bean", this);
@@ -105,7 +106,7 @@ public final class DefaultBeanProcessorFactory implements BeanProcessorFactory {
if (bean != null) {
beanHolder = new ConstantBeanHolder(bean, camelContext);
} else {
- if (singleton && ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
+ if (scope == BeanScope.Singleton && ObjectHelper.hasDefaultPublicNoArgConstructor(clazz)) {
// we can only cache if we can create an instance of the bean, and for that we need a public constructor
beanHolder = new ConstantTypeBeanHolder(clazz, camelContext).createCacheHolder();
} else {
@@ -127,7 +128,7 @@ public final class DefaultBeanProcessorFactory implements BeanProcessorFactory {
// check there is a method with the given name, and leverage BeanInfo for that
// which we only do if we are caching the bean as otherwise we will create a bean instance for this check
// which we only want to do if we cache the bean
- if (singleton) {
+ if (scope == BeanScope.Singleton) {
BeanInfo beanInfo = beanHolder.getBeanInfo();
if (bean != null) {
// there is a bean instance, so check for any methods
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanScope.java b/core/camel-api/src/main/java/org/apache/camel/BeanScope.java
similarity index 87%
rename from components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanScope.java
rename to core/camel-api/src/main/java/org/apache/camel/BeanScope.java
index 9012a55..b16808a 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanScope.java
+++ b/core/camel-api/src/main/java/org/apache/camel/BeanScope.java
@@ -14,14 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.camel.component.bean;
+package org.apache.camel;
-/**
- * Bean scopes.
- */
+import javax.xml.bind.annotation.XmlEnum;
+
+@XmlEnum
public enum BeanScope {
- Singleton,
- Delegate,
- Request;
+ Singleton, Request, Prototype;
+
}
diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/BeanProcessorFactory.java b/core/camel-api/src/main/java/org/apache/camel/spi/BeanProcessorFactory.java
index 1de2b82..256d1ae 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/BeanProcessorFactory.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/BeanProcessorFactory.java
@@ -18,6 +18,7 @@ package org.apache.camel.spi;
import java.lang.reflect.Method;
+import org.apache.camel.BeanScope;
import org.apache.camel.CamelContext;
import org.apache.camel.Processor;
@@ -50,10 +51,10 @@ public interface BeanProcessorFactory {
* @param beanClass or the bean class
* @param ref or bean reference to lookup the bean from the registry
* @param method optional name of method to invoke
- * @param singleton whether to use singleton scoped bean
+ * @param scope the scope of the bean
* @return the created processor
* @throws Exception is thrown if error creating the processor
*/
Processor createBeanProcessor(CamelContext camelContext, Object bean, String beanType, Class<?> beanClass, String ref,
- String method, boolean singleton) throws Exception;
+ String method, BeanScope scope) throws Exception;
}
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/model/BeanDefinition.java b/core/camel-core-engine/src/main/java/org/apache/camel/model/BeanDefinition.java
index 00b3c20..30cc5dc 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/model/BeanDefinition.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/model/BeanDefinition.java
@@ -22,6 +22,7 @@ import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
+import org.apache.camel.BeanScope;
import org.apache.camel.spi.Metadata;
import org.apache.camel.util.ObjectHelper;
@@ -43,8 +44,8 @@ public class BeanDefinition extends NoOutputDefinition<BeanDefinition> {
@Deprecated
private String cache;
@XmlAttribute
- @Metadata(defaultValue = "true", javaType = "java.lang.Boolean")
- private String singleton;
+ @Metadata(defaultValue = "Singleton", enums = "Singleton,Request,Prototype")
+ private String scope;
@XmlTransient
private Class<?> beanClass;
@XmlTransient
@@ -151,32 +152,34 @@ public class BeanDefinition extends NoOutputDefinition<BeanDefinition> {
}
public String getCache() {
- return getSingleton();
+ if (scope == null || BeanScope.Singleton.name().equals(scope)) {
+ return "true";
+ } else {
+ return "false";
+ }
}
/**
* Use singleton option instead
*/
public void setCache(String cache) {
- setSingleton(cache);
+ if ("true".equals(cache)) {
+ scope = BeanScope.Singleton.name();
+ } else {
+ scope = BeanScope.Prototype.name();
+ }
}
- public String getSingleton() {
- return singleton;
+ public String getScope() {
+ return scope;
}
- /**
- * Whether to use singleton scoped beans. If enabled then the bean is created or looked up once and reused (the bean should be thread-safe).
- * Setting this to false will let Camel create/lookup a new bean instance, per use; which acts as prototype scoped.
- * However beware that if you lookup the bean, then the registry that holds the bean, would return a bean
- * accordingly to its configuration, which can be singleton or prototype scoped.
- * For example if you use Spring, or CDI, which has their own settings for setting bean scopes.
- */
- public void setSingleton(String singleton) {
- this.singleton = singleton;
+ public void setScope(String scope) {
+ this.scope = scope;
}
- // Fluent API
- // -------------------------------------------------------------------------
+ public void setScope(BeanScope scope) {
+ this.scope = scope.name();
+ }
}
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/model/LogDefinition.java b/core/camel-core-engine/src/main/java/org/apache/camel/model/LogDefinition.java
index ad4c344..7a0e8cc 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/model/LogDefinition.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/model/LogDefinition.java
@@ -36,7 +36,7 @@ public class LogDefinition extends NoOutputDefinition<LogDefinition> {
@XmlAttribute(required = true)
private String message;
@XmlAttribute
- @Metadata(defaultValue = "INFO")
+ @Metadata(defaultValue = "INFO", enums = "TRACE,DEBUG,INFO,WARN,ERROR,OFF")
private String loggingLevel;
@XmlAttribute
private String logName;
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/model/ProcessorDefinition.java b/core/camel-core-engine/src/main/java/org/apache/camel/model/ProcessorDefinition.java
index 7c67196..75cc9fa 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/model/ProcessorDefinition.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/model/ProcessorDefinition.java
@@ -37,6 +37,7 @@ import javax.xml.bind.annotation.XmlTransient;
import javax.xml.namespace.QName;
import org.apache.camel.AggregationStrategy;
+import org.apache.camel.BeanScope;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
@@ -2476,10 +2477,8 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>
* Translator EIP:</a> Adds a bean which is invoked which could be a final
* destination, or could be a transformation in a pipeline
*
- * @param bean the bean to invoke, or a reference to a bean if the type is a
- * String
- * @param method the method name to invoke on the bean (can be used to avoid
- * ambiguity)
+ * @param bean the bean to invoke, or a reference to a bean if the type is a String
+ * @param method the method name to invoke on the bean (can be used to avoid ambiguity)
* @return the builder
*/
public Type bean(Object bean, String method) {
@@ -2513,17 +2512,31 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>
* destination, or could be a transformation in a pipeline
*
* @param bean the bean to invoke, or a reference to a bean if the type is a String
- * @param singleton whether to use singleton scoped beans, or not
+ * @param method the method name to invoke on the bean (can be used to avoid ambiguity)
+ * @param scope bean scope to use (singleton, request, prototype)
+ * @return the builder
+ */
+ public Type bean(Supplier<Object> bean, String method, BeanScope scope) {
+ return bean(bean.get(), method, scope);
+ }
+
+ /**
+ * <a href="http://camel.apache.org/message-translator.html">Message
+ * Translator EIP:</a> Adds a bean which is invoked which could be a final
+ * destination, or could be a transformation in a pipeline
+ *
+ * @param bean the bean to invoke, or a reference to a bean if the type is a String
+ * @param scope bean scope to use (singleton, request, prototype)
* @return the builder
*/
- public Type bean(Object bean, boolean singleton) {
+ public Type bean(Object bean, BeanScope scope) {
BeanDefinition answer = new BeanDefinition();
if (bean instanceof String) {
answer.setRef((String)bean);
} else {
answer.setBean(bean);
}
- answer.setSingleton(Boolean.toString(singleton));
+ answer.setScope(scope);
addOutput(answer);
return asType();
}
@@ -2535,10 +2548,10 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>
*
* @param bean the bean to invoke, or a reference to a bean if the type is a String
* @param method the method name to invoke on the bean (can be used to avoid ambiguity)
- * @param singleton whether to use singleton scoped beans, or not
+ * @param scope bean scope to use (singleton, request, prototype)
* @return the builder
*/
- public Type bean(Object bean, String method, boolean singleton) {
+ public Type bean(Object bean, String method, BeanScope scope) {
BeanDefinition answer = new BeanDefinition();
if (bean instanceof String) {
answer.setRef((String)bean);
@@ -2546,7 +2559,7 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>
answer.setBean(bean);
}
answer.setMethod(method);
- answer.setSingleton(Boolean.toString(singleton));
+ answer.setScope(scope);
addOutput(answer);
return asType();
}
@@ -2572,6 +2585,23 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>
* destination, or could be a transformation in a pipeline
*
* @param beanType the bean class, Camel will instantiate an object at runtime
+ * @param scope bean scope to use (singleton, request, prototype)
+ * @return the builder
+ */
+ public Type bean(Class<?> beanType, BeanScope scope) {
+ BeanDefinition answer = new BeanDefinition();
+ answer.setBeanType(beanType);
+ answer.setScope(scope);
+ addOutput(answer);
+ return asType();
+ }
+
+ /**
+ * <a href="http://camel.apache.org/message-translator.html">Message
+ * Translator EIP:</a> Adds a bean which is invoked which could be a final
+ * destination, or could be a transformation in a pipeline
+ *
+ * @param beanType the bean class, Camel will instantiate an object at runtime
* @param method the method name to invoke on the bean (can be used to avoid ambiguity)
* @return the builder
*/
@@ -2590,14 +2620,14 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type>
*
* @param beanType the bean class, Camel will instantiate an object at runtime
* @param method the method name to invoke on the bean (can be used to avoid ambiguity)
- * @param singleton whether to use singleton scoped beans, or not
+ * @param scope bean scope to use (singleton, request, prototype)
* @return the builder
*/
- public Type bean(Class<?> beanType, String method, boolean singleton) {
+ public Type bean(Class<?> beanType, String method, BeanScope scope) {
BeanDefinition answer = new BeanDefinition();
answer.setBeanType(beanType);
answer.setMethod(method);
- answer.setSingleton(Boolean.toString(singleton));
+ answer.setScope(scope);
addOutput(answer);
return asType();
}
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/BeanReifier.java b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/BeanReifier.java
index 7fac849..8a21e51 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/BeanReifier.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/BeanReifier.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.reifier;
+import org.apache.camel.BeanScope;
import org.apache.camel.CamelContext;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.Processor;
@@ -23,6 +24,7 @@ import org.apache.camel.model.BeanDefinition;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.spi.BeanProcessorFactory;
import org.apache.camel.spi.RouteContext;
+import org.apache.camel.support.CamelContextHelper;
public class BeanReifier extends ProcessorReifier<BeanDefinition> {
@@ -44,11 +46,12 @@ public class BeanReifier extends ProcessorReifier<BeanDefinition> {
if (fac == null) {
throw new IllegalStateException("Cannot find BeanProcessorFactory. Make sure camel-bean is on the classpath.");
}
- return fac.createBeanProcessor(camelContext, bean, beanType, beanClass, ref, method, isSingletonBean());
- }
-
- private boolean isSingletonBean() {
- return definition.getSingleton() == null || Boolean.parseBoolean(definition.getSingleton());
+ // use singleton as default scope
+ BeanScope scope = BeanScope.Singleton;
+ if (definition.getScope() != null) {
+ scope = CamelContextHelper.parse(routeContext.getCamelContext(), BeanScope.class, definition.getScope());
+ }
+ return fac.createBeanProcessor(camelContext, bean, beanType, beanClass, ref, method, scope);
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java
index 9b387ce..30f0474 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.bean;
import java.io.ByteArrayInputStream;
+import org.apache.camel.BeanScope;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.JndiRegistry;
@@ -66,11 +67,11 @@ public class BeanExplicitMethodAmbiguousTest extends ContextTestSupport {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
- from("direct:hello").bean("dummy", "hello", true);
+ from("direct:hello").bean("dummy", "hello", BeanScope.Singleton);
- from("direct:bye").bean("dummy", true);
+ from("direct:bye").bean("dummy", BeanScope.Singleton);
- from("direct:foo").bean("dummy", "bar", true);
+ from("direct:foo").bean("dummy", "bar", BeanScope.Singleton);
}
};
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanLifecycleTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanLifecycleTest.java
index 6154056..6f77e3b 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanLifecycleTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanLifecycleTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.bean;
import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.camel.BeanScope;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
import org.apache.camel.Service;
@@ -83,9 +84,9 @@ public class BeanLifecycleTest extends ContextTestSupport {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
- from("direct:foo").routeId("foo").bean(statefulInstance, "doSomething", false).bean(MyStatefulBean.class, "doSomething")
- .bean(MyStatefulBean.class.getName(), "doSomething", true).bean(MyStatelessBean.class.getName(), "doSomething", false)
- .to("bean:statefulInstanceInRegistry?method=doSomething&singleton=true").to("bean:statefulInstanceInRegistryNoCache?method=doSomething&singleton=false")
+ from("direct:foo").routeId("foo").bean(statefulInstance, "doSomething", BeanScope.Prototype).bean(MyStatefulBean.class, "doSomething")
+ .bean(MyStatefulBean.class.getName(), "doSomething", BeanScope.Singleton).bean(MyStatelessBean.class.getName(), "doSomething", BeanScope.Prototype)
+ .to("bean:statefulInstanceInRegistry?method=doSomething&scope=Singleton").to("bean:statefulInstanceInRegistryNoCache?method=doSomething&scope=Prototype")
.to("mock:result");
}
};
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanNoCacheTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanNoCacheTest.java
index 752a5e6..824ff70 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanNoCacheTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanNoCacheTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.bean;
import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.camel.BeanScope;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.builder.RouteBuilder;
import org.junit.Test;
@@ -42,7 +43,7 @@ public class BeanNoCacheTest extends ContextTestSupport {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
- from("direct:start").bean(MyCoolBean.class, "doSomething", false).to("mock:result");
+ from("direct:start").bean(MyCoolBean.class, "doSomething", BeanScope.Prototype).to("mock:result");
}
};
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanRefNoCacheTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanRefNoCacheTest.java
index 8a28189..9914b8f 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanRefNoCacheTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanRefNoCacheTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.bean;
import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.camel.BeanScope;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.builder.RouteBuilder;
import org.junit.Test;
@@ -42,7 +43,7 @@ public class BeanRefNoCacheTest extends ContextTestSupport {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
- from("direct:start").bean(MyCoolBean.class.getName(), "doSomething", false).to("mock:result");
+ from("direct:start").bean(MyCoolBean.class.getName(), "doSomething", BeanScope.Prototype).to("mock:result");
}
};
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/NewInstanceTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/NewInstanceTest.java
index 988a83a..8ce6598 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/bean/NewInstanceTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/NewInstanceTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.bean;
import javax.naming.Context;
+import org.apache.camel.BeanScope;
import org.apache.camel.Body;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.ExchangePattern;
@@ -60,7 +61,7 @@ public class NewInstanceTest extends ContextTestSupport {
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
- from("direct:start").bean("myBean", false).to("mock:result");
+ from("direct:start").bean("myBean", BeanScope.Prototype).to("mock:result");
}
};
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/issues/BeanThisAdviceWithIssueTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/issues/BeanThisAdviceWithIssueTest.java
index 1309f71..f752a01 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/bean/issues/BeanThisAdviceWithIssueTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/issues/BeanThisAdviceWithIssueTest.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.bean.issues;
+import org.apache.camel.BeanScope;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
import org.apache.camel.builder.AdviceWithRouteBuilder;
@@ -38,7 +39,7 @@ public class BeanThisAdviceWithIssueTest extends ContextTestSupport {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
- from("direct:mytest").id(ROUTE_ID).bean(this, "hello", false).to("log:out");
+ from("direct:mytest").id(ROUTE_ID).bean(this, "hello", BeanScope.Prototype).to("log:out");
}
public void hello(final Exchange exchange) {
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/BeanSingletonTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/BeanSingletonTest.java
index 8f2c857..730b2af 100644
--- a/core/camel-core/src/test/java/org/apache/camel/processor/BeanSingletonTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/BeanSingletonTest.java
@@ -36,7 +36,7 @@ public class BeanSingletonTest extends ContextTestSupport {
return new RouteBuilder() {
@Override
public void configure() {
- from("direct:noCache").to("bean:something?scope=Delegate");
+ from("direct:noCache").to("bean:something?scope=Prototype");
from("direct:cached").to("bean:something?scope=Singleton");
}
};
diff --git a/core/camel-core/src/test/java/org/apache/camel/runtimecatalog/impl/JSonSchemaHelperTest.java b/core/camel-core/src/test/java/org/apache/camel/runtimecatalog/impl/JSonSchemaHelperTest.java
index b0d113f..c5ff179 100644
--- a/core/camel-core/src/test/java/org/apache/camel/runtimecatalog/impl/JSonSchemaHelperTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/runtimecatalog/impl/JSonSchemaHelperTest.java
@@ -58,10 +58,10 @@ public class JSonSchemaHelperTest extends ContextTestSupport {
assertEquals("beanName", rows.get(0).values().iterator().next());
assertEquals("cache", rows.get(1).values().iterator().next());
assertEquals("method", rows.get(2).values().iterator().next());
- assertEquals("parameters", rows.get(3).values().iterator().next());
- assertEquals("singleton", rows.get(4).values().iterator().next());
- assertEquals("lazyStartProducer", rows.get(5).values().iterator().next());
- assertEquals("basicPropertyBinding", rows.get(6).values().iterator().next());
+ assertEquals("scope", rows.get(3).values().iterator().next());
+ assertEquals("lazyStartProducer", rows.get(4).values().iterator().next());
+ assertEquals("basicPropertyBinding", rows.get(5).values().iterator().next());
+ assertEquals("parameters", rows.get(6).values().iterator().next());
assertEquals("synchronous", rows.get(7).values().iterator().next());
}
diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BeanEndpointBuilderFactory.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BeanEndpointBuilderFactory.java
index 0930ec3..b7b49ff 100644
--- a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BeanEndpointBuilderFactory.java
+++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BeanEndpointBuilderFactory.java
@@ -18,6 +18,7 @@ package org.apache.camel.builder.endpoint.dsl;
import java.util.Map;
import javax.annotation.Generated;
+import org.apache.camel.BeanScope;
import org.apache.camel.builder.EndpointConsumerBuilder;
import org.apache.camel.builder.EndpointProducerBuilder;
import org.apache.camel.builder.endpoint.AbstractEndpointBuilder;
@@ -90,8 +91,7 @@ public interface BeanEndpointBuilderFactory {
* act as either singleton or prototype scope. so when using delegate
* then this depends on the delegated registry.
*
- * The option is a:
- * <code>org.apache.camel.component.bean.BeanScope</code> type.
+ * The option is a: <code>org.apache.camel.BeanScope</code> type.
*
* Group: common
*/
@@ -116,7 +116,7 @@ public interface BeanEndpointBuilderFactory {
* then this depends on the delegated registry.
*
* The option will be converted to a
- * <code>org.apache.camel.component.bean.BeanScope</code> type.
+ * <code>org.apache.camel.BeanScope</code> type.
*
* Group: common
*/
@@ -250,16 +250,6 @@ public interface BeanEndpointBuilderFactory {
return this;
}
}
-
- /**
- * Proxy enum for <code>org.apache.camel.component.bean.BeanScope</code>
- * enum.
- */
- enum BeanScope {
- Singleton,
- Delegate,
- Request;
- }
/**
* Bean (camel-bean)
* The bean component is for invoking Java beans from Camel.
diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/ClassEndpointBuilderFactory.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/ClassEndpointBuilderFactory.java
index 125d4d7..0624ba3 100644
--- a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/ClassEndpointBuilderFactory.java
+++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/ClassEndpointBuilderFactory.java
@@ -18,6 +18,7 @@ package org.apache.camel.builder.endpoint.dsl;
import java.util.Map;
import javax.annotation.Generated;
+import org.apache.camel.BeanScope;
import org.apache.camel.builder.EndpointConsumerBuilder;
import org.apache.camel.builder.EndpointProducerBuilder;
import org.apache.camel.builder.endpoint.AbstractEndpointBuilder;
@@ -90,8 +91,7 @@ public interface ClassEndpointBuilderFactory {
* act as either singleton or prototype scope. so when using delegate
* then this depends on the delegated registry.
*
- * The option is a:
- * <code>org.apache.camel.component.bean.BeanScope</code> type.
+ * The option is a: <code>org.apache.camel.BeanScope</code> type.
*
* Group: common
*/
@@ -116,7 +116,7 @@ public interface ClassEndpointBuilderFactory {
* then this depends on the delegated registry.
*
* The option will be converted to a
- * <code>org.apache.camel.component.bean.BeanScope</code> type.
+ * <code>org.apache.camel.BeanScope</code> type.
*
* Group: common
*/
@@ -250,16 +250,6 @@ public interface ClassEndpointBuilderFactory {
return this;
}
}
-
- /**
- * Proxy enum for <code>org.apache.camel.component.bean.BeanScope</code>
- * enum.
- */
- enum BeanScope {
- Singleton,
- Delegate,
- Request;
- }
/**
* Class (camel-bean)
* The Class component is for invoking Java classes (Java beans) from Camel.
diff --git a/docs/components/modules/ROOT/pages/bean-component.adoc b/docs/components/modules/ROOT/pages/bean-component.adoc
index c512d37..286bfd4 100644
--- a/docs/components/modules/ROOT/pages/bean-component.adoc
+++ b/docs/components/modules/ROOT/pages/bean-component.adoc
@@ -24,14 +24,15 @@ the Registry
// component options: START
-The Bean component supports 4 options, which are listed below.
+The Bean component supports 5 options, which are listed below.
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
-| *cache* (advanced) | If enabled, Camel will cache the result of the first Registry look-up. Cache can be enabled if the bean in the Registry is defined as a singleton scope. | | Boolean
+| *cache* (producer) | *Deprecated* Use singleton option instead. | true | Boolean
+| *scope* (producer) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while [...]
| *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean
@@ -59,17 +60,18 @@ with the following path and query parameters:
|===
-=== Query Parameters (6 parameters):
+=== Query Parameters (7 parameters):
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
-| *cache* (common) | If enabled, Camel will cache the result of the first Registry look-up. Cache can be enabled if the bean in the Registry is defined as a singleton scope. | | Boolean
+| *cache* (common) | *Deprecated* Use scope option instead. | | Boolean
| *method* (common) | Sets the name of the method to invoke on the bean | | String
-| *parameters* (common) | Used for configuring additional properties on the bean | | Map
+| *scope* (common) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while pr [...]
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *basicPropertyBinding* (advanced) | Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
+| *parameters* (advanced) | Used for configuring additional properties on the bean | | Map
| *synchronous* (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean
|===
// endpoint options: END
diff --git a/docs/components/modules/ROOT/pages/class-component.adoc b/docs/components/modules/ROOT/pages/class-component.adoc
index d95ac1d..e657e82 100644
--- a/docs/components/modules/ROOT/pages/class-component.adoc
+++ b/docs/components/modules/ROOT/pages/class-component.adoc
@@ -27,14 +27,15 @@ bean.
// component options: START
-The Class component supports 4 options, which are listed below.
+The Class component supports 5 options, which are listed below.
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
-| *cache* (advanced) | If enabled, Camel will cache the result of the first Registry look-up. Cache can be enabled if the bean in the Registry is defined as a singleton scope. | | Boolean
+| *cache* (producer) | *Deprecated* Use singleton option instead. | true | Boolean
+| *scope* (producer) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while [...]
| *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean
@@ -62,17 +63,18 @@ with the following path and query parameters:
|===
-=== Query Parameters (6 parameters):
+=== Query Parameters (7 parameters):
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
-| *cache* (common) | If enabled, Camel will cache the result of the first Registry look-up. Cache can be enabled if the bean in the Registry is defined as a singleton scope. | | Boolean
+| *cache* (common) | *Deprecated* Use scope option instead. | | Boolean
| *method* (common) | Sets the name of the method to invoke on the bean | | String
-| *parameters* (common) | Used for configuring additional properties on the bean | | Map
+| *scope* (common) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while pr [...]
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *basicPropertyBinding* (advanced) | Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
+| *parameters* (advanced) | Used for configuring additional properties on the bean | | Map
| *synchronous* (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean
|===
// endpoint options: END
diff --git a/docs/components/modules/ROOT/pages/index.adoc b/docs/components/modules/ROOT/pages/index.adoc
index 57e606f..94275f8 100644
--- a/docs/components/modules/ROOT/pages/index.adoc
+++ b/docs/components/modules/ROOT/pages/index.adoc
@@ -114,7 +114,7 @@ Number of Components: 307 in 245 JAR artifacts (1 deprecated)
| xref:chunk-component.adoc[Chunk] (camel-chunk) | 2.15 | Transforms the message using a Chunk template.
-| xref:class-component.adoc[Class] (camel-bean) | 2.4 | The class component is for invoking Java classes (Java beans) from Camel.
+| xref:class-component.adoc[Class] (camel-bean) | 2.4 | The Class component is for invoking Java classes (Java beans) from Camel.
| xref:cm-sms-component.adoc[CM SMS Gateway] (camel-cm-sms) | 2.18 | The cm-sms component allows to integrate with CM SMS Gateway.
diff --git a/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide.adoc b/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide.adoc
index 448c138..329a36a 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide.adoc
@@ -16,9 +16,9 @@ from both 3.0 to 3.1 and 3.1 to 3.2.
The bean component has been changed to behave as singleton scoped by default.
This means that the bean is created or looked up once and reused.
-The option `cache` has been deprecated in favour of the new `singleton` option that by default is `true`. You can set this to `false` to use the old behaviour.
+The option `cache` has been deprecated in favour of the new `scope` option that by default is `Singleton`. You can set this to `Prototype` to use the old behaviour.
-NOTE: Setting this to false will let Camel create/lookup a new bean instance, per use; which acts as prototype scoped. However beware that if you lookup the bean, then the registry that holds the bean, would return a bean accordingly to its configuration, which can be singleton or prototype scoped. For example if you use Spring, or CDI, which has their own settings for setting bean scopes.
+NOTE: Setting this to Prototype will let Camel create/lookup a new bean instance, per use; which acts as prototype scoped. However beware that if you lookup the bean, then the registry that holds the bean, would return a bean accordingly to its configuration, which can be singleton or prototype scoped. For example if you use Spring, or CDI, which has their own settings for setting bean scopes.
=== camel-nats
[camel] 03/08: CAMEL-14359: camel-bean is now singleton scoped by
default (cache=true).
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch bean-scope
in repository https://gitbox.apache.org/repos/asf/camel.git
commit bc61558af28a1d41c98f3f8068c50af50cb31d7d
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jan 6 19:46:27 2020 +0100
CAMEL-14359: camel-bean is now singleton scoped by default (cache=true).
---
.../camel/openapi/RestOpenApiReaderEnableVendorExtensionTest.java | 3 +++
.../apache/camel/openapi/RestOpenApiReaderModelApiSecurityTest.java | 6 +++---
.../apache/camel/openapi/RestOpenApiReaderModelBookOrderTest.java | 4 +++-
.../java/org/apache/camel/openapi/RestOpenApiReaderModelTest.java | 3 +++
.../camel/openapi/SpringRestOpenApiReaderModelApiSecurityTest.java | 6 ++----
.../camel/openapi/SpringRestOpenApiReaderModelApiSecurityTest.xml | 2 ++
.../camel/swagger/RestSwaggerReaderEnableVendorExtensionTest.java | 3 +++
.../apache/camel/swagger/RestSwaggerReaderModelApiSecurityTest.java | 3 +++
.../apache/camel/swagger/RestSwaggerReaderModelBookOrderTest.java | 3 +++
.../java/org/apache/camel/swagger/RestSwaggerReaderModelTest.java | 3 +++
.../camel/swagger/SpringRestSwaggerReaderModelApiSecurityTest.xml | 3 +++
11 files changed, 31 insertions(+), 8 deletions(-)
diff --git a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderEnableVendorExtensionTest.java b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderEnableVendorExtensionTest.java
index 8757dad..b37a601 100644
--- a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderEnableVendorExtensionTest.java
+++ b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderEnableVendorExtensionTest.java
@@ -33,6 +33,9 @@ public class RestOpenApiReaderEnableVendorExtensionTest extends CamelTestSupport
@BindToRegistry("dummy-rest")
private DummyRestConsumerFactory factory = new DummyRestConsumerFactory();
+ @BindToRegistry("userService")
+ private Object dummy = new Object();
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
diff --git a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderModelApiSecurityTest.java b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderModelApiSecurityTest.java
index 66c7147..9336964 100644
--- a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderModelApiSecurityTest.java
+++ b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderModelApiSecurityTest.java
@@ -28,14 +28,14 @@ import org.apache.camel.model.rest.RestParamType;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
-
-
-
public class RestOpenApiReaderModelApiSecurityTest extends CamelTestSupport {
@BindToRegistry("dummy-rest")
private DummyRestConsumerFactory factory = new DummyRestConsumerFactory();
+ @BindToRegistry("userService")
+ private Object dummy = new Object();
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
diff --git a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderModelBookOrderTest.java b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderModelBookOrderTest.java
index 91df38e..7685c7d 100644
--- a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderModelBookOrderTest.java
+++ b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderModelBookOrderTest.java
@@ -28,12 +28,14 @@ import org.apache.camel.model.rest.RestParamType;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
-
public class RestOpenApiReaderModelBookOrderTest extends CamelTestSupport {
@BindToRegistry("dummy-rest")
private DummyRestConsumerFactory factory = new DummyRestConsumerFactory();
+ @BindToRegistry("bookService")
+ private Object dummy = new Object();
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
diff --git a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderModelTest.java b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderModelTest.java
index 39459d8..df611f8 100644
--- a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderModelTest.java
+++ b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderModelTest.java
@@ -34,6 +34,9 @@ public class RestOpenApiReaderModelTest extends CamelTestSupport {
@BindToRegistry("dummy-rest")
private DummyRestConsumerFactory factory = new DummyRestConsumerFactory();
+ @BindToRegistry("userService")
+ private Object dummy = new Object();
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
diff --git a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/SpringRestOpenApiReaderModelApiSecurityTest.java b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/SpringRestOpenApiReaderModelApiSecurityTest.java
index 9efdbea..a7905ff 100644
--- a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/SpringRestOpenApiReaderModelApiSecurityTest.java
+++ b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/SpringRestOpenApiReaderModelApiSecurityTest.java
@@ -36,7 +36,6 @@ public class SpringRestOpenApiReaderModelApiSecurityTest extends CamelSpringTest
@Test
public void testReaderRead() throws Exception {
-
BeanConfig config = new BeanConfig();
config.setHost("localhost:8080");
config.setSchemes(new String[]{"http"});
@@ -75,13 +74,12 @@ public class SpringRestOpenApiReaderModelApiSecurityTest extends CamelSpringTest
assertTrue(json.contains("\"type\" : \"string\""));
assertTrue(json.contains("\"format\" : \"date\""));
assertFalse(json.contains("\"enum\""));
+
context.stop();
-
}
@Test
public void testReaderReadV3() throws Exception {
-
BeanConfig config = new BeanConfig();
config.setHost("localhost:8080");
config.setSchemes(new String[]{"http"});
@@ -120,7 +118,7 @@ public class SpringRestOpenApiReaderModelApiSecurityTest extends CamelSpringTest
assertTrue(json.contains("\"type\" : \"string\""));
assertTrue(json.contains("\"format\" : \"date\""));
assertFalse(json.contains("\"enum\""));
+
context.stop();
-
}
}
diff --git a/components/camel-openapi-java/src/test/resources/org/apache/camel/openapi/SpringRestOpenApiReaderModelApiSecurityTest.xml b/components/camel-openapi-java/src/test/resources/org/apache/camel/openapi/SpringRestOpenApiReaderModelApiSecurityTest.xml
index b39d317..cc5c91a 100644
--- a/components/camel-openapi-java/src/test/resources/org/apache/camel/openapi/SpringRestOpenApiReaderModelApiSecurityTest.xml
+++ b/components/camel-openapi-java/src/test/resources/org/apache/camel/openapi/SpringRestOpenApiReaderModelApiSecurityTest.xml
@@ -27,6 +27,8 @@
<!-- use a dummy rest consumer factory for the rest engine -->
<bean id="dummy-rest" class="org.apache.camel.openapi.DummyRestConsumerFactory"/>
+ <bean id="userService" class="java.lang.Object"/>
+
<camelContext xmlns="http://camel.apache.org/schema/spring">
<rest path="/user" tag="dude" consumes="application/json" produces="application/json">
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderEnableVendorExtensionTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderEnableVendorExtensionTest.java
index da42e83..a3a596e 100644
--- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderEnableVendorExtensionTest.java
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderEnableVendorExtensionTest.java
@@ -41,6 +41,9 @@ public class RestSwaggerReaderEnableVendorExtensionTest extends CamelTestSupport
@BindToRegistry("dummy-rest")
private DummyRestConsumerFactory factory = new DummyRestConsumerFactory();
+ @BindToRegistry("userService")
+ private Object dummy = new Object();
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelApiSecurityTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelApiSecurityTest.java
index 61a3237..2ddfdf2 100644
--- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelApiSecurityTest.java
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelApiSecurityTest.java
@@ -41,6 +41,9 @@ public class RestSwaggerReaderModelApiSecurityTest extends CamelTestSupport {
@BindToRegistry("dummy-rest")
private DummyRestConsumerFactory factory = new DummyRestConsumerFactory();
+ @BindToRegistry("userService")
+ private Object dummy = new Object();
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelBookOrderTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelBookOrderTest.java
index efe4467..0831552 100644
--- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelBookOrderTest.java
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelBookOrderTest.java
@@ -40,6 +40,9 @@ public class RestSwaggerReaderModelBookOrderTest extends CamelTestSupport {
@BindToRegistry("dummy-rest")
private DummyRestConsumerFactory factory = new DummyRestConsumerFactory();
+ @BindToRegistry("bookService")
+ private Object dummy = new Object();
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
diff --git a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelTest.java b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelTest.java
index 32e52e6..c3805a9 100644
--- a/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelTest.java
+++ b/components/camel-swagger-java/src/test/java/org/apache/camel/swagger/RestSwaggerReaderModelTest.java
@@ -41,6 +41,9 @@ public class RestSwaggerReaderModelTest extends CamelTestSupport {
@BindToRegistry("dummy-rest")
private DummyRestConsumerFactory factory = new DummyRestConsumerFactory();
+ @BindToRegistry("userService")
+ private Object dummy = new Object();
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
diff --git a/components/camel-swagger-java/src/test/resources/org/apache/camel/swagger/SpringRestSwaggerReaderModelApiSecurityTest.xml b/components/camel-swagger-java/src/test/resources/org/apache/camel/swagger/SpringRestSwaggerReaderModelApiSecurityTest.xml
index 9fc8609..ae5c087 100644
--- a/components/camel-swagger-java/src/test/resources/org/apache/camel/swagger/SpringRestSwaggerReaderModelApiSecurityTest.xml
+++ b/components/camel-swagger-java/src/test/resources/org/apache/camel/swagger/SpringRestSwaggerReaderModelApiSecurityTest.xml
@@ -27,6 +27,9 @@
<!-- use a dummy rest consumer factory for the rest engine -->
<bean id="dummy-rest" class="org.apache.camel.swagger.DummyRestConsumerFactory"/>
+ <!-- dummy -->
+ <bean id="userService" class="java.lang.Object"/>
+
<camelContext xmlns="http://camel.apache.org/schema/spring">
<rest path="/user" tag="dude" consumes="application/json" produces="application/json">
[camel] 08/08: CAMEL-14359: camel-bean - Adding bean scope option.
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch bean-scope
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 123f92f9e7c5c36c63d08aa465358b59a41df2fd
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Jan 7 09:56:11 2020 +0100
CAMEL-14359: camel-bean - Adding bean scope option.
---
components/camel-bean/src/main/docs/bean-component.adoc | 4 ++--
components/camel-bean/src/main/docs/class-component.adoc | 4 ++--
.../org/apache/camel/component/bean/BeanComponent.java | 2 +-
.../org/apache/camel/component/bean/BeanEndpoint.java | 4 ++--
core/camel-core-engine/src/main/docs/eips/bean-eip.adoc | 16 ++++++++++++++--
.../main/java/org/apache/camel/model/BeanDefinition.java | 12 ++++++++++++
.../builder/endpoint/dsl/BeanEndpointBuilderFactory.java | 8 ++++----
.../endpoint/dsl/ClassEndpointBuilderFactory.java | 8 ++++----
docs/components/modules/ROOT/pages/bean-component.adoc | 4 ++--
docs/components/modules/ROOT/pages/class-component.adoc | 4 ++--
docs/user-manual/modules/ROOT/pages/bean-eip.adoc | 16 ++++++++++++++--
11 files changed, 59 insertions(+), 23 deletions(-)
diff --git a/components/camel-bean/src/main/docs/bean-component.adoc b/components/camel-bean/src/main/docs/bean-component.adoc
index d8df9fa..728f725 100644
--- a/components/camel-bean/src/main/docs/bean-component.adoc
+++ b/components/camel-bean/src/main/docs/bean-component.adoc
@@ -31,7 +31,7 @@ The Bean component supports 5 options, which are listed below.
|===
| Name | Description | Default | Type
| *cache* (producer) | *Deprecated* Use singleton option instead. | true | Boolean
-| *scope* (producer) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while [...]
+| *scope* (producer) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while [...]
| *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean
@@ -67,7 +67,7 @@ with the following path and query parameters:
| Name | Description | Default | Type
| *cache* (common) | *Deprecated* Use scope option instead. | | Boolean
| *method* (common) | Sets the name of the method to invoke on the bean | | String
-| *scope* (common) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while pr [...]
+| *scope* (common) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while pr [...]
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *basicPropertyBinding* (advanced) | Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *parameters* (advanced) | Used for configuring additional properties on the bean | | Map
diff --git a/components/camel-bean/src/main/docs/class-component.adoc b/components/camel-bean/src/main/docs/class-component.adoc
index 2caeb1e..864ff11 100644
--- a/components/camel-bean/src/main/docs/class-component.adoc
+++ b/components/camel-bean/src/main/docs/class-component.adoc
@@ -34,7 +34,7 @@ The Class component supports 5 options, which are listed below.
|===
| Name | Description | Default | Type
| *cache* (producer) | *Deprecated* Use singleton option instead. | true | Boolean
-| *scope* (producer) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while [...]
+| *scope* (producer) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while [...]
| *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean
@@ -70,7 +70,7 @@ with the following path and query parameters:
| Name | Description | Default | Type
| *cache* (common) | *Deprecated* Use scope option instead. | | Boolean
| *method* (common) | Sets the name of the method to invoke on the bean | | String
-| *scope* (common) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while pr [...]
+| *scope* (common) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while pr [...]
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *basicPropertyBinding* (advanced) | Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *parameters* (advanced) | Used for configuring additional properties on the bean | | Map
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java
index 6142b4a..3168cc1 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java
@@ -48,7 +48,7 @@ public class BeanComponent extends DefaultComponent {
+ " The bean should not be thread-safe as the instance is only called from the same request. "
+ " When using delegate scope, then the bean will be looked up or created per call. However in case of lookup then this is delegated "
+ " to the bean registry such as Spring or CDI (if in use), which depends on their configuration can act as either singleton or prototype scope."
- + " so when using delegate then this depends on the delegated registry.")
+ + " so when using prototype then this depends on the delegated registry.")
private BeanScope scope = BeanScope.Singleton;
public BeanComponent() {
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
index a76a22ba..2de53ba 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
@@ -50,9 +50,9 @@ public class BeanEndpoint extends DefaultEndpoint {
+ " When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean"
+ " while processing a request and you want to call the same bean instance multiple times while processing the request."
+ " The bean should not be thread-safe as the instance is only called from the same request. "
- + " When using delegate scope, then the bean will be looked up or created per call. However in case of lookup then this is delegated "
+ + " When using prototype scope, then the bean will be looked up or created per call. However in case of lookup then this is delegated "
+ " to the bean registry such as Spring or CDI (if in use), which depends on their configuration can act as either singleton or prototype scope."
- + " so when using delegate then this depends on the delegated registry.")
+ + " so when using prototype then this depends on the delegated registry.")
private BeanScope scope = BeanScope.Singleton;
@UriParam(prefix = "bean.", label = "advanced", description = "Used for configuring additional properties on the bean", multiValue = true)
private Map<String, Object> parameters;
diff --git a/core/camel-core-engine/src/main/docs/eips/bean-eip.adoc b/core/camel-core-engine/src/main/docs/eips/bean-eip.adoc
index 77f733e..6b862c8 100644
--- a/core/camel-core-engine/src/main/docs/eips/bean-eip.adoc
+++ b/core/camel-core-engine/src/main/docs/eips/bean-eip.adoc
@@ -16,7 +16,7 @@ the xref:registry.adoc[Registry]
== EIP options
// eip options: START
-The Bean EIP supports 4 options which are listed below:
+The Bean EIP supports 5 options which are listed below:
[width="100%",cols="2,5,^1,2",options="header"]
|===
@@ -24,10 +24,22 @@ The Bean EIP supports 4 options which are listed below:
| *ref* | Sets a reference to a bean to use | | String
| *method* | Sets the method name on the bean to use | | String
| *beanType* | Sets the Class of the bean | | String
-| *cache* | Caches the bean lookup, to avoid lookup up bean on every usage. | true | Boolean
+| *cache* | *Deprecated* Use scope option instead. | | Boolean
+| *scope* | Scope of bean. See below for more details. | Singleton | String |
|===
// eip options: END
+=== Bean scope
+
+When using `singleton` scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint.
+The bean should be thread-safe in case concurrent threads is calling the bean at the same time.
+When using `request` scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean
+while processing a request and you want to call the same bean instance multiple times while processing the request.
+The bean should not be thread-safe as the instance is only called from the same request.
+When using `prototype` scope, then the bean will be looked up or created per call. However in case of lookup then this is delegated
+to the bean registry such as Spring or CDI (if in use), which depends on their configuration can act as either singleton or prototype scope.
+so when using `prototype` then this depends on the delegated registry.
+
== Bean as endpoint
Camel also supports invoking xref:components::bean-component.adoc[Bean] as an Endpoint. In the
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/model/BeanDefinition.java b/core/camel-core-engine/src/main/java/org/apache/camel/model/BeanDefinition.java
index 30cc5dc..3dc6b21 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/model/BeanDefinition.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/model/BeanDefinition.java
@@ -178,6 +178,18 @@ public class BeanDefinition extends NoOutputDefinition<BeanDefinition> {
this.scope = scope;
}
+ /**
+ * Scope of bean.
+ *
+ * When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint.
+ * The bean should be thread-safe in case concurrent threads is calling the bean at the same time.
+ * When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean
+ * while processing a request and you want to call the same bean instance multiple times while processing the request.
+ * The bean should not be thread-safe as the instance is only called from the same request.
+ * When using delegate scope, then the bean will be looked up or created per call. However in case of lookup then this is delegated
+ * to the bean registry such as Spring or CDI (if in use), which depends on their configuration can act as either singleton or prototype scope.
+ * so when using delegate then this depends on the delegated registry.
+ */
public void setScope(BeanScope scope) {
this.scope = scope.name();
}
diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BeanEndpointBuilderFactory.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BeanEndpointBuilderFactory.java
index b7b49ff..8325253 100644
--- a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BeanEndpointBuilderFactory.java
+++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BeanEndpointBuilderFactory.java
@@ -84,11 +84,11 @@ public interface BeanEndpointBuilderFactory {
* used if you want to store state on a bean while processing a request
* and you want to call the same bean instance multiple times while
* processing the request. The bean should not be thread-safe as the
- * instance is only called from the same request. When using delegate
+ * instance is only called from the same request. When using prototype
* scope, then the bean will be looked up or created per call. However
* in case of lookup then this is delegated to the bean registry such as
* Spring or CDI (if in use), which depends on their configuration can
- * act as either singleton or prototype scope. so when using delegate
+ * act as either singleton or prototype scope. so when using prototype
* then this depends on the delegated registry.
*
* The option is a: <code>org.apache.camel.BeanScope</code> type.
@@ -108,11 +108,11 @@ public interface BeanEndpointBuilderFactory {
* used if you want to store state on a bean while processing a request
* and you want to call the same bean instance multiple times while
* processing the request. The bean should not be thread-safe as the
- * instance is only called from the same request. When using delegate
+ * instance is only called from the same request. When using prototype
* scope, then the bean will be looked up or created per call. However
* in case of lookup then this is delegated to the bean registry such as
* Spring or CDI (if in use), which depends on their configuration can
- * act as either singleton or prototype scope. so when using delegate
+ * act as either singleton or prototype scope. so when using prototype
* then this depends on the delegated registry.
*
* The option will be converted to a
diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/ClassEndpointBuilderFactory.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/ClassEndpointBuilderFactory.java
index 0624ba3..93bae90 100644
--- a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/ClassEndpointBuilderFactory.java
+++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/ClassEndpointBuilderFactory.java
@@ -84,11 +84,11 @@ public interface ClassEndpointBuilderFactory {
* used if you want to store state on a bean while processing a request
* and you want to call the same bean instance multiple times while
* processing the request. The bean should not be thread-safe as the
- * instance is only called from the same request. When using delegate
+ * instance is only called from the same request. When using prototype
* scope, then the bean will be looked up or created per call. However
* in case of lookup then this is delegated to the bean registry such as
* Spring or CDI (if in use), which depends on their configuration can
- * act as either singleton or prototype scope. so when using delegate
+ * act as either singleton or prototype scope. so when using prototype
* then this depends on the delegated registry.
*
* The option is a: <code>org.apache.camel.BeanScope</code> type.
@@ -108,11 +108,11 @@ public interface ClassEndpointBuilderFactory {
* used if you want to store state on a bean while processing a request
* and you want to call the same bean instance multiple times while
* processing the request. The bean should not be thread-safe as the
- * instance is only called from the same request. When using delegate
+ * instance is only called from the same request. When using prototype
* scope, then the bean will be looked up or created per call. However
* in case of lookup then this is delegated to the bean registry such as
* Spring or CDI (if in use), which depends on their configuration can
- * act as either singleton or prototype scope. so when using delegate
+ * act as either singleton or prototype scope. so when using prototype
* then this depends on the delegated registry.
*
* The option will be converted to a
diff --git a/docs/components/modules/ROOT/pages/bean-component.adoc b/docs/components/modules/ROOT/pages/bean-component.adoc
index 286bfd4..18e8f7d 100644
--- a/docs/components/modules/ROOT/pages/bean-component.adoc
+++ b/docs/components/modules/ROOT/pages/bean-component.adoc
@@ -32,7 +32,7 @@ The Bean component supports 5 options, which are listed below.
|===
| Name | Description | Default | Type
| *cache* (producer) | *Deprecated* Use singleton option instead. | true | Boolean
-| *scope* (producer) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while [...]
+| *scope* (producer) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while [...]
| *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean
@@ -68,7 +68,7 @@ with the following path and query parameters:
| Name | Description | Default | Type
| *cache* (common) | *Deprecated* Use scope option instead. | | Boolean
| *method* (common) | Sets the name of the method to invoke on the bean | | String
-| *scope* (common) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while pr [...]
+| *scope* (common) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while pr [...]
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *basicPropertyBinding* (advanced) | Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *parameters* (advanced) | Used for configuring additional properties on the bean | | Map
diff --git a/docs/components/modules/ROOT/pages/class-component.adoc b/docs/components/modules/ROOT/pages/class-component.adoc
index e657e82..674f2ad 100644
--- a/docs/components/modules/ROOT/pages/class-component.adoc
+++ b/docs/components/modules/ROOT/pages/class-component.adoc
@@ -35,7 +35,7 @@ The Class component supports 5 options, which are listed below.
|===
| Name | Description | Default | Type
| *cache* (producer) | *Deprecated* Use singleton option instead. | true | Boolean
-| *scope* (producer) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while [...]
+| *scope* (producer) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while [...]
| *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean
@@ -71,7 +71,7 @@ with the following path and query parameters:
| Name | Description | Default | Type
| *cache* (common) | *Deprecated* Use scope option instead. | | Boolean
| *method* (common) | Sets the name of the method to invoke on the bean | | String
-| *scope* (common) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while pr [...]
+| *scope* (common) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while pr [...]
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *basicPropertyBinding* (advanced) | Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *parameters* (advanced) | Used for configuring additional properties on the bean | | Map
diff --git a/docs/user-manual/modules/ROOT/pages/bean-eip.adoc b/docs/user-manual/modules/ROOT/pages/bean-eip.adoc
index ddb5e4b..1f1fce4 100644
--- a/docs/user-manual/modules/ROOT/pages/bean-eip.adoc
+++ b/docs/user-manual/modules/ROOT/pages/bean-eip.adoc
@@ -17,7 +17,7 @@ the xref:registry.adoc[Registry]
== EIP options
// eip options: START
-The Bean EIP supports 4 options which are listed below:
+The Bean EIP supports 5 options which are listed below:
[width="100%",cols="2,5,^1,2",options="header"]
|===
@@ -25,10 +25,22 @@ The Bean EIP supports 4 options which are listed below:
| *ref* | Sets a reference to a bean to use | | String
| *method* | Sets the method name on the bean to use | | String
| *beanType* | Sets the Class of the bean | | String
-| *cache* | Caches the bean lookup, to avoid lookup up bean on every usage. | true | Boolean
+| *cache* | *Deprecated* Use scope option instead. | | Boolean
+| *scope* | Scope of bean. See below for more details. | Singleton | String |
|===
// eip options: END
+=== Bean scope
+
+When using `singleton` scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint.
+The bean should be thread-safe in case concurrent threads is calling the bean at the same time.
+When using `request` scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean
+while processing a request and you want to call the same bean instance multiple times while processing the request.
+The bean should not be thread-safe as the instance is only called from the same request.
+When using `prototype` scope, then the bean will be looked up or created per call. However in case of lookup then this is delegated
+to the bean registry such as Spring or CDI (if in use), which depends on their configuration can act as either singleton or prototype scope.
+so when using `prototype` then this depends on the delegated registry.
+
== Bean as endpoint
Camel also supports invoking xref:components::bean-component.adoc[Bean] as an Endpoint. In the
[camel] 07/08: CAMEL-14359: camel-bean - Adding bean scope option.
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch bean-scope
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 22f40215456fa6c53da2ed461212db22e032ee94
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Jan 7 08:35:29 2020 +0100
CAMEL-14359: camel-bean - Adding bean scope option.
---
.../component/bean/AbstractBeanProcessor.java | 8 +--
.../apache/camel/component/bean/BeanComponent.java | 2 +-
.../apache/camel/component/bean/BeanEndpoint.java | 6 +-
.../apache/camel/component/bean/BeanHolder.java | 3 +-
.../camel/component/bean/ConstantBeanHolder.java | 3 +-
.../bean/ConstantStaticTypeBeanHolder.java | 3 +-
.../component/bean/ConstantTypeBeanHolder.java | 5 +-
.../bean/DefaultBeanProcessorFactory.java | 10 +++-
.../apache/camel/component/bean/RegistryBean.java | 7 ++-
.../camel/component/bean/RequestBeanHolder.java | 65 ++++++++++++++++++++++
.../apache/camel/language/bean/BeanExpression.java | 18 +++---
.../camel/component/bean/BeanRegistryBeanTest.java | 10 ++--
.../apache/camel/component/bean/MyRequestBean.java | 18 ++----
.../bean/RequestScopedBeanComponentTest.java | 51 +++++++++++++++++
.../component/bean/RequestScopedBeanEIPTest.java | 27 ++++-----
15 files changed, 182 insertions(+), 54 deletions(-)
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java
index c9492fd..1964498 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java
@@ -69,7 +69,7 @@ public abstract class AbstractBeanProcessor extends AsyncProcessorSupport {
Object bean;
BeanInfo beanInfo;
try {
- bean = beanHolder.getBean();
+ bean = beanHolder.getBean(exchange);
// get bean info for this bean instance (to avoid thread issue)
beanInfo = beanHolder.getBeanInfo(bean);
if (beanInfo == null) {
@@ -159,7 +159,7 @@ public abstract class AbstractBeanProcessor extends AsyncProcessorSupport {
}
public Object getBean() {
- return beanHolder.getBean();
+ return beanHolder.getBean(null);
}
// Properties
@@ -210,7 +210,7 @@ public abstract class AbstractBeanProcessor extends AsyncProcessorSupport {
try {
// Start the bean if it implements Service interface and if cached
// so meant to be reused
- ServiceHelper.startService(beanHolder.getBean());
+ ServiceHelper.startService(beanHolder.getBean(null));
} catch (NoSuchBeanException e) {
// ignore
}
@@ -225,7 +225,7 @@ public abstract class AbstractBeanProcessor extends AsyncProcessorSupport {
try {
// Stop the bean if it implements Service interface and if cached
// so meant to be reused
- ServiceHelper.stopService(beanHolder.getBean());
+ ServiceHelper.stopService(beanHolder.getBean(null));
} catch (NoSuchBeanException e) {
// ignore
}
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java
index 5d2d74e..6142b4a 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java
@@ -27,7 +27,7 @@ import org.apache.camel.support.LRUCacheFactory;
import org.apache.camel.util.PropertiesHelper;
/**
- * The Bean component is for invoking Java beans from Camel.
+ * The bean component is for invoking Java beans from Camel.
*/
@org.apache.camel.spi.annotations.Component("bean")
public class BeanComponent extends DefaultComponent {
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
index c733b29..a76a22ba 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
@@ -31,7 +31,7 @@ import org.apache.camel.spi.UriPath;
import org.apache.camel.support.DefaultEndpoint;
/**
- * The <a href="http://camel.apache.org/bean.html">bean component</a> is for invoking Java beans from Camel.
+ * The bean component is for invoking Java beans from Camel.
*/
@UriEndpoint(firstVersion = "1.0.0", scheme = "bean", title = "Bean", syntax = "bean:beanName", producerOnly = true, label = "core,java")
public class BeanEndpoint extends DefaultEndpoint {
@@ -101,6 +101,10 @@ public class BeanEndpoint extends DefaultEndpoint {
holder = registryBean;
}
}
+ if (scope == BeanScope.Request) {
+ // wrap in registry scoped
+ holder = new RequestBeanHolder(holder);
+ }
processor = new BeanProcessor(holder);
if (method != null) {
processor.setMethod(method);
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanHolder.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanHolder.java
index 22bcc98..af56127 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanHolder.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanHolder.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.bean;
+import org.apache.camel.Exchange;
import org.apache.camel.NoSuchBeanException;
import org.apache.camel.Processor;
@@ -29,7 +30,7 @@ public interface BeanHolder {
*
* @throws NoSuchBeanException is thrown if the bean cannot be found.
*/
- Object getBean() throws NoSuchBeanException;
+ Object getBean(Exchange exchange) throws NoSuchBeanException;
/**
* Gets a {@link Processor} for this bean, if supported.
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java
index bbb758f..9f42b16 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.bean;
import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.support.CamelContextHelper;
import org.apache.camel.util.ObjectHelper;
@@ -51,7 +52,7 @@ public class ConstantBeanHolder implements BeanHolder {
}
@Override
- public Object getBean() {
+ public Object getBean(Exchange exchange) {
return bean;
}
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantStaticTypeBeanHolder.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantStaticTypeBeanHolder.java
index 55b7f12..f87fd78 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantStaticTypeBeanHolder.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantStaticTypeBeanHolder.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.bean;
import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
/**
* A constant {@link org.apache.camel.component.bean.BeanHolder} for a class or static class
@@ -29,7 +30,7 @@ public class ConstantStaticTypeBeanHolder extends ConstantTypeBeanHolder {
}
@Override
- public Object getBean() {
+ public Object getBean(Exchange exchange) {
// we cannot create a bean as there is no default constructor
return null;
}
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantTypeBeanHolder.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantTypeBeanHolder.java
index 400330e..3802380 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantTypeBeanHolder.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantTypeBeanHolder.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.bean;
import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.util.ObjectHelper;
@@ -45,7 +46,7 @@ public class ConstantTypeBeanHolder implements BeanTypeHolder {
* @return a new {@link org.apache.camel.component.bean.BeanHolder} that has cached the lookup of the bean.
*/
public ConstantBeanHolder createCacheHolder() throws Exception {
- Object bean = getBean();
+ Object bean = getBean(null);
return new ConstantBeanHolder(bean, beanInfo);
}
@@ -55,7 +56,7 @@ public class ConstantTypeBeanHolder implements BeanTypeHolder {
}
@Override
- public Object getBean() {
+ public Object getBean(Exchange exchange) {
// only create a bean if we have a default no-arg constructor
if (beanInfo.hasPublicNoArgConstructors()) {
return getBeanInfo().getCamelContext().getInjector().newInstance(type, false);
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java
index fadb831..f5748bd 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/DefaultBeanProcessorFactory.java
@@ -54,11 +54,15 @@ public final class DefaultBeanProcessorFactory implements BeanProcessorFactory {
// cache the registry lookup which avoids repeat lookup in the registry
beanHolder = new RegistryBean(camelContext, ref).createCacheHolder();
// bean holder will check if the bean exists
- bean = beanHolder.getBean();
+ bean = beanHolder.getBean(null);
} else {
// we do not cache so we invoke on-demand
beanHolder = new RegistryBean(camelContext, ref);
}
+ if (scope == BeanScope.Request) {
+ // wrap in registry scoped holder
+ beanHolder = new RequestBeanHolder(beanHolder);
+ }
answer = new BeanProcessor(beanHolder);
} else {
if (bean == null) {
@@ -118,6 +122,10 @@ public final class DefaultBeanProcessorFactory implements BeanProcessorFactory {
}
}
}
+ if (scope == BeanScope.Request) {
+ // wrap in registry scoped holder
+ beanHolder = new RequestBeanHolder(beanHolder);
+ }
answer = new BeanProcessor(beanHolder);
}
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/RegistryBean.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/RegistryBean.java
index e4f29cf..d8376fd 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/RegistryBean.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/RegistryBean.java
@@ -17,6 +17,7 @@
package org.apache.camel.component.bean;
import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
import org.apache.camel.NoSuchBeanException;
import org.apache.camel.Processor;
import org.apache.camel.spi.Registry;
@@ -62,13 +63,13 @@ public class RegistryBean implements BeanHolder {
* Creates a singleton (cached and constant) {@link org.apache.camel.component.bean.BeanHolder} from this holder.
*/
public ConstantBeanHolder createCacheHolder() {
- Object bean = getBean();
+ Object bean = getBean(null);
BeanInfo info = createBeanInfo(bean);
return new ConstantBeanHolder(bean, info);
}
@Override
- public Object getBean() throws NoSuchBeanException {
+ public Object getBean(Exchange exchange) throws NoSuchBeanException {
// must always lookup bean first
Object value = lookupBean();
@@ -108,7 +109,7 @@ public class RegistryBean implements BeanHolder {
@Override
public BeanInfo getBeanInfo() {
if (beanInfo == null) {
- Object bean = getBean();
+ Object bean = getBean(null);
this.beanInfo = createBeanInfo(bean);
}
return beanInfo;
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/RequestBeanHolder.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/RequestBeanHolder.java
new file mode 100644
index 0000000..ce4d789
--- /dev/null
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/RequestBeanHolder.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.bean;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.NoSuchBeanException;
+import org.apache.camel.Processor;
+
+/**
+ * Request scoped {@link BeanHolder} wrapper.
+ */
+public class RequestBeanHolder implements BeanHolder {
+
+ private final BeanHolder holder;
+ private final String key;
+
+ public RequestBeanHolder(BeanHolder holder) {
+ this.holder = holder;
+ this.key = "CamelBeanRequestScope-" + holder.getBeanInfo().getType().getName();
+ }
+
+ @Override
+ public Object getBean(Exchange exchange) throws NoSuchBeanException {
+ Object bean = exchange.getProperty(key);
+ if (bean == null) {
+ bean = holder.getBean(exchange);
+ exchange.setProperty(key, bean);
+ }
+ return bean;
+ }
+
+ @Override
+ public Processor getProcessor() {
+ return null;
+ }
+
+ @Override
+ public boolean supportProcessor() {
+ return false;
+ }
+
+ @Override
+ public BeanInfo getBeanInfo() {
+ return holder.getBeanInfo();
+ }
+
+ @Override
+ public BeanInfo getBeanInfo(Object bean) {
+ return holder.getBeanInfo(bean);
+ }
+}
diff --git a/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java b/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java
index b2dea7a..1219fe2 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java
@@ -151,7 +151,7 @@ public class BeanExpression implements Expression, Predicate, AfterPropertiesCon
if (e instanceof RuntimeBeanExpressionException) {
throw (RuntimeBeanExpressionException) e;
}
- throw new RuntimeBeanExpressionException(exchange, getBeanName(beanName, beanHolder), method, e);
+ throw new RuntimeBeanExpressionException(exchange, getBeanName(exchange, beanName, beanHolder), method, e);
}
} else {
// regular non ognl invocation
@@ -161,7 +161,7 @@ public class BeanExpression implements Expression, Predicate, AfterPropertiesCon
if (e instanceof RuntimeBeanExpressionException) {
throw (RuntimeBeanExpressionException) e;
}
- throw new RuntimeBeanExpressionException(exchange, getBeanName(beanName, beanHolder), method, e);
+ throw new RuntimeBeanExpressionException(exchange, getBeanName(exchange, beanName, beanHolder), method, e);
}
}
}
@@ -262,10 +262,10 @@ public class BeanExpression implements Expression, Predicate, AfterPropertiesCon
return holder;
}
- private static String getBeanName(String beanName, BeanHolder beanHolder) {
+ private static String getBeanName(Exchange exchange, String beanName, BeanHolder beanHolder) {
String name = beanName;
- if (name == null && beanHolder != null && beanHolder.getBean() != null) {
- name = beanHolder.getBean().getClass().getCanonicalName();
+ if (name == null && beanHolder != null && beanHolder.getBean(exchange) != null) {
+ name = beanHolder.getBean(exchange).getClass().getCanonicalName();
}
if (name == null && beanHolder != null && beanHolder.getBeanInfo() != null && beanHolder.getBeanInfo().getType() != null) {
name = beanHolder.getBeanInfo().getType().getCanonicalName();
@@ -326,7 +326,7 @@ public class BeanExpression implements Expression, Predicate, AfterPropertiesCon
private static Object invokeOgnlMethod(BeanHolder beanHolder, String beanName, String ognl, Exchange exchange) {
// we must start with having bean as the result
- Object result = beanHolder.getBean();
+ Object result = beanHolder.getBean(exchange);
// copy the original exchange to avoid side effects on it
Exchange resultExchange = ExchangeHelper.createCopy(exchange, true);
@@ -342,7 +342,7 @@ public class BeanExpression implements Expression, Predicate, AfterPropertiesCon
String ognlPath = "";
// loop and invoke each method
- Object beanToCall = beanHolder.getBean();
+ Object beanToCall = beanHolder.getBean(exchange);
Class<?> beanType = beanHolder.getBeanInfo().getType();
// there must be a bean to call with, we currently does not support OGNL expressions on using purely static methods
@@ -373,7 +373,7 @@ public class BeanExpression implements Expression, Predicate, AfterPropertiesCon
boolean nullSafe = OgnlHelper.isNullSafeOperator(methodName);
if (holder == null) {
- String name = getBeanName(null, beanHolder);
+ String name = getBeanName(exchange, null, beanHolder);
throw new RuntimeBeanExpressionException(exchange, name, ognl, "last method returned null and therefore cannot continue to invoke method " + methodName + " on a null instance");
}
@@ -411,7 +411,7 @@ public class BeanExpression implements Expression, Predicate, AfterPropertiesCon
key = lan.createExpression(key).evaluate(exchange, String.class);
}
if (key != null) {
- result = lookupResult(resultExchange, key, result, nullSafe, ognlPath, holder.getBean());
+ result = lookupResult(resultExchange, key, result, nullSafe, ognlPath, holder.getBean(exchange));
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanRegistryBeanTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanRegistryBeanTest.java
index 8a0470d..08abffc 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanRegistryBeanTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanRegistryBeanTest.java
@@ -36,7 +36,7 @@ public class BeanRegistryBeanTest extends ContextTestSupport {
public void testNoBean() {
RegistryBean rb = new RegistryBean(context, "bar");
try {
- rb.getBean();
+ rb.getBean(null);
fail("Should have thrown exception");
} catch (NoSuchBeanException e) {
assertEquals("bar", e.getName());
@@ -46,7 +46,7 @@ public class BeanRegistryBeanTest extends ContextTestSupport {
@Test
public void testBean() {
RegistryBean rb = new RegistryBean(context, "foo");
- Object bean = rb.getBean();
+ Object bean = rb.getBean(null);
assertIsInstanceOf(MyFooBean.class, bean);
assertNotNull(rb.getContext());
@@ -65,7 +65,7 @@ public class BeanRegistryBeanTest extends ContextTestSupport {
};
rb.setParameterMappingStrategy(myStrategy);
- Object bean = rb.getBean();
+ Object bean = rb.getBean(null);
assertIsInstanceOf(MyFooBean.class, bean);
assertNotNull(rb.getContext());
@@ -78,7 +78,7 @@ public class BeanRegistryBeanTest extends ContextTestSupport {
public void testLookupClass() throws Exception {
RegistryBean rb = new RegistryBean(context, "static");
- Object bean = rb.getBean();
+ Object bean = rb.getBean(null);
MyFooBean foo = assertIsInstanceOf(MyFooBean.class, bean);
assertEquals("foofoo", foo.echo("foo"));
}
@@ -87,7 +87,7 @@ public class BeanRegistryBeanTest extends ContextTestSupport {
public void testLookupFQNClass() throws Exception {
RegistryBean rb = new RegistryBean(context, "org.apache.camel.component.bean.MyDummyBean");
- Object bean = rb.getBean();
+ Object bean = rb.getBean(null);
MyDummyBean dummy = assertIsInstanceOf(MyDummyBean.class, bean);
assertEquals("Hello World", dummy.hello("World"));
}
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantStaticTypeBeanHolder.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/MyRequestBean.java
similarity index 60%
copy from components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantStaticTypeBeanHolder.java
copy to core/camel-core/src/test/java/org/apache/camel/component/bean/MyRequestBean.java
index 55b7f12..da09ce6 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantStaticTypeBeanHolder.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/MyRequestBean.java
@@ -16,21 +16,15 @@
*/
package org.apache.camel.component.bean;
-import org.apache.camel.CamelContext;
+public class MyRequestBean {
-/**
- * A constant {@link org.apache.camel.component.bean.BeanHolder} for a class or static class
- * where the intention is to only invoke static methods, without the need for creating an instance of the type.
- */
-public class ConstantStaticTypeBeanHolder extends ConstantTypeBeanHolder {
+ private int counter;
- public ConstantStaticTypeBeanHolder(Class<?> type, CamelContext context) {
- super(type, context);
+ public MyRequestBean() {
}
- @Override
- public Object getBean() {
- // we cannot create a bean as there is no default constructor
- return null;
+ public String doSomething(String body) {
+ counter++;
+ return body + counter;
}
}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/RequestScopedBeanComponentTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/RequestScopedBeanComponentTest.java
new file mode 100644
index 0000000..b9c2446
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/RequestScopedBeanComponentTest.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.bean;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class RequestScopedBeanComponentTest extends ContextTestSupport {
+
+ @Test
+ public void testRequestScope() throws Exception {
+ // creates a new instance so the counter starts from 1
+ getMockEndpoint("mock:a").expectedBodiesReceived("Hello1", "World1");
+ // and on the 2nd call its the same instance (request) so counter is now 2
+ getMockEndpoint("mock:b").expectedBodiesReceived("Hello12", "World12");
+
+ template.sendBody("direct:start", "Hello");
+ template.sendBody("direct:start", "World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .to("bean:org.apache.camel.component.bean.MyRequestBean?scope=Request")
+ .to("mock:a")
+ .to("bean:org.apache.camel.component.bean.MyRequestBean?scope=Request")
+ .to("mock:b");
+ }
+ };
+ }
+}
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantStaticTypeBeanHolder.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/RequestScopedBeanEIPTest.java
similarity index 59%
copy from components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantStaticTypeBeanHolder.java
copy to core/camel-core/src/test/java/org/apache/camel/component/bean/RequestScopedBeanEIPTest.java
index 55b7f12..ceb16d9 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantStaticTypeBeanHolder.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/RequestScopedBeanEIPTest.java
@@ -16,21 +16,22 @@
*/
package org.apache.camel.component.bean;
-import org.apache.camel.CamelContext;
+import org.apache.camel.BeanScope;
+import org.apache.camel.builder.RouteBuilder;
-/**
- * A constant {@link org.apache.camel.component.bean.BeanHolder} for a class or static class
- * where the intention is to only invoke static methods, without the need for creating an instance of the type.
- */
-public class ConstantStaticTypeBeanHolder extends ConstantTypeBeanHolder {
-
- public ConstantStaticTypeBeanHolder(Class<?> type, CamelContext context) {
- super(type, context);
- }
+public class RequestScopedBeanEIPTest extends RequestScopedBeanComponentTest {
@Override
- public Object getBean() {
- // we cannot create a bean as there is no default constructor
- return null;
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .bean(MyRequestBean.class, BeanScope.Request)
+ .to("mock:a")
+ .bean(MyRequestBean.class, BeanScope.Request)
+ .to("mock:b");
+ }
+ };
}
}
[camel] 05/08: CAMEL-14359: camel-bean - Adding bean scope option.
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch bean-scope
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 2745841d1ecf27e06766adf8716325a33e314138
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jan 6 20:41:11 2020 +0100
CAMEL-14359: camel-bean - Adding bean scope option.
---
.../camel-bean/src/main/docs/bean-component.adoc | 8 +-
.../camel-bean/src/main/docs/class-component.adoc | 8 +-
.../component/bean/AbstractBeanProcessor.java | 13 ++-
.../apache/camel/component/bean/BeanComponent.java | 34 +++---
.../apache/camel/component/bean/BeanEndpoint.java | 42 +++----
.../apache/camel/component/bean/BeanProcessor.java | 12 +-
.../org/apache/camel/component/bean/BeanScope.java | 27 +++++
.../apache/camel/processor/BeanSingletonTest.java | 4 +-
.../endpoint/dsl/BeanEndpointBuilderFactory.java | 122 ++++++++++++---------
.../endpoint/dsl/ClassEndpointBuilderFactory.java | 122 ++++++++++++---------
10 files changed, 240 insertions(+), 152 deletions(-)
diff --git a/components/camel-bean/src/main/docs/bean-component.adoc b/components/camel-bean/src/main/docs/bean-component.adoc
index 389a924..d8df9fa 100644
--- a/components/camel-bean/src/main/docs/bean-component.adoc
+++ b/components/camel-bean/src/main/docs/bean-component.adoc
@@ -31,7 +31,7 @@ The Bean component supports 5 options, which are listed below.
|===
| Name | Description | Default | Type
| *cache* (producer) | *Deprecated* Use singleton option instead. | true | Boolean
-| *singleton* (producer) | Whether to use singleton scoped beans. If enabled then the bean is created or looked up once and reused (the bean should be thread-safe). Setting this to false will let Camel create/lookup a new bean instance, per use; which acts as prototype scoped. However beware that if you lookup the bean, then the registry that holds the bean, would return a bean accordingly to its configuration, which can be singleton or prototype scoped. For example if you use Spring, or [...]
+| *scope* (producer) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while [...]
| *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean
@@ -65,12 +65,12 @@ with the following path and query parameters:
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
-| *cache* (common) | *Deprecated* Use singleton option instead. | | Boolean
+| *cache* (common) | *Deprecated* Use scope option instead. | | Boolean
| *method* (common) | Sets the name of the method to invoke on the bean | | String
-| *parameters* (common) | Used for configuring additional properties on the bean | | Map
-| *singleton* (common) | Whether to use singleton scoped beans. If enabled then the bean is created or looked up once and reused (the bean should be thread-safe). Setting this to false will let Camel create/lookup a new bean instance, per use; which acts as prototype scoped. However beware that if you lookup the bean, then the registry that holds the bean, would return a bean accordingly to its configuration, which can be singleton or prototype scoped. For example if you use Spring, or C [...]
+| *scope* (common) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while pr [...]
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *basicPropertyBinding* (advanced) | Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
+| *parameters* (advanced) | Used for configuring additional properties on the bean | | Map
| *synchronous* (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean
|===
// endpoint options: END
diff --git a/components/camel-bean/src/main/docs/class-component.adoc b/components/camel-bean/src/main/docs/class-component.adoc
index 0e3fa66..2caeb1e 100644
--- a/components/camel-bean/src/main/docs/class-component.adoc
+++ b/components/camel-bean/src/main/docs/class-component.adoc
@@ -34,7 +34,7 @@ The Class component supports 5 options, which are listed below.
|===
| Name | Description | Default | Type
| *cache* (producer) | *Deprecated* Use singleton option instead. | true | Boolean
-| *singleton* (producer) | Whether to use singleton scoped beans. If enabled then the bean is created or looked up once and reused (the bean should be thread-safe). Setting this to false will let Camel create/lookup a new bean instance, per use; which acts as prototype scoped. However beware that if you lookup the bean, then the registry that holds the bean, would return a bean accordingly to its configuration, which can be singleton or prototype scoped. For example if you use Spring, or [...]
+| *scope* (producer) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while [...]
| *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean
@@ -68,12 +68,12 @@ with the following path and query parameters:
[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
-| *cache* (common) | *Deprecated* Use singleton option instead. | | Boolean
+| *cache* (common) | *Deprecated* Use scope option instead. | | Boolean
| *method* (common) | Sets the name of the method to invoke on the bean | | String
-| *parameters* (common) | Used for configuring additional properties on the bean | | Map
-| *singleton* (common) | Whether to use singleton scoped beans. If enabled then the bean is created or looked up once and reused (the bean should be thread-safe). Setting this to false will let Camel create/lookup a new bean instance, per use; which acts as prototype scoped. However beware that if you lookup the bean, then the registry that holds the bean, would return a bean accordingly to its configuration, which can be singleton or prototype scoped. For example if you use Spring, or C [...]
+| *scope* (common) | Scope of bean. When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint. The bean should be thread-safe in case concurrent threads is calling the bean at the same time. When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean while processing a request and you want to call the same bean instance multiple times while pr [...]
| *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and [...]
| *basicPropertyBinding* (advanced) | Whether the endpoint should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean
+| *parameters* (advanced) | Used for configuring additional properties on the bean | | Map
| *synchronous* (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean
|===
// endpoint options: END
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java
index 25b8ac7..62deec6 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java
@@ -35,7 +35,7 @@ public abstract class AbstractBeanProcessor extends AsyncProcessorSupport {
private transient Processor processor;
private transient boolean lookupProcessorDone;
private final Object lock = new Object();
- private Boolean singleton;
+ private BeanScope scope;
private String method;
private boolean shorthandMethod;
@@ -88,7 +88,8 @@ public abstract class AbstractBeanProcessor extends AsyncProcessorSupport {
Processor target = getProcessor();
if (target == null) {
// only attempt to lookup the processor once or nearly once
- boolean allowCache = singleton == null || singleton; // allow cache by default
+ // allow cache by default or if the scope is singleton
+ boolean allowCache = scope == null || scope == BeanScope.Singleton;
if (allowCache) {
if (!lookupProcessorDone) {
synchronized (lock) {
@@ -167,12 +168,12 @@ public abstract class AbstractBeanProcessor extends AsyncProcessorSupport {
return method;
}
- public Boolean getSingleton() {
- return singleton;
+ public BeanScope getScope() {
+ return scope;
}
- public void setSingleton(Boolean singleton) {
- this.singleton = singleton;
+ public void setScope(BeanScope scope) {
+ this.scope = scope;
}
/**
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java
index 0d0a403..9bd470e 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanComponent.java
@@ -39,12 +39,16 @@ public class BeanComponent extends DefaultComponent {
@Deprecated
@Metadata(defaultValue = "true", description = "Use singleton option instead.")
private Boolean cache;
- @Metadata(defaultValue = "true", description = "Whether to use singleton scoped beans. If enabled then the bean"
- + " is created or looked up once and reused (the bean should be thread-safe). Setting this to false will let Camel create/lookup"
- + " a new bean instance, per use; which acts as prototype scoped. However beware that if you lookup the bean, then the registry that holds the bean, would return "
- + " a bean accordingly to its configuration, which can be singleton or prototype scoped. For example if you use Spring, or CDI, which"
- + " has their own settings for setting bean scopes.")
- private Boolean singleton = Boolean.TRUE;
+ @Metadata(defaultValue = "Singleton", description = "Scope of bean."
+ + " When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint."
+ + " The bean should be thread-safe in case concurrent threads is calling the bean at the same time."
+ + " When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean"
+ + " while processing a request and you want to call the same bean instance multiple times while processing the request."
+ + " The bean should not be thread-safe as the instance is only called from the same request. "
+ + " When using delegate scope, then the bean will be looked up or created per call. However in case of lookup then this is delegated "
+ + " to the bean registry such as Spring or CDI (if in use), which depends on their configuration can act as either singleton or prototype scope."
+ + " so when using delegate then this depends on the delegated registry.")
+ private BeanScope scope = BeanScope.Singleton;
public BeanComponent() {
}
@@ -58,7 +62,7 @@ public class BeanComponent extends DefaultComponent {
if (cache != null) {
endpoint.setCache(cache);
}
- endpoint.setSingleton(singleton);
+ endpoint.setScope(scope);
setProperties(endpoint, parameters);
// the bean.xxx options is for the bean
@@ -86,19 +90,23 @@ public class BeanComponent extends DefaultComponent {
@Deprecated
public Boolean getCache() {
- return singleton;
+ return scope == BeanScope.Singleton;
}
@Deprecated
public void setCache(Boolean cache) {
- this.singleton = cache;
+ if (cache) {
+ scope = BeanScope.Singleton;
+ } else {
+ scope = BeanScope.Delegate;
+ }
}
- public Boolean getSingleton() {
- return singleton;
+ public BeanScope getScope() {
+ return scope;
}
- public void setSingleton(Boolean singleton) {
- this.singleton = singleton;
+ public void setScope(BeanScope scope) {
+ this.scope = scope;
}
}
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
index 0e3b954..1c6dd74 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanEndpoint.java
@@ -41,15 +41,19 @@ public class BeanEndpoint extends DefaultEndpoint {
@UriParam(label = "common", description = "Sets the name of the method to invoke on the bean")
private String method;
@Deprecated
- @UriParam(label = "common", description = "Use singleton option instead.")
+ @UriParam(label = "common", description = "Use scope option instead.")
private Boolean cache;
- @UriParam(label = "common", defaultValue = "true", description = "Whether to use singleton scoped beans. If enabled then the bean"
- + " is created or looked up once and reused (the bean should be thread-safe). Setting this to false will let Camel create/lookup"
- + " a new bean instance, per use; which acts as prototype scoped. However beware that if you lookup the bean, then the registry that holds the bean, would return "
- + " a bean accordingly to its configuration, which can be singleton or prototype scoped. For example if you use Spring, or CDI, which"
- + " has their own settings for setting bean scopes.")
- private Boolean singleton = Boolean.TRUE;
- @UriParam(prefix = "bean.", label = "common", description = "Used for configuring additional properties on the bean", multiValue = true)
+ @UriParam(label = "common", defaultValue = "Singleton", description = "Scope of bean."
+ + " When using singleton scope (default) the bean is created or looked up only once and reused for the lifetime of the endpoint."
+ + " The bean should be thread-safe in case concurrent threads is calling the bean at the same time."
+ + " When using request scope the bean is created or looked up once per request (exchange). This can be used if you want to store state on a bean"
+ + " while processing a request and you want to call the same bean instance multiple times while processing the request."
+ + " The bean should not be thread-safe as the instance is only called from the same request. "
+ + " When using delegate scope, then the bean will be looked up or created per call. However in case of lookup then this is delegated "
+ + " to the bean registry such as Spring or CDI (if in use), which depends on their configuration can act as either singleton or prototype scope."
+ + " so when using delegate then this depends on the delegated registry.")
+ private BeanScope scope = BeanScope.Singleton;
+ @UriParam(prefix = "bean.", label = "advanced", description = "Used for configuring additional properties on the bean", multiValue = true)
private Map<String, Object> parameters;
public BeanEndpoint() {
@@ -100,7 +104,7 @@ public class BeanEndpoint extends DefaultEndpoint {
if (method != null) {
processor.setMethod(method);
}
- processor.setSingleton(singleton);
+ processor.setScope(scope);
if (parameters != null) {
setProperties(processor, parameters);
}
@@ -131,24 +135,24 @@ public class BeanEndpoint extends DefaultEndpoint {
@Deprecated
public Boolean getCache() {
- return getSingleton();
+ return scope == BeanScope.Singleton;
}
@Deprecated
public void setCache(Boolean cache) {
- setSingleton(cache);
- }
-
- public boolean isSingleton() {
- return singleton != null ? singleton : false;
+ if (cache) {
+ scope = BeanScope.Singleton;
+ } else {
+ scope = BeanScope.Delegate;
+ }
}
- public Boolean getSingleton() {
- return singleton;
+ public BeanScope getScope() {
+ return scope;
}
- public void setSingleton(Boolean singleton) {
- this.singleton = singleton;
+ public void setScope(BeanScope scope) {
+ this.scope = scope;
}
public String getMethod() {
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanProcessor.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
index b9ad238..72342e8 100644
--- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
@@ -76,16 +76,16 @@ public class BeanProcessor extends ServiceSupport implements AsyncProcessor {
return delegate.getMethod();
}
- public Boolean getSingleton() {
- return delegate.getSingleton();
+ public void setMethod(String method) {
+ delegate.setMethod(method);
}
- public void setSingleton(Boolean singleton) {
- delegate.setSingleton(singleton);
+ public BeanScope getScope() {
+ return delegate.getScope();
}
- public void setMethod(String method) {
- delegate.setMethod(method);
+ public void setScope(BeanScope scope) {
+ delegate.setScope(scope);
}
public boolean isShorthandMethod() {
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanScope.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanScope.java
new file mode 100644
index 0000000..9012a55
--- /dev/null
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanScope.java
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.bean;
+
+/**
+ * Bean scopes.
+ */
+public enum BeanScope {
+
+ Singleton,
+ Delegate,
+ Request;
+}
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/BeanSingletonTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/BeanSingletonTest.java
index b5104be..8f2c857 100644
--- a/core/camel-core/src/test/java/org/apache/camel/processor/BeanSingletonTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/BeanSingletonTest.java
@@ -36,8 +36,8 @@ public class BeanSingletonTest extends ContextTestSupport {
return new RouteBuilder() {
@Override
public void configure() {
- from("direct:noCache").to("bean:something?singleton=false");
- from("direct:cached").to("bean:something?singleton=true");
+ from("direct:noCache").to("bean:something?scope=Delegate");
+ from("direct:cached").to("bean:something?scope=Singleton");
}
};
}
diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BeanEndpointBuilderFactory.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BeanEndpointBuilderFactory.java
index 471c756..0930ec3 100644
--- a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BeanEndpointBuilderFactory.java
+++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BeanEndpointBuilderFactory.java
@@ -39,7 +39,7 @@ public interface BeanEndpointBuilderFactory {
return (AdvancedBeanEndpointBuilder) this;
}
/**
- * Use singleton option instead.
+ * Use scope option instead.
*
* The option is a: <code>java.lang.Boolean</code> type.
*
@@ -51,7 +51,7 @@ public interface BeanEndpointBuilderFactory {
return this;
}
/**
- * Use singleton option instead.
+ * Use scope option instead.
*
* The option will be converted to a <code>java.lang.Boolean</code>
* type.
@@ -75,65 +75,53 @@ public interface BeanEndpointBuilderFactory {
return this;
}
/**
- * Used for configuring additional properties on the bean.
+ * Scope of bean. When using singleton scope (default) the bean is
+ * created or looked up only once and reused for the lifetime of the
+ * endpoint. The bean should be thread-safe in case concurrent threads
+ * is calling the bean at the same time. When using request scope the
+ * bean is created or looked up once per request (exchange). This can be
+ * used if you want to store state on a bean while processing a request
+ * and you want to call the same bean instance multiple times while
+ * processing the request. The bean should not be thread-safe as the
+ * instance is only called from the same request. When using delegate
+ * scope, then the bean will be looked up or created per call. However
+ * in case of lookup then this is delegated to the bean registry such as
+ * Spring or CDI (if in use), which depends on their configuration can
+ * act as either singleton or prototype scope. so when using delegate
+ * then this depends on the delegated registry.
*
- * The option is a: <code>java.util.Map<java.lang.String,
- * java.lang.Object></code> type.
+ * The option is a:
+ * <code>org.apache.camel.component.bean.BeanScope</code> type.
*
* Group: common
*/
- default BeanEndpointBuilder parameters(Map<String, Object> parameters) {
- doSetProperty("parameters", parameters);
+ default BeanEndpointBuilder scope(BeanScope scope) {
+ doSetProperty("scope", scope);
return this;
}
/**
- * Used for configuring additional properties on the bean.
+ * Scope of bean. When using singleton scope (default) the bean is
+ * created or looked up only once and reused for the lifetime of the
+ * endpoint. The bean should be thread-safe in case concurrent threads
+ * is calling the bean at the same time. When using request scope the
+ * bean is created or looked up once per request (exchange). This can be
+ * used if you want to store state on a bean while processing a request
+ * and you want to call the same bean instance multiple times while
+ * processing the request. The bean should not be thread-safe as the
+ * instance is only called from the same request. When using delegate
+ * scope, then the bean will be looked up or created per call. However
+ * in case of lookup then this is delegated to the bean registry such as
+ * Spring or CDI (if in use), which depends on their configuration can
+ * act as either singleton or prototype scope. so when using delegate
+ * then this depends on the delegated registry.
*
* The option will be converted to a
- * <code>java.util.Map<java.lang.String, java.lang.Object></code>
- * type.
- *
- * Group: common
- */
- default BeanEndpointBuilder parameters(String parameters) {
- doSetProperty("parameters", parameters);
- return this;
- }
- /**
- * Whether to use singleton scoped beans. If enabled then the bean is
- * created or looked up once and reused (the bean should be
- * thread-safe). Setting this to false will let Camel create/lookup a
- * new bean instance, per use; which acts as prototype scoped. However
- * beware that if you lookup the bean, then the registry that holds the
- * bean, would return a bean accordingly to its configuration, which can
- * be singleton or prototype scoped. For example if you use Spring, or
- * CDI, which has their own settings for setting bean scopes.
- *
- * The option is a: <code>java.lang.Boolean</code> type.
+ * <code>org.apache.camel.component.bean.BeanScope</code> type.
*
* Group: common
*/
- default BeanEndpointBuilder singleton(Boolean singleton) {
- doSetProperty("singleton", singleton);
- return this;
- }
- /**
- * Whether to use singleton scoped beans. If enabled then the bean is
- * created or looked up once and reused (the bean should be
- * thread-safe). Setting this to false will let Camel create/lookup a
- * new bean instance, per use; which acts as prototype scoped. However
- * beware that if you lookup the bean, then the registry that holds the
- * bean, would return a bean accordingly to its configuration, which can
- * be singleton or prototype scoped. For example if you use Spring, or
- * CDI, which has their own settings for setting bean scopes.
- *
- * The option will be converted to a <code>java.lang.Boolean</code>
- * type.
- *
- * Group: common
- */
- default BeanEndpointBuilder singleton(String singleton) {
- doSetProperty("singleton", singleton);
+ default BeanEndpointBuilder scope(String scope) {
+ doSetProperty("scope", scope);
return this;
}
/**
@@ -212,6 +200,32 @@ public interface BeanEndpointBuilderFactory {
return this;
}
/**
+ * Used for configuring additional properties on the bean.
+ *
+ * The option is a: <code>java.util.Map<java.lang.String,
+ * java.lang.Object></code> type.
+ *
+ * Group: advanced
+ */
+ default AdvancedBeanEndpointBuilder parameters(
+ Map<String, Object> parameters) {
+ doSetProperty("parameters", parameters);
+ return this;
+ }
+ /**
+ * Used for configuring additional properties on the bean.
+ *
+ * The option will be converted to a
+ * <code>java.util.Map<java.lang.String, java.lang.Object></code>
+ * type.
+ *
+ * Group: advanced
+ */
+ default AdvancedBeanEndpointBuilder parameters(String parameters) {
+ doSetProperty("parameters", parameters);
+ return this;
+ }
+ /**
* Sets whether synchronous processing should be strictly used, or Camel
* is allowed to use asynchronous processing (if supported).
*
@@ -236,6 +250,16 @@ public interface BeanEndpointBuilderFactory {
return this;
}
}
+
+ /**
+ * Proxy enum for <code>org.apache.camel.component.bean.BeanScope</code>
+ * enum.
+ */
+ enum BeanScope {
+ Singleton,
+ Delegate,
+ Request;
+ }
/**
* Bean (camel-bean)
* The bean component is for invoking Java beans from Camel.
diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/ClassEndpointBuilderFactory.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/ClassEndpointBuilderFactory.java
index 2718f10..125d4d7 100644
--- a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/ClassEndpointBuilderFactory.java
+++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/ClassEndpointBuilderFactory.java
@@ -39,7 +39,7 @@ public interface ClassEndpointBuilderFactory {
return (AdvancedClassEndpointBuilder) this;
}
/**
- * Use singleton option instead.
+ * Use scope option instead.
*
* The option is a: <code>java.lang.Boolean</code> type.
*
@@ -51,7 +51,7 @@ public interface ClassEndpointBuilderFactory {
return this;
}
/**
- * Use singleton option instead.
+ * Use scope option instead.
*
* The option will be converted to a <code>java.lang.Boolean</code>
* type.
@@ -75,65 +75,53 @@ public interface ClassEndpointBuilderFactory {
return this;
}
/**
- * Used for configuring additional properties on the bean.
+ * Scope of bean. When using singleton scope (default) the bean is
+ * created or looked up only once and reused for the lifetime of the
+ * endpoint. The bean should be thread-safe in case concurrent threads
+ * is calling the bean at the same time. When using request scope the
+ * bean is created or looked up once per request (exchange). This can be
+ * used if you want to store state on a bean while processing a request
+ * and you want to call the same bean instance multiple times while
+ * processing the request. The bean should not be thread-safe as the
+ * instance is only called from the same request. When using delegate
+ * scope, then the bean will be looked up or created per call. However
+ * in case of lookup then this is delegated to the bean registry such as
+ * Spring or CDI (if in use), which depends on their configuration can
+ * act as either singleton or prototype scope. so when using delegate
+ * then this depends on the delegated registry.
*
- * The option is a: <code>java.util.Map<java.lang.String,
- * java.lang.Object></code> type.
+ * The option is a:
+ * <code>org.apache.camel.component.bean.BeanScope</code> type.
*
* Group: common
*/
- default ClassEndpointBuilder parameters(Map<String, Object> parameters) {
- doSetProperty("parameters", parameters);
+ default ClassEndpointBuilder scope(BeanScope scope) {
+ doSetProperty("scope", scope);
return this;
}
/**
- * Used for configuring additional properties on the bean.
+ * Scope of bean. When using singleton scope (default) the bean is
+ * created or looked up only once and reused for the lifetime of the
+ * endpoint. The bean should be thread-safe in case concurrent threads
+ * is calling the bean at the same time. When using request scope the
+ * bean is created or looked up once per request (exchange). This can be
+ * used if you want to store state on a bean while processing a request
+ * and you want to call the same bean instance multiple times while
+ * processing the request. The bean should not be thread-safe as the
+ * instance is only called from the same request. When using delegate
+ * scope, then the bean will be looked up or created per call. However
+ * in case of lookup then this is delegated to the bean registry such as
+ * Spring or CDI (if in use), which depends on their configuration can
+ * act as either singleton or prototype scope. so when using delegate
+ * then this depends on the delegated registry.
*
* The option will be converted to a
- * <code>java.util.Map<java.lang.String, java.lang.Object></code>
- * type.
- *
- * Group: common
- */
- default ClassEndpointBuilder parameters(String parameters) {
- doSetProperty("parameters", parameters);
- return this;
- }
- /**
- * Whether to use singleton scoped beans. If enabled then the bean is
- * created or looked up once and reused (the bean should be
- * thread-safe). Setting this to false will let Camel create/lookup a
- * new bean instance, per use; which acts as prototype scoped. However
- * beware that if you lookup the bean, then the registry that holds the
- * bean, would return a bean accordingly to its configuration, which can
- * be singleton or prototype scoped. For example if you use Spring, or
- * CDI, which has their own settings for setting bean scopes.
- *
- * The option is a: <code>java.lang.Boolean</code> type.
+ * <code>org.apache.camel.component.bean.BeanScope</code> type.
*
* Group: common
*/
- default ClassEndpointBuilder singleton(Boolean singleton) {
- doSetProperty("singleton", singleton);
- return this;
- }
- /**
- * Whether to use singleton scoped beans. If enabled then the bean is
- * created or looked up once and reused (the bean should be
- * thread-safe). Setting this to false will let Camel create/lookup a
- * new bean instance, per use; which acts as prototype scoped. However
- * beware that if you lookup the bean, then the registry that holds the
- * bean, would return a bean accordingly to its configuration, which can
- * be singleton or prototype scoped. For example if you use Spring, or
- * CDI, which has their own settings for setting bean scopes.
- *
- * The option will be converted to a <code>java.lang.Boolean</code>
- * type.
- *
- * Group: common
- */
- default ClassEndpointBuilder singleton(String singleton) {
- doSetProperty("singleton", singleton);
+ default ClassEndpointBuilder scope(String scope) {
+ doSetProperty("scope", scope);
return this;
}
/**
@@ -212,6 +200,32 @@ public interface ClassEndpointBuilderFactory {
return this;
}
/**
+ * Used for configuring additional properties on the bean.
+ *
+ * The option is a: <code>java.util.Map<java.lang.String,
+ * java.lang.Object></code> type.
+ *
+ * Group: advanced
+ */
+ default AdvancedClassEndpointBuilder parameters(
+ Map<String, Object> parameters) {
+ doSetProperty("parameters", parameters);
+ return this;
+ }
+ /**
+ * Used for configuring additional properties on the bean.
+ *
+ * The option will be converted to a
+ * <code>java.util.Map<java.lang.String, java.lang.Object></code>
+ * type.
+ *
+ * Group: advanced
+ */
+ default AdvancedClassEndpointBuilder parameters(String parameters) {
+ doSetProperty("parameters", parameters);
+ return this;
+ }
+ /**
* Sets whether synchronous processing should be strictly used, or Camel
* is allowed to use asynchronous processing (if supported).
*
@@ -236,6 +250,16 @@ public interface ClassEndpointBuilderFactory {
return this;
}
}
+
+ /**
+ * Proxy enum for <code>org.apache.camel.component.bean.BeanScope</code>
+ * enum.
+ */
+ enum BeanScope {
+ Singleton,
+ Delegate,
+ Request;
+ }
/**
* Class (camel-bean)
* The Class component is for invoking Java classes (Java beans) from Camel.