I have created two session beans.
One is ejbuser.HelloEJB.java
package ejbuser;
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class HelloEJB implements SessionBean {
public HelloEJB() {}
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
public String helloWorld() throws RemoteException {
String v = System.getProperty("java.vm.name");
return "Hello client, your javavm Name is " + v + ".";
}
}
The other one is ejbuser.OrderSBEJB.java
package ejbuser;
import java.rmi.RemoteException;
import javax.rmi.PortableRemoteObject;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Collection;
public class OrderSBEJB implements SessionBean {
public OrderSBEJB() {}
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
public String showGreeting() throws RemoteException {
return "This is the greeting message.";
}
public String helloWorld() throws RemoteException {
String sResult = null;
try {
InitialContext icHello = new InitialContext();
Object objHello = icHello.lookup("Hello");
HelloHome helloHome =
(HelloHome)PortableRemoteObject.narrow(objHello,
HelloHome.class);
Hello hello = helloHome.create();
sResult = hello.helloWorld();
} catch(Exception e) {
e.printStackTrace();
} finally {
}
return sResult;
}
}
I can call these two session beans from the servlet as below.
InitialContext icHello = new InitialContext();
Object objHello = icHello.lookup("java:comp/env/ejb/ejbuser/HelloHome");
HelloHome helloHome = (HelloHome)PortableRemoteObject.narrow(objHello,
HelloHome.class);
Hello hello = helloHome.create();
System.out.println(hello.helloWorld());
InitialContext icOrderSB = new InitialContext();
Object objOrderSB =
icOrderSB.lookup("java:comp/env/ejb/ejbuser/OrderSBHome");
OrderSBHome orderSBHome =
(OrderSBHome)PortableRemoteObject.narrow(objOrderSB, OrderSBHome.class);
OrderSB orderSB = orderSBHome.create();
System.out.println(orderSB.showGreeting());
But when I call
System.out.println(orderSB.helloWorld());
There is the error message.
1) Name "Hello" is not bound in this context.
The ejb-jar.xml file is:
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee" version="2.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
<enterprise-beans>
<session>
<display-name>OrderSB</display-name>
<ejb-name>OrderSB</ejb-name>
<home>ejbuser.OrderSBHome</home>
<remote>ejbuser.OrderSB</remote>
<ejb-class>ejbuser.OrderSBEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
<session>
<display-name>Hello</display-name>
<ejb-name>Hello</ejb-name>
<home>ejbuser.HelloHome</home>
<remote>ejbuser.Hello</remote>
<ejb-class>ejbuser.HelloEJB</ejb-class>
<session-type>Stateless</session-type>
</session>
</enterprise-beans>
</ejb-jar>
There is something wrong when calling session bean HelloEJB from method
helloWorld() in OrderSBEJB. What is the correct way in looking up HelloEJB
from inside OrderSBEJB? Please advise, thanks.
SK
--
View this message in context:
http://www.nabble.com/How-to-call-session-bean-from-another-session-bean-tp25715830p25715830.html
Sent from the OpenEJB User mailing list archive at Nabble.com.