Thursday, January 20, 2011

Tuesday, January 18, 2011

Cloud and Multitenant Architecture and thier Usage

Cloud and Multitenant Architecture and thier Usage

Looks like there is a confusion or misperception on how Cloud and Multitenant things work together. One thing we need to understand that if I develop any application which can be deployed in cloud doesn’t automatically means that the application is multitenant. At the same time if I develop any application which is multitenant doesn’t automatically means that it can be deployed on cloud.

So first lets understand what exactly these two things means before going into how these two complements each other.

Cloud: Cloud provides a readymade infrastructure to host your application along with the ability to increase / decrease the hardware resource requirement based on the requirement. Also there are multiple vendors providing this infrastructure.

Some of these providers are:

* Microsoft Azure
* Google Apps
* Force Platform (from Salesforce)
* …

Now many of these platforms provide good support for specific languages.

Multitenant Architecture: Multitenant architecture is needed when we develop any software to handle a domain requirement which is applicable to multiple customers. In that situation the configurability of the software is more important than customizing of the software (in some cases it cannot be avoided but needs to be handled differently). Majority of the time a single codebase provides services to all the customers. And at application and data storage level, care is taken to make sure that data from Customer A is fully secured from Data from Customer B. Also if there is requirement for any customization for a particular customer, there are patterns to handle this.

Some of the examples of Multitenant Applications:

* SharePoint Online
* 37 Signals Applications
* Accompa for Requirements
* …

As far as Multitenant Architecture is concerned, this itself is a BIG topic for which we will find many resources on the web which talk about multitenancy at database level, code level etc.

Usage: Now the question is how these two complements each other. Now we understand that multitenant architecture is to support multiple customers with the single codebase and data structure. Which means that it doesn’t matter if the customer count grows from 10 to 100 as our application can support it any time. However the questions is what about hardware infrastructure. This is where the elastic nature of the cloud comes into the picture and provide the necessary infrastructure which is required for scaling of the business.

E.g. Just by changing the configuration file we can change the number of instance of our application hosted in the cloud. At the same time if the business is not doing good we can very well reduce the resource usage and pay as per the requirement.

In cloud computing, multi-tenant is the phrase used to describe multiple customers using the same public cloud.

The idea of this blog is not to explain what is the best approach to achieve multitenancy.And make sure that we understand the difference between Multitenancy and Cloud and how they can used to get best of that.

Wednesday, April 7, 2010

HotSpot JVM Options

To learn more about the HotSpot JVM options –

I found a great resource that lists various JVM versions and the corresponding options -

Tuesday, January 12, 2010

Interface injection using Spring

I am adding one more sample for Interface Injection using spring. Spring directly does not support interface injection but there is one thing called autowire, that can be used in spring to achieve some sort of interface injection.

Consider below sample Java classes:

1. - interface which has getter and setter for String object

