不用rmic命令行的小例子,很简单,所以不作过多注释。给代码:
SERVER 端:
/**
*
*/
package rmi.server;
import java.rmi.Remote;
import java.rmi.RemoteException;
/**
* @author Administrator
*
*/
public interface HelloInterface extends Remote {
public String sayHello(String name) throws RemoteException;
public PersonInterface getHelloOwner(String name) throws RemoteException; //定义了一个Person类,不是简单Java类型
}
/**
*
*/
package rmi.server;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Random;
public class HelloImpl extends UnicastRemoteObject implements HelloInterface {
private static final long serialVersionUID = 201012231054l;
private String message;
public HelloImpl() throws RemoteException{}
public HelloImpl(String message) throws RemoteException
{
this.message = message;
}
public void setMessage(String message)
{
this.message=message;
}
public String getMessage()
{
return this.message;
}
public String sayHello(String name) throws RemoteException {
// TODO Auto-generated method stub
System.out.println("Say hello at server begins.");
return "Hello,"+name+".Your message is:"+this.getMessage();
}
public PersonInterface getHelloOwner(String name) throws RemoteException
{
PersonImpl person = new PersonImpl();
person.setName(name);
person.setAge(new Random().nextInt(100)); //随机的,没有什么意义
return (PersonInterface)person;
}
}
/**
* 简单的Person类
*/
package rmi.server;
import java.io.Serializable;;
public interface PersonInterface extends Serializable {
public String getName();
public int getAge();
}
/**
* 依然是简单的实现
*/
package rmi.server;
public class PersonImpl implements PersonInterface {
private static final long serialVersionUID=201012231350l;
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName()
{
return this.name;
}
public int getAge()
{
return this.age;
}
}
Server端一切OK,现在注册到服务端吧:
package rmi.server;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class HelloServer {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
LocateRegistry.createRegistry(1099);//默认的端口
HelloInterface hello = new HelloImpl("this is a message for you");
//Naming.rebind("hello", hello); //服务器和客户端在同一JAVA虚拟机时可以这么实现
Naming.rebind("//19.186.54.117:1099/hello", hello);//这种方式更具有普遍意义
System.out.println("Hello Server is already.");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行Server:
Hello Server is already.
CLIENT端:
package rmi.client;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import rmi.server.PersonInterface;
public class HelloClient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//HelloInterface hello = (HelloInterface)Naming.lookup("hello");//同一java虚拟机上时
rmi.server.HelloInterface hello = (rmi.server.HelloInterface)Naming.lookup("rmi://19.186.54.117:1099/hello");
System.out.println(hello.sayHello("LMC"));
PersonInterface person=hello.getHelloOwner("WY");
System.out.println("Name:"+person.getName()+" Age:"+person.getAge());
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意,客户端需要得到服务器端的interfaces,所以需要将PersonInterface和HelloInterface添加到classpath路径中,同时这个例子中有Person类,而非全部是简单的Java基本类型,客户端会需要PersonImpl类(hello.getHelloOwner()方法),所以需要将PersonImpl添加到classpath中。
运行客户端:
Hello,LMC.Your message is:this is a message for you
Name:WY Age:62
此时查看服务端:
Hello Server is already.
Say hello at server begins.
整体目录结构截图如下: