From a411fd056157c36af1ab470ab8300b7d7d7d95da Mon Sep 17 00:00:00 2001 From: michaelin007 Date: Mon, 25 Mar 2024 12:28:05 +0000 Subject: [PATCH] https://jira.baeldung.com/browse/BAEL-7667 --- apache-libraries-2/data/inbox/welcome.txt | 1 + apache-libraries-2/data/json/name.json | 4 +++ apache-libraries-2/data/outbox/welcome.txt | 1 + apache-libraries-2/data/output/name.json | 1 + apache-libraries-2/pom.xml | 7 +++-- .../CamelLoggingMainApp.java | 13 +++++++++ .../FileCopierCamelRoute.java | 27 +++++++++++++++++++ .../FileCopierTracerCamelRoute.java | 23 ++++++++++++++++ .../apachecamellogging/FileProcessor.java | 23 ++++++++++++++++ .../DynamicRouterRouteUnitTest.java | 2 +- 10 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 apache-libraries-2/data/inbox/welcome.txt create mode 100644 apache-libraries-2/data/json/name.json create mode 100644 apache-libraries-2/data/outbox/welcome.txt create mode 100644 apache-libraries-2/data/output/name.json create mode 100644 apache-libraries-2/src/main/java/com/baeldung/apachecamellogging/CamelLoggingMainApp.java create mode 100644 apache-libraries-2/src/main/java/com/baeldung/apachecamellogging/FileCopierCamelRoute.java create mode 100644 apache-libraries-2/src/main/java/com/baeldung/apachecamellogging/FileCopierTracerCamelRoute.java create mode 100644 apache-libraries-2/src/main/java/com/baeldung/apachecamellogging/FileProcessor.java rename apache-libraries-2/src/test/java/{ => com/baeldung}/dynamicrouter/DynamicRouterRouteUnitTest.java (98%) diff --git a/apache-libraries-2/data/inbox/welcome.txt b/apache-libraries-2/data/inbox/welcome.txt new file mode 100644 index 0000000000..9f55d12685 --- /dev/null +++ b/apache-libraries-2/data/inbox/welcome.txt @@ -0,0 +1 @@ +Welcome to Baeldung \ No newline at end of file diff --git a/apache-libraries-2/data/json/name.json b/apache-libraries-2/data/json/name.json new file mode 100644 index 0000000000..1f7e064a56 --- /dev/null +++ b/apache-libraries-2/data/json/name.json @@ -0,0 +1,4 @@ +{ + "name" : "phillip", + "age" : 5 +} \ No newline at end of file diff --git a/apache-libraries-2/data/outbox/welcome.txt b/apache-libraries-2/data/outbox/welcome.txt new file mode 100644 index 0000000000..c4c82085cb --- /dev/null +++ b/apache-libraries-2/data/outbox/welcome.txt @@ -0,0 +1 @@ +WELCOME TO BAELDUNG \ No newline at end of file diff --git a/apache-libraries-2/data/output/name.json b/apache-libraries-2/data/output/name.json new file mode 100644 index 0000000000..d075754d1b --- /dev/null +++ b/apache-libraries-2/data/output/name.json @@ -0,0 +1 @@ +{"name":"phillip","age":5,"transformedName":"PHILLIP","transformedAge":15} \ No newline at end of file diff --git a/apache-libraries-2/pom.xml b/apache-libraries-2/pom.xml index 2e7ef0344c..7ad70d8978 100644 --- a/apache-libraries-2/pom.xml +++ b/apache-libraries-2/pom.xml @@ -24,19 +24,22 @@ camel-core ${camel.version} - org.apache.camel camel-test-junit5 ${camel.version} test - org.apache.camel camel-main ${camel.version} + + org.apache.camel + camel-jackson + ${camel.version} + diff --git a/apache-libraries-2/src/main/java/com/baeldung/apachecamellogging/CamelLoggingMainApp.java b/apache-libraries-2/src/main/java/com/baeldung/apachecamellogging/CamelLoggingMainApp.java new file mode 100644 index 0000000000..c4b9b998ab --- /dev/null +++ b/apache-libraries-2/src/main/java/com/baeldung/apachecamellogging/CamelLoggingMainApp.java @@ -0,0 +1,13 @@ +package com.baeldung.apachecamellogging; + +import org.apache.camel.main.Main; + +public class CamelLoggingMainApp { + + public static void main(String[] args) throws Exception { + Main main = new Main(); + main.configure() + .addRoutesBuilder(new FileCopierTracerCamelRoute()); + main.run(args); + } +} diff --git a/apache-libraries-2/src/main/java/com/baeldung/apachecamellogging/FileCopierCamelRoute.java b/apache-libraries-2/src/main/java/com/baeldung/apachecamellogging/FileCopierCamelRoute.java new file mode 100644 index 0000000000..c8b52b4e6f --- /dev/null +++ b/apache-libraries-2/src/main/java/com/baeldung/apachecamellogging/FileCopierCamelRoute.java @@ -0,0 +1,27 @@ +package com.baeldung.apachecamellogging; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FileCopierCamelRoute extends RouteBuilder { + + private static Logger logger = LoggerFactory.getLogger(FileCopierCamelRoute.class); + + public void configure() { + from("file:data/inbox?noop=true").log("We got an incoming file ${file:name} containing: ${body}") + .to("log:com.baeldung.apachecamellogging?level=INFO") + .process(new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + logger.info("We are passing the message to a FileProcesor to Capitalize the message body"); + } + }) + .bean(FileProcessor.class) + .to("file:data/outbox") + .log("Successlly transfer file: ${file:name}"); + } +} \ No newline at end of file diff --git a/apache-libraries-2/src/main/java/com/baeldung/apachecamellogging/FileCopierTracerCamelRoute.java b/apache-libraries-2/src/main/java/com/baeldung/apachecamellogging/FileCopierTracerCamelRoute.java new file mode 100644 index 0000000000..666feac7ec --- /dev/null +++ b/apache-libraries-2/src/main/java/com/baeldung/apachecamellogging/FileCopierTracerCamelRoute.java @@ -0,0 +1,23 @@ +package com.baeldung.apachecamellogging; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.dataformat.JsonLibrary; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FileCopierTracerCamelRoute extends RouteBuilder { + + Logger logger = LoggerFactory.getLogger(FileCopierTracerCamelRoute.class); + + public void configure() { + getContext().setTracing(true); + from("file:data/json?noop=true").to("log:input?level=INFO") + .unmarshal() + .json(JsonLibrary.Jackson) + .bean(FileProcessor.class, "transform") + .marshal() + .json(JsonLibrary.Jackson) + .to("file:data/output"); + ; + } +} \ No newline at end of file diff --git a/apache-libraries-2/src/main/java/com/baeldung/apachecamellogging/FileProcessor.java b/apache-libraries-2/src/main/java/com/baeldung/apachecamellogging/FileProcessor.java new file mode 100644 index 0000000000..478fb0ced0 --- /dev/null +++ b/apache-libraries-2/src/main/java/com/baeldung/apachecamellogging/FileProcessor.java @@ -0,0 +1,23 @@ +package com.baeldung.apachecamellogging; + +import org.apache.camel.Body; + +import java.util.Map; + +public class FileProcessor { + + public String process(@Body String fileContent) { + String processedContent = fileContent.toUpperCase(); + return processedContent; + } + + public Map transform(Map input) { + String name = (String) input.get("name"); + int age = (int) input.get("age"); + + input.put("transformedName", name.toUpperCase()); + input.put("transformedAge", age + 10); + + return input; + } +} diff --git a/apache-libraries-2/src/test/java/dynamicrouter/DynamicRouterRouteUnitTest.java b/apache-libraries-2/src/test/java/com/baeldung/dynamicrouter/DynamicRouterRouteUnitTest.java similarity index 98% rename from apache-libraries-2/src/test/java/dynamicrouter/DynamicRouterRouteUnitTest.java rename to apache-libraries-2/src/test/java/com/baeldung/dynamicrouter/DynamicRouterRouteUnitTest.java index 6401fa4be2..4b93d40dac 100644 --- a/apache-libraries-2/src/test/java/dynamicrouter/DynamicRouterRouteUnitTest.java +++ b/apache-libraries-2/src/test/java/com/baeldung/dynamicrouter/DynamicRouterRouteUnitTest.java @@ -1,4 +1,4 @@ -package dynamicrouter; +package com.baeldung.dynamicrouter; import com.baeldung.dynamicrouter.DynamicRouterRoute; import org.apache.camel.RoutesBuilder;