From d727a324344f9d066a048fbcb6d5fd6a4945b684 Mon Sep 17 00:00:00 2001 From: Ante Pocedulic Date: Sun, 11 Dec 2016 19:41:05 +0100 Subject: [PATCH] -added new files for integrations patterns with apache camel article (#887) --- .../camel/file/ContentBasedFileRouter.java | 16 ++++++ .../file/DeadLetterChannelFileRouter.java | 22 ++++++++ .../file/MessageTranslatorFileRouter.java | 14 +++++ .../camel/file/MulticastFileRouter.java | 18 ++++++ .../camel/file/SplitterFileRouter.java | 15 +++++ ...mel-context-ContentBasedFileRouterTest.xml | 13 +++++ ...el-context-DeadLetterChannelFileRouter.xml | 13 +++++ ...ontext-MessageTranslatorFileRouterTest.xml | 13 +++++ .../camel-context-MulticastFileRouterTest.xml | 13 +++++ .../camel-context-SplitterFileRouter.xml | 13 +++++ .../src/main/resources/log4j.xml | 18 ++++++ ...ContentBasedFileRouterIntegrationTest.java | 56 +++++++++++++++++++ ...etterChannelFileRouterIntegrationTest.java | 44 +++++++++++++++ ...geTranslatorFileRouterIntegrationTest.java | 50 +++++++++++++++++ .../MulticastFileRouterIntegrationTest.java | 53 ++++++++++++++++++ .../SplitterFileRouterIntegrationTest.java | 52 +++++++++++++++++ 16 files changed, 423 insertions(+) create mode 100644 spring-apache-camel/src/main/java/com/baeldung/camel/file/ContentBasedFileRouter.java create mode 100644 spring-apache-camel/src/main/java/com/baeldung/camel/file/DeadLetterChannelFileRouter.java create mode 100644 spring-apache-camel/src/main/java/com/baeldung/camel/file/MessageTranslatorFileRouter.java create mode 100644 spring-apache-camel/src/main/java/com/baeldung/camel/file/MulticastFileRouter.java create mode 100644 spring-apache-camel/src/main/java/com/baeldung/camel/file/SplitterFileRouter.java create mode 100644 spring-apache-camel/src/main/resources/camel-context-ContentBasedFileRouterTest.xml create mode 100644 spring-apache-camel/src/main/resources/camel-context-DeadLetterChannelFileRouter.xml create mode 100644 spring-apache-camel/src/main/resources/camel-context-MessageTranslatorFileRouterTest.xml create mode 100644 spring-apache-camel/src/main/resources/camel-context-MulticastFileRouterTest.xml create mode 100644 spring-apache-camel/src/main/resources/camel-context-SplitterFileRouter.xml create mode 100644 spring-apache-camel/src/main/resources/log4j.xml create mode 100644 spring-apache-camel/src/test/java/org/apache/camel/file/processor/ContentBasedFileRouterIntegrationTest.java create mode 100644 spring-apache-camel/src/test/java/org/apache/camel/file/processor/DeadLetterChannelFileRouterIntegrationTest.java create mode 100644 spring-apache-camel/src/test/java/org/apache/camel/file/processor/MessageTranslatorFileRouterIntegrationTest.java create mode 100644 spring-apache-camel/src/test/java/org/apache/camel/file/processor/MulticastFileRouterIntegrationTest.java create mode 100644 spring-apache-camel/src/test/java/org/apache/camel/file/processor/SplitterFileRouterIntegrationTest.java diff --git a/spring-apache-camel/src/main/java/com/baeldung/camel/file/ContentBasedFileRouter.java b/spring-apache-camel/src/main/java/com/baeldung/camel/file/ContentBasedFileRouter.java new file mode 100644 index 0000000000..9106e996c3 --- /dev/null +++ b/spring-apache-camel/src/main/java/com/baeldung/camel/file/ContentBasedFileRouter.java @@ -0,0 +1,16 @@ +package com.baeldung.camel.file; + +import org.apache.camel.builder.RouteBuilder; + +public class ContentBasedFileRouter extends RouteBuilder { + + private static final String SOURCE_FOLDER = "src/test/source-folder"; + private static final String DESTINATION_FOLDER_TXT = "src/test/destination-folder-txt"; + private static final String DESTINATION_FOLDER_OTHER = "src/test/destination-folder-other"; + + @Override + public void configure() throws Exception { + from("file://" + SOURCE_FOLDER + "?delete=true").choice().when(simple("${file:ext} == 'txt'")).to("file://" + DESTINATION_FOLDER_TXT).otherwise().to("file://" + DESTINATION_FOLDER_OTHER); + } + +} diff --git a/spring-apache-camel/src/main/java/com/baeldung/camel/file/DeadLetterChannelFileRouter.java b/spring-apache-camel/src/main/java/com/baeldung/camel/file/DeadLetterChannelFileRouter.java new file mode 100644 index 0000000000..0388560175 --- /dev/null +++ b/spring-apache-camel/src/main/java/com/baeldung/camel/file/DeadLetterChannelFileRouter.java @@ -0,0 +1,22 @@ +package com.baeldung.camel.file; + +import org.apache.camel.Exchange; +import org.apache.camel.LoggingLevel; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; + +public class DeadLetterChannelFileRouter extends RouteBuilder { + private static final String SOURCE_FOLDER = "src/test/source-folder"; + + @Override + public void configure() throws Exception { + errorHandler(deadLetterChannel("log:dead?level=ERROR").maximumRedeliveries(3).redeliveryDelay(1000).retryAttemptedLogLevel(LoggingLevel.ERROR)); + + from("file://" + SOURCE_FOLDER + "?delete=true").process(new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + throw new IllegalArgumentException("Exception thrown!"); + } + }); + } +} diff --git a/spring-apache-camel/src/main/java/com/baeldung/camel/file/MessageTranslatorFileRouter.java b/spring-apache-camel/src/main/java/com/baeldung/camel/file/MessageTranslatorFileRouter.java new file mode 100644 index 0000000000..b99de99dac --- /dev/null +++ b/spring-apache-camel/src/main/java/com/baeldung/camel/file/MessageTranslatorFileRouter.java @@ -0,0 +1,14 @@ +package com.baeldung.camel.file; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; + +public class MessageTranslatorFileRouter extends RouteBuilder { + private static final String SOURCE_FOLDER = "src/test/source-folder"; + private static final String DESTINATION_FOLDER = "src/test/destination-folder"; + + @Override + public void configure() throws Exception { + from("file://" + SOURCE_FOLDER + "?delete=true").transform(body().append(header(Exchange.FILE_NAME))).to("file://" + DESTINATION_FOLDER); + } +} diff --git a/spring-apache-camel/src/main/java/com/baeldung/camel/file/MulticastFileRouter.java b/spring-apache-camel/src/main/java/com/baeldung/camel/file/MulticastFileRouter.java new file mode 100644 index 0000000000..75a6e81d45 --- /dev/null +++ b/spring-apache-camel/src/main/java/com/baeldung/camel/file/MulticastFileRouter.java @@ -0,0 +1,18 @@ +package com.baeldung.camel.file; + +import org.apache.camel.builder.RouteBuilder; + +public class MulticastFileRouter extends RouteBuilder { + private static final String SOURCE_FOLDER = "src/test/source-folder"; + private static final String DESTINATION_FOLDER_WORLD = "src/test/destination-folder-world"; + private static final String DESTINATION_FOLDER_HELLO = "src/test/destination-folder-hello"; + + @Override + public void configure() throws Exception { + from("file://" + SOURCE_FOLDER + "?delete=true").multicast().to("direct:append", "direct:prepend").end(); + + from("direct:append").transform(body().append("World")).to("file://" + DESTINATION_FOLDER_WORLD); + + from("direct:prepend").transform(body().prepend("Hello")).to("file://" + DESTINATION_FOLDER_HELLO); + } +} diff --git a/spring-apache-camel/src/main/java/com/baeldung/camel/file/SplitterFileRouter.java b/spring-apache-camel/src/main/java/com/baeldung/camel/file/SplitterFileRouter.java new file mode 100644 index 0000000000..551f9c9685 --- /dev/null +++ b/spring-apache-camel/src/main/java/com/baeldung/camel/file/SplitterFileRouter.java @@ -0,0 +1,15 @@ +package com.baeldung.camel.file; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; + +public class SplitterFileRouter extends RouteBuilder { + private static final String SOURCE_FOLDER = "src/test/source-folder"; + private static final String DESTINATION_FOLDER = "src/test/destination-folder"; + + @Override + public void configure() throws Exception { + + from("file://" + SOURCE_FOLDER + "?delete=true").split(body().convertToString().tokenize("\n")).setHeader(Exchange.FILE_NAME, body()).to("file://" + DESTINATION_FOLDER); + } +} diff --git a/spring-apache-camel/src/main/resources/camel-context-ContentBasedFileRouterTest.xml b/spring-apache-camel/src/main/resources/camel-context-ContentBasedFileRouterTest.xml new file mode 100644 index 0000000000..f6bf8c7503 --- /dev/null +++ b/spring-apache-camel/src/main/resources/camel-context-ContentBasedFileRouterTest.xml @@ -0,0 +1,13 @@ + + + + + + + + + + \ No newline at end of file diff --git a/spring-apache-camel/src/main/resources/camel-context-DeadLetterChannelFileRouter.xml b/spring-apache-camel/src/main/resources/camel-context-DeadLetterChannelFileRouter.xml new file mode 100644 index 0000000000..3ed80da7e0 --- /dev/null +++ b/spring-apache-camel/src/main/resources/camel-context-DeadLetterChannelFileRouter.xml @@ -0,0 +1,13 @@ + + + + + + + + + + \ No newline at end of file diff --git a/spring-apache-camel/src/main/resources/camel-context-MessageTranslatorFileRouterTest.xml b/spring-apache-camel/src/main/resources/camel-context-MessageTranslatorFileRouterTest.xml new file mode 100644 index 0000000000..d3d2706e22 --- /dev/null +++ b/spring-apache-camel/src/main/resources/camel-context-MessageTranslatorFileRouterTest.xml @@ -0,0 +1,13 @@ + + + + + + + + + + \ No newline at end of file diff --git a/spring-apache-camel/src/main/resources/camel-context-MulticastFileRouterTest.xml b/spring-apache-camel/src/main/resources/camel-context-MulticastFileRouterTest.xml new file mode 100644 index 0000000000..4982b61dbd --- /dev/null +++ b/spring-apache-camel/src/main/resources/camel-context-MulticastFileRouterTest.xml @@ -0,0 +1,13 @@ + + + + + + + + + + \ No newline at end of file diff --git a/spring-apache-camel/src/main/resources/camel-context-SplitterFileRouter.xml b/spring-apache-camel/src/main/resources/camel-context-SplitterFileRouter.xml new file mode 100644 index 0000000000..1f5945707a --- /dev/null +++ b/spring-apache-camel/src/main/resources/camel-context-SplitterFileRouter.xml @@ -0,0 +1,13 @@ + + + + + + + + + + \ No newline at end of file diff --git a/spring-apache-camel/src/main/resources/log4j.xml b/spring-apache-camel/src/main/resources/log4j.xml new file mode 100644 index 0000000000..4a86fb2357 --- /dev/null +++ b/spring-apache-camel/src/main/resources/log4j.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-apache-camel/src/test/java/org/apache/camel/file/processor/ContentBasedFileRouterIntegrationTest.java b/spring-apache-camel/src/test/java/org/apache/camel/file/processor/ContentBasedFileRouterIntegrationTest.java new file mode 100644 index 0000000000..29a5b4fc0f --- /dev/null +++ b/spring-apache-camel/src/test/java/org/apache/camel/file/processor/ContentBasedFileRouterIntegrationTest.java @@ -0,0 +1,56 @@ +package org.apache.camel.file.processor; + +import java.io.File; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +@RunWith(JUnit4.class) +public class ContentBasedFileRouterIntegrationTest { + + private static final long DURATION_MILIS = 10000; + private static final String SOURCE_FOLDER = "src/test/source-folder"; + private static final String DESTINATION_FOLDER_TXT = "src/test/destination-folder-txt"; + private static final String DESTINATION_FOLDER_OTHER = "src/test/destination-folder-other"; + + @Before + public void setUp() throws Exception { + File sourceFolder = new File(SOURCE_FOLDER); + File destinationFolderTxt = new File(DESTINATION_FOLDER_TXT); + File destinationFolderOther = new File(DESTINATION_FOLDER_OTHER); + + cleanFolder(sourceFolder); + cleanFolder(destinationFolderTxt); + cleanFolder(destinationFolderOther); + + sourceFolder.mkdirs(); + File file1 = new File(SOURCE_FOLDER + "/File1.txt"); + File file2 = new File(SOURCE_FOLDER + "/File2.csv"); + file1.createNewFile(); + file2.createNewFile(); + } + + private void cleanFolder(File folder) { + File[] files = folder.listFiles(); + if (files != null) { + for (File file : files) { + if (file.isFile()) { + file.delete(); + } + } + } + } + + @Test + @Ignore + public void routeTest() throws InterruptedException { + ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-ContentBasedFileRouterTest.xml"); + Thread.sleep(DURATION_MILIS); + applicationContext.close(); + + } +} \ No newline at end of file diff --git a/spring-apache-camel/src/test/java/org/apache/camel/file/processor/DeadLetterChannelFileRouterIntegrationTest.java b/spring-apache-camel/src/test/java/org/apache/camel/file/processor/DeadLetterChannelFileRouterIntegrationTest.java new file mode 100644 index 0000000000..49d91aa661 --- /dev/null +++ b/spring-apache-camel/src/test/java/org/apache/camel/file/processor/DeadLetterChannelFileRouterIntegrationTest.java @@ -0,0 +1,44 @@ +package org.apache.camel.file.processor; + +import java.io.File; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class DeadLetterChannelFileRouterIntegrationTest { + + private static final long DURATION_MILIS = 10000; + private static final String SOURCE_FOLDER = "src/test/source-folder"; + + @Before + public void setUp() throws Exception { + File sourceFolder = new File(SOURCE_FOLDER); + + cleanFolder(sourceFolder); + + sourceFolder.mkdirs(); + File file = new File(SOURCE_FOLDER + "/File.txt"); + file.createNewFile(); + } + + private void cleanFolder(File folder) { + File[] files = folder.listFiles(); + if (files != null) { + for (File file : files) { + if (file.isFile()) { + file.delete(); + } + } + } + } + + @Test + public void routeTest() throws InterruptedException { + ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-DeadLetterChannelFileRouter.xml"); + Thread.sleep(DURATION_MILIS); + applicationContext.close(); + + } +} diff --git a/spring-apache-camel/src/test/java/org/apache/camel/file/processor/MessageTranslatorFileRouterIntegrationTest.java b/spring-apache-camel/src/test/java/org/apache/camel/file/processor/MessageTranslatorFileRouterIntegrationTest.java new file mode 100644 index 0000000000..d9d889c4df --- /dev/null +++ b/spring-apache-camel/src/test/java/org/apache/camel/file/processor/MessageTranslatorFileRouterIntegrationTest.java @@ -0,0 +1,50 @@ +package org.apache.camel.file.processor; + +import java.io.File; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class MessageTranslatorFileRouterIntegrationTest { + + private static final long DURATION_MILIS = 10000; + private static final String SOURCE_FOLDER = "src/test/source-folder"; + private static final String DESTINATION_FOLDER = "src/test/destination-folder"; + + @Before + public void setUp() throws Exception { + File sourceFolder = new File(SOURCE_FOLDER); + File destinationFolder = new File(DESTINATION_FOLDER); + + cleanFolder(sourceFolder); + cleanFolder(destinationFolder); + + sourceFolder.mkdirs(); + File file1 = new File(SOURCE_FOLDER + "/File1.txt"); + File file2 = new File(SOURCE_FOLDER + "/File2.txt"); + file1.createNewFile(); + file2.createNewFile(); + } + + private void cleanFolder(File folder) { + File[] files = folder.listFiles(); + if (files != null) { + for (File file : files) { + if (file.isFile()) { + file.delete(); + } + } + } + } + + @Test + @Ignore + public void routeTest() throws InterruptedException { + ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-MessageTranslatorFileRouterTest.xml"); + Thread.sleep(DURATION_MILIS); + applicationContext.close(); + + } +} diff --git a/spring-apache-camel/src/test/java/org/apache/camel/file/processor/MulticastFileRouterIntegrationTest.java b/spring-apache-camel/src/test/java/org/apache/camel/file/processor/MulticastFileRouterIntegrationTest.java new file mode 100644 index 0000000000..960d310692 --- /dev/null +++ b/spring-apache-camel/src/test/java/org/apache/camel/file/processor/MulticastFileRouterIntegrationTest.java @@ -0,0 +1,53 @@ +package org.apache.camel.file.processor; + +import java.io.File; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class MulticastFileRouterIntegrationTest { + + private static final long DURATION_MILIS = 10000; + private static final String SOURCE_FOLDER = "src/test/source-folder"; + private static final String DESTINATION_FOLDER_WORLD = "src/test/destination-folder-world"; + private static final String DESTINATION_FOLDER_HELLO = "src/test/destination-folder-hello"; + + @Before + public void setUp() throws Exception { + File sourceFolder = new File(SOURCE_FOLDER); + File destinationFolderWorld = new File(DESTINATION_FOLDER_WORLD); + File destinationFolderHello = new File(DESTINATION_FOLDER_HELLO); + + cleanFolder(sourceFolder); + cleanFolder(destinationFolderWorld); + cleanFolder(destinationFolderHello); + + sourceFolder.mkdirs(); + File file1 = new File(SOURCE_FOLDER + "/File1.txt"); + File file2 = new File(SOURCE_FOLDER + "/File2.txt"); + file1.createNewFile(); + file2.createNewFile(); + } + + private void cleanFolder(File folder) { + File[] files = folder.listFiles(); + if (files != null) { + for (File file : files) { + if (file.isFile()) { + file.delete(); + } + } + } + } + + @Test + @Ignore + public void routeTest() throws InterruptedException { + ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-MulticastFileRouterTest.xml"); + Thread.sleep(DURATION_MILIS); + applicationContext.close(); + + } +} diff --git a/spring-apache-camel/src/test/java/org/apache/camel/file/processor/SplitterFileRouterIntegrationTest.java b/spring-apache-camel/src/test/java/org/apache/camel/file/processor/SplitterFileRouterIntegrationTest.java new file mode 100644 index 0000000000..9e942dd4c2 --- /dev/null +++ b/spring-apache-camel/src/test/java/org/apache/camel/file/processor/SplitterFileRouterIntegrationTest.java @@ -0,0 +1,52 @@ +package org.apache.camel.file.processor; + +import java.io.File; +import java.io.FileWriter; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class SplitterFileRouterIntegrationTest { + + private static final long DURATION_MILIS = 10000; + private static final String SOURCE_FOLDER = "src/test/source-folder"; + private static final String DESTINATION_FOLDER = "src/test/destination-folder"; + + @Before + public void setUp() throws Exception { + File sourceFolder = new File(SOURCE_FOLDER); + File destinationFolder = new File(DESTINATION_FOLDER); + + cleanFolder(sourceFolder); + cleanFolder(destinationFolder); + + sourceFolder.mkdirs(); + File file = new File(SOURCE_FOLDER + "/File.txt"); + FileWriter fileWriter = new FileWriter(file, false); + fileWriter.write("Hello\nWorld"); + file.createNewFile(); + fileWriter.close(); + } + + private void cleanFolder(File folder) { + File[] files = folder.listFiles(); + if (files != null) { + for (File file : files) { + if (file.isFile()) { + file.delete(); + } + } + } + } + + @Test + @Ignore + public void routeTests() throws InterruptedException { + ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-SplitterFileRouter.xml"); + Thread.sleep(DURATION_MILIS); + applicationContext.close(); + + } +}