PDA

View Full Version : Spring invoking Session Bean



strbuf
May 20th, 2005, 02:42 AM
I'm trying to invoke method from session bean. All I know is doing it the usual way like this:


private net.streambuffer.FooHome getHome() throws NamingException {
return (net.streambuffer.FooHome) getContext().lookup(
net.streambuffer.FooHome.JNDI_NAME);
}

private InitialContext getContext() throws NamingException {
Hashtable props = new Hashtable();

props.put(InitialContext.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(InitialContext.PROVIDER_URL, "jnp://192.168.0.1:1099");

// This establishes the security for authorization/authentication
// props.put(InitialContext.SECURITY_PRINCIPAL,"username");
// props.put(InitialContext.SECURITY_CREDENTIALS,"password");

InitialContext initialContext = new InitialContext(props);
return initialContext;
}

public void testBean() {

try {
net.streambuffer.Foo myBean = getHome().create();

//--------------------------------------
//This is the place you make your calls.
System.out.println(myBean.message("Hi"));

} catch (RemoteException e) {
e.printStackTrace();
} catch (CreateException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}
}


Now does spring offer a better way to access EJB. I'm using spring web and deploying it on Tomcat. So the EJB will be on JBoss and on a different machine.

wpoitras
May 20th, 2005, 09:14 AM
Take a look in the reference manual here (http://static.springframework.org/spring/docs/1.2/reference/ejb.html)

If you are just looking to invoke a bean you can use SimpleRemoteStatelessSessionProxyFactoryBean (http://www.springframework.org/docs/api/org/springframework/ejb/access/SimpleRemoteStatelessSessionProxyFactoryBean.html)

This simplies things for you.

You can also refactor your server side by creating a business object that is just like your EJB but without the EJB parts. Then you can wrap it in a AbstractStatelessSessionBean (http://www.springframework.org/docs/api/org/springframework/ejb/support/AbstractStatelessSessionBean.html). This isn't required but it helps seperate your business logic from the EJB container. It also will allow you to change the way you make remote calls (like using Http instead of EJB (or move it locally) without changing your client code, just your client-side config files.

hadi.muchtar
May 31st, 2005, 03:51 AM
In the case that you need spring as a service locator (which i did in my project), you can use as follow

the application context :

<beans>
<bean id="SysFacadeHome"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>ejb.session.SysFacadeLocalHome</value>
</property>
</bean>
</beans>

your java code:

this.sysFacadeLocal = ((SysFacadeLocalHome)beanFactory.getBean("SysFacadeHome")).create();