昨天倒騰了一天終於配置好了jboss as 7的域,今天又倒騰了一整天在上面部署了個EJB,然後試了一個利用JNDI來進行遠端呼叫。下面記錄一下過程中那些亂七八糟的問題:

首先是這個jboss-client.properties檔案,各個屬性值的含義可以從jboss的官方文件上得到解釋https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI

 endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=192.168.1.33
remote.connection.default.port=4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.default.username=mosmith
remote.connection.default.password=MTIzNDU2

endpoint.name是個可選的引數,用於EJB Receiver建立連線所用的名稱,如果不設定將使用預設值:config-based-ejb-client-endpoint

remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false官方上說是用在建立connection provider 上處理 remote://協議的引數,但具體是什麼沒有說,照這名字看來好像與安全有關的(純屬猜測)

remote.connections=default,這是指定連線配置名的,我們可以建立多個配置,具體做法請看官方文件

上面需要注意的是:

username是Jboss中application user 中的使用者

password這個屬性是用使用者密碼的base64編碼。

下面是我的程式遠端呼叫程式碼,基本和官方給出的一樣:

 package com.bes.training.ejb.EJBRemoteInvocation;

 import com.bes.training.ejb.Helloworld.*;

 import javax.naming.*;
import java.util.*; public class RemoteInvocationDemo {
public static void main(String[] args){ Hashtable<String,String> jndiProperties=new Hashtable<String,String>();
jndiProperties.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
//jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.remote.client.InitialContextFactory");
jndiProperties.put("jboss.naming.client.ejb.context","true"); try{
Context context=new InitialContext(jndiProperties);
Object obj=context.lookup("ejb:/temp//SayHelloImpl!com.bes.training.ejb.Helloworld.SayHelloImplRemote" ); SayHelloImplRemote intf=(SayHelloImplRemote) obj;
String result=intf.sayHello("Mosmith");
System.out.println(result); }catch(NamingException e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}

但是在eclipse中執行的時候出現了No EJB receiver available for handling的異常:倒騰了好長時間,於是懷疑-Djboss.ejb.client.properties.file.path=jndi.properties引數沒有起作用,於是在命令列下運行了一下,正常!回eclipse仔細檢查,發現是引數的設定錯了:

應該設定在VM arguments而不是Program arguments裡。初學者一定要注意這裡的區別啊。