Saturday 12 January 2019

Camel: writing custom data format


In this post, I am going to explain how to write your own custom data formatter.

You can write the custom data formatter by implementing DataFormat interface.

For example, below custom data formatter, reverse the data while marshalling and unreversed the data while unmarshalling.

public class TextReverseFormatter implements DataFormat {

 @Override
 public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception {
  byte[] bytes = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, graph);
  String body = reverseData(bytes);
  stream.write(body.getBytes());
 }

 @Override
 public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
  byte[] bytes = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, stream);
  String body = reverseData(bytes);
  return body;
 }
 
}

Below statement uses TextReverseFormatter before copying the data from soruceFolder to destFolder.

from(sourceFolder).marshal(new TextReverseFormatter()).to(destFolder);

Follow below steps to create complete working application.


Create new maven project ‘camelDataFormatter’ in Eclipse.


Open pom.xml and update 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>camelDataFormatter</groupId>
 <artifactId>camelDataFormatter</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.formatters’ and define the class TextReverseFormatter.


TextReverseFormatter.java
package com.sample.app.formatters;

import java.io.InputStream;
import java.io.OutputStream;

import org.apache.camel.Exchange;
import org.apache.camel.spi.DataFormat;

public class TextReverseFormatter implements DataFormat {

 @Override
 public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception {
  byte[] bytes = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, graph);
  String body = reverseData(bytes);
  stream.write(body.getBytes());
 }

 @Override
 public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
  byte[] bytes = exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, stream);
  String body = reverseData(bytes);
  return body;
 }

 private static String reverseData(byte[] data) {
  StringBuilder builder = new StringBuilder();

  for (int i = data.length - 1; i >= 0; i--) {
   builder.append((char) data[i]);
  }

  return builder.toString();
 }

}

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


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

import org.apache.camel.builder.RouteBuilder;

import com.sample.app.formatters.TextReverseFormatter;

public class FileCopyRoute extends RouteBuilder {

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

  from(sourceFolder).marshal(new TextReverseFormatter()).to(destFolder);

 }

}

Define the class Application 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();
 }
}


Project structure looks like below.





Previous                                                 Next                                                 Home<

No comments:

Post a Comment