Wednesday 9 January 2019

Camel: Transform message using transform method


In this post, I am going to explain how to transform the message using transform() method of camel framework.

Suppose a folder ‘f1’ has text files, and you want to copy all the files from folder ‘f1’ to ‘f2’. While copying, you would like to covert the content to uppercase.

You can achieve above requirement using transform method.

Assume ‘sourceFolder’ has a file ‘quotes.txt’.

quotes.txt
If you work just for money, you'll never make it, but if you love what you're doing and you always put the customer first, success will be yours.

Your application should copy quotes.txt and place it in folder ‘destFolder’.

Follow below steps to build complete working application.


Create new maven project ‘camelMsgTransform’ in eclipse. Project structure looks like below.

Update pom.xml with maven dependencies.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>camelMsgTransform</groupId>
 <artifactId>camelMsgTransform</artifactId>
 <version>1</version>

 <dependencies>
  <!-- https://mvnrepository.com/artifact/org.apache.camel/camel-core -->
  <dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-core</artifactId>
   <version>2.22.1</version>
  </dependency>

 </dependencies>
</project>

Create a package ‘com.sample.app.routes’, and define the class FileCopyRoute.java.


FileCopyRoute.java
package com.sample.app.routes;

import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.builder.RouteBuilder;

public class FileCopyRoute extends RouteBuilder {

 @Override
 public void configure() throws Exception {
  String sourceFolder = "file:C:\\Users\\Public\\sourceFolder?noop=true";
  String destFolder = "file:C:\\Users\\Public\\destFolder";

  from(sourceFolder).transform(new Expression() {

   @Override
   public <T> T evaluate(Exchange exchange, Class<T> type) {
    String body = exchange.getIn().getBody(String.class);
    return (T) body.toUpperCase();
   }

  }).to(destFolder);

 }

}

Define Application.java in com.sample.app package.


Application.java
package com.sample.app;

import java.util.concurrent.TimeUnit;

import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;

import com.sample.app.routes.FileCopyRoute;

public class Application {
 public static void main(String args[]) throws Exception {
  CamelContext context = new DefaultCamelContext();

  context.addRoutes(new FileCopyRoute());

  context.start();

  TimeUnit.MINUTES.sleep(1);

  context.stop();
 }
}

Run Application.java, you can observe a file quotes.txt is created in folder destFolder.


Open the content of quotes.txt, you can see the content in uppercase like below.

IF YOU WORK JUST FOR MONEY, YOU'LL NEVER MAKE IT, BUT IF YOU LOVE WHAT YOU'RE DOING AND YOU ALWAYS PUT THE CUSTOMER FIRST, SUCCESS WILL BE YOURS.


Project structure looks like below.




Previous                                                 Next                                                 Home<

No comments:

Post a Comment