Spring Batch

When we need to perform batch jobs, better to use Spring Batch.
This is matured framework with many options.

1. Easy to configure
2. Repeat options / Retry options
3. Scalability with parallel process
4. Support for multiline records
Many more

http://docs.spring.io/spring-batch/reference/html/index.html

#spring

Spring Web Services 2 Cookbook

Spring Web Services 2 Cookbook
Paperback: 322 pages
Publisher: Packt Publishing (May 13, 2012)
Language: English
ISBN-10: 1849515824
ISBN-13: 978-1849515825
Books is available at Amazon.com

Spring Web Services 2 Cookbook

This is very nice book on Spring Web Services.

#cookbook, #spring-web, #web-services

New Webapp – Spring, GWT EXT, Restlet, Hibernate,….etc

Problem Statement: How to build very nice rich website?

Option 1: Single tier architecture
Advantages: Saves Time, Easy to implement
Disadvantages: Difficult to break up later to support other user interfaces.

Option 2: Two tier architecture
Advantages: Both are in its own spheres. Easy to modify and re-write later if required.
Disadvantages: Takes more time to setup the environment and looks like more code.

Technologies:
UI: GWT
Services: Spring, RESTful Services, Hibernate

Notes:
RESTful – http://www.xfront.com/REST-Web-Services.html
Restlet with Spring – http://jgoday.wordpress.com/2009/04/10/restlet-with-spring/
RESTful from Wiki: http://en.wikipedia.org/wiki/Representational_State_Transfer
Archtypes:
Archetype – HibernateSpringGwt https://opensource.fastconnect.org/redmine/projects/show/spring-gwt-archetype
maven-gwtext-archetype http://code.google.com/p/maven-gwtext-archetype/

-o-

#gwt-ext, #hibernate, #restlet, #spring

Spring Bean – Order of injecting properties

Problem Statement: We need to start some task, after injecting all properties. How to resolve this?

Solution: Dont / Never depend on order of injecting values into bean. It is not controlled.


import org.springframework.beans.factory.InitializingBean;

public class BeanName implements InitializingBean
{

public void afterPropertiesSet() throws Exception
  {
    System.out.println( "It will be called after completion of properties setting.");
    //do whatever is required here.
  }

}

#afterpropertiesset, #injecting-properties-order, #spring-framework

Injecting boolean values into Spring bean

Problem Statement: How to inject boolean values into spring bean?

Wrong:

<property name="isFlagEnabled" value="true"/>
private boolean isFlagEnabled = false;

Correct:

<property name="isFlagEnabled">
	<value type="java.lang.Boolean">true</value>
</property>
private Boolean isFlagEnabled = false;

-o-

#inject-boolean-value, #spring-bean

Spring – Inject Properties file into Bean

Problem Statement: Spring – Inject Properties file into Bean

Spring Bean configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

<util:properties id="propertiesFileID1" location="classpath:complete_file_pathfilename.properties"/>

<bean name="BeadID" class = "com.abc.ClassNameABCD" >
	<property name="propFile1" ref="propertiesFileID1"/>
</bean>

</beans>

Source code


package com.abc;

import java.util.Properties;

public class ClassNameABCD
{

 private Properties propFile1;

 //Prepare getter and setters for propFile1;

}

Read this for more information
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/apcs02.html#xsd-config-body-schemas-util-properties

#spring-inject-properties

Spring – Oracle Connection Pool Configuration

Problem Statement: Configure Oracle Connection Pool in Spring

This is basic data source (Only testing)

&lt;bean id=&quot;datasource1&quot;
 class=&quot;org.springframework.jdbc.datasource.DriverManagerDataSource&quot;&gt;
&lt;property name=&quot;driverClassName&quot;&gt;
         &lt;value&gt;oracle.jdbc.driver.OracleDriver&lt;/value&gt;
    &lt;/property&gt;
&lt;property name=&quot;url&quot;&gt;
         &lt;value&gt;ORACLE URL&lt;/value&gt;
    &lt;/property&gt;
&lt;property name=&quot;username&quot;&gt;
         &lt;value&gt;user id&lt;/value&gt;
    &lt;/property&gt;
&lt;property name=&quot;password&quot;&gt;
         &lt;value&gt;user password&lt;/value&gt;
    &lt;/property&gt;
&lt;/bean&gt;

This is dbcp data source (Preferred for Testing.)

&lt;bean id=&quot;datasource2&quot;
 class=&quot;org.apache.commons.dbcp.BasicDataSource&quot; destroy-method=&quot;close&quot;&gt;
&lt;property name=&quot;driverClassName&quot;&gt;
         &lt;value&gt;org.apache.commons.dbcp.BasicDataSource&lt;/value&gt;
    &lt;/property&gt;
