Sunday, September 13, 2015

Apache Camel - Simple Routing Example

This post is continuation of the previous post Apache Camel overview. In this post, we will see how to define a sample route using Apache Camel (with Spring Integration).

Dependencies

Apache Camel can work easily with Spring. The following dependencies are required to make Apache Camel to work with Spring.
  <dependency>
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-core</artifactId>
       <version>2.15.3</version>
  </dependency>
  <dependency>
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-spring</artifactId>
       <version>2.15.3</version>
  </dependency>
camel-core is the actual dependency and camel-spring is required for the spring integration.

Spring Configuration

To add camel to Spring Configuration, add the following

Namespace

xmlns:camel="http://camel.apache.org/schema/spring"

Schema Location

xsi:schemaLocation="http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd"

Combined Spring XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:camel="http://camel.apache.org/schema/spring"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://camel.apache.org/schema/spring 
              http://camel.apache.org/schema/spring/camel-spring.xsd">

</beans>         

Camel Context and Route

Camel context is the camel runtime. In Spring, we need to define the Camel Context to define the Route. Within the context, route must be defined.
Problem Statement: Read a file from a directory, process it and copy the processed file to a destination folder.

Define the Processor

package com.test.camel.processor;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SampleProcessor implements Processor
{
    private static final Logger LOG = LoggerFactory.getLogger(SampleProcessor.class);

    public void process(Exchange exchange) throws Exception
    {
        LOG.info("Input is "+exchange.getIn().getBody());
        //Processing goes here. Set the output to same as input. (Dummy Processing) 
        exchange.setOut(exchange.getIn());
    }

}

Points to be noted:

  • Processor is a class which implements org.apache.camel.Processor. 
  • Processor implements a method which takes only one argument (Exchange) - which consists of three messages (in, out and exception) and few other properties related to the route and it's camel context.

Define the Bean

     <bean id="processor" class="com.test.camel.processor.SampleProcessor" />

Define the Route

 <camel:camelContext id="camelContext">
      <camel:route id="file-route">
            <camel:from uri="file:/tmp/input.test" />
            <camel:process ref="processor" />
            <camel:to uri="file:/tmp/output.test" />
      </camel:route>
  </camel:camelContext>

Points to be noted:

  • Route is defined inside the Spring Config (using DSL). It also be defined using RouteBuilder. (We will concentrate only on DSL).
  • Define the route inside the Camel Context 
  • A very basic route consists of From (Where to fetch from), Processor (Which processor the input) and To (Where to write the processed input). 
  • Processor is optional - if we don't want to do any processing. 
  • Here in this route (file-route), files will be read from directory /tmp/input.test, and will be written into /tmp/output.test directory after processing.
  • Camel Context and Route(s) will be automatically started once the spring application context is loaded and started. So, once spring configuration file is loaded, camel looks for the files in the input directory. 
  • Camel Context and Route(s) will be shutdown once the application context is closed.

Exception Handling

Apache Camel provides try, catch and finally (optional) blocks to be defined within a route to handle the exceptions while routing or processing. The above route can be re-defined with try-catch blocks to check for Exception 
 <camel:camelContext id="camelContext">
      <camel:route id="file-route">
            <camel:from uri="file:/tmp/input.test" />
            <camel:doTry>
                 <camel:process ref="processor" />
                 <camel:to uri="file:/tmp/output.test" />
                 <camel:doCatch>
                      <camel:exception>java.lang.Exception</camel:exception>
                      <camel:log message="Exception while processing "></camel:log>
                      <camel:to uri="file:/tmp/error.test" />
                 </camel:doCatch>
            </camel:doTry>
      </camel:route>
  </camel:camelContext>
In the above case, if an exception is occurred while routing/processing, the exception will be caught, a log will be written and the file will be moved to another directory /tmp/output.test.

Full Spring Configuration

Complete spring configuration file is as below
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:camel="http://camel.apache.org/schema/spring" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://camel.apache.org/schema/spring 
           http://camel.apache.org/schema/spring/camel-spring.xsd">

     <bean id="processor" class="com.test.camel.processor.SampleProcessor" />

     <camel:camelContext id="camelContext">
         <camel:route id="file-route">
              <camel:from uri="file:/tmp/input.test" />
              <camel:doTry>
                   <camel:process ref="processor" />
                   <camel:to uri="file:/tmp/output.test" />
                   <camel:doCatch>
                        <camel:exception>java.io.IOException</camel:exception>
                        <camel:log message="Exception while processing "></camel:log>
                        <camel:to uri="file:/tmp/error.test" />
                   </camel:doCatch>
              </camel:doTry>
         </camel:route>
     </camel:camelContext>
