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 3448385 CAMEL-16861: Polished bean language 3448385 is described below commit 344838585b99f2fe99df3bcc452cb39cc9a238c1 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon Sep 13 16:47:53 2021 +0200 CAMEL-16861: Polished bean language --- .../org/apache/camel/language/bean/bean.json | 4 +- .../camel-bean/src/main/docs/bean-language.adoc | 148 ++++++--------------- docs/components/modules/languages/nav.adoc | 2 +- 3 files changed, 45 insertions(+), 109 deletions(-) diff --git a/components/camel-bean/src/generated/resources/org/apache/camel/language/bean/bean.json b/components/camel-bean/src/generated/resources/org/apache/camel/language/bean/bean.json index f4c5ca0..645f237 100644 --- a/components/camel-bean/src/generated/resources/org/apache/camel/language/bean/bean.json +++ b/components/camel-bean/src/generated/resources/org/apache/camel/language/bean/bean.json @@ -2,8 +2,8 @@ "language": { "kind": "language", "name": "bean", - "title": "Bean method", - "description": "Call a method of the specified Java bean passing the Exchange, Body or specific headers to it.", + "title": "Bean Method", + "description": "Calls a Java bean method.", "deprecated": false, "firstVersion": "1.3.0", "label": "language,core,java", diff --git a/components/camel-bean/src/main/docs/bean-language.adoc b/components/camel-bean/src/main/docs/bean-language.adoc index 97a4ebc..2f8fd4e 100644 --- a/components/camel-bean/src/main/docs/bean-language.adoc +++ b/components/camel-bean/src/main/docs/bean-language.adoc @@ -1,35 +1,27 @@ [[bean-language]] -= Bean method Language += Bean Method Language //THIS FILE IS COPIED: EDIT THE SOURCE FILE: :page-source: components/camel-bean/src/main/docs/bean-language.adoc -:docTitle: Bean method +:docTitle: Bean Method :artifactId: camel-bean -:description: Call a method of the specified Java bean passing the Exchange, Body or specific headers to it. +:description: Calls a Java bean method. :since: 1.3 :supportLevel: Stable include::{cq-version}@camel-quarkus:ROOT:partial$reference/languages/bean.adoc[opts=optional] *Since Camel {since}* -The purpose of the Bean Language is to be able to implement an -xref:manual::expression.adoc[Expression] or xref:manual::predicate.adoc[Predicate] using -a simple method on a bean. The bean name is resolved using a xref:manual::registry.adoc[Registry], such as the -xref:ROOT:spring-summary.adoc[Spring] *`ApplicationContext`*, then a method is -invoked to evaluate the xref:manual::expression.adoc[Expression] or -xref:manual::predicate.adoc[Predicate]. If no method name is provided then one -is chosen using the rules for xref:manual::bean-binding.adoc[Bean Binding]; -using the type of the message body and using any annotations on the bean -methods. +The Bean language is used for calling a method on an existing Java bean. -The xref:manual::bean-binding.adoc[Bean Binding] rules are used to bind the -xref:{eip-vc}:eips:message.adoc[Message] Exchange to the method parameters; so you can -annotate the bean to extract headers or other expressions such as -xref:xpath-language.adoc[XPath] or xref:xquery-language.adoc[XQuery] from the message. +Camel adapts to the method being called via xref:manual::bean-binding.adoc[Bean Binding]. +The binding process will for example automatic convert the message payload to the parameter +of type of the first parameter in the method. The binding process has a lot more features so +it is recommended to read the xref:manual::bean-binding.adoc[Bean Binding] documentation for mor details. -== Bean Language options +== Bean Method options // language options: START -The Bean method language supports 5 options, which are listed below. +The Bean Method language supports 5 options, which are listed below. @@ -44,21 +36,25 @@ The Bean method language supports 5 options, which are listed below. |=== // language options: END +== Examples -[[BeanLanguage-UsingBeanExpressionsinJava]] -== Using Bean Expressions in Java +In the given route below, we call a Java Bean Method with `method`, where "myBean" +is the id of the bean to use (lookup from xref:manual::registry.adoc[Registry]), +and "isGoldCustomer" is the name of the method to call. -[source,syntaxhighlighter-pre] +[source,java] ---- from("activemq:topic:OrdersTopic") .filter().method("myBean", "isGoldCustomer") .to("activemq:BigSpendersQueue"); ---- -[[BeanLanguage-UsingBeanExpressionsinSpringXML]] -== Using Bean Expressions in Spring XML +TIP: It is also possible to omit the method name, then Camel would have to choose the best suitable +method to use; this process is a little bit complex, so it is good practice to specify the method name, -[source,syntaxhighlighter-pre] +And in XML DSL + +[source,xml] ---- <route> <from uri="activemq:topic:OrdersTopic"/> @@ -69,26 +65,9 @@ from("activemq:topic:OrdersTopic") </route> ---- -[WARNING] -==== - **Bean Attribute Now Deprecated** - -The *`bean`* attribute of the method expression element is now -deprecated. Use the *`ref`* attribute instead. - -==== - -[[BeanLanguage-WritingtheExpressionBean]] -== Writing the Expression Bean +The bean could be implemented as follows: -The bean in the above examples is just any old Java Bean with a method -called *`isGoldCustomer()`* that returns some object that is easily -converted to a *`boolean`* value in this case, as its used as a -predicate. - -Example: - -[source,syntaxhighlighter-pre] +[source,java] ---- public class MyBean { public boolean isGoldCustomer(Exchange exchange) { @@ -97,96 +76,53 @@ public class MyBean { } ---- -We can also use the xref:manual::bean-integration.adoc[Bean Integration] -annotations. - -Example: +How this method uses `Exchange` in the method signature. You would often not do that, +and use non Camel types. For example by using `String` then Camel will automatic +convert the message body to this type when calling the method: -[source,syntaxhighlighter-pre] +[source,java] ---- public boolean isGoldCustomer(String body) {...} ---- -or +=== Using Annotations for bean integration -[source,syntaxhighlighter-pre] +You can also use the xref:manual::bean-integration.adoc[Bean Integration] +annotations, such as `@Header`, `@Body` etc + +[source,java] ---- public boolean isGoldCustomer(@Header(name = "foo") Integer fooHeader) {...} ---- -So you can bind parameters of the method to the Exchange, the +So you can bind parameters of the method to the `Exchange`, the xref:{eip-vc}:eips:message.adoc[Message] or individual headers, properties, the body or other expressions. -[[BeanLanguage-Non-RegistryBeans]] -== Non-Registry Beans +=== Non-Registry Beans -The xref:bean-language.adoc[Bean Language] also supports invoking beans -that isn't registered in the xref:manual::registry.adoc[Registry]. This is -usable for quickly to invoke a bean from Java DSL where you don't need -to register the bean in the xref:manual::registry.adoc[Registry] such as the -xref:ROOT:spring-summary.adoc[Spring] *`ApplicationContext`*. Camel can instantiate -the bean and invoke the method if given a class or invoke an already -existing instance. +The Bean Method Language also supports invoking beans +that isn't registered in the xref:manual::registry.adoc[Registry]. -Example: +Camel can instantiate the bean and invoke the method if given a class +or invoke an already existing instance. -[source,syntaxhighlighter-pre] +[source,java] ---- from("activemq:topic:OrdersTopic") - .filter().expression(BeanLanguage(MyBean.class, "isGoldCustomer")) + .filter().method(MyBean.class, "isGoldCustomer") .to("activemq:BigSpendersQueue"); ---- -The 2nd parameter *`isGoldCustomer`* is an optional parameter to -explicit set the method name to invoke. If not provided Camel will try -to invoke the most suitable method. If case of ambiguity Camel will -thrown an Exception. In these situations the 2nd parameter can solve -this problem. Also the code is more readable if the method name is -provided. The 1st parameter can also be an existing instance of a Bean -such as: +The 1st parameter can also be an existing instance of a Bean such as: -[source,syntaxhighlighter-pre] +[source,java] ---- -private MyBean my; - -from("activemq:topic:OrdersTopic") - .filter().expression(BeanLanguage.bean(my, "isGoldCustomer")) - .to("activemq:BigSpendersQueue"); ----- - -In *Camel 2.2*: you can avoid the *`BeanLanguage`* and have it just as: - -[source,syntaxhighlighter-pre] ----- -private MyBean my; - -from("activemq:topic:OrdersTopic") - .filter().expression(bean(my, "isGoldCustomer")) - .to("activemq:BigSpendersQueue"); ----- - -Which also can be done in a bit shorter and nice way: - -[source,syntaxhighlighter-pre] ----- -private MyBean my; +private MyBean my = ...; from("activemq:topic:OrdersTopic") .filter().method(my, "isGoldCustomer") .to("activemq:BigSpendersQueue"); ---- -[[BeanLanguage-OtherExamples]] -== Other Examples - -We have some test cases you can look at if it'll help - -* https://github.com/apache/camel/blob/main/core/camel-core/src/test/java/org/apache/camel/processor/MethodFilterTest.java[MethodFilterTest] -is a JUnit test case showing the Java xref:manual::dsl.adoc[DSL] use of the bean -expression being used in a filter -* https://github.com/apache/camel/blob/main/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aggregator.xml[aggregator.xml] -is a Spring XML test case for the xref:{eip-vc}:eips:aggregate-eip.adoc[Aggregator] which -uses a bean method call to test for the completion of the aggregation. - include::{page-component-version}@camel-spring-boot::page$bean-starter.adoc[] diff --git a/docs/components/modules/languages/nav.adoc b/docs/components/modules/languages/nav.adoc index 9b43525..90c163d 100644 --- a/docs/components/modules/languages/nav.adoc +++ b/docs/components/modules/languages/nav.adoc @@ -2,7 +2,7 @@ // make edits in docs/*nav.adoc.template files instead * xref:languages:index.adoc[Languages] -** xref:bean-language.adoc[Bean method] +** xref:bean-language.adoc[Bean Method] ** xref:constant-language.adoc[Constant] ** xref:csimple-language.adoc[CSimple] ** xref:datasonnet-language.adoc[DataSonnet]