&lt;property name=&quot;url&quot;&gt;
         &lt;value&gt;ORACLE URL&lt;/value&gt;
    &lt;/property&gt;
&lt;property name=&quot;username&quot;&gt;
         &lt;value&gt;user id&lt;/value&gt;
    &lt;/property&gt;
&lt;property name=&quot;password&quot;&gt;
         &lt;value&gt;user password&lt;/value&gt;
    &lt;/property&gt;
&lt;property name=&quot;initialSize&quot; value=&quot;5&quot;/&gt;
&lt;property name=&quot;maxActive&quot; value=&quot;20&quot;/&gt;
&lt;/bean&gt;

This is Oracle Connection Pool (Production Quality)


&lt;bean id=&quot;connectionPool1&quot; class=&quot;oracle.jdbc.pool.OracleDataSource&quot; destroy-method=&quot;close&quot;&gt;
&lt;property name=&quot;connectionCachingEnabled&quot; value=&quot;true&quot; /&gt;
&lt;property name=&quot;URL&quot;&gt;
         &lt;value&gt;ORACLE URL&lt;/value&gt;
    &lt;/property&gt;
&lt;property name=&quot;user&quot;&gt;
         &lt;value&gt;user id&lt;/value&gt;
    &lt;/property&gt;
&lt;property name=&quot;password&quot;&gt;
         &lt;value&gt;user password&lt;/value&gt;
    &lt;/property&gt;
&lt;property name=&quot;connectionCacheProperties&quot;&gt;
      &lt;value&gt;
		MinLimit:1
		MaxLimit:5
		InitialLimit:1
		ConnectionWaitTimeout:120
		InactivityTimeout:180
		ValidateConnection:true
      &lt;/value&gt;
   &lt;/property&gt;
&lt;/bean&gt;

Oracle Connection Pool is better than DBCP and C3P0

-o-

select * from v$version;

“BANNER”
“Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 – 64bit Production”
“PL/SQL Release 11.2.0.1.0 – Production”
“CORE 11.2.0.1.0 Production”
“TNS for Linux: Version 11.2.0.1.0 – Production”
“NLSRTL Version 11.2.0.1.0 – Production”

Go to Oracle and Choose correct Driver and Download it.
http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html

Option 1: Added to Maven Repo and pom.xml

Add jar to maven Repo
http://www.mkyong.com/maven/how-to-add-oracle-jdbc-driver-in-your-maven-local-repository/

Refer it in pom file.

&lt;project ...&gt;
 
	&lt;dependencies&gt;&gt;
 
		&lt;!-- ORACLE database driver --&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;com.oracle&lt;/groupId&gt;
			&lt;artifactId&gt;ojdbc6&lt;/artifactId&gt;
			&lt;version&gt;11.2.0.1.0&lt;/version&gt;
		&lt;/dependency&gt;
 
	&lt;/dependencies&gt;
&lt;/project&gt;

Option 2: Add the jar file to Lib folder.

#oracle-jdbc-pool-oracledatasource

Description: Could not locate TransactionManager

Exception:

Chained Exception Object: org.hibernate.HibernateException
Description: Could not locate TransactionManager

Solution:
When we use org.springframework.orm.hibernate3.LocalSessionFactoryBean, the following two properties are not required.

http://docs.codehaus.org/display/BTM/Hibernate13

&lt;!--
org.hibernate.transaction.TransactionManagerLookup
org.hibernate.transaction.JTATransactionFactory  --&gt;

-o-

#localsessionfactorybean

Spring Dynamic Modules – OSGi

Last few days I am reading on OSGi. It is difficult to imagine life with out Spring Dynamic Modules for projects who needs to support multiple environments. Example: A jar file needs to work in application server. Also need to work in OSGi container. Spring DM Configuration files will help to achieve this. To know more about it read the following.

1. “Pro Spring Dynamic Modules for OSGi Service Platforms” book
2. Spring Dynamic Modules reference manual: http://static.springframework.org/osgi/docs/1.2.0/reference/pdf/spring-dm-reference.pdf

Spring Dynamic Modules is not much coding. It is all about writing configuration files. Mainly there are two files

1. modulename-context.xml
2. modulename-osgi-context.xml

Rest is in the book and reference manual.

Note: Dont get confused with Spring DM and Spring DM Server.
Spring DM means Spring Dynamic Modules
Spring DM Server supports deployment of Spring DM Modules and standard J2EE jar/war files.
When we are working in OSGi environment we dont need Spring DM Server.

-o-

#osgi, #spring-dm, #spring-dynamic-modules