- 相關(guān)軟件
>weblogic——遠(yuǎn)程/近程調(diào)用EJB的方法總結(jié) 創(chuàng)建者:webmaster 更新時(shí)間:2005-05-16 22:08
1、 客戶端程序中調(diào)用EJB
前提:EJB要實(shí)現(xiàn)了REMOTE接口
客戶端調(diào)用的代碼可以用EJB Test Client工具生成。自己寫就是這個(gè)樣子:
String url="t3://localhost:7001";
Properties prop=new Properties();
prop.put(Context.PROVIDER_URL,url);
prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
prop.put(Context.SECURITY_PRINCIPAL, "name");
prop.put(Context.SECURITY_CREDENTIALS,"code");
Context context =new InitialContext(prop);
//通過ejb的JNDI name查找到EJBHome對(duì)象
Object ref = context.lookup("ejb/com/J2EE/first/ejb/HelloHome ");
//得到EJBHome
HelloHome trH=(HelloHome) PortableRemoteObject.narrow(obj,HelloHome.class);
//得到EJBObject
DigestSession digestSession = digestSessionHome.create();
Hello tr=trH.create();
System.out.println(tr.hello());
byte[] ret = digestSession.md5(temp.getBytes());//ejb方法調(diào)用
注意:Context.SECURITY_PRINCIPAL和Context.SECURITY_CREDENTIALS是可選的,涉及到對(duì)ejb的操作的權(quán)限。
2、SERVLET中調(diào)用EJB
前提:被調(diào)用的EJB實(shí)現(xiàn)了REMOTE接口
在Servlet中,調(diào)用的代碼應(yīng)該是這個(gè)樣子:
try {
Context context = new InitialContext();
Object ref = context.lookup("UserFacade");
//look up jndi name and cast to Home interface
UserFacadeHome userFacadeHome = (UserFacadeHome) PortableRemoteObject.
narrow(ref, UserFacadeHome.class);
UserFacade userFacade = userFacadeHome.create();
userFacade.updateUser("002","老二"); }
catch (Exception ex) {
ex.printStackTrace();
}
跟客戶端程序中調(diào)用EJB的差別是在Context的生成上,servlet中直接用
Context context = new InitialContext();
而客戶端程序中是用
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, "t3://localhost:7001");
Context context= new InitialContext(properties);
3、 EJB中調(diào)用其他的EJB(同一EJB模塊)
前提:
(1)被調(diào)用者實(shí)現(xiàn)了LOCAL接口,調(diào)用者則實(shí)現(xiàn)了REMOTE接口
(2)調(diào)用者和被調(diào)用者應(yīng)該在同一EJB模塊打包文件(jar)??
(3)調(diào)用者的部署描述(ejb-jar.xml)中有關(guān)于Local ref的描述,如下所示:
UserFacade
UserFacade
ejbtest.test.UserFacadeHome
ejbtest.test.UserFacade
ejbtest.test.UserFacadeBean
Stateless
Container
ejb/user
Entity
ejbtest.test.UserHome
ejbtest.test.User
User
在調(diào)用者中,調(diào)用的程序代碼應(yīng)該是下面的樣子:
package ejbtest.test;import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
import javax.ejb.*;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.rmi.RemoteException;
public class UserFacadeBean
implements SessionBean {
SessionContext sessionContext;
private UserHome userHome;
private static Context context;
public void ejbCreate() throws CreateException {
}
public void ejbRemove() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void setSessionContext(SessionContext sessionContext) {
System.out.println("@@@@@@@@@@@@@@@@ UserFacadeBean.setSessionContext()");
this.sessionContext = sessionContext;
try {
findUserHome();
}
catch (Exception e) {
throw new EJBException(e.getMessage());
}
}
private void findUserHome() throws Exception {
final String ENTITY_NAME = "java:comp/env/ejb/user";
context = new InitialContext();
if (userHome == null) {
try {
Object object = context.lookup(ENTITY_NAME);
userHome = (UserHome) object;
}
catch (Exception e) {
throw new EJBException(e.getMessage());
}
}
}
public void addUser(String id, String name) throws RemoteException {
try {
User user = userHome.create(id);
user.setName(name);
}
catch (Exception ex) {
throw new RemoteException(ex.getMessage());
}
}
}
4、EJB中調(diào)用其他的EJB(不同的EJB模塊)
前提:被調(diào)用者實(shí)現(xiàn)了REMOTE接口
最簡(jiǎn)單的方法是按客戶端程序(或者SERVLET)中調(diào)用EJB的方法。相關(guān)文章
本頁查看次數(shù):