Friday, May 31, 2013

Managing EJB calls from spring in an distributed environment


Only reason to write this blog is that to reduce the pains of developers while working with distributed environment using Spring.
Hope this blog will clear most of the doubts.
I am working on the project with Java technologies  where we have following requirements,

We have a number of routers, then a firewall. Then we have four linux boxes in the environment. On the first two boxes we need to deploy only JSP pages and on the remaining two linux boxes we need to have business logic deployed.(services, DAO objects,  Beans etc). 

Basically we have this distributed architecture and on top of that we need to use weblogic server for the deployments. We have been asked to use Spring, Hibernate and JSP for this. Main problem was how we can use Spring in this environment and this approach. Using the only EJBs was not good option.

So we decided to try out if we can call the EJBs from the Spring, however very less sources were available on the net for the same. So it was a big challenge to overcome this problem. Finally we resolved this problem. I have tried to brief the steps we followed so that one can get an understanding of how it works.

Step 1: Installing, Configuring, and Testing WebLogic Server 12c Developer Zip Distribution
 You can get it everything from here.

Step 2: Spring EJB intigraation
The most important part is intigration of Spring with EJB, so configure your ApplicationContext.xml as below 

<?xml version="1.0" encoding="UTF-8"?>
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/tx
              http://www.springframework.org/schema/aop
              http://www.springframework.org/schema/jee
               <mvc:resources mapping="/css/**" location="/css/"/>
              <mvc:resources mapping="/img/**" location="/img/"/>
              <mvc:resources mapping="/js/**" location="/js/"/>
        <mvc:annotation-driven/> 
       <context:component-scan base-package="***"/>
       <context:component-scan base-package="***"/>
       <context:component-scan base-package="***"/>
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
              <property name="prefix" value="/"/>
              <property name="suffix" value=".jsp"/>
       </bean>
      
       <!-- Addded for Hibernate -->
             
       <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
              <property name="url" value="jdbc:mysql://****/mysql"/>
              <property name="username" value="root"/>
              <property name="password" value="root"/>
       </bean>
      
      
<!-- This sessionFactory with the help of Transaction , does all the data base related opration .
packageToScan is the property which , gives where are persitancey beans are located.
hibernate.dialect : tells which type of data base to talk
-->
       <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
              <property name="dataSource" ref="dataSource"/>
              <property name="packagesToScan" value="GDC.Beans"/>
                     <property name="hibernateProperties">
                     <props>
                     <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                     </props>
                     </property>
       </bean>
      
       <!-- -Added for International -->
       <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
              <property name="basename" value="classpath:welcome" />
              <property name="defaultEncoding" value="ISO8859-1"/>
       </bean>
              <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver ">
              <property name="defaultLocale" value="en" />
             
       </bean>
       <mvc:interceptors>  
       <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
              <property name="paramName" value="lang" />
       </bean>
       </mvc:interceptors>
      
       <jee:remote-slsb id="simple"  jndi-name="Hello#com.Hello"
              business-interface="com.Hello" expose-access-context="true">
              <jee:environment>java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
                     java.naming.provider.url=t3://10.102.75.188:7001
              </jee:environment>
       </jee:remote-slsb>

      
</beans>

Step 3: Create two different projects, one EJB and other Web project. Create the stateless session beans , remote interfaces and try this.
Also create a context file to get a context object for JDI lookup.

Step 4: Now in that file provide the remote IP address of the system where all the code is deployed. (Execute the business logic). And now run the client that will execute the business logic.

No comments:

Post a Comment