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.

Thursday, February 9, 2012

Running Oracle Stored Procedure With “In/Out Parameters” using Hibernate, Spring WebFlow and Maven

On a recent project I needed to run an Oracle stored procedure using hibernate. Just a little background as to what I was using because it played a big role in running the stored procedure:
For Build: Maven 2.2.1
For ORM: Hibernate 3.3.1.GA, hibernate-annotations and hibernate-commons
annotations 3.4.0.GA
org.hibernate
hibernate
3.3.1 GA

org.hibernate
hibernate-commons-annotations
3.4.0.GA

org.hibernate
hibernate-annotations
3.3.0.GA

I’m also using Spring WebFlow 3.2 and JSF 2.2
3.0.4.RELEASE
2.2.0.RC1
DBMS: Oracle SQL Developer

Stored Procedure: The stored procedure uses four In and one out parameter.The out parameter I am getting here is the new Id on which I will update all existing records. That is Stored Proc with some conditions will copy everything from the original Id and create new Output Id and update/delete everything on this newly created Id which is the output of the Stored Proc. Out parameters in Oracle are like passing an argument by reference. You create a variable, pass it in and when the procedure is done, that variable will have the value. For the purposes of this post let’s assume there are 4 in and 1 out parameters in our stored proc like so:
call myStoredProc(’in param 1′,2,3,4..., outParam1);
where outParam1 is a number.

I tried using NamedNativeQuery annotation on my model class and running it through hibernate but that didn’t work and apparently hibernate doesn’t like out parameter stored procedures but instead would like the stored procedure to return or set the first parameter to an out ref cursos. The only way to call our stored procedure is to get to the jdbc connection directly.But I cant set output parameter directly as I am using Hiberante. This is the hectic.......

To do this
1)I am using my hibernate session’s connection (this.getSession().connection )that is marked as deprecated.

2)I found the new 3.3+ hibernate versions have a “doWork” method on the session class. With that in place you could run a CallableStatement to the stored procedure.
So I decided to go with first option.

Here is what you need to do in the DaoImpl class in order to set the parameters and call the store proc using connection object

public Integer validateXXX(Integer selectedXXXOid, UserSystem userSystem, String vEntityCode,String selectedXXXOid)
{
Integer outputValue = 0;
try
{
Connection con = ((Session)getEntityManager().getDeleg())
.connection();
con.setAutoCommit(false);
CallableStatement cs = con
.prepareCall("call XXX_validation(?, ?, ?, ?)");

cs.setInt(1, selectedXXXOid);
cs.setString(2, userSystem.getUserName());
cs.setString(3, vEntityCode);
cs.setInt(4, Integer.parseInt(selectedXXXOid));
cs.registerOutParameter(4, java.sql.Types.INTEGER);
cs.execute();
outputValue = cs.getInt(4);
System.out.println("Value returned by stored procedure--->"+outputValue);

getEntityManager().flush();
getEntityManager().clear();
}
catch (SQLException sqe)
{
log.error("SQLException in XXX: "+sqe);
}
catch (Exception ex)
{
log.error("Exception in XXX: " +ex);
}

return outputValue;
}
}
I have a running example that is working for me but I rewrote the code for this post so the above code may not even compile and you will probably have to change the code to fit your needs but this gives you an idea of how to do it.

Tuesday, August 23, 2011

Innocent Minds Before Now and Then

Note : These are my own personal ideas and not relate to anyone’s personal life.

When we were kids we all had a dream in our eyes. When we used to play with the friends many of us told I will become pilot, Doctor, Engineer, Zansi ki Rani, PM……..
When we were busy in connecting the molded toys, wires and things as our hobby and got satisfied when we did something great!!!!!
We had spent a lot of time when we were kids, in doing great experiments and producing funny results, we had lot of great ideas and implemented it.

As we grow, our priorities, destinies, way of thinking everything gets changed and which has to be as its lagging indicator of growth. You will always meet a kind of persons in the life which can turn your life either in to good or bad, which has huge impact on our lives. I agree to the fact totally, however we should accept the reality of the 80-20 principle here. At last it all depends on us to take good and make our way towards following the passion for our dreams. As Steve Jobs told, We cannot connect the dots looking forwards the only way you can connect them by looking backward. And you have to believe that somehow these dots will connect your future

I have so many great friends however I will always get surprised by them when I listen to their thoughts, ideas, future plans etc. They always talk about Castisum, Jobs, Salary, Status in the society, Money etc. I totally agree with the fact that everyone needs money to live the life but should it be destiny of life?
Do you not think that
we have changed a lot?
We are not connecting the dots of future?
We are not willing to work hard to make the world better place?
Innocent minds have changed their ideas?
Where are those days and those innocent mind thinking’s?


I am a strong believer that “There is always room for improvement for everyone”.
We have got a good education, good life and everything that we want, so can we all come together and bring back those innocent minds, ideas to fly freely to create something great, meaningful, instead of talking on something which is not of any use?
Because it doesn’t matter that how long you lived your life but what really matters is how good you live.

Best Regards,
Anant Shrote.

Thursday, April 14, 2011

Count every failure as experience

“Success is when skill meets opportunity.
Failure is when fantasy meets reality”.

Failures have more close relation every human beings life as they have huge impacts on our lives.
As per my understanding Failure is not something which occurs frequently. However I am damp sure when it occurs it will provide you a great chance to grow and more importantly it creates a creativity and innovation power which inspires you to work hard and stand again.
Failures provides opportunity for diligent persons to defeat the problem and succeed. It itself says it should not be a master for root cause. I agree that failure is a great agony when it occurs and it has to be, as we all worked hard to get succeed.
It will force us to think and come up with solution to defeat the agony of failure and succeed which is remarkable characteristic of a great leader.

According to Swami Vivekananda “The day we have not come across any problem/failure, we missed the opportunity to learn” which says, never stop learning.
So, start counting your every failure as a experience.