问题描述
我刚刚开始使用 Java RMI,在使用 java.io.Serializable 时遇到了一些问题,所以谁能给我一个必须实现 java.io.Serializable 的 RMI 示例.
谢谢!!!更新:我做了一个简单的例子,但是,我认为仍然存在问题,因为输出不正确.人机界面
包服务器;导入 java.rmi.Remote;导入 java.rmi.RemoteException;导入 java.rmi.server.UnicastRemoteObject;
公共接口 PersonInterface 扩展 Remote{public void setName(String name) 抛出 RemoteException;public String getPerson() 抛出 RemoteException;public void setAddress(Address address) 抛出 RemoteException;}
个人实施
包服务器;导入 java.rmi.server.UnicastRemoteObject;导入 java.rmi.RemoteException;导入 java.rmi.Naming;导入 java.rmi.Remote;类 Person 扩展 UnicastRemoteObject 实现 PersonInterface{私人字符串名称;私人整数年龄;私人地址地址;Person() 抛出 RemoteException {super();}Person(String name,int age, Address address) 抛出 RemoteException {this.name = 名称;this.age = 年龄;this.address = 地址;}public void setName(String name) 抛出 RemoteException {this.name = 名称;}public void setAddress(Address address) 抛出 RemoteException{this.address = 地址;}public String getPerson() 抛出 RemoteException {返回人:"+姓名+年龄:"+年龄+地址:"+地址;}}
地址类
包服务器;导入 java.io.Serializable;公共类地址实现可序列化{私有静态最终长serialVersionUID = 227L;私人字符串地址1;私人字符串地址2;公共地址() {}公共地址(字符串 addre1,字符串 addre2){this.addre1 = addre1;this.addre2 = addre2;}}
服务器
包服务器;导入 java.rmi.Naming;类服务器{public static void main(String[] args){尝试{//创建一个RemoteDatabaseServer的实例人人 = 新人();//rmi://[host][:port]/objectString namePerson = "rmi://localhost:9999/person";//将此实例绑定到localhost port999,名称为databaseNaming.bind(namePerson, person);System.out.println("服务器正在运行...");}catch(异常前){System.out.println("服务器异常...");ex.printStackTrace();}}}
客户
打包客户端;导入 java.rmi.RMISecurityManager;导入 java.rmi.Naming;导入 server.PersonInterface;导入服务器.地址;类客户{public static void main(String[] args){尝试{System.setSecurityManager(new RMISecurityManager());String namePerson = "rmi://localhost:9999/person";PersonInterface 人 =(PersonInterface)Naming.lookup(namePerson);person.setName("myName");System.out.println(person.getPerson());person.setName("myNewName");地址 address = new Address("123","123");person.setAddress(地址);System.out.println(person.getPerson());}catch(异常前){System.out.println("客户端失败...");ex.printStackTrace();}}}
我得到的输出是
D:\java -Djava.security.policy=d:\Client\policy\client.policy client.Client人:myName 年龄:0 地址:server.Address@1d6776d人员:myNewName 年龄:0 地址:server.Address@10a2d64地址打印不正确PS:正如您从 Client 类导入中看到的
import server.PersonInterface;导入服务器.地址;
我已将 PersonInterface.class 和 Address.class 复制到客户端以进行客户端编译.
最终的:这么笨!!!将以下代码添加到 Address.java
public String toString(){返回地址1+"+地址2;}
好的,问题解决了!!:)
interface MyInterface extends Remote {MyClass f(MyClass x) 抛出 RemoteException;}class MyClass 实现了可序列化的 {私有整数值;公共 MyClass(int 值) {this.value = 值;}公共 int getValue() {返回值;}}
你需要Serializable接口来告诉你的类可以通过网络发送
服务器代码
class Service extends UnicastRemoteObject 实现 MyInterface {公共服务() {}公共 MyClass f(MyClass v) 抛出 RemoteException {返回新的 MyClass(v.getValue() + 1)}公共静态无效主(Strint arg []){注册表 r = LocateRegistry.createRegistry(1099);r.rebind("service", new Service());}}
客户端代码
class 客户端 {公共静态无效主(Strint arg []){Registry r = LocateRegistry.getRegistry("localhost", 1099);MyInterface service = (MyInterface)r.lookup("service");MyClass 结果 = service.f(new MyClass(123));System.out.println(result.getValue());//这里打印124}}
I am just starting Java RMI and have some problems with when to use java.io.Serializable, so can anyone give me a RMI example that java.io.Serializable has to be implemented.
Thanks!!!
UPDATE:i had made a simple example, however, i think there are still problems as the output is not correct.Person Interface
package server;import java.rmi.Remote;import java.rmi.RemoteException;import java.rmi.server.UnicastRemoteObject;
public interface PersonInterface extends Remote
{
public void setName(String name) throws RemoteException;
public String getPerson() throws RemoteException;
public void setAddress(Address address) throws RemoteException;
}
Person Implementation
package server;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.rmi.Naming;
import java.rmi.Remote;
class Person extends UnicastRemoteObject implements PersonInterface
{
private String name;
private int age;
private Address address;
Person() throws RemoteException {super();}
Person(String name,int age, Address address) throws RemoteException {
this.name = name;
this.age = age;
this.address = address;
}
public void setName(String name) throws RemoteException {
this.name = name;
}
public void setAddress(Address address) throws RemoteException{
this.address = address;
}
public String getPerson() throws RemoteException {
return "Person : " + name + " age : " + age + " address : " + address;
}
}
Address Class
package server;
import java.io.Serializable;
public class Address implements Serializable
{
private static final long serialVersionUID = 227L;
private String addre1;
private String addre2;
public Address() {}
public Address(String addre1,String addre2){
this.addre1 = addre1;
this.addre2 = addre2;
}
}
Server
package server;
import java.rmi.Naming;
class Server
{
public static void main(String[] args)
{
try{
//create an instance of the RemoteDatabaseServer
Person person = new Person();
//rmi://[host][:port]/object
String namePerson = "rmi://localhost:9999/person";
//bind this instance to localhost port999 with name database
Naming.bind(namePerson, person);
System.out.println("Server is running...");
}catch(Exception ex){
System.out.println("Server Exception...");
ex.printStackTrace();
}
}
}
Client
package client;
import java.rmi.RMISecurityManager;
import java.rmi.Naming;
import server.PersonInterface;
import server.Address;
class Client
{
public static void main(String[] args)
{
try{
System.setSecurityManager(new RMISecurityManager());
String namePerson = "rmi://localhost:9999/person";
PersonInterface person =
(PersonInterface)Naming.lookup(namePerson);
person.setName("myName");
System.out.println(person.getPerson());
person.setName("myNewName");
Address address = new Address("123","123");
person.setAddress(address);
System.out.println(person.getPerson());
}catch(Exception ex){
System.out.println("Client failure...");
ex.printStackTrace();
}
}
}
The output i got is
D:\java -Djava.security.policy=d:\Client\policy\client.policy client.Client Person : myName age : 0 address : server.Address@1d6776d Person : myNewName age : 0 address : server.Address@10a2d64
The address is not printed correctlyPS:As you can see from Client class import
import server.PersonInterface;
import server.Address;
I had copy PersonInterface.class and Address.class to client side to make Client compiled.
Final:So stupid!!!Add following code to Address.java
public String toString(){
return addre1+ " " + addre2;
}
OK, problems are solved!! :)
interface MyInterface extends Remote {
MyClass f(MyClass x) throws RemoteException;
}
class MyClass implements Serializable {
private int value;
public MyClass(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
you need Serializable interface to tell that your class can be sent via network
server code
class Service extends UnicastRemoteObject implements MyInterface {
public Service() {
}
public MyClass f(MyClass v) throws RemoteException {
return new MyClass(v.getValue() + 1)
}
public static void main(Strint arg[]) {
Registry r = LocateRegistry.createRegistry(1099);
r.rebind("service", new Service());
}
}
client code
class Client {
public static void main(Strint arg[]) {
Registry r = LocateRegistry.getRegistry("localhost", 1099);
MyInterface service = (MyInterface)r.lookup("service");
MyClass result = service.f(new MyClass(123));
System.out.println(result.getValue()); //print 124 here
}
}
这篇关于我什么时候应该在 RMI 中实现 java.io.Serializable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!