Showing posts with label camel exchange. Show all posts
Showing posts with label camel exchange. Show all posts

Saturday, 29 December 2018

Apache Camel: Exploring Message interface

'org.apache.camel.Message' interface represents an inbound (or) outbound message that is encapsulated in an exchange.

A message object contains message body, message headers and message attachments.

Below is the definition of Message interface.

Message.java
public interface Message {

    String getMessageId();
    void setMessageId(String messageId);

    Exchange getExchange();

  
    boolean isFault();
    void setFault(boolean fault);


    Object getHeader(String name);
    Object getHeader(String name, Object defaultValue);
    Object getHeader(String name, Supplier<Object> defaultValueSupplier);
    <T> T getHeader(String name, Class<T> type);
    <T> T getHeader(String name, Object defaultValue, Class<T> type);
    <T> T getHeader(String name, Supplier<Object> defaultValueSupplier, Class<T> type);
    void setHeader(String name, Object value);
    Object removeHeader(String name);
    boolean removeHeaders(String pattern);
    boolean removeHeaders(String pattern, String... excludePatterns);
    Map<String, Object> getHeaders();
    void setHeaders(Map<String, Object> headers);
    boolean hasHeaders();

    Object getBody();
    Object getMandatoryBody() throws InvalidPayloadException;
    <T> T getBody(Class<T> type);
    <T> T getMandatoryBody(Class<T> type) throws InvalidPayloadException;
    void setBody(Object body);
    <T> void setBody(Object body, Class<T> type);
 
    Message copy();
    void copyFrom(Message message);
    void copyFromWithNewBody(Message message, Object newBody);
 
    void copyAttachments(Message message);
    DataHandler getAttachment(String id);
    Attachment getAttachmentObject(String id);
    Set<String> getAttachmentNames();
    void removeAttachment(String id);
    void addAttachment(String id, DataHandler content);
    void addAttachmentObject(String id, Attachment content);
    Map<String, DataHandler> getAttachments();
    Map<String, Attachment> getAttachmentObjects();
    void setAttachments(Map<String, DataHandler> attachments);
    void setAttachmentObjects(Map<String, Attachment> attachments);
    boolean hasAttachments();
}


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 {
     Map<String, Object> headers = exchange.getMessage().getHeaders();
     printMap(headers);
    }

   }).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
breadcrumbId : ID-INLN50911363A-1540830736880-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





Previous                                                 Next                                                 Home

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