EJB客戶端如何不用任何client jar去動態呼叫遠端伺服器的EJB?

zxp8088發表於2008-03-16
我現在的專案遇到一個這樣的問題,希望高手能給與解答。萬分感謝!

客戶端呼叫stateless EJB,客戶端Classpath沒有ejb client jar以及ejb的任何類。試圖透過反射來解決,
但是一直沒有成功。使用如下程式碼,發現依然需要匯入home介面才能執行。無法滿足不匯入任何介面的要求。

//REFLECT EJB..REFLECT..

import javax.ejb.*;
//no-need to import anything..!!
public Class client_ejb_reflection {

public static void main(String args[]){
//1. create initial context
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory" );
env.put(Context.PROVIDER_URL, "jnp://192.168.66.66:1099" );
env.put("Java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");Context c = new InitialContext(env);
//2. look-up home
Object home = ctx.lookup(JNDI_NAME);
EJBHome theHome =(EJBHome) PortableRemoteObject.narrow(home, EJBHome.class)

//3. create the remote

EJBMetaData md =
this.theHome.getEJBMetaData();
Class homeClass =
md.getHomeInterfaceClass();
EJBObject theRemote = (EJBObject)
this.invoke(homeClass,theHome,"create");

//4. use the remote
Class remoteClass =
md.getRemoteInterfaceClass();
String message =
this.invoke(remoteClass,theRemote,"getMessage");
}

static Object invoke(Class c,Object obj,String mname){

Method[] methods = c.getMethods();
String name=null;
Object r= null;
try {
for(int i = 0; i <methods.length ;i++) {
name = methods.getName();
if(name.startsWith(mname)) {
r = methods.invoke(obj,null);
}
}
} catch(Exception e){
e.printStackTrace();

}
return r;
}
}

如果不匯入home介面,就會報如下錯誤:

javax.naming.CommunicationException [Root exception is java.lang.ClassNotFoundException: com.rchema.GET_CUSTOMERHome (no security manager: RMI class loader disabled)]
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:728)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at com.rchema.EJBClientDynamic.dynamic_invoke(EJBClientDynamic.java:73)
at com.rchema.EJBClientDynamic.main(EJBClientDynamic.java:52)

相關文章