Saturday 12 January 2019

Camel: Type Converters


Type converters are used to convert the message content from one format to other.

For example, you can convert file to string, string to byte[] using type converters.

For example,
Below statement converts the file data to string.
from("file:C:\\Users\\Public\\demo")
         .convertBodyTo(String.class, "UTF-8")
         .to("file:C:\\Users\\Public\\demoCopy");

Application.java
package com.sample.app;

import java.util.concurrent.TimeUnit;

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

public class Application {
 public static class FileCopyRoute extends RouteBuilder {

  @Override
  public void configure() throws Exception {
   from("file:C:\\Users\\Public\\demo").convertBodyTo(String.class, "UTF-8")
     .to("file:C:\\Users\\Public\\demoCopy");
  }

 }

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

  context.addRoutes(new FileCopyRoute());

  context.start();

  TimeUnit.MINUTES.sleep(1);

  context.stop();
 }
}

How to get the type converter from Exchange?

org.apache.camel.TypeConverter tc = exchange.getContext().getTypeConverter();


Previous                                                 Next                                                 Home<

No comments:

Post a Comment