问一个简单的关于EJB2.0的问题,300分,哪位大侠帮个忙,谢谢(300分)

隐儿

Unregistered / Unconfirmed
GUEST, unregistred user!
在ejb2.0中,怎么在一个bean中用local interface调用
另外一个bean中的方法,给点代码如何,谢谢!!!
 
我也刚在学,你是不是指这个。
Passing a Session Bean’s Object
Reference
Suppose that your session bean needs to pass a reference to itself
to another bean.You might want to pass the reference, for example,
so that the second bean can call your session bean’s methods.
You can’t pass the this reference because the session
bean is not a remote object. Instead, your bean must pass an
object reference for the instance. It gets the reference to
itself by calling the getEJBObject method of the SessionContext
interface. This interface provides a session bean with access to the
instance context maintained by the EJB container. Typically, the bean
saves the context in the setSessionContext method. The following
code fragment shows how you might use these methods.
public class WagonEJB implements SessionBean {
SessionContext context;
. . .
public void setSessionContext(SessionContext sc) {
this.context = sc;
}
. . .
public void passItOn(Basket basket) {
. . .
basket.copyItems(context.getEJBObject());
}
. . .
 
无论在ejb、Bean 、servlet。。。
调用其他EJB的方法都一样,
只要能够找到被调用BEAN的home 、remote接口。
HOME接口 MHome
REMOTE接口 MObject
Context mCtx = new InitialContext();
MHome mHome = (MHome)mCtx.lookup("code.MHome");
Object pOb = mHome.findByPrimaryKey("000");
MObject object = (MObject)PortableRemoteObject.narrow(pOb,MObject.class);
String value = object.getValue();
or
MObject object = (MObject)mHome.findByPrimaryKey("000");
String value = object.getValue();

 
To zhuny:
你用的是远程(remote)的调用,
如果用 local interface怎么做呢?
 
使用EJB2的HOMY方法,在BEAN中的方法实现一样
也就是在HOME接口中申明HOME方法,在BEAN中实现
你指ENTITY还是SESSTION BEAN。SESSION没有HOME方法的概念
 
to zhuny:
就是不会代码,
给个代码的实例怎么样?
谢谢!
就是怎么创建另外一个bean的local home对象,
然后用这个对象找到local object,
然后调用。
 
前面已经写了的!
我不知道你是要调用一个ENTITY还是一个SESSION BEAN?
比如要调用一个ENTITY EJB,该EJB有的接口为:
HOME接口 MHome
REMOTE接口 MObject
创建一个上下文
Context mCtx = new InitialContext();
从上下文中通过JNDI名找并强制转换成HOME接口对象(捷径)
MHome mHome = (MHome)mCtx.lookup("code.MHome");
调用HOME接口的方法,返回一个REMOTE接口对象(一个REMOTE接口对象,可以看作是一条记录)
Object pOb = mHome.findByPrimaryKey("000");
MObject object = (MObject)PortableRemoteObject.narrow(pOb,MObject.class);
调用REMOTE接口的方法
String value = object.getValue();
************************************************
如果是调用一个SESSION BEAN
创建一个上下文
Context mCtx = new InitialContext();
从上下文中通过JNDI名找并强制转换成HOME接口对象(捷径)
MHome mHome = (MHome)mCtx.lookup("code.MHome");
调用HOME接口的CREATE方法,就可返回一个REMOTE接口对象
MObject object = mHome.Create();
调用REMOTE接口的方法
String value = object.getValue();
 
to zhuny:
你的方法用了narrow,
那是rmi/iiop才用的,
local interface似乎不用那个吧?
我的只是两个stateless session bean中互相调用的,
谢谢!
 
没错!
如果是在WEBLOGIC环境下,两种方式都可行!
正如:
MHome mHome = (MHome)mCtx.lookup("code.MHome");
可以写成
Object or = mCtx.lookup("code.MHome");
MHome mHome = (MHome)PortableRemoteObject.narrow(or, MHome.class);
 
那我要local interface呢?
在其他的环境下。
 
是否支持哪种方式
我想应该与EJB CONTAINER有关系。
不同的服务商实现的程度不一样。
BTW:你的 local interface 到底是指什么?
 
例子: REMOTE INTERFACE
public interface Hello extends javax.ejb.EJBObject
{ /** * The one method - hello - returns a greeting to the client. */
public String hello() throws java.rmi.RemoteException;
}
LOCAL INTERFACE
public interface HelloLocal extends javax.ejb.EJBLocalObject
{ /** * The one method - hello - returns a greeting to the client. */
public String hello();
}
HOME INTERFACE
public interface HelloHome extends javax.ejb.EJBHome
{ /* * This method creates the EJB Object. * * @return The newly created EJB Object. */
Hello create() throws java.rmi.RemoteException, javax.ejb.CreateException;
}
LOCAL HOME INTERFACE
public interface HelloLocalHome extends javax.ejb.EJBLocalHome
{ /* * This method creates the EJB Object. * * @return The newly created EJB Object. */
HelloLocal create() throws javax.ejb.CreateException;
}
 
实际上问题也就是怎么得到另外一个
bean的localhome interface的引用
 
一样的过程:
比如在session bean的 setSessionContext(SessionContext c) 方法中写上:
Context mCtx = new InitialContext();
MHome mHome = (MHome)mCtx.lookup("code.MHome");
 

public interface Band extends EJBLocalObject
{
public Date getStartDate();

}
*********************************
/**
*/
public interface BandHome extends EJBLocalHome
{
public Band create(String name, String founder, Date startDate)
throws CreateException;
}
*******************************************调用者******************
public class MusicLibraryBean implements SessionBean {
private SessionContext ctx;
private BandHome bandHome;

//
// business methods


public void addBand(BandInfo bandInfo) {
try {
Band b = bandHome.create(bandInfo.getName(), bandInfo.getFounder(), bandInfo.getStartDate());
} catch (CreateException ce) {
throw new EJBException(ce);
}
}
//
// EJB lifecycle methods
//
public void setSessionContext(SessionContext c) {
ctx = c;
try {
Context ic = new InitialContext();
bandHome = (BandHome) ic.lookup("BandEJB.BandHome");

} catch (NamingException ne) {
throw new EJBException(ne);
}
}
public void ejbCreate() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbRemove() {}
}
 
接受答案了.
 
顶部