http://hedaoyuan.blog.51cto.com/4639772/813702java
package com.itjob;
import java.rmi.*;
public interface RmiSample extends Remote{
public int sum(int a,int b) throws RemoteException;
}
|
package com.itjob.rmi;
import java.rmi.*;
import java.rmi.server.*;
import com.itjob.RmiSample ;
/**
遠程接口實現類,繼承了UnicastRemoteObject並實現了RmiSample遠程接口
*/
public class RmiSampleImpl extends UnicastRemoteObject implements RmiSample{
//覆蓋默認構造函數並拋出RemoteException
public RmiSampleImpl() throws RemoteException{
super();
}
//全部遠程實現方法必須拋出RemoteException
public int sum(int a,int b) throws RemoteException{
return a+b;
}
}
|
package com.itjob.rmi;
import java.rmi.*;
import java.rmi.registry.*;
public class RmiSampleServer{
public static void main(String[] args){
/*建立和安裝一個安全管理器,令其支持RMI.做爲Java開發包的一部分
*適用於RMI惟一一個是RMISecurityManager.
*
if(System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
*/
try{
LocateRegistry.createRegistry(8808);
RmiSampleImpl server=new RmiSampleImpl();
Naming.rebind("//localhost:8808/SAMPLE-SERVER",server);
System.out.println ("遠程對象註冊成功,RMI服務已經啓動,等待客戶端調用....");
}catch(java.net.MalformedURLException me){
System.out.println ("Malformed URL:"+me.toString());
}catch(RemoteException re){
System.out.println ("Remote exception:"+re.toString());
}catch(AlreadyBoundException abe){
System.out.println ("(AlreadyBound exception:"+ abe.toString());
}
}
}
|
package com.itjob.rmi;
import java.rmi.*;
import java.rmi.server.*;
public class RmiSampleClient {
public static void main(String[] args)
{
try {
String url = "//localhost:8808/SAMPLE-SERVER";
RmiSample RmiObject = (RmiSample)Naming.lookup(url);
System.out.println(" 1 + 2 = " + RmiObject.sum(1,2) );
} catch (RemoteException exc) {
System.out.println("Error in lookup: " + exc.toString());
} catch (java.net.MalformedURLException exc) {
System.out.println("Malformed URL: " + exc.toString());
} catch (java.rmi.NotBoundException exc) {
System.out.println("NotBound: " + exc.toString());
}
}
}
|
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "localhost:1099");
InitialContext = new InitialContext(props);
|
名稱
|
服務提供程序工廠
|
文件系統
|
com.sun.jndi.fscontext.RefFSContextFactory
|
LDAP
|
com.sun.jndi.ldap.LdapCtxFactory
|
RMI
|
com.sun.jndi.rmi.registry. RegistryContextFactory
|
CORBA
|
com.sun.jndi.cosnaming.CNCtxFactory
|
DNS
|
com.sun.jndi.dns.DnsContextFactory
|
package com.itjob.rmi;
import java.rmi.*;
import java.rmi.registry.*;
import javax.naming.*;
public class RmiSampleServerJndi{
public static void main(String[] args) throws Exception{
LocateRegistry.createRegistry(8808);
RmiSampleImpl server=new RmiSampleImpl();System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.rmi.registry.RegistryContextFactory");
System.setProperty(Context.PROVIDER_URL,"rmi://localhost:8808");
InitialContext ctx=new InitialContext();
ctx.bind("java:comp/env/SampleDemo",server);
ctx.close();
}
}
|
package com.itjob.rmi;
import java.rmi.*;
import java.rmi.server.*;
import javax.naming.*;
public class RmiSampleClientJndi {
public static void main(String[] args) throws Exception
{
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.rmi.registry.RegistryContextFactory");
System.setProperty(Context.PROVIDER_URL,"rmi://localhost:8808");
InitialContext ctx=new InitialContext();
String url = "java:comp/env/SampleDemo";
RmiSample RmiObject = (RmiSample)ctx.lookup(url);
System.out.println(" 1 + 2 = " + RmiObject.sum(1,2) );
}
}
|