You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2020/07/08 06:17:26 UTC

[camel-quarkus] branch master updated: Add example about how to use @Handler with beans registered to the Camel Context

This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/master by this push:
     new ab24056  Add example about how to use @Handler with beans registered to the Camel Context
ab24056 is described below

commit ab24056238f15d687a891e74ad6c08cfdf317f5e
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Tue Jul 7 15:49:06 2020 +0200

    Add example about how to use @Handler with beans registered to the Camel Context
---
 .../quarkus/component/bean/CamelResource.java      |  8 +++++
 .../camel/quarkus/component/bean/CamelRoute.java   | 11 ++++++
 .../quarkus/component/bean/WithHandlerBean.java    | 40 ++++++++++++++++++++++
 .../camel/quarkus/component/bean/BeanTest.java     | 10 ++++++
 4 files changed, 69 insertions(+)

diff --git a/integration-tests/bean/src/main/java/org/apache/camel/quarkus/component/bean/CamelResource.java b/integration-tests/bean/src/main/java/org/apache/camel/quarkus/component/bean/CamelResource.java
index 666a5a0..7c52d6d 100644
--- a/integration-tests/bean/src/main/java/org/apache/camel/quarkus/component/bean/CamelResource.java
+++ b/integration-tests/bean/src/main/java/org/apache/camel/quarkus/component/bean/CamelResource.java
@@ -63,6 +63,14 @@ public class CamelResource {
         return template.requestBody("direct:method", statement, String.class);
     }
 
+    @Path("/handler")
+    @POST
+    @Consumes(MediaType.TEXT_PLAIN)
+    @Produces(MediaType.TEXT_PLAIN)
+    public String handler(String statement) {
+        return template.requestBody("direct:handler", statement, String.class);
+    }
+
     @Path("/increment")
     @GET
     @Produces(MediaType.TEXT_PLAIN)
diff --git a/integration-tests/bean/src/main/java/org/apache/camel/quarkus/component/bean/CamelRoute.java b/integration-tests/bean/src/main/java/org/apache/camel/quarkus/component/bean/CamelRoute.java
index 54787a0..acef305 100644
--- a/integration-tests/bean/src/main/java/org/apache/camel/quarkus/component/bean/CamelRoute.java
+++ b/integration-tests/bean/src/main/java/org/apache/camel/quarkus/component/bean/CamelRoute.java
@@ -26,6 +26,7 @@ import java.util.concurrent.atomic.AtomicInteger;
 import io.quarkus.runtime.annotations.RegisterForReflection;
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.AsyncProcessor;
+import org.apache.camel.BindToRegistry;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
@@ -71,6 +72,10 @@ public class CamelRoute extends RouteBuilder {
                 .bean(MyBean.class, "sayHello")
                 .to("log:named");
 
+        from("direct:handler")
+                .to("bean:withHandler")
+                .to("log:named");
+
     }
 
     @SuppressWarnings("unchecked")
@@ -158,4 +163,10 @@ public class CamelRoute extends RouteBuilder {
             return "Hello " + body + " from the MyBean";
         }
     }
+
+    @BindToRegistry
+    public static WithHandlerBean withHandler() {
+        return new WithHandlerBean();
+    }
+
 }
diff --git a/integration-tests/bean/src/main/java/org/apache/camel/quarkus/component/bean/WithHandlerBean.java b/integration-tests/bean/src/main/java/org/apache/camel/quarkus/component/bean/WithHandlerBean.java
new file mode 100644
index 0000000..806617c
--- /dev/null
+++ b/integration-tests/bean/src/main/java/org/apache/camel/quarkus/component/bean/WithHandlerBean.java
@@ -0,0 +1,40 @@
+/*
+ * 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.quarkus.component.bean;
+
+import io.quarkus.runtime.annotations.RegisterForReflection;
+import org.apache.camel.Exchange;
+import org.apache.camel.Handler;
+
+@RegisterForReflection
+public class WithHandlerBean {
+    /**
+     * Just set an hello message.
+     */
+    @Handler
+    public void sayHello1(Exchange exchange) {
+        String body = exchange.getMessage().getBody(String.class);
+        exchange.getMessage().setBody("Hello " + body + " from the WithHandlerBean");
+    }
+
+    /**
+     * Just set an hello message.
+     */
+    public void sayHello2(Exchange exchange) {
+        throw new IllegalStateException("This method should not be invoked");
+    }
+}
diff --git a/integration-tests/bean/src/test/java/org/apache/camel/quarkus/component/bean/BeanTest.java b/integration-tests/bean/src/test/java/org/apache/camel/quarkus/component/bean/BeanTest.java
index 9d06b26..42a50ae 100644
--- a/integration-tests/bean/src/test/java/org/apache/camel/quarkus/component/bean/BeanTest.java
+++ b/integration-tests/bean/src/test/java/org/apache/camel/quarkus/component/bean/BeanTest.java
@@ -59,6 +59,16 @@ public class BeanTest {
     }
 
     @Test
+    public void handler() {
+        RestAssured.given()
+                .contentType(ContentType.TEXT)
+                .body("Kermit")
+                .post("/bean/handler")
+                .then()
+                .body(equalTo("Hello Kermit from the WithHandlerBean"));
+    }
+
+    @Test
     public void inject() {
 
         /* Ensure that @Inject works */