PDA

View Full Version : Why we need Home and Remote for EJB clients using JNDITempla



kevin_405
Mar 21st, 2005, 05:11 AM
Hi

I am using JNDI template to look up EJB.
My ejb-jar.xml is as follows

<session >
<description><![CDATA[Description for Customer]]></description>
<display-name>Name for Customer</display-name>

<ejb-name>Customer</ejb-name>

<home>com.sf.ejb.CustomerHome</home>
<remote>com.sf.ejb.Customer</remote>
<ejb-class>com.sf.ejb.CustomerBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>

<env-entry>
<env-entry-name>ejb/BeanFactoryPath</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value><![CDATA[/spring-config-server.xml]]></env-entry-value>
</env-entry>

</session>


CustomerBean implements ICustomer which is the business logic interface.

I am using JNDI template with SimpleRemoteStatelessSessionProxyFactoryBean.

In my client I see no trace of Home and Remote interface.

Only the ICustomer is used cast the lookedup bean.


If i delete the Home and Remote interfaces from classpath, i get an exception.

Can someone explain , how the Home and remote interfaces work woth Proxy and Jndi template.

My Client config.xml

<beans>
<bean id="jndiTemplate"
class="org.springframework.jndi.JndiTemplate">
<constructor-arg>
<props>
<prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
<prop key="java.naming.provider.url">jnp://localhost:1099</prop>
<prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop>
</props>
</constructor-arg>
</bean>

<bean id="customerEjb"
class="org.springframework.ejb.access.SimpleRemoteStatele ssSessionProxyFactoryBean">
<property name="jndiTemplate"><ref bean="jndiTemplate"/></property>
<property name="jndiName">
<value>ejb/Customer</value>
</property>
<property name="businessInterface">
<value>com.sf.intf.ICustomer</value>
</property>
</bean>



</beans>



My Client Code

ClassPathXmlApplicationContext appContext =
new ClassPathXmlApplicationContext(
new String[] {"spring-config-client.xml"});

BeanFactory factory = (BeanFactory) appContext;


//ejb test

// Try 1
try{
ICustomer customer1=(ICustomer)factory.getBean("customerEjb");
System.out.println(customer1.getCustomer(6));
ICustomer customer2=(ICustomer)factory.getBean("customerEjb");
if(customer1==customer2)
System.out.println("Singleton");
else
System.out.println("Non Singleton");
System.out.println(customer2.getCustomer(10));

}catch(Exception e)
{
e.printStackTrace();
}


Thanks
Kevin

Rod Johnson
Mar 21st, 2005, 05:19 AM
I am using JNDI template with SimpleRemoteStatelessSessionProxyFactoryBean.

In my client I see no trace of Home and Remote interface.

Only the ICustomer is used cast the lookedup bean.

What you're describing is Spring's success in abstracting away the client side of the clunky EJB contract. However, the remote and home interfaces are still required: Spring is using them under the covers, just as you would if you had to write the JNDI lookup, EJB home.create call yourself. What Spring is doing behind the scenes is a JNDI lookup for the home, then calling the create() method on the home and enabling you to work with that.

The requirement for home and remote interfaces does not relate to Spring, but is part of the EJB contract.

kevin_405
Mar 21st, 2005, 05:56 AM
Hi Rod,

Thanks for anwsering my queries.

BTW , i throughly impressed with the capabulities of Spring.

Hats off to you and others on creating a superlative framework.

Kevin




I am using JNDI template with SimpleRemoteStatelessSessionProxyFactoryBean.

In my client I see no trace of Home and Remote interface.

Only the ICustomer is used cast the lookedup bean.

What you're describing is Spring's success in abstracting away the client side of the clunky EJB contract. However, the remote and home interfaces are still required: Spring is using them under the covers, just as you would if you had to write the JNDI lookup, EJB home.create call yourself. What Spring is doing behind the scenes is a JNDI lookup for the home, then calling the create() method on the home and enabling you to work with that.

The requirement for home and remote interfaces does not relate to Spring, but is part of the EJB contract.

kevin_405
Mar 21st, 2005, 06:16 AM
Just another question.

How does JNDITemplate/SimpleRemoteStatelessSessionProxyFactoryBean know what is the name of the Home and the remote interface , my client configuration xml does not map the names other than the deployed ejb name.

Thanks
Navin


Hi Rod,

Thanks for anwsering my queries.

BTW , i throughly impressed with the capabulities of Spring.

Hats off to you and others on creating a superlative framework.

Kevin




I am using JNDI template with SimpleRemoteStatelessSessionProxyFactoryBean.

In my client I see no trace of Home and Remote interface.

Only the ICustomer is used cast the lookedup bean.

What you're describing is Spring's success in abstracting away the client side of the clunky EJB contract. However, the remote and home interfaces are still required: Spring is using them under the covers, just as you would if you had to write the JNDI lookup, EJB home.create call yourself. What Spring is doing behind the scenes is a JNDI lookup for the home, then calling the create() method on the home and enabling you to work with that.

The requirement for home and remote interfaces does not relate to Spring, but is part of the EJB contract.

Andreas Senft
Mar 21st, 2005, 06:28 AM
How does JNDITemplate/SimpleRemoteStatelessSessionProxyFactoryBean know what is the name of the Home and the remote interface , my client configuration xml does not map the names other than the deployed ejb name.


Simple: It does not know. :wink:

In the specified JNDI location Spring expects an instance of EJBHome which it uses to create the session bean, invoking the create()-method via reflection.
The type Spring uses for its proxy is declared as businessInterface property in your factory bean. The methods of this proxy are also mapped to the created session bean using reflection.
So using reflection allows Spring to be not aware of the concrete home-interface type or remote-interface type.

Regards,
Andreas