</beans>
         
In the next post, we will see more details about routing. Happy Learning!!!!

Sunday, August 30, 2015

Apache Camel and KeyWords

To be very simple, Apache Camel is an open source Java Based API which implements most of the commonly used EIPs. So, the next question is what is an EIP?. Let me briefly explain

Enterprise Integration Patterns

EIPs is a set of design patterns (65 design patterns) on how to integrate different enterprise application running on various technologies and makes them to communicate.  These patterns are explained in a book by Gregor Hohpe and Bobby Woolf. So, EIPs is a book in which, the ways to create communication between applications and integrate them are defined. Check the link for more details.

Apache Camel

Let's come back to Apache Camel. As I mentioned at the beginning, Apache Camel is an implementation of EIPs. So, we can integrate the enterprise applications using Camel. It supports almost all types of protocols (FTP, HTTP, JMS, WebService, etc,), to communicate either by itself or by leveraging it. See below an overview of Camel
Using Camel, we can define a set of route(s) within Camel Context to create the communication. The received messages can be filtered, processed, validated, aggregated, split or routed to different other systems.

Camel Context

Camel Context creates runtime for Apache Camel.

EndPoint

As the name indicates, an endpoint is the final or intermediate source/destination of the communication. The endpoint can be a physical address like FTP server or a logical address like JMS Queue, WebService etc. Apache Camel uses URI (of course, Uniform Resource Identifier) to define endpoints. Ex: queue:SAMPLE for a Queue.

Exchange

Exchange is analogous to Message in JMS Specification. But the Exchange carries three messages i.e. Incoming, Outgoing and an Exception for processing. These three are defined as in, out and fault messages. Each of theses Messages has it's own headers.

Template  

CamelTemplate is a class which reads from/writes to an EndPoint. Earlier versions of the camel has the name as CamelClient, but the convention is changed in later versions to be in sync with the other implementations (like Spring JmsTemplate). Template reads/writes Exchanges to/from EndPoint.

Component

Component is a factory class through which you can create an instance of an EndPoint. JmsComponent is the factory for creating JMS Endpoints using a method called JmsComponent.createEndpoint();

Processor

The points discussed so far are very basic to integrate at-least two applications as it is (exchange the messages as it is with no processing). In order to process the message before sending to destination application, we need processor. The processor is an interface which has only one method called void process(Exchange exchange);

Route

A route is a step by step movement of the message between the two endpoints (including exception handling). There are two ways to define a route in Apache Camel. One is using the XML file (like spring bean file). Second is by using Java DSL (Domain Specific Language).

In the next post, we will see how to use these concepts to create a route and process the message between two endpoints (within the Camel Context).

Happy Learning!!!!!

Sunday, May 24, 2015

Lambda Expression and Functional Interface

Lambda expressions are one of the major features of Java 8 (Project Lambda). With this, Java adopts a feature of Functional Programming Language. This feature already present in other languages which runs on JVM (If you familiar of Scala, you might have noticed it). Anyway, we will see what is Lambda expression; how to use it and what are the advantages to use it.

Why Lambda Expressions

Lambda expression is a block of code written in a more readable and compact way. This piece of code can be executed later stage one or more times. This means, Lambda expression allows to pass a block of code as an argument to function or to a constructor. So far, we have been using Anonymous classes to do that. I will give you couple of most common examples on this and will convert them into Lambda expression.

Example - I

We write a anonymous thread using Runnable interface like this
        Runnable r = new Runnable() {
            public void run()
            {
                System.out.println("Anonymous Runnable Example");
            }
        };
        
        Thread t = new Thread(r);
        t.start();
If we use lambda expression, then the above code can be written as
        Runnable r2 = () -> System.out.println("Lambda Runnable Example"); 
        
        Thread t2 = new Thread(r2);
        t2.start();

Example - II

To sort list of elements (or an array), we use Collections.sort of Arrays.sort using a comparator like this
        String[] names1 = new String[]{"Java", "Scala", "Lisp"};
        Arrays.sort(names1, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2)
            {
                return o1.compareTo(o2);
            }
        });
        
        for(String s : names1)
        {
            System.out.println(s);
        }
