RMI入门小例子--代理模式

本文提供了一个使用Java RMI的简单示例,包括服务器端和客户端的实现代码。该示例通过远程调用实现了字符串消息的传递及一个简单Person对象的创建与返回。

不用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.

整体目录结构截图如下:

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值