How to connect MongoDB using Java

How to connect MongoDB using Java

Here are the steps to connect the MongoDB from the java class.

Obviously this is not all you are looking for, this will provide you a basic idea of connecting a mongo DB using java. The easiest DB connection so far I have seen is this. All you need to do is download the driver class or include it in your pom dependency and create a main class with two method calls.

You are ready to go !!!

Step 1: Download / Add your pom dependency.

pom.xml looks like this

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">

Step2: Java class looks like this



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

public class Main {

* @param args
* @throws UnknownHostException
public static void main(String[] args) throws UnknownHostException {

MongoClient client = new MongoClient( new ServerAddress(&quot;localhost&quot;, 27017));

DB db = client.getDB(&quot;DBNAME&quot;); \\ Usally there is always a DB with name test

DBCollection collection = db.getCollection(&quot;COLLECTIONNAME&quot;); \\ You can easily create a collection by going to Mongo Shell.

DBObject document = collection.findOne();



Try to run this main program and you would be able to see the output of the Collection results on the console.

Hope this was helpful. Thanks

Other Useful references :

MongoDB Commands Help

MongoDB University Where you can attend MooC

What is IoT ( Internet of Things ) ?

What is IoT ( Internet of Things ) ?

internet of things
Internet of things


We keep hearing about this new thing called IoT and Big Data. Let’s discuss the primer for today. I will try to give my understanding of IoT ( Internet of Things). There is nothing new about Internet. We all know what is internet and how we can use this for our daily life. The simplistic definition would be, we can’t imagine the life with out internet. It is part of our life. I love this image, which says “Internet is our basic need”.

Internet - Basic Human Need
Internet – Basic Human Need

Courtesy: Google ( I really don’t know who created this image)

Now, let’s get to our main point of today – IoT

Now, we are talking about Machines coming into Internet. So, each and every Machine/ Device / Thing will be connected to Internet. Let me try to give some real example I felt in my life. When I switched my internet vendor /Provider 2 years back the guy told me our Wi-Fi router can support 10 devices. I really felt 10 is too many that time as I was having only 1 smart phone at that time and 2 laptops. If I look back the same thing now, 2 smart phones (me and Wife), 1 tablet, 2 laptops, 1 kindle and the list may go up very soon… like Smart TV, smart Fridge, smart Air Conditioner, so on….  what all these devices doing on Internet and Why?

Personal scenarios:

Lets take some examples why you need your Fridge / Air Conditioner / TV on internet.

You have started from your office at 5 in the evening of a summer day. you need to travel 30 mins to reach home, you want your house cozy before you reach there. If your A/C is in internet, you would be able to switch it on when you start through your smart phone.

Your colleagues asked you for a party which is planned suddenly in office today. You want to have ice cubes ready before you get your friends home. If your Fridge is in internet, you would be able to set temperature before you start from office, and your ice cubes will be ready for the party.

Business Scenarios:

Lets think you manufacture a UPS/ Cooling unit/ Fridge / TV etc, if you don’t have this smart feature in your product there could be potential problems as below. Firstly customers might not be interested to buy your product as competitors already provide this feature for little more price. Whoever could effort little more money would buy that. Secondly, you won’t be able to analyse your product correctly. If you have this feature in your product you would be able to receive statistics of your product every second or minute. This will enable you to understand the usage of your product more and the problems in your product as well. So, You would be able to serve your customers what they are looking for.

So, As you know this is not a new thing at all. We already use this for many things in our real life. IoT is the new term tossed by industry to popularize this M2M (Machine to Machine) connectivity.

Reference: Here are some really good articles about IoT you would like to read.

IoT Blog – Schneider Electric

Cisco IoT Blog

Hope this was helpful.

Happy reading 🙂

How to explain Cloud Computing to your Granny ?

How to explain Cloud Computing to your Granny ?

In my previous post I explained the characteristics of cloud computing, perhaps people say if you know something very well you should be able to explain the thing to your granny. If you could do it successfully then you know about this technology well.

I consider this correct, So I gave this attempt. Lets hope this will workout well for you as well. I hope this would be useful for some of you as well in understanding the concepts.

Lets discuss about the Service Models of Cloud computing. IaaS, PaaS, SaaS.

Lets try to explain these models to our Granny 😉

IaaS (Infrastructure as a Service)

If you have to build a house, what is that you need before you start constructing the house ? Foundation – which is the basic need to build in our case this is  If you don’t have sufficient infrastructure at your end, you will hire the Infrastructure from these cloud companies.

If you don’t have sufficient time and energy to build the foundation of the house, you will hire the labor to build it and pay them hourly basis. The same way you will pay for the infrastructure you have used either based on the number of cores / memory utilized.

Eg: Rackspace, VMWare

PaaS (Platform as a Service)

If you don’t want to build house or you don’t have budget to build a house what you would do? Most likely you would rent a house … right ? This is much cheaper option and you will get a ready house where you can live in immediately. still you might have to spend some money on furniture like Sofa, Fridge, TV, etc.

In the same way, the service provider would provide you the platforms like Google App Engine, Microsoft Azure, Red Hat’s OpenShift, provides the platform to you where you can write your own Apps and add-ons using given platform. These platforms offer you the basic building blocks for you to start your work quickly and scale easily.

SaaS (Software as a Service)

If you take the previous example, you might think of taking a fully furnished apartment for rent. So you don’t even need to develop your own software on the platform. Just use the software and pay as per you use. This again has many models based on the application for charge back, some applications charge per size, some per request etc. Like based on the furniture and amenities you have in your house the rent will be charged. Eg SaaS services are  Microsoft’s Office 365, and millions of software.

Hope you can try explaining cloud computing to your grand parents too.

Happy reading 🙂

Is Service Orientation a need for today’s Enterprise ?

Is Service Orientation a need for today’s Enterprise ?

As part of my learning, I started the SOA ( Service Oriented Architecture ) in the recent past. I started reading some blogs, sites and books about SOA to understand the basics and why we need to go towards SOA for today’s enterprises. I would like to share my ideas and knowledge I got in this process. Hope this would give you some pointers to start your journey of SOA.

What is Service Orientation :  Wiki tells us that, this is a paradigm to build software in form of services. We would take it further and can tell – “Think all logically divisible parts of your program as service”. Lets take a real time example for better understanding.

Case: Lets take a house which is having a 1 Hall, 1 Kitchen and 2 bed rooms (of-course couple of bath rooms as well). If you think in terms of services what are the services you could think of to form a house.

  • Electrical Wiring
  • Plumbing
  • Doors
  • Windows
  • Taps
  • Microwave
  • Fridge
  • Shower
  • Etc.

All these things together make a house. So, shall we think all these things as services?

Why not ?

If we have these many services, are we saying our house as SOA implementation ?

Now, what does SOA principles has to say about this –

Very similar to SOLID principles in Object Orientation, SOA has their own SOA principles. We have to look at them and figure out our model is matching with the SOA principles. You might think we already have these services as web services then SOA is the same. Are web service is the only way to achieve SOA ? that is a different question to answer but to be precise the current/contemporary way of implementing SOA is web services (May be in future we might have something different). Anyway, Lets get back to Principles of SOA.

Don’t get puzzled if they look very familiar 🙂

Loose Coupling: The services defined should maintain a relationship that minimizes dependencies, and only requires the awareness of each other.

Service Contract: All services should adhere to a service agreement. This could be a service description of a single or multiple related services. Services should do only whatever is mentioned in the contract.

Autonomy: Services should have a control over the logic they encapsulate. This means that each service should do the specific task and it should not do anything more than that.

Abstraction: Beyond what is described in the service contract, services hide the logic from outside world.

Reusability: Logic is divided into services with the intention of promoting reuse.

Composability: Collection of services can be coordinated and assembled to form composite services.

Statelessness: Services should minimize/zero retaining information specific to an activity / state.

Discoverability: Services are designed to be outwardly descriptive, so that they can be found and accessed via available discovery mechanisms.

Considering these principles, If you design any SOA system that would be considered as better architecture of your application. So, I feel these are the needs of current enterprise applications. And SOA can give a perfect solution if you are able to see the needs through your Service Orientation lenses.

Here are some Good Reads about SOA:

Book: Service-Oriented Architecture: Concepts, Technology, and Designjoi (Prentice Hall Service-Oriented Computing Series from Thomas Erl)


SOA in a Nutshell by IBM Developer works

New to SOA and Web Services?

Another Good start would be –

Hope this read was helpful, Happy learning and reading !!!

How to create a Quartz Job?

How to create a Quartz Job?

The Quartz Jobs can be created with a very simple program by just adding the required libraries to the class path of your project. Here I will try to explain two different ways to configure a Quartz job. This complete post is divided into 3 sections.

Section 1: Common steps for both the approaches

Section 2: Simple Job with out any XML/Properties file involved

Section 3: With XML files and properties files to manage the Jobs.

So, lets put our hands in dirt.

Tools you need before you start this example: Eclipse Juno/Kepler.

Section 1: Common steps to create a Quartz Job/Scheduler

Step 1: Download the Jars for Quartz either from Maven or manual download.

Step 2: Create a Java project  {I am going with older approach}.

Step 3: Refer your downloaded jars in your class path (by Right Click on Project -> Build path -> Add External Archives ) See screen shot below, You could even use jta-1.1.jar instead of jboss-transaction-spi. But don’t miss this jar as this is a mandatory dependency for Quartz 2.2.1.


Note: Once these steps are over either you can choose Section 2 or Section 3. [I recommend Section 3]

Section 2:  Simple Job using Java Code

Step 1: Create a class which implements org.quartz.Job interface.

Step 2: Implement execute() method of this interface.

Step 3: Write your business logic inside this execute method i.e. what has to be done when your job gets executed.

See below code as example

package com.ravi.schedulers;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class ExampleQuartzJob implements Job{

    public void execute(JobExecutionContext arg0) throws JobExecutionException    {
       // TODO Auto-generated method stub
       System.out.println("Logic Goes here........");

Create another class.

package com.ravi.schedulers.test;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class TestBJob {

    public static void main(String[] args) {
        try {
            // Create Job details 
            JobDetail job = JobBuilder.newJob(TestJob.class)
           // Create the trigger with running interval of the job
           Trigger trigger = TriggerBuilder.newTrigger()
          //This is how you start the job
          SchedulerFactory schFactory = new StdSchedulerFactory();
          Scheduler scheduler = schFactory.getScheduler();
          // tricky step, if you don't start the scheduler, it won't run.
          // This is the thing which will run your Job at the triggered time/interval.
          scheduler.scheduleJob(job, trigger);
          //Use it to stop the scheduler. 
      } catch (SchedulerException e) {

Here are the next steps ….

You might get some warning as below as I missed the dependency of slf4j –

SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.
SLF4J: Defaulting to no-operation (NOP) logger implementation

But the program will run as it is not a mandatory dependency. If you want to avoid warning please add this jar as well in your references.

Lets move on to other section.

Section 3: Scheduler Jobs with XML, Properties files.

Step1: Create a XML file with name quartz-config.xml (You can even use another name :), just for identification I am using this name.)

<?xml version="1.0" encoding="UTF-8"?>


<description>This is Test Job</description>
<!-- It will run every 2 seconds -->
<cron-expression>0/2 * * * * ?</cron-expression>

Step2: Create a properties file with name, in the same folder.

org.quartz.scheduler.instanceName = ExampleQuartzJob
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.plugin.jobInitializer.class =org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = com/ravi/schedulers/quartz-config.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true

Once you are done with these two changes, you have to create a main method class to test this job.

package com.ravi.schedulers;

import javax.naming.NamingException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzRunner {
     public static void main(String[] args) throws SchedulerException, NamingException {
         // Creating scheduler factory and scheduler
         SchedulerFactory factory = new StdSchedulerFactory(
         Scheduler scheduler = factory.getScheduler();
         // Start scheduler

This is how you can create a scheduler job using Quartz API. Hope this was helpful and useful for your work.

If you are using Spring-Boot, your scheduling job become dam easy….

Here is how you need to configure your scheduling in Spring boot.

Use @EnableScheduling annotation at your application class/ Main program of Spring boot.

Then simply put @Scheduled(fixedDelay=150000) on your method which need to be scheduled.

For Example, I want to update my Log data into DB every 15 seconds. Here is the code for it,


public void updateDBLogs() {

// Read log file

// apply business logic

// insert into DB


There are many options than fixed delay for scheduling … you can even try Cron job like this.

@Scheduled(cron = “0 0 0/1 * * ?”)

Happy Scheduling 🙂

Code Review, Is it worth spending time?

Code Review, Is it worth spending time?

Recently, I have been asked to check and deploy a process for Design and Code review in many different projects. All projects already have this process in place either officially or un-officially. When I said process some Engineering managers said, do we really need this as a process? We are already doing it on a need basis, and we have static code analysis tools in place. So, is it worth wasting two developers (Reviewer, Author) for Code review ?

What tools can do? 

  • Check the code follows conventions like lines of code/ per method
  • Variables or method naming conventions 
  • Unit test coverage %
  • Find Duplicate Code
  • Cyclomatic complexity [Some smart tools does this too…]

Then I thought what HUMAN brain can do, NOT any tool, 

  • Have the customer requirements met ? 
  • Possible use of a Design Pattern [ Strategy, Decorator or Observer ]?
  • or a Design principles like SRP, OCP or LSP ? 
  • Is it an Anti pattern ? 
  • Use of existing library [ gson, Jackson, etc]
  • Domain Knowledge
  • Project Experience [ very important to understand the implications and regressions of the code ]                                                                                                                                   last but not least 
  • Knowledge Sharing

Your list can grow much bigger than what I have written. I feel though you have Analysis tools of your code and they definitely help you improve your code, you need somebody who is senior to you in the project and technically sound to have a look at your code and give you a fair way to improve your code. 

Please share your thoughts and experiences. 

Enterprise Architecture Frameworks

Enterprise Architecture Frameworks

In the current world of complex Enterprise software systems, we might not be able to design, build and document the Architecture properly. The Enterprise Architecture Frameworks help us do all these steps efficiently. They provide tools, approaches and processes defined for us to follow and reach our goals efficiently and on time. Brief History of Enterprise Architecture (EA) Frameworks In 1987 John Zachman published a paper on “A Framework for Information systems Architecture” which brought this topic into light and after that many people suggested many different frameworks and altered the Zachman’s proposal. All these frameworks at high level talk about the components which should be streamline the Architecture development and the documentation process. Important Components of Enterprise Architecture frameworks are as below

  • Provide Architecture vision
  • Design Business Architecture
  • Design Technology Architecture
  • Implementation of Governance
  • Architecture Change Management

Lets try to understand these frameworks in a bit detail.

Zachman Framework: 

The oldest among all these framework is Zachman as he is the one who came up with the idea of Architectural Process to reduce the complexity of existing systems and their integration, as well as the lack of documentation on the Architecture.  The highlights of this framework are

  • Analogy behind this Framework comes from Building Industry (Civil)
  • Players – Owner (Who pays for the project), Builder ( Who co-ordinates the things) and Control board (Who ensures the construction follows the local regulations)
  • Focus of the Artifacts: What, How, Where, Who, When and Why, and Players to Artifact mapping. Eg: Owner and Builder should know “What” but only Builder would know “How”. Only Owner might know “Why”.

The matrix of Zachman Framework is as below.


TOGAF (The Open Group Architecture Framework) :

This Framework divides Enterprise Architecture into four different parts based on the expertise on their fields.

  • Business Architect
  • Application Architect
  • Technical Architect
  • Data Architect

Though this is told as Architecture framework it works mostly as a development method for Architecture. This is a process of creating artifacts for your architecture. This process drives to create proper documents at each phase. Below are the phases involved in the TOGAF process.


So, the above way the life cycle of an Architecture will Iterate. There are some certifications also available for both Frameworks, Check them out if you are looking for one -> TOGAF and Zachman.

Hope this was helpful. Happy Reading.