2.客户端:
1)通过符合JRMP规范的URL字符串在注册表中获取并强转成Remote子接口对象;
2)调用这个Remote子接口对象中的某个方法就是为一次远程方法调用行为
IHelloService
package com.lwl.demo;
import com.lwl.pojo.User;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface IHelloService extends Remote {
//定义sayHello 方法
public String sayHello(User user)throws RemoteException;
}
User
package com.lwl.pojo;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = -1;
private String username;
private Integer age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User(String username, Integer age) {
this.username = username;
this.age = age;
}
}
RMIClient
package com.lwl.client;
import com.lwl.demo.IHelloService;
import com.lwl.pojo.User;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class RMIClient {
public static void main(String[] args) throws RemoteException, NotBoundException, MalformedURLException {
//从注册表中获取远程对象 强转
IHelloService service = (IHelloService) Naming.lookup("//127.0.0.1:8888/rmiServer");
//准备参数
User user = new User("laowang", 18);
//调用远程方法sayHello
String s = service.sayHello(user);
System.out.println(s+"------------");
//
}
}