51,407
社区成员
发帖
与我相关
我的任务
分享
//helloServer.java
public interface HelloServer extends Remote
{
public String echo(String msg) throws RemoteException ;
}
//HelloServiceImpl.java
public class HelloServiceImpl extends UnicastRemoteObject implements HelloServer
{
private String name ;
public HelloServiceImpl(String name) throws RemoteException
{
this.name = name ;
}
public String echo(String msg) throws RemoteException
{
System.out.println(name + ":调用echo()方法") ;
return "echo: " + msg + " from " + name ;
}
}
//SimpleServer.java
public class SimpleServer
{
public static void main(String[] args) throws Exception
{
HelloServer service1 = new HelloServiceImpl("service1") ;
HelloServer service2 = new HelloServiceImpl("service2") ;
Context namingContext = new InitialContext() ;
namingContext.rebind("rmi://localhost:1099/HelloService1" , service1) ;
namingContext.rebind("rmi://localhost:1099/HelloService2" , service2) ;
namingContext.rebind("rmi://10.10.160.16:1099/HelloService1" , service1) ;
namingContext.rebind("rmi://10.10.160.16:1099/HelloService2" , service2) ;
namingContext.rebind("rmi:HelloService1" , service1) ;
namingContext.rebind("rmi:HelloService2" , service2) ;
System.out.println("服务器注册了两个HelloService对象") ;
}
}
