Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Java Integration Framework: First Steps with Apache Camel

See in this article what is Apache Camel, how we can install the framework and finally we will make an application integration.

Apache Camel is an integration framework open source that makes the integration among systems very simple. Using an integrator we can focus more on how to interoperate with the system, rather than worrying about how the system we are integrating actually works internally. Apache Camel provides simple and manageable abstractions for complex systems. Due to these characteristics, Apache Camel is considered an excellent framework for integration. In addition, it makes integration into software projects very productive. Basically, with little code we can do many things that would need a lot of code, external libraries, internal control, etc.

Apache Camel project started in 2007 and despite being a young project, it is an open-source project quite mature, available on the Apache 2 license and has an extremely strong community.

See also some of our other articles regarding software integration:

Installing Apache Camel

To install Apache Camel we must first download it through its official website (refer to Links section).

On this screen we have different distributions so we can choose the equivalent of our operating system, as is shown in Figure 1.

Figure 1. Available downloads for Apache Camel

Now, the next step is to unzip the file on a system folder. In my case I unzipped in: D:tests\camel\apache-camel-2.16.1 (equivalet to the most recent version in the time of writing this article).

Once Apache Camel is unpacked, we can simply place the jars in Eclipse. We will use Eclipse Mars as our development IDE. To download Eclipse just download the ".zip" or eclipse executable in www.eclipse.org/downloads/.

Let's create a new project in Eclipse to start testing the features that Apache Camel provides us. For this, follow the sequence: Click New -> Other, select Java Project and click Next. Then give a name to your project, let's call HelloCamel, like in Figure 2 set all the options as shown and click Finish.

Figure 2. Create a New Java Project wizard

Then we need to add all the unzipped Java libraries of Apache Camel to the classpath of our project. For this, click the right button at your project and select Properties option. Find for Java Build Path option and, in the Libraries tab, click the button Add External JARs.... and a window will open for you to navigate to the directory where you've unzipped the downloaded file.

Go to camel\apache-camel-2.16.1\lib folder and select all the jars. Click in Open. Your Properties window must be exactly as the shown in Figure 3. Then click OK to finish.

Figure 3. All jars selected in Properties window

Observation: Although we have selected all the JARS, we will not use all, but a good part of them will be used in the following examples. When you develop your own application integration, select the components that you will actually use. Or use Maven to declare the dependencies needed for a particular project.

To begin the testing of the potential of Camel, let's make a simple example of integration that makes a routing between files. For example, let's start from the assumption that we need to copy a file located in data/inbox to data/outbox. Making this apparently simple example in Java we would have about 34 lines of code, since it would be necessary to create folders, copy the entire file located in inbox and take it to the outbox folder as well as opening files, closing files and even controlling possible exceptions.

Using Camel we don't need to waste so much time doing it, since it is not necessary to reinvent the wheel again. We need just the code below to the operation be performed. Therefore, we created a file in Eclipse by clicking the right mouse button in "src" and selecting New Class. Give it a Package name (camel-example) and a Class Name (CopyPasteFileCamel), for example, and click Finish, like we see in Figure 4.

Figure 4. Setting up the package and class names.

So, add the code find in Listing 1 to the same class.

Listing 1. Coding the integration between two files in Camel.

package camel.example;


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


public class CopyPasteFileCamel {


	public static void main(String args[]) throws Exception {
		CamelContext context = new DefaultCamelContext();
		context.addRoutes(new RouteBuilder() {
			public void configure() {
				from("file:data/inbox?noop=true").to("file:data/outbox");
			}
		});


		context.start();
		Thread.sleep(10000);
		context.stop();
	}
}

Before running the code, go to your Eclipse workspace where is the folder of your project ("HelloCamel") and create a folder in the root called "data" (Figure 5) and within this folder create another folder called "inbox". That folder will store a txt file named "test" with the message "Hello Apache Camel!".

Figure 5. Creating new data folder

Now run the class by clicking the right button anywhere in this class and selecting "Run As" and finally "Java Application".

After that, go back to the folder of your project and view it within the "data" folder that a new folder called "outbox" was created with the file "test.txt" with the respective message.

We note that the language is extremely simple to understand, so that's why it becomes simple to use Apache Camel, and all this is due to the DSL it uses. In the above example we only have one CamelContext that starts and stops. Also, we added a sleep to give time for copying files.

Another interesting point is the Route of Camel which is defined such that they flow from one side to the other as they are being read. This route can be read as follows: consume the message file located on a data/inbox with noop option enabled, and then send the file to data/outbox. The noop option tells Camel to leave the source file the way it is. Otherwise, the file would be moved.

Now we create our first integration components, we go a little further and see a second example where we contacted an external ftp and we upload the files to a location of our file system.

Create a new class as shown above and enter the code shown in Listing 2 in Eclipse.

Listing 2. Transfering a file from ftp to local disk

package camel.example;


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


public class FileCopierWithCamel {


	public static void main(String args[]) throws Exception {
		CamelContext context = new DefaultCamelContext();
		context.addRoutes(new RouteBuilder() {
			public void configure() {
				from("ftp://ftp.pucrs.br/mysql/img?delay=5s&move=done").to("file:target/download");
			}
		});


		context.start();
		Thread.sleep(10000);
		context.stop();
	}
}

We can see the target/download folder and check that the external FTP files are easily downloaded to the destination folder. If we created an FTP client to make this operation would need several lines of code and several classes and extra packages to integrate. The same code could be changed to another route, for example, instead of sending the external FTP files to a folder we could send to a queue in java, changing the route to:

from("ftp://ftp.pucpr.br/mysql/img?delay=5s&move=done").to("jms:incomingOrders");

Apache Camel also provides several other features such as integration with Web Services, and many others. Virtually, everything can be integrated with Apache Camel.

Conclusion

In this article we study what is Apache Camel, how can we install the framework inEclipseand finally we made an application integration with files and ftp.

Links

Official Apache Camel page: http://camel.apache.org/download.html



Web developer and passioned for web design, SEO and front end technologies.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login