Archive

Archive for the ‘spring framework’ Category

#LeanDevelopment for #LeanStartups

March 23, 2011 1 comment
To align with #LeanStartups way of doing things, we also need lean development,  lean technical stack and lean project cycle. Startups are big news these days. And when we talk about startups, there is big push for quick turn-over and get to market before time & money runs out. Hence we talking about #LeanStartups. (If you wondering, why I am using ‘#’ sign, because, that’s how I am getting my topic news on Twitter. So keeping up lingo).


Coming to topic of this blog, whenever I think of implementing some idea or start chalking up some plans to develop something, first thing comes to my mind, which web framework? Which technology stack of APIs. How I can get other developers involved with-out spending time on discussing these things. When we talk about Lean startup, it means you should be able to develop some features quickly and deliver it. As user experience ‘demands’, you are agile enough to change backlog continuously, with-out changing technologies or whole project direction.

Keyword is ‘Continuous’. (This explains it better, http://goo.gl/q6Pnk)

  • You change backlog continuously
  • You build continuously (every commit)
  • You deploy continuous. (even 5-6 times a day)
Now problem is, when we start spending time developing code using traditional frameworks (or technical stack), then we are introducing rigidness to whole continuous process. 
Example: Lets say, you are using Apache tiles + JSP + Spring framework + Hibernate, to formulate any development efforts. If you need to change DB schema, need to change JSPs & their layouts, then it will take a lot of effort to change everything.  That’s one of reason, I think, we have a lot of  PHP based frameworks for startups. Because they are missing layers of layers of configurations. (See my older post https://jframeworks.com/2008/07/03/java-products-but-php-based-website/ touching this subject)
So What I Propose?
Lean Development (& technologies)
(Note: This is written with Java/JEE APIs in mind)

  1. If you are in same boat as me, I have choosen ‘Spring Framework’. Spring Framework is not a framework, it has become underlying nerve of almost all project. It’s all IOC. 
  2. I recently dump technologies like ORM (Hibernate/JPA) in favor of old school JDBC (See my other post on Why?)
  3. I am sticking to JSPs and old school JSP includes. Why? Because my team should be able to introduce any jsp with minimal time and minimal impact. Any developer with little experience of java, can work around JSPs. 
  4. At last but least, standardize html technologies including CSS and JavaScript libraries.
Sample Recipe (If I choose my stack now)
  1. MySQL DB (or you can go with PostgresSQL )
  2. Spring Framework (JDBC, MVC, rich set of Annotations for transaction, cache, webservices )
  3. JSPs for displaying content. No templating framework. Plain JSP includes. 
  4. HTML 4.01 Strict transitional standardized UI
  5. JQuery 1.4.2+ (including JQuery UI and plugins as needed). Google it, you will find thousands
  6. Yahoo YUI CSS grids, to stanardized your grids once for all. (including reset css)

Why not PHP? 

Now speaking of PHP, yes, you can argue why not use PHP (hence products like Drupal)? That’s main concern with us (or me). Acting on a idea is not everything. Having passion to develop it with technologies you love and you are confident in it. I won’t discuss pros/cons of PHP v/s JAVA right now. I have friends, who are in same boat.  Many of them belongs to group, “Why you thinking technology? Just think of idea and pursue it. Just pick Drupal or any existing CMS application.” Yes this is what I been told many times. But I am technologist and I want to know about technology that will be used. I am java web developer with decade of experience. So with so many tested web frameworks available, sticking to java/j2ee technologies makes sense. I don’t advise, against PHP but this is something, doesn’t suit me or my time. Why learn new skills and figure out things, whenever you need to do something different. 
Lean Project Cycle: 
When we talk about this, we talk about all other tasks to give light to a written code. This involves everything like recording requirements, maintaining backlog, continuous builds & deploys.
Traditionally 
Generally, if we are bunch of developers, we think, of getting one build server and install some open source tools like ‘Hudson’. Also make maintain source control repositories using subversion ourselves.

  • Provision a development integration server.
  • Provision DB servers for different stages of a project
  • Provision a Unix box, for source control. 
  • And then find people, to install those and manage those. 

Welcome to world of PaaS and Saas

(Platform As A Service and Software As A Service)

If you have experience enough, we know, getting into maintaining tools ourselves, we will be putting hours, which we could have been writing code. Hence, I am in favor of, why not use online services? For example, altassian group provides execellent suite of all tools in one single subscription package. Link http://www.atlassian.com/hosted/studio/ .  Personally, I am big fan of JIRA and used Confluence. So I don’t want to waste my time, learning new tool or spend time, how to install/maintain it. There are lot of other paid subscription services available like http://beanstalkapp.com/ and many others. Point is, I will rather buy subscription for these services and use it.  (If I need my car serviced, I will drive to auto shop, rather than setting up a new one in my garage 🙂 )

Excellent examples of PaaS & Saas

  • Xeround, They are new kid on block, providing DB hosting as a service (SaaS model). I have experimented with their services. At least for startup or low volume work, they can be right fit. You can always do cost-benefit-DBA_salary-Ping_time analysis.
  • Atlassian Studio: Provide hosted services for Subversion/ JIRA / Bamboo, confluence wiki and even agile tools like Greenhopper. Frankly speaking, I am ok with spending $125/mo with them, if I am seriously spending my time for startup. Think of productivity. 
  • Amazon EC2, If you need to provision a server for any reason (or live production hosting) This is one very good option. I am using their micro instance for running my dedicated MySQL server. I even hosted wordpress blog there. (A week back, I moved to blogger.com) (To save some more bucks, I am using ‘Spot Instances’. )
Basically, you need Laptop and your favorite IDE to start a #LeanStartup. By the way, if you not using Maven for builds, you need to start from there. 

Why or Why Not AppFuse ?
For those, who don’t know, AppFuse is excellent open source project, which allows you to download & use it’s pre-built projects. It provides different projects with different technology stack with web interface. Yes, it is good starting point, when you want to see certain API in action and modify, experiment with it. But from my point of view, it’s more like proof of concept. It can be good starting point for you, if you starting from scratch, as long as it matches you pre-determined technical stack. For my own project, it doesn’t. I was spending more time, working around it, hence I decided to write some ‘generic’ web framework from scratch. Also adding different goodies like separate admin, user interface, cache support using annotations etc. Hopefully, I will release it, it’s first alpha release as open source soon.

End Note:
In this post, I tried to look at technologies side of #LeanStartups.  This is something, which can be black hole for many startups if not controlled to begin with. Programming languages like PHP can be easy step for day 1, but when things get serious, we need serious enterprise friendly language and tools supporting it. And Lean development model (as described above, using Java), can serve both purposes of starting easily and able to sustain long term.

Spring 3.1 with Cache and ORM technologies

Recently Spring Framework released Milestone 1 of version 3.1 (New features).  One of new key feature caught my eye is, support for cache using annotations. Now you can annotate your DAOs (or Repositories) with provided cache specific annotations and let spring take case of behind the scene bridge to cache provider.

To be fair, Cache using annotation is not new. It’s been available as part of open source projects like http://code.google.com/p/ehcache-spring-annotations/. But now cache annotations, being part of core spring framework, it is going to be used more seriously and developed pro-actively.

Another major impact is, I am revisiting actual benefits of using ORM API for data access. As we all know, we have pretty much two ways to access database, either plain JDBC or ORM API like Hibernate. I am big fan of Hibernate, due to obvious reasons. And one of main reason is, adding cache capabilities by enabling 2nd level cache.  But in design sense, cache is more like cross-cutting requirement for a running system. Like any other cross-cutting concerns like Transactions, it makes more sense to use ‘annotations’ way.

Why back to JDBC now?
That’s one decision, I recently made. I switched back to JDBC based DAO implementations instead of Hibernate backed DAOs.

  1. We all have experience of writing SQLs. And using Spring JDBC templates, it’s very easy to implement methods.
  2. When we actually implementing DAOs, most of operations are not CRUD type. It’s mostly join of multiple tables. Although HQL/JPQL can achieve can anything but it’s another learning curve. 
  3. Personally, I don’t want to add another layer API in whole technical stack. Hibernate (or any other ORM layer) is great and might have obvious benefits but it always have learning curve and yes, we spend time in debugging HQL or JPQL. 
  4. Major factor was ‘cache’ feature in Hibernate. Now Spring Framework provides cache annotations. It gives me more flexibility on what to cache and what not to. (even eviction policies are supported) 
  5. Plus spring cache annotation can be applied anywhere. Not just DAO, even remote webservices, or any method which is resource intensive. 
You can be setup and running by simple steps like upgrading spring dependency to 3.1.0-M1 (3.1.0.M1) and read this useful article at spring blog. 
Things which might need polishing
  • There seems to be incomplete code, how to generate key. (It’s using just method name to generate key? )
  • Spring team is working on adding more providers (apart from ehcache). Support for providers like JBoss treecache will be nice. Specially in multi-cluster environment.
References:

Categories: cache, spring framework

Spring scope using Example

October 31, 2009 Leave a comment

This post is about to experiment about spring framework ‘singleton’ and ‘prototype’ scope. I created couple of tests to show proof of concept and clear our understanding of ‘singleton’ scope.

Those are new or fairly new to spring framework, they sometimes get confused, when they read the term ‘singleton’ in spring beans. As traditionally, we know, Singleton class pattern makes sure, we have single instance of that class per jvm. But when we talk about singleton pattern in spring framework world or lets say spring context world,  it’s singleton pattern applied per spring container. Now we can have multiple containers initiated in a java virtual machine, hence we can have multiple instance of same class possible, but single instance per container per bean definition.

Example, Lets use simple java bean, which stores it’s own creation time and also defines equals method to check that equality.

public class SpringBean {
private long createTime;

public SpringBean()
{
this.createTime = System.currentTimeMillis();
}

 

public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
SpringBean other = (SpringBean) obj;
if (this.createTime != other.createTime) {
return false;
}
return true;
}
}

My spring context file defined only two beans

 

<bean id=”singletonBean”/>

<bean id=”prototypeBean” scope=”prototype”/>

 

  • <bean id=”singletonBean” class=”com.jframeworks.learn.SpringBean”/>
  • <bean id=”prototypeBean” class=”com.jframeworks.learn.SpringBean” scope=”prototype”/>

Both beans use same class but different scope. Lets define a testsuite class which will initiate these beans and run some basic tests.

 

Class: SpringScopeTestSuite, method = runTests

public SpringScopeTestSuite(ApplicationContext ctx, ApplicationContext ctx2) {
this.ctx = ctx;
this.ctx2 = ctx2;
}
...............
public void runTests(String name) throws InterruptedException
{
SpringBean singletonBean0 = this.getBeanUsingContext("singletonBean");
Thread.sleep(50);//To make sure, next bean from context is at different timestamp.
SpringBean singletonBean1 = (SpringBean) this.getCtx().getBean("singletonBean");
Thread.sleep(50);
SpringBean protoTypeBean0 = (SpringBean) this.getCtx().getBean("prototypeBean");
Thread.sleep(50);
SpringBean protoTypeBean1 = (SpringBean) this.getCtx().getBean("prototypeBean");
Thread.sleep(50);
SpringBean singletonBean2 = (SpringBean) this.getCtx2().getBean("singletonBean");
Thread.sleep(50);
SpringBean protoTypeBean2 = (SpringBean) this.getCtx2().getBean("prototypeBean");

System.out.println(name + " =====================================================");
System.out.println(name + " Equality Tests");
System.out.println(name + " =====================================================");
System.out.println(name + " Two beans references with Scope 'singleton'");
System.out.println(name + " singletonBean0.equals(singletonBean1) = " + singletonBean0.equals(singletonBean1));
System.out.println(name + " singletonBean1.equals(singletonBean2) = " + singletonBean1.equals(singletonBean2));
System.out.println(name + " singletonBean1.equals(protoTypeBean1) = " + singletonBean1.equals(protoTypeBean1));
System.out.println(name + " protoTypeBean0.equals(protoTypeBean1) = " + protoTypeBean0.equals(protoTypeBean1));
System.out.println(name + " protoTypeBean1.equals(protoTypeBean2) = " + protoTypeBean1.equals(protoTypeBean2));
System.out.println(name + " =====================================================");
}

To run this test, lets define test runner class, ‘BasicSpringScopeTest’

public class BasicSpringScopeTest
{
public static void main( final String[] args )
{
try {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationConfig.xml");
ApplicationContext ctx2 = new ClassPathXmlApplicationContext("applicationConfig.xml");
SpringScopeTestSuite test = new SpringScopeTestSuite(ctx, ctx2);
test.runTests("singleThread");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

Now executing this class, we see this output…

singleThread =====================================================
singleThread Equality Tests
singleThread =====================================================
singleThread Two beans references with Scope 'singleton'
singleThread singletonBean0.equals(singletonBean1) = true
singleThread singletonBean1.equals(singletonBean2) = false
singleThread singletonBean1.equals(protoTypeBean1) = false
singleThread protoTypeBean0.equals(protoTypeBean1) = false
singleThread protoTypeBean1.equals(protoTypeBean2) = false
singleThread =====================================================

Analysis.
As singletonBean0 and singletonBean1 comes from same container, and their scope is default ‘singleton’, hence they are same instance.

singletonBean1 and singletonBean2, though are same bean and ‘singleton’ scope, but they comes from different container. Hence they are different instance of same class.

singletonBean1 and prototypeBean1 comes from same container and same class, but prototypeBean1 is different bean definition and ‘prototype’ scope. Every time, this bean retrieved from container, spring container creates new instance. Hence they are not equals. They are not equal for two reasons

 

  1. They are two different bean definition (even same class type)
  2. prototypeBean is defined as ‘prototype’ scope’

 

Next equality check is for prototypeBean0 and prototypeBean1 beans.  Even they come from same container but they still not equal, because they refer to spring bean defined as ‘prototype’ scope.

Last equality check is for prototypeBean1  and prototypeBean2 beans. They are not equal for two reasons..

  1. They both comes from different spring container
  2. They refer to spring bean defined as ‘prototype’ scope.

Conclusion
Spring framework takes away boiler plate code of defining Singleton classes in code base. This aligns with their philosphy of letting developers concentrate on business logic instead of writing boiler plate code.

Just to be clear, in spring world, a bean definition is singleton not bean class. We can have multiple of bean definitions of same class, each representing different singleton class. This makes spring container light weight container. Prototype scope is not used widely, but it can specified depending upon technical requirements.

For further testing, there is another code, which runs test suite in mutli-threaded environment.

public class AdvancedSpringScopeTest
{
public static void main( final String[] args )
{
try
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationConfig.xml");
ApplicationContext ctx2 = new ClassPathXmlApplicationContext("applicationConfig.xml");

SpringScopeTestSuite testSuite = new SpringScopeTestSuite(ctx, ctx2);

 

 

Thread thread1 = new Thread(testSuite, "thread1");
Thread thread2 = new Thread(testSuite, "thread2");
Thread thread3 = new Thread(testSuite, "thread3");

 

 

}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}

This class output is..

thread2 =====================================================
thread2 Equality Tests
thread2 =====================================================
thread2 Two beans references with Scope 'singleton'
thread1 =====================================================
thread1 Equality Tests
thread1 =====================================================
thread1 Two beans references with Scope 'singleton'
thread1 singletonBean0.equals(singletonBean1) = true
thread3 =====================================================
thread1 singletonBean1.equals(singletonBean2) = false
thread2 singletonBean0.equals(singletonBean1) = true
thread2 singletonBean1.equals(singletonBean2) = false
thread2 singletonBean1.equals(protoTypeBean1) = false
thread1 singletonBean1.equals(protoTypeBean1) = false
thread1 protoTypeBean0.equals(protoTypeBean1) = false
thread3 Equality Tests
thread3 =====================================================
thread3 Two beans references with Scope 'singleton'
thread3 singletonBean0.equals(singletonBean1) = true
thread3 singletonBean1.equals(singletonBean2) = false
thread3 singletonBean1.equals(protoTypeBean1) = false
thread1 protoTypeBean1.equals(protoTypeBean2) = false
thread1 =====================================================
thread2 protoTypeBean0.equals(protoTypeBean1) = false
thread3 protoTypeBean0.equals(protoTypeBean1) = false
thread2 protoTypeBean1.equals(protoTypeBean2) = false
thread2 =====================================================
thread3 protoTypeBean1.equals(protoTypeBean2) = false
thread3 =====================================================

thread1.start();
thread2.start();
thread3.start();

//make sure, all threads finish.
Thread.sleep(2500);

Categories: Java, spring framework