Saturday, 1 August 2015

Apache Camel Demo Example


As we know that now a days Apache camel is getting popular in market and it is best choice for integration system.

Let’s investigate the Apache Camel.

  1.  Apache camel is use to define routing and mediation rules in a variety of domain specific languages.
  2. Apache camel uses URI(s) to work directly with any kind of transport mechanism or messaging model like HTTP, Active MQ, JMS, and CXF.
  3.  Apache camel provides pluggable components and data format options.
  4. It is easy to learn its API and you can interact with all the components provided out-of-box.
  5. Apache camel uses the same API regardless which kind of transport is used.
  6. Apache camel is small library with minimal dependencies for easy embedding in any java application. 
  7.  Apache camel provides support for bean binding and seamless integration with popular framework such as spring, blueprint and Guice. 
  8. Camel provides extensive support for UNIT testing for defined routes

 Camel Life Cycle:

Apache camel uses a simple lifecycle interface called SERVICE.

SERVICE interface has provides below methods to manage the camel Life Cycle.
  1. Start() throws Exception – Starts the service and throws an exception if service starting failed
  2. Stop() throws Exception – Stop the service and throws an exception if service stopping failed
Various classes implement service like CamelContext along with component and Endpoint classes.

When we use the apache camel, we need to start camel context service which will start all the various components and end points as well as active the route rules until the context is stopped again.

Let Looks at Simple Example:


Add the Maven Dependencies first


 <dependency>

                     <groupId>org.apache.camel</groupId>
                     <artifactId>camel-core</artifactId>
                     <version>2.13.0</version>
              </dependency>
             
              <dependency>
                     <groupId>org.apache.camel</groupId>
                     <artifactId>camel-stream</artifactId>
                     <version>2.13.0</version>
              </dependency>


Java Code


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

public class CamelDemoExample {

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

              camelContext.addRoutes(new RouteBuilder() {
                     @Override
                     public void configure() throws Exception {
                           from("stream:in?promptMessage=Enter Your Name : ")
                                         .transform(body().append(" Welcome to my site !!!"))
                                         .to("stream:out");
                     }
              });
              camelContext.start();            
              Thread.sleep(120000);            
              camelContext.stop();
       }

}



 Let us understand the above example

  • First we need to create the instance of CamelContext interface using DefaultCamelContext Implementation class.

CamelContext is used to represent the context to define routes and policies to use during message exchange between endpoints.

Context provides the below methods to control the lifecycles.
  • Start() – To Start    
  • stop() – To shutdown
  • suspend() - To pause routing messages
  • resume() – To resume after suspend
                              CamelContext camelContext = new DefaultCamelContext();

  • Add the routes and define the policies to use during message exchange


camelContext.addRoutes(new RouteBuilder() {

                     @Override
                     public void configure() throws Exception {
                           from("stream:in?promptMessage=Enter Your Name : ")
                            .transform(body().append(" Welcome to my site !!!"))
                                         .to("stream:out");
                     }
              });
  •       Start the camel                             
                          camelContext.start();
  •     Wait for few second to process the camel for message exchange between endpoints. If we don’t add this so camel immediately close the service.
                          Thread.sleep(120000);
  •     Stop the camel
                        camelContext.stop();

This is simple example which you can try in your system and also you can share your finding with us.

Happy to see your comment.

No comments:

Post a Comment