Saturday 29 December 2018

Apache Camel: Exploring Exchange interface

An Exchange is a container that holds the message and metadata associated with the message, Exchnage also contains temporary out message that represents the reply (out message).




If an Exchange failed during routing the Exception that caused the failure is stored and accessible via the getException() method.

out and in messages
The rule Camel uses is to take the out Message produced by the previous Processor and set it as the in for the next Processor.
If the previous Processor did not produce an out, then the in of the previous Processor is sent as the next in. At the end of the processing chain, depending on the Message Exchange Pattern the last out (or in of no out available) is sent by the Consumer back to the original caller.

Below snippet convert the data to uppercase.

from("file:C:\\Users\\Public\\demo?noop=true").process(new Processor() {

         @Override
         public void process(Exchange exchange) throws Exception {
                  String messageBody = exchange.getIn().getBody(String.class);

                  System.out.println("\nmessage before processing");
                  System.out.println(messageBody);

                  messageBody = messageBody.toUpperCase();

                  exchange.getIn().setBody(messageBody, String.class);
         }

}).to("file:C:\\Users\\Public\\demoCopy").process(new Processor() {

         @Override
         public void process(Exchange exchange) throws Exception {
                  System.out.println("\n\nmessage after processing");
         System.out.println(exchange.getIn().getBody(String.class));        
         }
});

Find the below working application.

Application.java
package com.sample.app;

import java.util.concurrent.TimeUnit;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
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?noop=true").process(new Processor() {

    @Override
    public void process(Exchange exchange) throws Exception {
     String messageBody = exchange.getIn().getBody(String.class);

     System.out.println("\nmessage before processing");
     System.out.println(messageBody);

     messageBody = messageBody.toUpperCase();

     exchange.getIn().setBody(messageBody, String.class);
    }

   }).to("file:C:\\Users\\Public\\demoCopy").process(new Processor() {

    @Override
    public void process(Exchange exchange) throws Exception {
     System.out.println("\n\nmessage after processing");
     System.out.println(exchange.getIn().getBody(String.class)); 
    }
   });
  }

 }

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

  context.addRoutes(new FileCopyRoute());

  context.start();

  TimeUnit.MINUTES.sleep(1);

  context.stop();
 }
}

Output
message before processing
Hello How are you
I am fine, thannk you!!!!!


message after processing
HELLO HOW ARE YOU
I AM FINE, THANNK YOU!!!!!

Exchange interface provides 'getProperties' method, it returns all of the properties associated with the exchange.

‘exchange.getIn().getHeaders()’ method return all the headers associated with the message.

Application.java
package com.sample.app;

import java.util.Map;
import java.util.concurrent.TimeUnit;

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

import java.util.*;

public class Application {

 private static void printMap(Map<String, Object> map) {
  Set<String> properties = map.keySet();

  for (String property : properties) {
   System.out.println(property + " : " + map.get(property));
  }
 }

 public static class FileCopyRoute extends RouteBuilder {

  @Override
  public void configure() throws Exception {
   from("file:C:\\Users\\Public\\demo?noop=true").process(new Processor() {

    @Override
    public void process(Exchange exchange) throws Exception {
     System.out.println("\nProperties are : ");
     printMap(exchange.getProperties());
     
     System.out.println("\nheaders are : ");
     printMap(exchange.getIn().getHeaders());
    }

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

Sample Output
Properties are : 
CamelBatchSize : 1
CamelMessageHistory : [DefaultMessageHistory[routeId=route1, node=process1]]
CamelBatchComplete : true
CamelExternalRedelivered : false
CamelBatchIndex : 0
CamelFileExchangeFile : GenericFile[C:\Users\Public\demo\helloWorld.txt]
CamelCreatedTimestamp : Mon Oct 29 21:45:06 IST 2018

headers are : 
breadcrumbId : ID-INLN50911363A-1540829704584-0-1
CamelFileAbsolute : true
CamelFileAbsolutePath : C:\Users\Public\demo\helloWorld.txt
CamelFileLastModified : 1540827492847
CamelFileLength : 45
CamelFileName : helloWorld.txt
CamelFileNameConsumed : helloWorld.txt
CamelFileNameOnly : helloWorld.txt
CamelFileParent : C:\Users\Public\demo
CamelFilePath : C:\Users\Public\demo\helloWorld.txt
CamelFileRelativePath : helloWorld.txt

You can use below methods to set, get and remove the properties of exchange.
void setProperty(String name, Object value);
Object removeProperty(String name);
boolean removeProperties(String pattern);
Object getProperty(String name);
Object getProperty(String name, Object defaultValue);
<T> T getProperty(String name, Class<T> type);
<T> T getProperty(String name, Object defaultValue, Class<T> type);



Previous                                                 Next                                                 Home

No comments:

Post a Comment