-added new files for integrations patterns with apache camel article (#887)

This commit is contained in:
Ante Pocedulic 2016-12-11 19:41:05 +01:00 committed by Grzegorz Piwowarek
parent 2dc0cf6c36
commit d727a32434
16 changed files with 423 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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!");
}
});
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="contentBasedFileRouter" class="org.baeldung.camel.file.ContentBasedFileRouter" />
<camelContext xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="contentBasedFileRouter" />
</camelContext>
</beans>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="deadLetterChannelFileRouter" class="org.baeldung.camel.file.DeadLetterChannelFileRouter" />
<camelContext xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="deadLetterChannelFileRouter" />
</camelContext>
</beans>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="messageTranslatorFileRouter" class="org.baeldung.camel.file.MessageTranslatorFileRouter" />
<camelContext xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="messageTranslatorFileRouter" />
</camelContext>
</beans>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="multicastFileRouter" class="org.baeldung.camel.file.MulticastFileRouter" />
<camelContext xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="multicastFileRouter" />
</camelContext>
</beans>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="splitterFileRouter" class="org.baeldung.camel.file.SplitterFileRouter" />
<camelContext xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="splitterFileRouter" />
</camelContext>
</beans>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value="WARN" />
<appender-ref ref="console" />
</root>
</log4j:configuration>

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}