This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new dffa627 CAMEL-16861: Cleanup and update EIP docs dffa627 is described below commit dffa62798b6336c30b5c6e9a3acd9eaca69ca302 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Sep 15 15:04:47 2021 +0200 CAMEL-16861: Cleanup and update EIP docs --- .../docs/modules/eips/pages/batch-config-eip.adoc | 2 +- .../src/main/docs/modules/eips/pages/bean-eip.adoc | 95 ++++++++++++++-------- 2 files changed, 64 insertions(+), 33 deletions(-) diff --git a/core/camel-core-engine/src/main/docs/modules/eips/pages/batch-config-eip.adoc b/core/camel-core-engine/src/main/docs/modules/eips/pages/batch-config-eip.adoc index b1e6bc9..c98c118 100644 --- a/core/camel-core-engine/src/main/docs/modules/eips/pages/batch-config-eip.adoc +++ b/core/camel-core-engine/src/main/docs/modules/eips/pages/batch-config-eip.adoc @@ -5,7 +5,7 @@ :since: :supportLevel: Stable -Batch-processing resequence EIP +Configuring for xref:resequence-eip.adoc[Resequence EIP] in batching mode. // eip options: START The Batch-config EIP supports 5 options which are listed below: diff --git a/core/camel-core-engine/src/main/docs/modules/eips/pages/bean-eip.adoc b/core/camel-core-engine/src/main/docs/modules/eips/pages/bean-eip.adoc index 8003b0b..4506d52 100644 --- a/core/camel-core-engine/src/main/docs/modules/eips/pages/bean-eip.adoc +++ b/core/camel-core-engine/src/main/docs/modules/eips/pages/bean-eip.adoc @@ -5,7 +5,11 @@ :since: :supportLevel: Stable -The Bean EIP binds beans to Camel message exchanges. +The Bean EIP is used for invoking a method on a bean, and the returned value +is the new message body. + +The Bean EIP is similar to the xref:components::bean-component.adoc[Bean] component +which also is used for invoking beans, but in the form as a Camel component. == URI Format @@ -15,7 +19,7 @@ bean:beanID[?options] ---- Where *beanID* can be any string which is used to look up the bean in -the xref:latest@manual:ROOT:registry.adoc[Registry] +the xref:latest@manual:ROOT:registry.adoc[Registry]. == EIP options @@ -37,56 +41,83 @@ The Bean EIP supports 5 options which are listed below: 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. +while processing a request, and you want to call the same bean instance multiple times while processing the request. The bean does not have to 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 + +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. +However, when using `prototype` then behaviour is dependent on the delegated registry (such as Spring, Quarkus or CDI). -== Bean as endpoint +== Example -Camel also supports invoking xref:components::bean-component.adoc[Bean] as an Endpoint. In the -route below: +The Bean EIP can be used directly in the routes as shown below: -What happens is that when the exchange is routed to the `myBean` Camel -will use the xref:latest@manual:ROOT:bean-binding.adoc[Bean Binding] to invoke the bean. + - The source for the bean is just a plain POJO: +[source,java] +---- +// lookup bean from registry and invoke the given method by the name +from("direct:foo").bean("myBean", "myMethod"); -Camel will use xref:latest@manual:ROOT:bean-binding.adoc[Bean Binding] to invoke the -`sayHello` method, by converting the Exchange's In body to the `String` -type and storing the output of the method on the Exchange Out body. +// lookup bean from registry and invoke best matching method +from("direct:bar").bean("myBean"); +---- -== Java DSL bean syntax +And with Spring XML you can declare the bean using `<bean>` as shown: +[source,xml] +---- +<bean id="myBean" class="com.foo.ExampleBean"/> +---- -Java DSL comes with syntactic sugar for the xref:components::bean-component.adoc[Bean] -component. Instead of specifying the bean explicitly as the endpoint -(i.e. `to("bean:beanName")`) you can use the following syntax: +And in XML DSL you can call this bean: -[source,java] +[source,xml] ---- -// Send message to the bean endpoint -// and invoke method resolved using Bean Binding. -from("direct:start").beanRef("beanName"); - -// Send message to the bean endpoint -// and invoke given method. -from("direct:start").beanRef("beanName", "methodName"); +<routes> + <route> + <from uri="direct:foo"/> + <bean ref="myBean" method="myMethod"/> + </route> + <route> + <from uri="direct:bar"/> + <bean ref="myBean"/> + </route> +</routes> ---- Instead of passing name of the reference to the bean (so that Camel will -lookup for it in the registry), you can specify the bean itself: +lookup for it in the registry), you can provide the bean: [source,java] ---- // Send message to the given bean instance. -from("direct:start").bean(new ExampleBean()); +from("direct:foo").bean(new ExampleBean()); // Explicit selection of bean method to be invoked. -from("direct:start").bean(new ExampleBean(), "methodName"); +from("direct:bar").bean(new ExampleBean(), "myMethod"); -// Camel will create the instance of bean and cache it for you. -from("direct:start").bean(ExampleBean.class); +// Camel will create a singleton instance of the bean, and reuse the instance for following calls (see scope) +from("direct:cheese").bean(ExampleBean.class); +---- + +In XML DSL this is also possible using `beanType`: + +[source,xml] +---- +<routes> + <route> + <from uri="direct:foo"/> + <bean beanType="com.foo.ExampleBean" method="myMethod"/> + </route> + <route> + <from uri="direct:bar"/> + <bean beanType="com.foo.ExampleBean"/> + </route> + <route> + <from uri="direct:cheese"/> + <bean beanType="com.foo.ExampleBean"/> + </route> +</routes> ---- == Bean binding @@ -95,5 +126,5 @@ How bean methods to be invoked are chosen (if they are not specified explicitly through the *method* parameter) and how parameter values are constructed from the xref:message.adoc[Message] are all defined by the xref:latest@manual:ROOT:bean-binding.adoc[Bean Binding] mechanism which is used throughout -all of the various xref:latest@manual:ROOT:bean-integration.adoc[Bean Integration] +all the various xref:latest@manual:ROOT:bean-integration.adoc[Bean Integration] mechanisms in Camel.