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.
- Apache camel is use to define routing and mediation rules in a variety of domain specific languages.
- Apache camel uses URI(s) to work directly with any kind of transport mechanism or messaging model like HTTP, Active MQ, JMS, and CXF.
- Apache camel provides pluggable components and data format options.
- It is easy to learn its API and you can interact with all the components provided out-of-box.
- Apache camel uses the same API regardless which kind of transport is used.
- Apache camel is small library with minimal dependencies for easy embedding in any java application.
- Apache camel provides support for bean binding and seamless integration with popular framework such as spring, blueprint and Guice.
- Camel provides extensive support for UNIT testing for defined routes
Camel Life Cycle:
Apache camel uses a simple lifecycle interface called
SERVICE.
- Start() throws Exception – Starts the service and throws an exception if service starting failed
- Stop() throws Exception – Stop the service and throws an exception if service stopping failed
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
- 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.
- Stop the camel
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