You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by SK Leung <si...@yahoo.com.hk> on 2009/10/02 15:26:45 UTC

How to call session bean from another session bean

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.


Re: How to call session bean from another session bean

Posted by David Blevins <da...@visi.com>.
Hi SK,

For EJB 2.x you just need to use xml in the deployment descriptor  
(web.xml or ejb-jar.xml).  Here are a couple examples:

  http://openejb.apache.org/3.0/ejb-ref.html
  http://openejb.apache.org/3.0/ejb-local-ref.html

-David

On Oct 5, 2009, at 6:13 AM, SK Leung wrote:

>
> Hi Jean-Louis,
> My OpenEJB version is 3.0.
> The examples that I got are all with EJB3. Do you have the example for
> EJB2.x? If yes, please send it to me or email me the URL link. Much  
> thanks.
>
> SK
>
> Jean-Louis MONTEIRO wrote:
>>
>> Hi,
>>
>> Actually, i don't have a big experience with EJB 2.x.
>> Which OpenEJB version are you using?
>>
>> Did you have a look to examples?
>> As far as i know, there are EJB 2.x examples.
>>
>> Jean-Louis
>>
>>
>> SK Leung wrote:
>>>
>>> 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-tp25715830p25750609.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>
>


Re: How to call session bean from another session bean

Posted by SK Leung <si...@yahoo.com.hk>.
Hi Jean-Louis,
My OpenEJB version is 3.0.
The examples that I got are all with EJB3. Do you have the example for
EJB2.x? If yes, please send it to me or email me the URL link. Much thanks.

SK

Jean-Louis MONTEIRO wrote:
> 
> Hi,
> 
> Actually, i don't have a big experience with EJB 2.x.
> Which OpenEJB version are you using?
> 
> Did you have a look to examples?
> As far as i know, there are EJB 2.x examples.
> 
> Jean-Louis
> 
> 
> SK Leung wrote:
>> 
>> 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-tp25715830p25750609.html
Sent from the OpenEJB User mailing list archive at Nabble.com.


Re: How to call session bean from another session bean

Posted by Jean-Louis MONTEIRO <je...@atosorigin.com>.
Hi,

Actually, i don't have a big experience with EJB 2.x.
Which OpenEJB version are you using?

Did you have a look to examples?
As far as i know, there are EJB 2.x examples.

Jean-Louis


SK Leung wrote:
> 
> 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-tp25715830p25741508.html
Sent from the OpenEJB User mailing list archive at Nabble.com.