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 08cac8f CAMEL-16630: camel-core - Allow to use groovy script for
aggregation strategy pojo.
08cac8f is described below
commit 08cac8f92fa3aed31ac0a18b34aafec10e34cb0c
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Jun 29 14:42:13 2021 +0200
CAMEL-16630: camel-core - Allow to use groovy script for aggregation
strategy pojo.
---
.../kamelet/KameletEipAggregateGroovyTest.java | 73 ++++++++++++++++++++++
.../aggregate/AggregationStrategyBeanAdapter.java | 8 +++
2 files changed, 81 insertions(+)
diff --git
a/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletEipAggregateGroovyTest.java
b/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletEipAggregateGroovyTest.java
new file mode 100644
index 0000000..6e222b1
--- /dev/null
+++
b/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletEipAggregateGroovyTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.kamelet;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.AggregationStrategies;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+public class KameletEipAggregateGroovyTest extends CamelTestSupport {
+
+ @Test
+ public void testAggregate() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("A,B,C,D,E");
+
+ template.sendBody("direct:start", "A");
+ template.sendBody("direct:start", "B");
+ template.sendBody("direct:start", "C");
+ template.sendBody("direct:start", "D");
+ template.sendBody("direct:start", "E");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ // **********************************************
+ //
+ // test set-up
+ //
+ // **********************************************
+
+ protected RoutesBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ routeTemplate("my-aggregate")
+ .templateBean("myAgg", "groovy",
+ // for aggregation we need a class that has the
method with how to aggregate the messages
+ // the logic can of course be much more than just
to append with comma
+ "class MyAgg { String agg(b1, b2) { b1 + ',' +
b2 } }; new MyAgg()")
+ // the groovy is evaluated as a script so must
return an instance of the class
+ .templateParameter("count")
+ .from("kamelet:source")
+ .aggregate(constant(true))
+ .completionSize("{{count}}")
+ // use the groovy script bean for aggregation
+ .aggregationStrategyRef("{{myAgg}}")
+ .to("log:aggregate")
+ .to("kamelet:sink")
+ .end();
+
+ from("direct:start")
+ .kamelet("my-aggregate?count=5")
+ .to("log:info")
+ .to("mock:result");
+ }
+ };
+ }
+}
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/aggregate/AggregationStrategyBeanAdapter.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/aggregate/AggregationStrategyBeanAdapter.java
index b0fd36f..2b314b4 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/aggregate/AggregationStrategyBeanAdapter.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/aggregate/AggregationStrategyBeanAdapter.java
@@ -177,6 +177,14 @@ public final class AggregationStrategyBeanAdapter extends
ServiceSupport impleme
return false;
}
+ // must not be groovy meta class
+ if (method.getName().equals("getMetaClass") ||
method.getName().equals("setMetaClass")) {
+ return false;
+ }
+ if (method.getDeclaringClass().getName().startsWith("groovy.lang")) {
+ return false;
+ }
+
// return type must not be void and it should not be a bridge method
if (method.getReturnType().equals(Void.TYPE) || method.isBridge()) {
return false;