May 3, 2014

Comparable and Comparator

In this tutorial we will see how to use two interfaces Comparable and Comparator for sorting collection data. When to use which interface and what if both the interfaces are used in same application. 

So lets start with Comparable. This interface has declaration for only one method compareTo(T a) where T is a type object. For better clarity we will create a bean class ComparableEmployeeBean that has few instance variables to store employee information like id, first name, last name, date of birth. Since this class is implementing interface Comparable we need to provide implementation for compareTo method. This method provides a logic to sort ComparableEmployeeBean(s) in a Sorted collection e.g. SortedSet, TreeSet etc. So following is our bean class with implementation that will sort employees based on their employee id.

From this code we can understand following about Comparable

  • When we want to have default sorting for some bean class we will implement Comparable.
  • When code bean/VO to be sorted is available to us we can implement Comparable.

So now we will insert few employees in this bean class and display them. Following code will insert three employees and add them in a sorted set.

This code will print following output.

[1:Foo:Bar, 2:Foo:Baz, 3:Boo:Baz]

There is a word of caution here. If your implementation returns 0 for comparison between bean to be added and any bean in collection, these two beans will be treated as equal and new bean will not be added in Collection. So in our example if we try to add another employee with different first name and last name but same employee id, Set will assume that this record is already present and will ignore new entry. Interested user can trying adding employee with id 2, name Bar and last name Baz. So remember implementation of Comparable (or even Comparator) should be in line with implementation of equals method of our bean/VO class.

So now lets see what is another interface Comparator. Comparator interface too will have method compare with two arguments. So class that implements this interface will provide logic external to bean/VO to be compared/sorted. So lets create a class that will use Comparator interface to sort employees based on first name and last name. 

This implementation will override the default behavior implemented by Comparable. So if we initialize SortedSet with this Comparator, it will sort employees based on their first name and last name. So following is the code.

This code will result in following output.

[3:Boo:Baz, 1:Foo:Bar, 2:Foo:Baz]

We can say following about Comparator.
  • It provides sorting logic out side the bean.
  • It will over ride default behavior of bean achieved by implementing Comparable.
  • When there is no access to code for bean class Comparator will be default option for sorting.

Mar 14, 2014

Monitoring Akka Actor in Scala (basic introduction)

In this tutorial we will see how we can add a watch on a child Actor (i.e. get a notification when child Actor is killed). Refer following code.

It will give following output.

