通过 RMI 发送存储在 ArrayList 中的数据
sending data stored inside an ArrayList via RMI
如果我想让客户端请求存储在服务器ArrayList中的文本数据,实现过程将如何使用Java RMI?
请提供 class 图表或示例代码。
为服务器和客户端定义一个接口(相同的包名)
package arrayListRMI;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
public interface IArrayList extends Remote {
public ArrayList<String> getText() throws RemoteException;
public void setText(ArrayList<String> text) throws RemoteException;
}
定义实用程序classConstants.java
package arrayListRMI;
public class Constants {
public static final String RMI_ID = "WhosebugAnswer";
public static final int RMI_PORT = 222 ;
}
为 IArrayList
添加一个实现(在服务器端)
package arrayListRMI;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
public class ArrayListImpl extends UnicastRemoteObject implements IArrayList{
private static final long serialVersionUID = 1L;
private ArrayList<String> text;
protected ArrayListImpl() throws RemoteException {
super();
}
public ArrayList<String> getText() {
return text;
}
public void setText(ArrayList<String> text) {
this.text = text;
}
}
所以你可以在服务器端实例化一个String
(文本)的ArrayList
,方法是实例化一个ArrayListImpl
并将它绑定到rmiregistry
方便客户随时索取。
ArrayList<String> textRequested = new ArrayList<String>();
textRequested.add("example1");
textRequested.add("example2");
ArrayListImpl arrayListToSend = new ArrayListImpl();
arrayListToSend.setText(textRequested);
服务器
package arrayListRMI;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.sql.SQLException;
public class Server {
public static void main(String args[]) throws AlreadyBoundException, SQLException, ClassNotFoundException {
try {
ArrayList<String> textRequested = new ArrayList<String>();
textRequested.add("example1");
textRequested.add("example2");
ArrayListImpl arrayListToSend = new ArrayListImpl();
arrayListToSend.setText(textRequested);
Registry registry = LocateRegistry.createRegistry(Constants.RMI_PORT);
registry.bind(Constants.RMI_ID, arrayListToSend);
System.out.println("Server starts....");
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
客户端
package arrayListRMI;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
public class Client {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost", Constants.RMI_PORT);
IArrayList cmp = (IArrayList) registry.lookup(Constants.RMI_ID);
ArrayList<String> received = cmp.getText();
System.out.println(received);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
现在 运行 服务器。你得到这个输出
Server starts....
然后运行客户端。你得到这个输出
[example1, example2]
如果我想让客户端请求存储在服务器ArrayList中的文本数据,实现过程将如何使用Java RMI?
请提供 class 图表或示例代码。
为服务器和客户端定义一个接口(相同的包名)
package arrayListRMI;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
public interface IArrayList extends Remote {
public ArrayList<String> getText() throws RemoteException;
public void setText(ArrayList<String> text) throws RemoteException;
}
定义实用程序classConstants.java
package arrayListRMI;
public class Constants {
public static final String RMI_ID = "WhosebugAnswer";
public static final int RMI_PORT = 222 ;
}
为 IArrayList
添加一个实现(在服务器端)
package arrayListRMI;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
public class ArrayListImpl extends UnicastRemoteObject implements IArrayList{
private static final long serialVersionUID = 1L;
private ArrayList<String> text;
protected ArrayListImpl() throws RemoteException {
super();
}
public ArrayList<String> getText() {
return text;
}
public void setText(ArrayList<String> text) {
this.text = text;
}
}
所以你可以在服务器端实例化一个String
(文本)的ArrayList
,方法是实例化一个ArrayListImpl
并将它绑定到rmiregistry
方便客户随时索取。
ArrayList<String> textRequested = new ArrayList<String>();
textRequested.add("example1");
textRequested.add("example2");
ArrayListImpl arrayListToSend = new ArrayListImpl();
arrayListToSend.setText(textRequested);
服务器
package arrayListRMI;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.sql.SQLException;
public class Server {
public static void main(String args[]) throws AlreadyBoundException, SQLException, ClassNotFoundException {
try {
ArrayList<String> textRequested = new ArrayList<String>();
textRequested.add("example1");
textRequested.add("example2");
ArrayListImpl arrayListToSend = new ArrayListImpl();
arrayListToSend.setText(textRequested);
Registry registry = LocateRegistry.createRegistry(Constants.RMI_PORT);
registry.bind(Constants.RMI_ID, arrayListToSend);
System.out.println("Server starts....");
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
客户端
package arrayListRMI;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
public class Client {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost", Constants.RMI_PORT);
IArrayList cmp = (IArrayList) registry.lookup(Constants.RMI_ID);
ArrayList<String> received = cmp.getText();
System.out.println(received);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
现在 运行 服务器。你得到这个输出
Server starts....
然后运行客户端。你得到这个输出
[example1, example2]