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

No comments:

Post a Comment