Apache Camel
è
Hello World for Camel application.
Ø
New project in Eclipe
Ø
New project in Eclipe
Ø
Add camel core dependency : in pom
Ø
Now in
main/entry class we need to create object for CamelContext
Ø
Above ,Here context method addRoutes takes
parameter as of type RouteBuilder so now we’ll need to create that class
extending RouteBuilder.
RouteBuilder is an abstract class with abstract method name configure() , so we
gonna override this .
Ø
Here route will be printing above print
statement , so now we’ll create instance for this routeBuilder type object and
pass it to parameter of add routes.
and so on as we call start method of this context it will call routeBuilder
configure() method.
è
How to copy file from one folder to another
folder :
Ø
Initial process same as above , here also.
here above we can see like , we created anonymous implementation for
RouteBuilder .
Ø
So from and to have paramters those are :
file : is the compenent i.e it will take care of all the file operations
input_box : is the folder name i.e the end point.
noop : will write further
> (Google) If noop=true, Camel will
set idempotent=true as well, to avoid consuming the same files over and over
again.
Ø
At last while is for infinite looping of start
so that whenever someone puts new file in input_box folder it automatically
gets copied.
è
How to use Producer and Consumer template
Ø
Here too we will use same add routes
routeBuilder and thereafter use createProducerTemplate for producing object in
direct:start so as object reaches from() it get passed in to() method for
seda:end component & endpoints, now createProducerTemplate
will use that component & endpoint ,
Ø
Now createConsumerTemplate get the object in
seda:end that will be used by for consuming object that was produced earlier.
Note : below is the SS to doubt – What is direct : start means in from
endpoint.
è
Adding processor to routes : used for making
some customization or some business logic in between.
Ø
So the output from processor gets executed after from() and before getting int
to().
Ø
So here below we are using same process() with
Exchange type parameter to manipulate the data coming as input and the setting
at back to Exchange type object and that gets return from to() method.
è
Setting up ActiveMQ server for camel integration
Ø
Go to site activemq.apache.org
Ø
Download the latest version there for windows.
Ø
Go to download location Extract the folder.
Ø
Now go inside to the bin folder and then open
cmd there inside bin folder.
Ø
Type : activemq start
Ø
It will start in any port will be shown there I
cmd for ex (8161)
Ø
Go to that port in browser:
Now for accessing admin portal you can just click on to Manage ActiveMQ broker.
It will ask for username and password i.e bydefault:
username : admin
password : admin
Ø
Now for creating queues , go to the queues :
go to queues tab thereafter provide queue name and then create.
è
Sending file to ActiveMQ Server :
1. Go to maven project.
2. Add maven dependency ActiveMQ Camel.
3. Now create a class in project :
here what we are doing is :
1. We need to create connection for ActiveMQconnectionFactory
2. Then add this to as JMS component to CamelContext and there after pass the
ActiveMQconnectionFactory instance.
3. Then simple provide from and to endpoints in routeBuilder.
from : here above for file
component it is input_box folder/directory.
to : is the queue name we provided
in active MQ admin .
4. Now after running the code it will start reflecting the newly entered file
as pending messages in the ActiveMQ server.
è
Sending object to ActiveMQ Server : will be done
similarly as above .
Ø
So here we are sending for ex date object using
producerTemplate
So here after running this we’ll able to see date object inside ActiceMQ
server.
è
Consuming Message from ActiveMQ Server.
Ø
Just the changes are made in from and to
endpoints :
Ø
Now using sendTo in ActiveMQ server we can send
message which will be consumed by consumer template.
è
Calling a method using class component :
Ø
So above we will be receiving message in
doSomething() method of MyService class as a parameter String here , and this
msg is produced from main class using producer template in from() end point and
as from endpoint receives this message it sends it in to() endpoint which is
having information about receiving class and method.
è
Calling a Method using Bean Component
Ø
Here every thing is same as above just for bean
we create bean instance for the class/bean for which we have to call the
function .
Ø
Then we create bean instance for SimpleRegistry for
registering above calling bean instance.
calling bean : here refers to the instance
of bean whose method will be consumed/called.
Ø
put() method in registry takes 1 parameter as
calling beans name that will be used for providing in to() endpoint and 2
parameter is the instance name that we created for calling bean.
è
SQL Operation using JDBC Component:
Ø
First creating table in SQL and inserting
records in it.
Ø
Then add camel-jdbc dependency & mysql connector
for database in pom.xml file :
Ø
Now create a camel application for communicating
between DB and CamelContext.
1. Here we will first be creating dataSource for MySQL database and will be
providing url, username and password.
in url after host and port is the database name.
2. Now we have to add the above data source to registry.
3. Now we pass that registry in CamelContext’s implementation class as
parameter.
4. So here in route builder
implementation in from() endpoint we are providing query that is to be executed
using producer template.
Now the to() endpoint is consuming that query injected to it and further
processing the data out of it.
5. Now to print data coming from database it can be displayed or used using
bean() method in same routeBuilder implementation so bean() method will take 2 parameter
1 the instance of bean and 2 the method of that bean that need to be called.
So here is the output from DB as per above method implementation.
Ø
Add camel core dependency : in pom
Ø
Now in
main/entry class we need to create object for CamelContext
Ø
Above ,Here context method addRoutes takes
parameter as of type RouteBuilder so now we’ll need to create that class
extending RouteBuilder.
RouteBuilder is an abstract class with abstract method name configure() , so we
gonna override this .
Ø
Here route will be printing above print
statement , so now we’ll create instance for this routeBuilder type object and
pass it to parameter of add routes.
and so on as we call start method of this context it will call routeBuilder
configure() method.
è
How to copy file from one folder to another
folder :
Ø
Initial process same as above , here also.
here above we can see like , we created anonymous implementation for
RouteBuilder .
Ø
So from and to have paramters those are :
file : is the compenent i.e it will take care of all the file operations
input_box : is the folder name i.e the end point.
noop : will write further
> (Google) If noop=true, Camel will
set idempotent=true as well, to avoid consuming the same files over and over
again.
Ø
At last while is for infinite looping of start
so that whenever someone puts new file in input_box folder it automatically
gets copied.
è
How to use Producer and Consumer template
Ø
Here too we will use same add routes
routeBuilder and thereafter use createProducerTemplate for producing object in
direct:start so as object reaches from() it get passed in to() method for
seda:end component & endpoints, now createProducerTemplate
will use that component & endpoint ,
Ø
Now createConsumerTemplate get the object in
seda:end that will be used by for consuming object that was produced earlier.
Note : below is the SS to doubt – What is direct : start means in from
endpoint.
è
Adding processor to routes : used for making
some customization or some business logic in between.
Ø
So the output from processor gets executed after from() and before getting int
to().
Ø
So here below we are using same process() with
Exchange type parameter to manipulate the data coming as input and the setting
at back to Exchange type object and that gets return from to() method.
è
Setting up ActiveMQ server for camel integration
Ø
Go to site activemq.apache.org
Ø
Download the latest version there for windows.
Ø
Go to download location Extract the folder.
Ø
Now go inside to the bin folder and then open
cmd there inside bin folder.
Ø
Type : activemq start
Ø
It will start in any port will be shown there I
cmd for ex (8161)
Ø
Go to that port in browser:
Now for accessing admin portal you can just click on to Manage ActiveMQ broker.
It will ask for username and password i.e bydefault:
username : admin
password : admin
Ø
Now for creating queues , go to the queues :
go to queues tab thereafter provide queue name and then create.
è
Sending file to ActiveMQ Server :
1. Go to maven project.
2. Add maven dependency ActiveMQ Camel.
3. Now create a class in project :
here what we are doing is :
1. We need to create connection for ActiveMQconnectionFactory
2. Then add this to as JMS component to CamelContext and there after pass the
ActiveMQconnectionFactory instance.
3. Then simple provide from and to endpoints in routeBuilder.
from : here above for file
component it is input_box folder/directory.
to : is the queue name we provided
in active MQ admin .
4. Now after running the code it will start reflecting the newly entered file
as pending messages in the ActiveMQ server.
è
Sending object to ActiveMQ Server : will be done
similarly as above .
Ø
So here we are sending for ex date object using
producerTemplate
So here after running this we’ll able to see date object inside ActiceMQ
server.
è
Consuming Message from ActiveMQ Server.
Ø
Just the changes are made in from and to
endpoints :
Ø
Now using sendTo in ActiveMQ server we can send
message which will be consumed by consumer template.
è
Calling a method using class component :
Ø
So above we will be receiving message in
doSomething() method of MyService class as a parameter String here , and this
msg is produced from main class using producer template in from() end point and
as from endpoint receives this message it sends it in to() endpoint which is
having information about receiving class and method.
è
Calling a Method using Bean Component
Ø
Here every thing is same as above just for bean
we create bean instance for the class/bean for which we have to call the
function .
Ø
Then we create bean instance for SimpleRegistry for
registering above calling bean instance.
calling bean : here refers to the instance
of bean whose method will be consumed/called.
Ø
put() method in registry takes 1 parameter as
calling beans name that will be used for providing in to() endpoint and 2
parameter is the instance name that we created for calling bean.
è
SQL Operation using JDBC Component:
Ø
First creating table in SQL and inserting
records in it.
Ø
Then add camel-jdbc dependency & mysql connector
for database in pom.xml file :
Ø
Now create a camel application for communicating
between DB and CamelContext.
1. Here we will first be creating dataSource for MySQL database and will be
providing url, username and password.
in url after host and port is the database name.
2. Now we have to add the above data source to registry.
3. Now we pass that registry in CamelContext’s implementation class as
parameter.
4. So here in route builder
implementation in from() endpoint we are providing query that is to be executed
using producer template.
Now the to() endpoint is consuming that query injected to it and further
processing the data out of it.
5. Now to print data coming from database it can be displayed or used using
bean() method in same routeBuilder implementation so bean() method will take 2 parameter
1 the instance of bean and 2 the method of that bean that need to be called.
So here is the output from DB as per above method implementation.
Comments
Post a Comment