This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch var in repository https://gitbox.apache.org/repos/asf/camel.git
commit a93137d6145f47ba6dfba8a3277045417f161c70 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Dec 30 14:56:02 2023 +0100 CAMEL-19749: Add variables as concept to Camel --- .../engine/DefaultVariableRepositoryFactory.java | 9 ++- .../mbean/ManagedVariableRepositoryMBean.java | 4 +- .../working-with-camel-core/pages/index.adoc | 1 + docs/user-manual/modules/ROOT/pages/variables.adoc | 82 ++++++++++++++++++++++ 4 files changed, 91 insertions(+), 5 deletions(-) diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultVariableRepositoryFactory.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultVariableRepositoryFactory.java index 2190fc9242a..0410f5afc1c 100644 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultVariableRepositoryFactory.java +++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultVariableRepositoryFactory.java @@ -47,11 +47,14 @@ public class DefaultVariableRepositoryFactory extends ServiceSupport implements @Override public VariableRepository getVariableRepository(String id) { - if ("global".equals(id)) { + if (global != null && "global".equals(id)) { return global; } VariableRepository repo = CamelContextHelper.lookup(camelContext, id, VariableRepository.class); + if (repo == null) { + repo = CamelContextHelper.lookup(camelContext, id + "-variable-repository", VariableRepository.class); + } if (repo == null) { // try via factory finder Class<?> clazz = factoryFinder.findClass(id).orElse(null); @@ -79,8 +82,8 @@ public class DefaultVariableRepositoryFactory extends ServiceSupport implements protected void doStart() throws Exception { super.doStart(); - VariableRepository repo - = CamelContextHelper.lookup(camelContext, GLOBAL_VARIABLE_REPOSITORY_ID, VariableRepository.class); + // let's see if there is a custom global repo + VariableRepository repo = getVariableRepository("global"); if (repo != null) { LOG.info("Using VariableRepository: {} as global repository", repo.getId()); global = repo; diff --git a/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedVariableRepositoryMBean.java b/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedVariableRepositoryMBean.java index 8614f2b812c..6a976f07bee 100644 --- a/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedVariableRepositoryMBean.java +++ b/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedVariableRepositoryMBean.java @@ -18,11 +18,11 @@ package org.apache.camel.api.management.mbean; import java.util.Set; +import javax.management.openmbean.TabularData; + import org.apache.camel.api.management.ManagedAttribute; import org.apache.camel.api.management.ManagedOperation; -import javax.management.openmbean.TabularData; - public interface ManagedVariableRepositoryMBean extends ManagedServiceMBean { @ManagedAttribute(description = "Repository ID") diff --git a/docs/main/modules/working-with-camel-core/pages/index.adoc b/docs/main/modules/working-with-camel-core/pages/index.adoc index fa5127d92e6..4ce6e00653a 100644 --- a/docs/main/modules/working-with-camel-core/pages/index.adoc +++ b/docs/main/modules/working-with-camel-core/pages/index.adoc @@ -94,6 +94,7 @@ Learn about additional ways to customize your integrations. Explore alternatives ** xref:manual::spring.adoc[Spring] ** xref:manual::spring-xml-extensions.adoc[Spring XML Extensions] ** xref:manual::validator.adoc[Validator] +** xref:manual::variables.adoc[Variables] ** xref:manual::what-are-the-dependencies.adoc[Camel Requirements] ** xref:manual::testing.adoc[Testing] diff --git a/docs/user-manual/modules/ROOT/pages/variables.adoc b/docs/user-manual/modules/ROOT/pages/variables.adoc new file mode 100644 index 00000000000..4715d124d98 --- /dev/null +++ b/docs/user-manual/modules/ROOT/pages/variables.adoc @@ -0,0 +1,82 @@ += Variables + +*Available from Camel 4.4* + +In Camel 4.4, we have introduced the concept of _variables_. + +A variable is a key/value that can hold a value that can either be private per `Exchange` or global shared +in the `CamelContext`. + +NOTE: You can also use _exchange properties_ as variables but the exchange properties are also used internally by Camel, +and some EIPs and components. With the newly introduced _variables_ then these are exclusively for end users. + +== Variable Repository + +The variables are stored in one or more `org.apache.camel.spi.VariableRepository`. By default, there are the following repositories + +- `ExchangeVariableRepository` - A private repository per `Exchange` that holds variables that are private for the lifecycle of each `Exchange`. +- `GlobalVariableRepository` - Uses `global` as id. A shared global repository for the entire `CamelContext`. + +The `ExchangeVariableRepository` is special as its private per exchange and is the default repository when used during routing. + +TIP: There is also `org.apache.camel.spi.BrowsableVariableRepository` which is an extension to `VariableRepository` that +has APIs to browse the currently variables. This is used by Camel with Camel JBang, and JMX to be able to see the current variables +from management tooling, CLI and the developer console. + +=== Custom variable repositories + +You can implement custom `org.apache.camel.spi.VariableRepository` and plugin to be used out of the box with Apache Camel. +For example, you can build a custom repository that stores the variables in a database, so they are persisted. + +Each repository must have its own unique id. However, it's also possible to replace the default `global` repository with another. + +== Getting and setting variables from Java API + +To get a local variable from the current exchange, you can do this via Java API: + +[source,java] +---- +String myValue = "..."; +exchange.setVariable("myKey", myValue); + +// and later to get the variable +Object val = exchange.getVariable("myKey"); + +// you can get the value as a specific type +String str = exchange.getVariable("myKey", String.class); +---- + +The API on `Exchange` will by default get the variables from its local private repository. +However, you can also get variables from other repositories, such as the `global` as show: + +[source,java] +---- +Object val = exchange.getVariable("global:myGlobalKey"); +---- + +And you can also assign a global variable by prefixing with `global:` as follows: + +[source,java] +---- +exchange.setVariable("global:myGlobalKey", someObjectHere); +---- + +There is also API on `CamelContext` to get variables. However, this API will by default get from the `global` repository, +as it's not possible to get variables from any inflight `Exchange` currently being routed. + +[source,java] +---- +Object val = context.getVariable("myGlobalKey"); + +// you can get the value as a specific type +String str = context.getVariable("myGlobalKey", String.class); +---- + +== Setting and getting variables from DSL + +It is also possible to use variables in Camel xref:routes.adoc[routes] using the +xref:components:eips:setVariable-eip.adoc[SetVariable], and xref:components:eips:removeVariable-eip.adoc[RemoveVariable] EIPs. + +These EIPs makes it possible to set and remove variables from routes. And you can also access variables from the xref:components:languages:simple-language.adoc[Simple] language. + +TODO: Some examples here