If we use lambda expression, the this code can be changed to
        String[] names2 = new String[]{"Java", "Scala", "Lisp"};
        
        Comparator<String> comparator = (s1, s2) -> { return s1.compareTo(s2);};
        Arrays.sort(names2, comparator); 
        
        for(String s : names1)
        {
            System.out.println(s);
        }
Similarly event listeners in Swing can also be written like this. Example ActionListener interface where you implement the action performed by an event.
Pretty easy, isn't it, now we will see more details on the lambda expression

Syntax

Syntax of lambda expression is also simple (as we saw in above examples).
Syntax: (arguments) -> { Code }
  • Lambda expressions takes one or more arguments separated by comma (,) 
  • Type of the arguments are optional
  • Code and Arguments are separated by the symbol (->).
  • Code is just a set of statements like we write in methods
  • The code needs to end with a return statement (if it needs to return a value). 
  • If there is only one statement in the code, then braces {} are optional 
See some more examples of Lambda expressions
Example : () -> System.out.println("Without arguments");
Example : (event) -> System.out.println("One argument without type");
Example : (String s1, String s2) -> {
            System.out.println("two arguments and return type");
            return s1.compareTo(s2);
          };

By looking at the code, examples and usage we can list the advantages of Lambda expressions as
  • It increase the readability of the code 
  • Reduces unnecessary boiler plate code
  • It also adds to the performance of the code using parallel processing. 

Functional Interface 

To incorporate the lambda expression into Java, designers introduced a new feature called Functional Interface. Functional Interface is an interface with only one method. To make it more semantic, we can add an annotation @FunctionalInterface. If we add more than one method, it will break the contract and it will no more be a functional interface, instead we can add default or static methods. Example of Functional Interfaces are
Examples
//Example 1 - Only one method
@FunctionalInterface
public interface FInterface
{
    public void method();
}

//Example 2 - with default method
@FunctionalInterface
public interface FInterface
{
    public void method();
    
