Tip

High Scalable & Distributed Architecture with EJB & Spring Framework

In an Enterprise world, Spring Framework with some standard ORM tool like Hibernate gained considerable acceptance as a light-weight architecture for mid size applications. Also in Java EE 5 specification, major changes has been done on component architecture, tried overcome the gaps like IoC, ORM etc.

 

How to Achieve High Scalability with Java EE EJB 3.0 and the Spring Framework
Scaling a hybrid solution in the cloud space

In an Enterprise world, the Spring Framework combined with some a ORM tool like Hibernate has gained considerable acceptance as a light-weight architecture for mid size applications. In corollary, the Java EE 5 specification has adapted to overcome the apparent gaps previous specifications had in the IoC, ORM etc. This article demonstrates how to build a highly scalable application with hybrid technology like EJB 3.0, Spring 3.0.x, and Hibernate. By designing a flexible, component based architecture, your applications will leverage the power of EJB 3.0, IoC & APO of Spring 3.0.x and ORM capability of Hibernate.

Architecture & Design Consideration

Any web based application can be built on the concept of a multi-tier, MVC model. For better maintainability, applications should be designed in such a way that the Presentation Layer and the Business Layers can be deployed separately. This will give the edge to any organization that needs to deploy each Presentation Layer in different geographic locations while using a common centralized Business Layer. This way, the Presentation Layer can look-up the appropriate Services through EJBs (Remote or Local) or exposed web services. A nice hybrid Business Layer will employ EJBs as well as components from the Spring Framework.

Below diagram illustrates the application layer and the technologies to which they are mapped.

Just think about the overall application flow for a second. When a user interacts with the system by clicking on the any link of an application, the application will perform the following steps:

•         The action component part of the Presentation Layer, will invoke the respective service locator component.

•         The service locator will invoke the corresponding Service, which might be a Stateless Session Bean or Web Service.

•         This exposed Service will instantiate the Spring Framework (single instance per application) and will invoke the respective Business Delegator.

•         If application supports different geographies then security should be in place around the exposed services. Security can be handled by EJB annotations or Spring Security i.e Spring Acegi. A Business Delegator will first check the user authorization on respective business operation. If the user has the permission to execute a particular service then the application will then process the business logic. Otherwise the application would throw up some type of security exception.

•          The Business Delegator Layer will invoke the business component of Domain Layer and process the business logic.

•          The business component will then call the data access component(s). Application can use either of the following two methods to leverage Spring effectively in J2EE applications using EJB 3.0 components:

            o Spring Framework for IoC-AOP container, and EJB components as regular beans to implement business functionality and manage transactions.

            o Spring Framework as a provider of EntityManager for the underlying JPA implementation (as Hibernate).

•         A response is sent back to the Presentation Layer.

 

A Sample Application

This Sample application demonstrates how one might integrate components of the Spring Framework and EJB specification.

The Presentation Layer will use the RMI-IIOP protocol (Resource Access through the Java EE component model) for interaction with the Service Layer. With minor changes in the code, the application can also be connect to Service Layer using Java method calls or Web Services.

The Service Layer will consist of application business services, which can be exposed as EJB and wired with Spring Framework. It also can easy be exposed as Web Services by using a webservice annotation. The Business Delegator Layer consists of business delegator components which will be managed by the Spring Framework. Integration of SLSB with Spring Framework will be done by initializing Spring Framework proxy delegator POJO classes. Stateless session beans will use the ContextSingletonBeanFactoryLocator to load the Spring Framework in an EJB post create method.

@PostConstruct

Public void loadSpring() {

      setBeanFactoryLocatorKey("MyApplication");

      setBeanFactoryLocator(ContextSingletonBeanFactoryLocator.getInstance());

 }

The Spring Framework will look for the XML context file using the following resource path "classpath:beanRefContext.xml".

<bean id="MyApplication" class="org.springframework.context.support.ClassPathXmlApplicationContext">

   <constructor-arg>

     <list><value>MyApplicationContext.xml</value></list>

   </constructor-arg>

 </bean>

 

SLSB will invoke domain objects. These business components will hold the core logic required to perform business functions. Also these domain objects will be managed by the Spring Framework container which does the auto-wiring among the components through dependency injection or inversion of control pattern. The application will use the dependency injection pattern of Spring Framework for wiring and initializing the components.

