不用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.
整体目录结构截图如下:
分享到:
相关推荐
最简单的RMI入门好例子,看看远程调用的效果吧,直接在eclipse下导入就可以运行。
自己收集了一些跑得通的教程,并自己整理给出了,入门例子。包括:最原生的使用javac和rmic的例子;还有进一步的,使用IDM,用代码代理原生rmic编译的例子;还有RMI和Spring整合的例子。 另外附有:例子说明 和 一些...
SpringRMI小例子代码,仅供参考。SpringRMI小例子代码,仅供参考。
描述java rmi的实用方式,有实用简单的入门介绍,很容易理解!!!
通过这个例子可以大概了解RMI的很多机制
rmi原理介绍及介绍,适合重新温习,中文版pdf
闲来无事,做了个Spring RMI的小例子,非常易懂。
rmi multi-core mips cpu program manual
RMI应用开发小例子源码.rar RMI应用开发小例子源码.rar RMI应用开发小例子源码.rar
RMI教程 Java RMIRMI教程 Java RMIRMI教程 Java RMIRMI教程 Java RMIRMI教程 Java RMI
最简单 最有效而且是最易懂的rmi理解例子
Java RMI远程方法调用详解-例子代码,例子详解在:http://blog.csdn.net/guyuealian/article/details/51992182
NULL 博文链接:https://wulinhaoxia5.iteye.com/blog/1526803
这是本人刚学习RMI时整理出来的一份RMI入门级的资料,其中有几个简单的RMI应用的例子,公布出来供初学RMI的朋友参考学习,提高初学者对RMI学习的速度...
现在虽然我们很少用到RMI进行编程,但是做为Java一个最早的分布式解决方案,我们还是有了解的必要,从例子开始吧!
NULL 博文链接:https://dolphin-ygj.iteye.com/blog/539379
spring RMI 简单例子
RMI例子,可以试试看
rmi简单例子,service实例和clent实例
Lucene 7.1 RMI远程搜索例子 Lucene 7.1 RMI远程搜索例子 Lucene 7.1 RMI远程搜索例子