Archive

Archive for the ‘Java’ Category

Fast Forward To MVC 'ONE'

April 25, 2010 1 comment
Recently, I worked on adding a feature to legacy webapp of my client, based upon MVC one model. And  it’s simplicity and getting work done, made me think again, about MVC one based webapps. I am always on hunt for simple web framework, which i can embark on for proof of concept of my startup ideas.
This is year 2010 and we as java developers, have spent countless time in arguing and reading (fun though) about various java web frameworks in play. Many bloggers publish their findings, opinions and countless others copied their content, republished them anyway. One common subject has been, which web framework? Cult is increasing everyday, for Spring MVC, Wicket, now JBoss Seam. I had personally spent time thinking about choosing one framework of choice, for my own projects, but I could never conclude on one. Spring MVC has become strong contender and safe choice, because I will be using spring for services and data access layer. Have we ever took step back and thought, we have surrounded ourselves plethora of frameworks for unlimited reasons, but never thought of ROI and time to market any product. Java always been treated as enterprise solution for big companies with big projects with big budgets. What about simple B2C startup projects which has limited user base and no funds. In today’s real web applications, PHP is thriving as before. People are still coding & adding more to PHP based projects. And those projects have been more successful and widely accepted. Example would be ‘WordPress’ , ‘Drupal’, ‘OSCommerce’. (Even springframework.org used drupal. Not sure whether they still using it). So what is main charishma or any characteristics of PHP? We all have argued, bloggoed about How PHP is inferior compare to Java, but PHP is productive.
Having said that, lets come to drawing board, as new developer or even CIO of a company. What we need in a web application (lets assume startup webapp, upto 100 concurrent users in one cluster)
  1. Pretty interface, which users to love to use it. (Example mint.com)
  2. Interactive & Intuitive interface.
  3. Functional interface. It does what it promises and always does that.
  4. Low or almost NIL learning curve for any new developer.
  5. Able to extend interface + functionality by 3rd party developers. (That strives any web product)
  6. Quick turn around of code to market
  7. Able to remove or disable code or by choice of end user. (user is deployer)
Having listed all the requirements, how many requirements really demand for use of MVC II framework?
First two are more of DHTML only.
Number 3 is quality of code and how well tested code is.
Number 4 is not good for MVC II frameworks like spring or wicket or even struts. They have some learning curve and you need resources, comfortable on that.
Number 5, people can argue, here MVC II excels. But at what cost? Lets analyze that more (I will use example of Spring MVC, because of popularity. Nothing against it)
First drawback is, to write any java code, you need to be java developer. It eliminates pool of casual web programmers who just like to write scripting (like PHP) and DHTML. Second is, if you write anything in JAVA, webapp has to be repackaged (and ofcourse released again). So there is no on-fly programming or releasing code on running webapp. (Lets forget about OSGi for now. It’s different ball game).
What about writing code using JSPs only?

That’s this post is all about. I know, many of you, thinking, c’mon man, Nineties called, they want their MVC one back. But lets entertain this idea for a while. For example, I want to write simple webapp ‘java press’ blogging engine, able to deploy it with in couple of weeks and my available heap size is 64MB (pretty common for VPS plans). If I go by spring mvc route, it will take me a couple of weeks, just enough to setup my environment, DBs, etc etc. I will have re-hash my knowledge of spring, and spring mvc controllers etc etc. But if I just pick JSPs model, i am ready to convert static HTMLs with-in a day.

Using JSPs and JSTL tags, I get lots of java functionality baked in. I can divide my jsps using includes (keeping option of apache tiles open). I can keep two sets of JSPs, presentation jsp and worker jsps. I can use extensively extra request parameters, to drive backend jsp, where to forward or redirect my request after finishing work.
If we still on this concept, few custom tags can be written and being delivered as part of SDK. So anyone who wants to customize it or extend it, he has JSPs, JSTL and some custom util tags at his disposal.
Advantages or trying to look at positive aspects.
  1. For deployments for small companies or concept deliveries or Y combinator kinda projects, time to deliver is small.
  2. You don’t have to muck around which framework to use. You think of doing something, and you doing it.
  3. Think of 3rd party developers pool, you get access to. If you document well, even PHP developer can pick JSP skills in few days and write JSPs for your webapp. (Imagine hiring PHP developer for spring MVC work. I don’t have time & budget for training).
  4. With new JEE specs coming out, you are always safe to upgrade your app and take advantages of new features without rewriting anything. (That’s what spring says, we give you another layer of abstraction. But you have upgrade code for spring 3.x now, isn’t it?)
  5. For scalability, it’s cheaper than before, clustering solutions based upon clouds. So my one deployment supports 50 concurrent sessions, I will just throw couple extra virtual tomcat instances, with in same cloud instance. or couple other different ways.
  6. I am less worried about performance, because recent benchmarks for tomcat 6 using JDK 6, is impressive.
  7. Using data source pooling, getting connections from JSP isn’t resource intensive. (As part of SDK, some classes with static methods can provided, to provide some encapsulation on common used API calls)
  8. web.xml supports basic security model. And again, as part of SDK, some common servlet filters can be provided for cross cutting concerns like security, logging, transactions.
  9. DB Caching can be ignored for now, assuming DB server and tomcat instance are co-located or installed in same instance. Caching adds lots of code maintenance, research & testing. Benefits of using caching diminishes in small db and small users.
  10. Well known advantage of JSP, do code change and see changes immediately on web browser. Hence time to see results, while doing development is almost nil. We all have taken coffee breaks in our past projects while waiting for server to bounce.
This post is not comparison between MVC I and MVC II model. It’s more of another look at MVC I model and putting on my thoughts, why It could make sense to use it.
Categories: Java Tags:

Spring scope using Example

October 31, 2009 2 comments

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 Tags: ,

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