    public default String printName()
    {
        return "Functional Interface";
    }
    
}
All the famous java interfaces like Runnable, Callable, Comparator etc.. are now functional interfaces. If you open the JavaDoc or code of these interfaces, you will see that these are annotated with @FunctionalInterface. See the samples below
//Comparator
@FunctionalInterface
public interface Comparator<T> {
...

//Runnable
@FunctionalInterface
public interface Runnable {
As i mentioned earlier in the post, lambdas are the most important and prominent feature of Java 8 which attracts most the developers, hope for you as well.

Thanks for reading and Happy Leaning!!!

Monday, May 18, 2015

Programming using VIM editor

There would be some cases, where we need to change source on the host machines and run them. For those instances, VIM (VI - improved) is very good editor to make use of. As we are most used to modern GUI editors, we may find it difficult to use the terminal. We can make best use of terminal using some tips and tricks. I will try to explain what i generally use.

Syntax Highlighting

We do make mistakes/typos when typing, so better to use syntax highlighting to make sure that we are typing the right command/keyword/phrase etc. Syntax highlighting is simple enough in VIM. Use the following commands to set that. Open VIM (empty or with any java/c file) and type
:syntax on
VIM detects the language automatically, but if you would like to change the language, use syn variable to set the preference (java or xml etc). See some samples below on how to use
:set syn=java
:set syn=xml
If you are using a terminal, syntax highlighting may not work for you, unless you set your term variable to ansi
:set term=ansi
You may need to add this in your .vimrc file (exists in $HOME directory. If not exists, create it) to default it for all the files opened by VIM.

Color-Schemes

Syntax highlighting will be shown with default color schemes. There are few color schemes available by vim by default, you can switch the color shown on the highlighting. use colorscheme keyword.
:colorscheme <color-scheme-name>
You can download the color schemes available on web and place in your vim colors directory. Generally it will be at $HOME/.vim/colors. You can download the color-schemes from from github, and install. Copy the downloaded files into .vim/colors (in $HOME), if not exists - create it, and copy all the colors to colors folder.
If want to default the syntax highlighting and color schemes defaulted when you open VIM, simply put all the commands into .vimrc files in your home directory.
See one sample below
set term=ansi
syntax on

colorscheme eclipse

Compile and Run

To run your program, you can use the compile or execute command prefixed with :! inside VIM editor. See sample below. Open vim and write the code and compile using
:!javac Sample.java
When you run the command, a new screen will be opened with the javac command output (Compilation errors/success of compilation). Once the compilation is successful, run the program using the following command.
:!java Sample
  • Note: Just a point to note, javac and java should be in PATH variable to be recognized. 
But it will be too cumbersome to type the same command multiple times, if any compilation errors, you have two options to make it simple and easy. 

QuickRun plugin

As VIM allows to add plugins, there is a plugin QuickRun which compiles and run a program in one go. Download the plugin at github. Follow the installation instructions (just copy the mentioned files to your vim plugins directory - $HOME/.vim/plugins). Once installed, you can run your program by the command QucikRun. Open your program and execute the command
:QuickRun
This will compile and run the program and output will be shown by splitting the screen. QuickRun supports few programming languages by default and it recognizes based on the file type (See documentation for more details).

MakePrg command

If you wouldn't like to install a plugin, or if the plugin doesn't support the programming language that you are working on, then you can use makeprg command to compile your program.
Edit .vimrc file in your $HOME directory to include the following lines (I am giving an example on how to use javac command in makeprg)
"Compiling Java Code
autocmd Filetype java set makeprg=javac\ %
set errorformat=%A%f:%l:\ %m,%-Z%p^,%-C%.%#
map <F9> :make<Return>:copen<Return>
map <F10> :cprevious<Return>
map <F11> :cnext<Return>
By doing this, you are mapping some of the control characters to default compile command (i.e. make). What we are doing in the above lines are
  • Setting the detault make program to use the command javac
  • Mapping F9 (Function-9) key to compile the java code and return to the current window without any prompt.
  • If any compilation error,
    • use F11 to see the next error message (printed in the given error-format).
    • use F10 to see the previous error message (printed in the given error-format).
How to use
Open the program using vim, write the code and compile using by pressing F9. To navigate through the errors, use F10 and F11.
Using this, you can only compile your code, it doesn't execute (Of course javac doesn't execute your java program). Use :!java command from the vim editor to execute the compiled program (as mentioned in the starting of the post), or instead use the java command on the command line (after exiting from vim editor). 

Hope this post helped you with the commands. Happy Learning!!!

Friday, April 17, 2015

Java Management Extensions - JMX

Java Management Extensions is a way to create management interface to Java applications. In brief, adding extra wrapper (MBean) to your application with a name (ObjectName) and register it on management server MBeanServer, so that it can be accessed/managed externally. JMX allows the developers to integrate the application by assigning management attributes/operations.

Instrumentation

To use JMX, we need to instrument the Java classes (or resources). The instrumented objects are called MBeans. MBeans are nothing but POJOs which adhere to JMX specifications.

MBeanServer

MBeanServer is the very core component of the JMX. MBeanServer is the JMX agent which hosts the MBeans on it and make them available for the remote applications. The MBeanServer need not to know how the MBeans are implemented and vice-versa. 

Accessing MBeans

MBeans running on MBeanServer can be accessed using Connectors, typically called as JMX Connectors, using JMX Service URL. The main intention of the connector is to provide an interface to establish communication between JMX agent and the application which is trying to access the MBean. There are various connectors available, but it always recommended use a standard RMIConnector.  

JMX Service URL

The URL is used to connect to MBean agent and locate MBeans to acess. There are two types of URLs depending on how the JMX is implemented on broker.
  • Static URL: As the name suggests, Static URL will create constant identity for MBeans irrespective of when and how broker is started, stopped etc. This uses RMI Connector.
  • Dynamic URL: Dynamic URL keeps changes for each time when the broker is started/re-started.

URL looks like

service:jmx:rmi://hostname:port/<urlpath>
The explanation goes here
  • service:jmx:rmi is constant
  • hostname:port is the name and port of the broker host where the MBean agent resides (Ideally, where the application is running)
  • urlpath is depends on where Service URL is static or dynamic
    • Static URL looks like : /jndi/rmi://hostname[:rmiPort]/jndiname
    • Dynamic URL looks like : /stub/serialnumber-with-base64encoded

Static JMX Service URL

Static URL contains the details of the RMI registry like hostname, port number and the connector name 
/jndi/rmi://hostname[:rmiPort]/connectorname 
  • hostname[:rmiPort] : RMI host and post number to locale JMX agent. By default, RMI port will be 1099 (if not specified). 
  • connectorname specifies the name which needs to be look-up in the RMI registry.
See the post, how to implement JMX MBean using Spring

Dynamic JMX Service URL

The URL consists of the serialized JMX object with Base 64 encoded value. See an example below
/stub/rO0ABdmVyLlJlpIDJyGvQkwAAAARod97VdgAEAeA==
We will see how to create JMX MBeans using dynamic URLs and how to access.

Happy Learning!!!!