通过RMI发送存储在ArrayList中的数据

时间:2015-05-07 15:17:54

标签: java swing arraylist client-server rmi

如果我希望客户端请求存储在服务器中的ArrayList内的文本数据,那么实现过程将如何使用Java RMI?

类图或示例代码会很棒。

1 个答案:

答案 0 :(得分:3)

为服务器和客户端(相同的包名称)定义接口

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;
}

定义实用工具类Constants.java

package arrayListRMI;

public class Constants {    
    public static final String RMI_ID = "StackOverflowAnswer";
    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;
    }

}

因此,您可以通过实例化ArrayList在服务器端实例化String 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]