实例化客户端对象并将其添加到服务器并调用这些方法

时间:2018-11-04 14:32:38

标签: java rmi

我目前正在研究rmi,但是一旦调用远程对象的方法,我就会遇到问题。实际上,我有一种方法可以使我恢复车辆的注释列表,并且注释由注释和注释组成。 这是我的课程和接口:

Note界面:

public interface Vehicle extends Remote {
    public String getComment() throws RemoteException;
    public int getNote() throws RemoteException;
}

注释类:

public class Note extends UnicastRemoteObject implements NoteInterface, Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private final String comment;
    private final int note;

    public Note(String comment, int note) throws RemoteException {
        super();
        if (note < -1) {
            throw new IllegalArgumentException("Invalid input");
        }

        this.comment = comment;
        this.note = note;
    }

    @Override
    public final String getComment() throws RemoteException{
        return comment;
    }

    @Override
    public final int getNote() throws RemoteException {
        return note;
    }
}

车辆界面:

public interface Vehicle extends Remote {
    public void addNote(NoteInterface note) throws RemoteException;
    public List<NoteInterface> getListNote() throws RemoteException;
}

车辆类别:

public class Car extends UnicastRemoteObject implements Vehicle {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private final List<NoteInterface> listNotes;

    public Car(){
        this.listNotes = new ArrayList<>();
    }

    @Override
    public final void addNote(NoteInterface note) throws RemoteException {
        Objects.requireNonNull(note, "Note can't null");
        listNotes.add(note);
    }

    @Override
    public List<NoteInterface> getListNote() throws RemoteException {
        return listNotes;
    }
}

服务器类:

public class Server {

    public static void main(String[] args) throws UnknownHostException, RemoteException, MalformedURLException {
        Registry registry = LocateRegistry.createRegistry(1099);

        Vehicle v = new Car();

        String url = "rmi://" + InetAddress.getLocalHost().getHostAddress() + "/project";
        registry.rebind(url+"/vehicles", v);

        System.out.println(url);
    }
}

客户端类:

public class Client {

    public static void main(String[] args) throws NotBoundException, IOException{
        String url = "rmi://" + InetAddress.getLocalHost().getHostAddress() +,"/project";
        Registry registry = LocateRegistry.getRegistry();

        Vehicle v = (Vehicle ) registry.lookup(url + "/vehicles");
}

如果可以的话,是客户端:

  

v.addNote(new Note(“这辆车很棒”,10));

     

v.addNote(new Note(“这辆车很坏”,0));

     

v.getListNote()。size(); //它告诉我2

如果我这样做:

  

System.out.println(v.getListNote()。get(0)); //显示给我

     

Proxy [NoteInterface,RemoteObjectInvocationHandler [UnicastRef [liveRef:   [端点:192.168.1.11:51593,objID:[-25af4c2d:166df1339f1:-7ffd,   1351068100677398612]]]]]

例如,当我调用note方法时:

  

System.out.println(list.get(0).getComment()

我有一个java.rmi.ConnectException

  

线程“ AWT-EventQueue-0”中的异常   java.lang.IllegalArgumentException:java.rmi.ConnectException:   连接拒绝主机:192.168.1.11;嵌套的异常是:     java.net.ConnectException:连接被拒绝:connect

我不知道此问题的出处,也不知道如何解决。 如果有人有主意,谢谢您的帮助。

0 个答案:

没有答案