Saturday 12 January 2019

Apache Camel: Built-in UUID generators


A universally unique identifier (UUID) is an identifier standard used in software construction. A UUID is simply a 128-bit value. The meaning of each bit is defined by any of several variants.

For human-readable display, many systems use a canonical format using hexadecimal text with inserted hyphen characters.

For example:
510c7b5b-a8c1-454a-be90-9904a97ab780

For more information on UUID, I would recommend you to go through my below tutorial post.

Camel Built-in UUID generators
Camel uses UUID generators to generate unique id for exchange, message.

Below table summarizes the UUID generators provided by Camel.
UUID Generator
Description
org.apache.camel.impl.ActiveMQUuidGenerator
It generates UUID based on how Apache ActiveMQ generates its UUID.
org.apache.camel.spi.UuidGenerator
It internally maintains 'AtomicLong' which starts with value 1. It increments the value on every new id request. This genrator is unique per camel context.
org.apache.camel.impl.JavaUuidGenerator
Generates random UUID using java.util.UUID class. It uses below snippet to generate random uuid.
UUID.randomUUID().toString()

Assume 'C:\\Users\\Public\\demo" folder is one file in it.

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 exchangeId = exchange.getExchangeId();
     String messageId = exchange.getIn().getMessageId();
     
     System.out.println("Exchange Id : " + exchangeId);
     System.out.println("Message Id : " + messageId);
    }

   }).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
Exchange Id : ID-INLN50911363A-1540869608560-0-1
Message Id : ID-INLN50911363A-1540869608560-0-2

You can set the UUID generator by calling 'setUuidGenerator' method of camel context.
context.setUuidGenerator(new org.apache.camel.impl.SimpleUuidGenerator());

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 exchangeId = exchange.getExchangeId();
     String messageId = exchange.getIn().getMessageId();
     
     System.out.println("Exchange Id : " + exchangeId);
     System.out.println("Message Id : " + messageId);
    }

   }).to("file:C:\\Users\\Public\\demoCopy");
  }

 }

 public static void main(String args[]) throws Exception {
  CamelContext context = new DefaultCamelContext();
  
  context.setUuidGenerator(new org.apache.camel.impl.SimpleUuidGenerator());

  context.addRoutes(new FileCopyRoute());

  context.start();

  TimeUnit.MINUTES.sleep(1);

  context.stop();
 }
}

Output
Exchange Id : 1
Message Id : 2



Previous                                                 Next                                                 Home<

No comments:

Post a Comment