Introduction To Using Beans With Camel

Reading Time: 3 minutes

We’ve been developing software for a long time, and have dealt with several component models like CORBA, EJB, JBI, SCA, and many more. Some of these, particularly the older ones, put significant restrictions on the programming paradigm, defining what we could and couldn’t do. Because of the increasing complexity and its accompanying difficulties, the open-source community developed a simpler, more pragmatic programming style: the Plain Old Java Object (POJO) model. In this blog, we’ll go through How To Use Beans with Camel in both the Hard way and the Easy way.

Using Beans the Hard way and the Easy way

Let’s assume that we already have a bean that provides an operation (a service) that you need to utilize in your integration application. HelloBean, for example, provides the hello function as a service:

Various ways to use this bean in our application – The Hard Way

Invoking a Bean using pure JAVA – using the new() method

The camel “processor” allows us to invoke a bean using java code

In the below code snippet, we see a RouteBuilder which defines a route. Using the Camel Processor, we get the process method in which we can implement our java code. We first extract the message body from the input. The next step is to instantiate the bean using the new method. The last step is to set the output.

Invoking a Bean using pure JAVA – using Autowired

In the below code snippet, we initialize a variable using the @Autowired annotation, which injects our bean. Using the Camel Processor, we get the process method in which we can implement our functionality. We first extract the message body from the input. The next step is to invoke the bean and then set the output.

The difference between the above approaches is that in the first approach we instantiate the bean using the new() method and in the second approach we inject the bean using the @Autowired Annotation.

As we can see, it’s hard to work with beans because of the following reasons :

  • To invoke the bean, you must use Java code.
  • You must use the Camel Processor, which clutters the path and makes it more difficult to interpret what is happening.
  • Bean must be instantiated explicitly or we can use dependency injection

Using Beans The Easier Way

Invoking a Bean using Camel’s bean() method 

The way to call a bean in Camel is easy. We just have to use .bean() method. Take a look at its implementation below :

The implementation is now substantially shorter, using only two lines of code. You still let Spring inject HelloBean as a dependency. This time, instead of using a Processor to invoke the bean, the Camel route uses Camel’s bean() method.

Camel will call the hello method on the bean instance named helloBean with the first parameter being the bean to call and hello being the name of the method to call. 

The reduction from seven lines of Processor code to one line with the bean is great. Furthermore, the single code line is considerably simpler to understand. It’s all high-level abstraction, with no low-level code details, as was the case with inline Processors.

Conclusion

In the above example, we saw that how easy it is to implement beans. In pure Java we have two approaches i.e. using the new() method and the second method is to use the @Autowired annotation. Apache Camel provides a much simpler approach which is to use the bean() method. This way it becomes extremely easy to implement and understand the working of code as well.

Reference links: http://camel.apache.org

Reference for code snippets: https://github.com/camelinaction/camelinaction2

Stay tuned for more blogs on: https://blog.knoldus.com/

Written by 

Sakshi Mittal is a Software Consultant at Knoldus Software. She has completed her MCA from BCIIT and Bachelors in Computer Applications from GGSIPU. Her practice area is Java but she loves to explore machine learning field as well. She likes writing tech blogs and contribute to open source. When not working you will find her watching travel and food vlogs.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading