我想使用Jboss RMI(或JNDI)端口1099部署RMI。當Jboss服務器不在圖片中時(即使用主程序),每件事情都正常工作。爲了部署在服務器上,我嘗試了兩種方法。
1)獲取Jboss註冊表端口並將RMI註冊表綁定到該端口。 以下是我已經寫在servlet的 的init方法的代碼(這裏服務器是我的接口,擴展Remote,ServerImplementor是類實現服務器)Jboss服務器中的RMI部署
public void init() throws ServletException { { Server implementor=new ServerImplementor(); Remote obj = UnicastRemoteObject.exportObject(implementor, 0); Registry r=LocateRegistry.getRegistry(); System.out.println("Registry post::"+r.REGISTRY_PORT); r.bind("TestRMI", obj); } catch(Exception e) { e.printStackTrace(); } }
這種方法的問題是,當我運行客戶端用下面的代碼:
registry = LocateRegistry.getRegistry("localhost",1099); hello = (Server) (registry.lookup("TestRMI"));
The error which i get is: java.rmi.ConnectIOException: non-JRMP server at remote endpoint
2) The 2nd approach which i tried is using JNDI, exposing a RMI(This is one approach which i came to know through googling)
Following is the code for the init method of servlet.Here the protocol used is jnp
try { Server remoteObj=new ServerImplementor(); Properties properties=new Properties(); properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); properties.put("java.naming.provider.url", "jnp://localhost:1099"); properties.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); InitialContext initialContext=new InitialContext(properties); initialContext.rebind("TestRMI", remoteObj); System.out.println("server up"); } catch (RemoteException e) { e.printStackTrace(); } catch(NamingException n) { n.printStackTrace(); }
When i run the client for the 2nd approach it gives error "TestRMI is not bound"
Client code is:
main(){ Properties properties=new Properties(); properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); properties.put("java.naming.provider.url", "jnp://localhost:1099"); properties.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); InitialContext initialContext=new InitialContext(properties); Server server=(Server)PortableRemoteObject.narrow(initialContext.lookup("TestRMI"), Server.class); }
So, i am not able to figure out what is the correct approach for deploying RMI in Jboss. Is it correct to write code in Servlet init method and create a registry.Or we have to use EJB,etc.. If any one have done this before please help me.
Thanks
您是否在(i)'bind()'上發生了該錯誤? (ii)'lookup()'? (ii)調用遠程方法? – EJP