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 2019/11/21 11:11:41 UTC
[camel-quarkus] 01/02: Fix #416 Use @Inject in the timer-log-cdi
example
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
commit bb206481775d825f64baa6f7f47c1fd8b2195c57
Author: Peter Palaga <pp...@redhat.com>
AuthorDate: Wed Nov 20 17:14:57 2019 +0100
Fix #416 Use @Inject in the timer-log-cdi example
---
.../acme/timer/{TimerRoute.java => Counter.java} | 22 ++++++++++++----------
.../src/main/java/org/acme/timer/TimerRoute.java | 15 ++++++++++++++-
2 files changed, 26 insertions(+), 11 deletions(-)
diff --git a/examples/timer-log-cdi/src/main/java/org/acme/timer/TimerRoute.java b/examples/timer-log-cdi/src/main/java/org/acme/timer/Counter.java
similarity index 70%
copy from examples/timer-log-cdi/src/main/java/org/acme/timer/TimerRoute.java
copy to examples/timer-log-cdi/src/main/java/org/acme/timer/Counter.java
index 5a1dc36..67b7739 100644
--- a/examples/timer-log-cdi/src/main/java/org/acme/timer/TimerRoute.java
+++ b/examples/timer-log-cdi/src/main/java/org/acme/timer/Counter.java
@@ -16,20 +16,22 @@
*/
package org.acme.timer;
-import javax.enterprise.context.ApplicationScoped;
+import java.util.concurrent.atomic.AtomicInteger;
-import org.apache.camel.builder.RouteBuilder;
-import org.eclipse.microprofile.config.inject.ConfigProperty;
+import javax.enterprise.context.ApplicationScoped;
+/**
+ * A counter bean.
+ */
@ApplicationScoped
-public class TimerRoute extends RouteBuilder {
+public class Counter {
+ private final AtomicInteger value = new AtomicInteger(0);
- @ConfigProperty(name = "timer.period", defaultValue = "1s")
- String period;
+ public int increment() {
+ return value.incrementAndGet();
+ }
- @Override
- public void configure() throws Exception {
- fromF("timer:foo?period=%s", period)
- .log("Hello World");
+ public int getValue() {
+ return value.get();
}
}
diff --git a/examples/timer-log-cdi/src/main/java/org/acme/timer/TimerRoute.java b/examples/timer-log-cdi/src/main/java/org/acme/timer/TimerRoute.java
index 5a1dc36..d9fb5d7 100644
--- a/examples/timer-log-cdi/src/main/java/org/acme/timer/TimerRoute.java
+++ b/examples/timer-log-cdi/src/main/java/org/acme/timer/TimerRoute.java
@@ -17,19 +17,32 @@
package org.acme.timer;
import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
import org.apache.camel.builder.RouteBuilder;
import org.eclipse.microprofile.config.inject.ConfigProperty;
+/**
+ * A {@link RouteBuilder} demonstrating the use of CDI (Contexts and Dependency Injection).
+ * <p>
+ * Note that for the {@code @Inject} and {@code @ConfigProperty} annotations to work, this class has to be annotated
+ * with {@code @ApplicationScoped}.
+ */
@ApplicationScoped
public class TimerRoute extends RouteBuilder {
+ /** {@code timer.period} is defined in {@code src/main/resources/application.properties} */
@ConfigProperty(name = "timer.period", defaultValue = "1s")
String period;
+ /** An injected bean */
+ @Inject
+ Counter counter;
+
@Override
public void configure() throws Exception {
fromF("timer:foo?period=%s", period)
- .log("Hello World");
+ .setBody(() -> "Incremented the counter: " + counter.increment())
+ .to("log:cdi-example?showExchangePattern=false&showBodyType=false");
}
}