Apache camel is an open-source project under the Apache Foundation. It is a rule-based routing and processing engine that provides Java object implementation in the enterprise integration mode, configure routing and processing rules through application interfaces or declarative Java domain-specific language (DSL. Its core idea is to get data from a from source, process it through processor, and then send it to a to-goal.
This from and to can be the types we often encounter during project integration: files in an FTP folder, MQ queue, HTTP Request/response, and WebService.
Camel can be easily integrated into standalone applications, web applications running in containers, and integration with spring.
The following example shows how to develop a simple camel application.
1. Download The jarpackage from http://camel.apache.org/download.html. The latest version is 2.9. This article uses 2.7. jre1.6 is required from 2.7.
The downloaded zip contains the jar packages required for various features of camel.
In this example, the use of the jar package only need: camel-core-2.7.5.jar, commons-management-1.0.jar, slf4j-api-1.6.1.jar.
2. Create an Eclipse project and set the jar package listed above to the classpath of the project.
Create the following class: after running, move all the files under D:/temp/inbox/to D:/temp/Outbox.
public class FileMoveWithCamel { public static void main(String args[]) throws Exception { CamelContext context = new DefaultCamelContext(); context.addRoutes(new RouteBuilder() { public void configure() { //from("file:d:/temp/inbox?noop=true").to("file:d:/temp/outbox"); from("file:d:/temp/inbox/?delay=30000").to("file:d:/temp/outbox"); } }); context.start(); boolean loop =true; while(loop){ Thread.sleep(25000); } context.stop(); }}
The preceding example shows a simple routing function. For example, D:/temp/inbox/is a receiving directory from a system FTP to the system where camel is located.
D:/temp/outbox is the receiving directory of another system that camel wants to send.
From/to can be in the following form. Can you see that camel can be used for routing in System Integration? Does the process control have a very good framework?
From ("file: D:/temp/inbox /? Delay = 30000 "). To (" JMS: queue: Order "); // delay = 30000 indicates whether a file exists in the folder every 30 seconds.
3. An example of process processing from to is given:
public class FileProcessWithCamel { public static void main(String args[]) throws Exception { CamelContext context = new DefaultCamelContext(); context.addRoutes(new RouteBuilder() { public void configure() { FileConvertProcessor processor = new FileConvertProcessor(); from("file:d:/temp/inbox?noop=true").process(processor).to("file:d:/temp/outbox"); } }); context.start(); boolean loop =true; while(loop){ Thread.sleep(25000); } context.stop(); }}
Here, we simply convert multiple lines of the received file into one line.
public class FileConvertProcessor implements Processor{ @Override public void process(Exchange exchange) throws Exception { try { InputStream body = exchange.getIn().getBody(InputStream.class); BufferedReader in = new BufferedReader(new InputStreamReader(body)); StringBuffer strbf = new StringBuffer(""); String str = null; str = in.readLine(); while (str != null) { System.out.println(str); strbf.append(str + " "); str = in.readLine(); } exchange.getOut().setHeader(Exchange.FILE_NAME, "converted.txt"); // set the output to the file exchange.getOut().setBody(strbf.toString()); } catch (IOException e) { e.printStackTrace(); } }}
When running in eclipse, camel does not print the log information to the console by default. If this error occurs, the exception is invisible and you need to configure log4j to the project.
log4j.appender.stdout = org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target = System.outlog4j.appender.stdout.layout = org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern = %-5p %d [%t] %c: %m%nlog4j.rootLogger = debug,stdout