public interface Text {
void setTextString(String textString);
String getTextString();

2. - implements above Text interface

public class MyText implements Text {

private String textString;

public String getTextString() {
return textString;

public void setTextString(String textString) {
this.textString = textString;


3. - which has getter and setter for above Text object

public interface Service {
void setText(Text text);
Text getText();

4. - implements above Service Interface

public class MyService implements Service {
private Text text;

public Text getText() {
return text;

public void setText(Text text) {
this.text = text;

5. - which has main method to test interface injection

import org.springframework.beans.factory.xml.XmlBeanFactory;
import static java.lang.System.out;

public class MyServiceApp {

public static void main(String...strings) {
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
MyService myService = (MyService) beanFactory.getBean("myService");
out.println( myService.getText().getTextString());

Now, in above sample to get interface injection major file is configuration file(myservice.xml).

Look at below configuration

Configuration file: myservice.xml

<beans xmlns=""

<bean id="text" class="MyText" p:textString="My First String...!"/>

<!-- <bean id="myService" class="MyService" p:text-ref="text"/> -->
<!-- The above can be used if you want to inject the dependency yourself-->

<!-- The below can be used to leave the dependency injection decision to the IoC container -->
<!-- <bean id="myService" class="MyService" autowire="byName"/> -->

<!-- The following line is also almost same as the above except for this case IoC will
detect dependency by type -->
<bean id="myService" class="MyService" autowire="byType"/>


In above sample you can configure interface injection by two ways:

1. <bean id="myService" class="MyService" p:text-ref="text"/>

Here, injection is done by yourself by giving reference to another bean object.

2. <bean id="myService" class="MyService" autowire="byName"/>

Here, injection decision is taken by IoC container as you have specified autowire attribute. autowire value can be set as byName or byType.

Thursday, January 7, 2010

Constructor Injection using Spring

I wrote about Spring & Setter Injection in my previous blog now I am adding one more sample using constructor Injection. Constructor injection is almost same like setter injection except only difference that here IoC container initializes the constructor parameters.

Consider below sample java class.

Java Class:

import org.springframework.context.ApplicationContext;

public class TestConstructor {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"constructorinjection.xml"});
Inject inject = (Inject) context.getBean( "myInject" );
System.out.println( inject.getName() );

class Inject {

private String name;

public Inject(String name, String text)
{ = name + text;

public String getName() {

In above class constructor parameter values(name & text) would be injected from constructorinjection.xml configuration file.

Configuration file: constructorinjection.xml

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

<bean id="myInject" class="Inject">
<constructor-arg type="java.lang.String" index="1">
<value>Welcome to example to constructor injection </value>
<constructor-arg type="java.lang.String" index="0">
<value>A text </value>

Here, </constructor-arg> has two optional attributes type and index. Type is not significant in this particular example but can be useful when using some other complex types. Specify type value based on your constructor type. Index is also one useful mechanism to be used within. Based on index value it assigns value to constructor parameter.

In above sampe output is:
A text Welcome to example to constructor injection

Now if you change index vaue 1 to 0 and 0 to 1 output is:
Welcome to example to constructor injection A text

If you have noticed I made one more change in this sample compare to my previous blog. I used ApplicationContext instead of BeanFactory. What is the difference? We will now see why ApplicationContext. The first thing you can notice is that you can (of course optionally) pass more than one XML bean definition/configuration files. Ultimately we are assigning the ApplicatioContetx instance to BeanFactory.

Wednesday, January 6, 2010

Use CodeModel to generate Java Source Code

CodeModel is a library that allows you to generate Java source code in a type-safe fashion.

When to use CodeModel:

If you have huge chunk of data in terms of text file or XML and want to generate some Java class based on those data you can use CodeModel. It also helps when your data changes frequently.

To know more about the CodeModel visit following link:,r=1.601/jaxb-architecture-document/www/doc/com/sun/codemodel/package-summary.html

Giving you small sample how to use CodeModel to generate your own class.


import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JDocComment;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JPackage;
import com.sun.codemodel.JType;
import com.sun.codemodel.JVar;

* @author naman
public class CodeFactory {

// Method to get JType based on any String Value
public JType getTypeDetailsForCodeModel(JCodeModel jCodeModel, String type) {
if (type.equals("Unsigned32")) {
return jCodeModel.LONG;
} else if (type.equals("Unsigned64")) {
return jCodeModel.LONG;
} else if (type.equals("Integer32")) {
return jCodeModel.INT;
} else if (type.equals("Integer64")) {
return jCodeModel.LONG;
} else if (type.equals("Enumerated")) {
return jCodeModel.INT;
} else if (type.equals("Float32")) {
return jCodeModel.FLOAT;
} else if (type.equals("Float64")) {
return jCodeModel.DOUBLE;
} else {
return null;

// Function to generate CodeModel Class
public void writeCodeModel(String factroyPackage) {
try {

/* Creating java code model classes */
JCodeModel jCodeModel = new JCodeModel();

/* Adding packages here */
JPackage jp = jCodeModel._package(factroyPackage);

/* Giving Class Name to Generate */
JDefinedClass jc = jp._class("GeneratedFactory");

/* Adding annotation for the Class */

/* Adding class level coment */
JDocComment jDocComment = jc.javadoc();
jDocComment.add("Class Level Java Docs");

/* Adding method to the Class which is public static and returns com.somclass.AnyXYZ.class */
String mehtodName = "myFirstMehtod";
JMethod jmCreate = jc.method(JMod.PUBLIC | JMod.STATIC, com.somclass.AnyXYZ.class, "create" + mehtodName);

/* Addign java doc for method */
jmCreate.javadoc().add("Method Level Java Docs");

/* Adding method body */
JBlock jBlock = jmCreate.body();

/* Defining method parameter */
JType jt = getTypeDetailsForCodeModel(jCodeModel, "Unsigned32");
if (jt != null) {
jmCreate.param(jt, "data");
} else {
jmCreate.param(java.lang.String.class, "data");

/* Defining some class Variable in mthod body */
JClass jClassavpImpl = jCodeModel.ref(com.somclass.AnyXYZ.class);
jvarAvpImpl = jBlock.decl(jClassavpImpl, "varName");

/* Adding some direct statement */

/* returning varibalbe */

/* Building class at given location */ File("generated/src"));

} catch (JAXBException ex) {
logger.log(Level.SEVERE, "JAXBException:" + ex);
} catch (Exception ex) {
logger.log(Level.SEVERE, "Other Exception which in not catched:" + ex);

// Wirte main mehtod and call writeCodeModel("com.test") function to generate class

After running above class it generates GeneratedFactory class under generated/src/com/test folder. It includes all required imports and also format the class as per Java Standard. It generates as described below.

Generated Class:

package com.test;

import com.myannotation.AnyXYZ;
import com.somclass.AnyXYZ;

* Class Level Java Docs
public class GeneratedFactory {

* Method Level Java Docs
public static com.somclass.AnyXYZ myFirstMehtod(long data) {
com.somclass.AnyXYZ varName = new com.somclass.AnyXYZ();
return varName;

Tuesday, January 5, 2010

Java Design Patterns

You can go through below link to get more idea on Different design patterns. You must have to try sample also there.


I will add more details on the same later on.