当前位置: 技术问答>java相关
请教:两个EJB这间怎样互相调用?给个例子好不?
来源: 互联网 发布时间:2015-10-14
本文导语: 请教:两个EJB这间怎样互相调用?给个例子好不? | 下面的例子,取自WebLogic下的bans. 上面两位说的都对,我这里是示范如何在程序中调用。 1。先找到EJB的Home.例子中要调的EJB是:MusicLibrary ...
请教:两个EJB这间怎样互相调用?给个例子好不?
|
下面的例子,取自WebLogic下的bans.
上面两位说的都对,我这里是示范如何在程序中调用。
1。先找到EJB的Home.例子中要调的EJB是:MusicLibrary
/**
* Look up the MusicLibrary bean's home interface using JNDI.
*/
private MusicLibraryHome lookupMusicLibraryHome()
throws NamingException
{
Context ctx = getInitialContext();
try {
Object home =
(MusicLibraryHome)ctx.lookup("MusicLibraryEJB.MusicLibraryHome");
return (MusicLibraryHome)PortableRemoteObject.narrow(
home,
MusicLibraryHome.class);
} catch (NamingException ne) {
log("The client was unable to lookup the EJBHome. Please make sure " +
"that you have deployed the ejb with the JNDI name " +
"MusicLibraryEJB.MusicLibraryHome on the WebLogic server at "+url);
throw ne;
}
}
/**
* Get an initial context into the JNDI tree.
*
* Java2 clients can use the jndi.properties file to set the
* INITIAL_CONTEXT_FACTORY and the PROVIDER_URL
* private Context getInitialContext() throws NamingException {
* return new InitialContext();
* }
*
*
* Using a Properties object will work on JDK 1.1.x and Java2
* clients
*/
private Context getInitialContext() throws NamingException {
try {
// Get an InitialContext
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
if (user != null) {
log ("user: " + user);
h.put(Context.SECURITY_PRINCIPAL, user);
if (password == null)
password = "";
h.put(Context.SECURITY_CREDENTIALS, password);
}
return new InitialContext(h);
} catch (NamingException ne) {
log("We were unable to get a connection to the WebLogic server at "+url);
log("Please make sure that the server is running.");
throw ne;
}
}
2。创建实例:(一句话而已,但前面要写对)
MusicLibrary musicLib = musicLibraryHome.create();
3。调用EJB的方法:
// Remove any old data from the database.
musicLib.removeRecordingsThatExist(recInfos);
musicLib.removeBandsThatExist(bandInfos);
// Create the Bands in the Database
musicLib.addBands(bandInfos);
我想可以了吧! 你的JNDI name不要搞错
上面两位说的都对,我这里是示范如何在程序中调用。
1。先找到EJB的Home.例子中要调的EJB是:MusicLibrary
/**
* Look up the MusicLibrary bean's home interface using JNDI.
*/
private MusicLibraryHome lookupMusicLibraryHome()
throws NamingException
{
Context ctx = getInitialContext();
try {
Object home =
(MusicLibraryHome)ctx.lookup("MusicLibraryEJB.MusicLibraryHome");
return (MusicLibraryHome)PortableRemoteObject.narrow(
home,
MusicLibraryHome.class);
} catch (NamingException ne) {
log("The client was unable to lookup the EJBHome. Please make sure " +
"that you have deployed the ejb with the JNDI name " +
"MusicLibraryEJB.MusicLibraryHome on the WebLogic server at "+url);
throw ne;
}
}
/**
* Get an initial context into the JNDI tree.
*
* Java2 clients can use the jndi.properties file to set the
* INITIAL_CONTEXT_FACTORY and the PROVIDER_URL
* private Context getInitialContext() throws NamingException {
* return new InitialContext();
* }
*
*
* Using a Properties object will work on JDK 1.1.x and Java2
* clients
*/
private Context getInitialContext() throws NamingException {
try {
// Get an InitialContext
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
if (user != null) {
log ("user: " + user);
h.put(Context.SECURITY_PRINCIPAL, user);
if (password == null)
password = "";
h.put(Context.SECURITY_CREDENTIALS, password);
}
return new InitialContext(h);
} catch (NamingException ne) {
log("We were unable to get a connection to the WebLogic server at "+url);
log("Please make sure that the server is running.");
throw ne;
}
}
2。创建实例:(一句话而已,但前面要写对)
MusicLibrary musicLib = musicLibraryHome.create();
3。调用EJB的方法:
// Remove any old data from the database.
musicLib.removeRecordingsThatExist(recInfos);
musicLib.removeBandsThatExist(bandInfos);
// Create the Bands in the Database
musicLib.addBands(bandInfos);
我想可以了吧! 你的JNDI name不要搞错
|
在实施描述符中加入(在一个包中)时
在调用bean的
//加入
(被调bean的JNDI Name)(形式为ejb/名)
(被调bean的ejb-name)
调用时用查找EJB的方法就行
(lookup("java:comp/env/ejb/名"));
还有,你要是使用weblogic作为服务器的话还要在weblogic-ejb-jar.xml
的
//加入
(注意与前面的一致)
(注意与前面的一致)
(把包中的所有bean都要按此形式写上)
在调用bean的
//加入
(被调bean的JNDI Name)(形式为ejb/名)
(被调bean的ejb-name)
调用时用查找EJB的方法就行
(lookup("java:comp/env/ejb/名"));
还有,你要是使用weblogic作为服务器的话还要在weblogic-ejb-jar.xml
的
//加入
(注意与前面的一致)
(注意与前面的一致)
(把包中的所有bean都要按此形式写上)
|
ejb的client包括application、applet、jsp/servlet等等,也包括其他ejb。调用代码是一样的。
|
上面两个人已经很好的回答你的问题了.应该可以结局了.