Home >> Spring Framework
Using SpringFramework's LocalStatelessSessionProxyFactoryBean class
Extending that already developed example using
SimpleRemoteStatelessSessionProxyFactoryBean, I am going to add one more
stateless session bean, that is going to be called from the session facade
stateless session bean by using LocalStatelessSessionProxyFactoryBean in
configuration and injectable way.
As both these stateless session beans are co-located within the same EJB
container, provided by the JBoss Application Server version 4.0.5, I don't
see any issues using LocalStatelessSessionProxyFactoryBean, in order to
provide business interface runtime instance being injected into the
example POJOhelper class "PaymentProcessInterface.java", as shown in the
configuration below:
applicationContext.xml
This XML configuration file is to be read by Spring in the
EJB Container at the time when any of the declared bean is
retrieved from the Spring Application Context.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sessionFactoryBean" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- Either by reading hibernate.cfg.xml file or by defining
hibernateProperties one can create SessionFactory for using
along with Hibernate API. -->
<!--
<property name="configLocation" value="classpath:config/hibernate.cfg.xml"/>
-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.datasource">java:MysqlDS</prop>
<prop key="hibernate.connection.pool_size">2</prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</prop>
<prop key="hibernate.transaction.manager_lookup_class">
org.hibernate.transaction.JBossTransactionManagerLookup
</prop>
<prop key="hibernate.current_session_context_class">
jta
</prop>
</props>
</property>
<property name="mappingResources">
<array>
<value>example/businessobject/Account.hbm.xml</value>
</array>
</property>
</bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<constructor-arg type="org.hibernate.SessionFactory"
ref="sessionFactoryBean"/>
</bean>
<bean id="hibernateDAO" class="example.dao.HibernateDAO">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>
<bean id="paymentOperation"
class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">
<property name="jndiName" value="payment-operation"/>
<property name="jndiEnvironment">
<props>
<prop key="java.naming.provider.url">
jnp://localhost:1099/
</prop>
<prop key="java.naming.factory.initial">
org.jnp.interfaces.NamingContextFactory
</prop>
<prop key="java.naming.factory.url.pkgs">
org.jboss.naming:org.jnp.interfaces
</prop>
</props>
</property>
<property name="businessInterface"
value="example.process.IPaymentOperation"/>
</bean>
<bean id="paymentProcessInterface"
class="example.facade.PaymentProcessInterface">
<property name="paymentProcess"
ref="paymentOperation"/>
</bean>
</beans>
The section marked as green above, will be used in the AccountDAO.java
file for retrieving HibernateTemplate, and the section marked as red
above, will be used by the SessionBean to fetch the helper class
(PaymentProcessInterface.java), this helper class is having the local
stateless session bean being injected by using SpringFramework's
LocalStatelessSessionProxyFactoryBean.
As shown above, JNDI name "payment-operation" is used to bind local
stateless session bean, such as "IPaymentOperation" on deployment
onto the EJB Application server.
This stateless session bean is made simplest by having a single business
method like "sendPayment", and I am going to make this SessionBean
compatible with EJB 2.0 specification with deployment descriptor files
used, such as ejb-jar.xml and jboss.xml files under META-INF folder.
This example is targeted to use a test client java program and a remote
session bean is called from this test harness using a separate
applicationContext.xml file. The remote session bean is using a helper
bean from the server side Spring application context. This helper bean
is having another local stateless session bean instance being injected
to it.
The test client has the Spring's configuration file as follows:
client-applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="exampleBD" class="example.test.ExampleBusinessDelegate">
<property name="exampleFacade" ref="remoteSLBean"/>
</bean>
<bean id="remoteSLBean"
class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">
<property name="jndiName" value="example-facade"/>
<property name="jndiEnvironment">
<props>
<prop key="java.naming.provider.url">
jnp://localhost:1099/
</prop>
<prop key="java.naming.factory.initial">
org.jnp.interfaces.NamingContextFactory
</prop>
<prop key="java.naming.factory.url.pkgs">
org.jboss.naming:org.jnp.interfaces
</prop>
</props>
</property>
<property name="businessInterface"
value="example.facade.ExampleFacade"/>
</bean>
</beans>
Let us take a look at the test client java code and try to understand
this example's end to end logical execution flow.
TestClient.java
package example.test;
import java.math.BigDecimal;
import java.rmi.RemoteException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import example.businessobject.Account;
import example.facade.ExampleFacade;
/**
* This is provide AS IS without any Guarantee of any kind
* Author: Guddu from IQTF
* Date: 15th April 2009
*/
public class TestClient {
private static ExampleBusinessDelegate exampleBD;
static {
try {
ClassPathXmlApplicationContext clsCtx =
new ClassPathXmlApplicationContext("config/client-applicationContext.xml");
exampleBD = (ExampleBusinessDelegate) clsCtx.getBean("exampleBD");
System.out.println("Business Delegate instance: "+exampleBD);
}catch(Exception ex) {
ex.printStackTrace();
}
}
public TestClient(){
try {
ExampleFacade remote = exampleBD.getExampleFacade();
Account account = new Account();
account.setAccountNumber("Test_1");
account.setAccountType("test type");
account.setHolderName("Test Holder");
account.setBalanceAmount(new BigDecimal(12000.50));
account.setAccountActiveStatus(true);
String accountNumber = remote.createAccount(account);
System.out.println("Account created: "+accountNumber);
Account account1 = new Account();
account1.setAccountNumber("Test_2");
account1.setAccountType("test type");
account1.setHolderName("Test Holder");
account1.setBalanceAmount(new BigDecimal(62000.50));
account1.setAccountActiveStatus(true);
String accountNumber1 = remote.createAccount(account1);
System.out.println("Account created: "+accountNumber1);
boolean status = remote.
transferAmount(account, account1, new BigDecimal(4563.55));
System.out.println("Status of the Amount Transfer func :"+status);
} catch (RemoteException e) {
e.printStackTrace();
}
public static void main(String[] args) {
new TestClient();
}
}
The section marked in green in the above code, is a one time execution
logic when this class is getting loaded in the classloader.
This example's delegate called as "ExampleBusinessDelegate", is just
holding the remote interface of the stateless session bean ExampleFacade.
ExampleBusinessDelegate.java
package example.test;
import example.facade.ExampleFacade;
public class ExampleBusinessDelegate {
private ExampleFacade exampleFacade;
public ExampleFacade getExampleFacade() {
return exampleFacade;
}
public void setExampleFacade(ExampleFacade exampleFacade) {
this.exampleFacade = exampleFacade;
}
}
This means there is a stateless session bean ExampleFacade that is
remote in nature and being deployed on a remote application server
not even located in the machine where this test client is being
executed. So SimpleRemoteStatelessSessionProxyFactoryBean class
from Spring api requires JNDI name and environment including Naming
provider URL, initial factory etc., in order to find the remotely
bound session bean.
Once this test client is used to call the remote session bean, then
comes understanding of the portion from the application server and
Spring's application context from within application server.
This example has the flow from the remote stateless session bean
to the helper class, then to the local stateless session bean, then
to the example service, DAO and then using HibernateTemplate to
persist domain object to the corresponding mapped table in database.
If you like to share your comment/suggestions/feedback relating to this Page,
you can do so by droping us an email at
usingframeworks @ gmail . com
with the subject line mentioning URL for this Page (i.e,
/Spring-Local-SLBean.php) or use this
LINK.
As per this website's privacy policy, we never disclose your email id,
though we shall post your comments/suggestions/feedback with
your name (optional) and date on this Page. If you don't want your
comments/suggestions/feedback to be shared in this Page, please
mention so in your email to us. Thank you very much.....
If anything missed out , please let me know at
techienjoy at yahoo . com