Now, domain objects will call the objects of Data Access Layer, which abstract the data access and manipulation operations of the application. These components follow the data access object (DAO) pattern in abstracting the data access operations from the business logic. This DOA layer will use the Hibernate ORM for converting data between incompatible type systems in relational databases and object-oriented programming languages. The advantages of using Hibernate ORM are as follows:

•         High performance object/relational persistence and query service for Java.

•         Provide support for association, inheritance, polymorphism, composition and the Java collections framework.

•         JPA support

•         Support express queries using native SQL or Java-based criteria and example queries. Hibernate is now the most popular object/relational mapping solution for Java.

•         Support for object caching. The second-level cache of Hibernate will be managed by the distributed cache provider.

 

As JPA EntityManager will be instantiated by the container in a SLSB and pass on to Spring Framework.

 

@Stateless

@TransactionManagement(TransactionManagementType.CONTAINER)

@TransactionAttribute(TransactionAttributeType.REQUIRED)

public class MyServiceBean implements MyLocal, MyRemote {

            @PersistenceContext

            private EntityManager entityManager;

            …………

           ………..

}

 

The Data Access Layer will wrap the Hibernate APIs through Java EE standard JPA. This abstraction design helps easily plugging of different ORM tools. As Spring Framework provides integration with Hibernate, JDO and JPA in terms of resource management, DAO implementation support and transaction strategies also Hibernate also supports IoC convenience features, The Spring Framework will wire the JPA EntityManager with Hibernate by passing application objects via dependency injection, and managing transactions for it. The sample DAO class is shown below:

 

Public class MyDaoImpl implements MyDao {

 

public Collection getUsers(EntityManager em, String companyId){

………………………………..

………………………………..

 

}

}

 

Furthermore, the injected JPA EntityManager will behave just like an EntityManager fetched from an application server’s JNDI environment, as defined by the JPA specification. It will delegate all calls to the current transactional EntityManager, if any; else, it will fall back to a newly created EntityManager per operation, making it thread-safe. The JDBC DataSource will be defined through a JNDI location in the META-INF/persistence.xml file. EntityManager transactions will be integrated with the server’s JTA subsystem.

 

So, we have discussed all the layers and integration of EJB, Spring Framework and Hibernate ORM. In next section we will discuss how this approach is scalable in Cloud Environment.

 

3.       Cloud Enablement

EJB as well as WebService can easily be scalable depending upon the load of the system especially bean usage load and if any of the application’s EJB takes or consume more resources then it can be deployed separately in a different hardware.

In a Cloud environment like Amazon EC2, which has many of the proven IBM platform technologies including IBM WebSphere Application Server, IBM DB2, IBM Informix, and IBM WebSphere Portal Server etc ., where EJB/WebService along with Spring & Hibernate can be efficiently used in a high performance web sites.

 

4.       Conclusion

We have demonstrated how to leverage tools like Spring Framework with the new EJB 3.0 component architecture. Also we have demonstrated how to implement EJB 3.0 JPA using Hibernate and effectively integrate it with the EJB-based domain tier using Spring Framework. EJB 3.0 architecture has a few features found in Spring Framework and Hibernate, but we can have an alternative with better capability. So Spring Framework/Hibernate can easily and seamlessly integrate with EJB 3.0 architecture.

 

5.       Resources

•         EJB 3 Specification: http://java.sun.com/products/ejb/docs.html

•         Spring Framework: http://www.springsource.org/

•         Spring Framework with Hibernate: http://static.springsource.org/spring/docs/2.0.8/reference/orm.html

•         Spring Framework Remoting APIs and Support: http://static.springsource.org/spring/docs/2.0.x/reference/remoting.html

 

6.       About Author

I'm (http://in.linkedin.com/in/gauravtripathi) working at one of the leading IT conglomerate as a Solution Architect. My primary job responsibility is to provide consulting / design of large scale/ complex enterprise applications and GAP analysis for existing landscape. My other keen interest areas are participation and contribution towards architecture governance, mentoring, technical training and blogging. Please visit: http://visitgaurav.blogspot.com/  to know more.

Dig Deeper on Front-end, back-end and middle-tier frameworks

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close