Will monitor actor here
[ERROR] [03/14/2014 00:29:18.190] [] [akka://ActorWatchSystem/user/parent/child] Boom....
java.lang.Exception: Boom....
       at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:230)
       at akka.dispatch.ForkJoinExecutorConfigurator$MailboxExecutionTask.exec(AbstractDispatcher.scala:506)
       at scala.concurrent.forkjoin.ForkJoinTask.doExec(
       at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(
       at scala.concurrent.forkjoin.ForkJoinPool.runWorker(

Got handle to child actor
Child Terminated
Shutting down system

Now let's see how this code works. In order to understand this we will go back and forth in our code.

-> From line 29 to 40 we are creating an Actor class called Parent. When this class is created it will create a Child Actor using context (line# 30) and will add a watch on this child (line# 31). Since we have added watch on child if this child is terminate notification/message will be sent to Parent and receive method will get a message Terminate(ActorRef) so line# 37 will be executed. This Parent Actor will be called by our main method and this Actor will first get the message. Once it gets method it will print the message and send it to Child Actor (line# 35). If message is anything other then text it will simply print text "Parent got a message" without any processing.

-> Line# 42 to 48 shows our Child Actor (which is created by Parent Actor on line# 30). This class will get message from parent. We have also over loaded life cycle methods preStart, postStop, preRestart and postRestart of this Actor class. When this Actor gets message from parent (and when message is received receive method of this class will be called) it will check if this message has some text or not. If this message is a blank message it will throw an Exception which in turn will force actor to restart and hence its preRestart and postRestart methods will be called.
If message is anything other than String it will simply print text "Child got a message" (line# 60)

-> In our main method (line# 10 to 28) we are creating our ActorSystem (line# 12) then creating an instance of Parent Actor (line# 13) with name parent. We will pass some text messages to this Actor (also by passing blank message we will force it Child Actor to restart on line#15).

-> On line# 18 we are getting access to Child Actor's reference directly and we are giving this class a PoisonPill on line# 20 which will kill this child class once it has processed all message it has received before getting PoisonPill.

-> Once all operations are completed we will shutdown the actor system. If your system is continuously processing data then you need not to shutdow the system.

So this was brief introduction to Akka using Scala for Java developers. Interested users can try developing application where they are continuously getting some data from some stream and wants to process this data in parallel by using Akka Actors.

Happy coding. :)

Mar 12, 2014

Introduction to SBT (Scala Build Tool)

SBT is abbreviation for Scala Build Tool. This tool is a great help if you are a newbie in Scala like me. Its simple yet very powerful as far as Scala projects are concerned. In this tutorial we will see
  • How to create a small project (first without any dependency and later with dependency on some external jar files)
  • How to compile our code with sbt tool
  • How to run this code with sbt
  • What will be folder structure of our say decently complex project and how to segregate our main code and our test cases code.

So first lets start with installation of this tool. This tool can be downloaded from here. Once you download the tool extract the zip file on your local machine and set environment variable SBT_HOME to location where the file is extracted. Also add %SBT_HOME%/bin in your path so that you can run sbt command from anywhere you want.

Now go to directory where you want to write and run your Scala code and create a file HelloWorld.scala in this folder as follows

Once this file is created, open DOS shell and go to this folder and execute following command. 

sbt run

This command will not require any build file. It will automatically compile as well as run the HelloWorld.scala as shown in image below.

So now we know about basic sbt command so now we can look at few more commands and bit complicated code that will require some third party jar that sbt will try to download from maven/ivy repository.

Mar 8, 2014

Quick start with Scala for Java Developers

Let’s say we have number 1 to 100 in a list we will see how to perform different operations on this List in Java and Scala.

Add numbers 1 to 100 in a list
Java Way of doing it
Scala way of doing it
public static void main(String[] args) {
       List<Integer> list = new ArrayList<>();
       for(int i=0;i<=100;i++){
val numbers = (1 to 100).toList

Get the sum of numbers
Java Way of doing it
Scala way of doing it
int sum = 0;
for (Integer num : list) {

Get the sum of even numbers
Java Way of doing it
Scala way of doing it
int sum = 0;
for (Integer num : list) {

Get the sum of odd numbers that are greater than 15
Java Way of doing it
Scala way of doing it
int sum = 0;
for (Integer num : list) {
       if(num%2==1 && num>15){
println(numbers.filter(x=>(x%2==1 && x>15))

From above examples we can see that by passing function to a list we can reduce the line of code that we need to write. This also have an advantage when code is running in multithreaded environment. I will keep adding similar examples for comparing Java and Scala in this tutorial.

Mar 2, 2014

Quick start MongoDB MapReduce with Java

In this tutorial we will how to use MapRedulce feature of MongoDB with java. First we will see few commands of MongoDB and then we will connect this DB with Java Connector and perform basic MapReduce feature and then a slightly more complex MapReduce function. So first lets start with MongoDB.
Download latest version of MongoDB from One which I have used for this tutorial is  Now extract this file on local machine.
Go to %MONGODB_HOME%/bin and execute command mongod –help this will show options for starting instance on MongoDB.

So we will start instance by executing following command. We will pass location where the data will be stored by using --dbpath as follows.

You can also see files created at this location.

Now in another window we will start mongodb console by executing command mongo as follows.

Now we will use following three commands that will show us what databases are present in MongoDB (show databases), how to select particular DB (use <db_name>) and how to check what tables exists in particular database (show tables).

Now we will download driver for connecting to MongoDB from and add this jar in your Eclipse project. Also we will create a utility class that will create a connection to MongoDB and will create a DBCollection (Object where data will be stored in mongo db). So following will be our utility class.
package com.techcielo.mongodb.util;


import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class MongoDBUtil {
       public static DBCollection createCollection(String name,boolean forceRecreate){
              try {
                     MongoClient client = new MongoClient();
                     DB db = client.getDB("test");
                                  return db.getCollection(name);
                     DBObject mydata = BasicDBObjectBuilder.start().add("capped", true).add("size", 200000000l).get();
               DBCollection collection = db.createCollection(name, mydata);
               return collection;
              catch (UnknownHostException e) {
                     // TODO Auto-generated catch block
              return null;

Here since we have only one instance which we want to connect so we are not passing server list also since it is on local machine (localhost) we are also not passing server name and we use default Constructor to create instance of MongoClient. Second thing we will call getDB method on MongoDB to connect to test database of MongoDB.
Once this is done now we will insert some data in this collection. So we will insert following data in this collection. We will fire following query in our open source database northwind to get the data that we will insert here.
SELECT od.OrderID, ProductID, UnitPrice, Quantity, CustomerId, EmployeeID, ShipCountry
FROM order_details od
JOIN orders o ON o.orderId = od.orderId
LIMIT 0 , 100
So here I will just create one class that will give me list of bean with appropriate data, leaving it to user how he/she wants to load the data e.g. by hitting DB or by flat file anything that he/she likes. We are interested in end result. So following will be our class.

package com.techcielo.mongodb.main;

import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.techcielo.mongodb.bean.OrderBean;
import com.techcielo.mongodb.util.DBUtil;
import com.techcielo.mongodb.util.MongoDBUtil;

public class Main {
       public static void main(String[] args) {
       public static void insertData(){
              List<OrderBean> orderlist = DBUtil.getOrderData();           
              DBCollection coll = MongoDBUtil.createCollection("orderdata",true);
              for(OrderBean order:orderlist){
                     BasicDBObject obj = new BasicDBObject();
              System.out.println("Total Count:"+coll.getCount());
When this code is executed following will be output

Total Count:99

So now we have inserted the data now let’s check this data with MapReduce. So now let’s find total order value of each order id.

public static void getMapReduceData(){
              DBCollection coll = MongoDBUtil.createCollection("orderdata", false);
              String map = "function() {emit(this.orderId,{cost: this.quantity*this.unitPrice});}";
              String reduce = "function(key, values) { "
                                         + "var sum = 0; "
                                         + "values.forEach(function(doc) { "
                                         + "sum += doc.cost;}); "
                                         + "return sum;} ";        
              MapReduceCommand cmd = new MapReduceCommand(coll, map, reduce, null, MapReduceCommand.OutputType.INLINE, null);
              MapReduceOutput out = coll.mapReduce(cmd);
        for (DBObject o : out.results())
              Long orderid = (Long) o.get("_id");
              Double cost = 0.0;
              Object obj = o.get("value");
              if (obj instanceof DBObject) {                 
                           DBObject new_name = (DBObject) obj;
                           cost = (Double) new_name.get("cost");
                     cost = (Double) o.get("value");

And call this method from your main class after loading data from northwind database. So following will be output of this method