2012-03-08 28 views
9

Tôi đang tạo ứng dụng máy khách-ứng dụng RMI đầu tiên, rất đơn giản của mình.

Đây là mã:

Interface "ICommunication" thực hiện

package itu.exercies.RMI.server; 

    import java.rmi.Remote; 
    import java.rmi.RemoteException; 

public interface ICommunication extends Remote 
{ 
    public String doCommunicate(String name) throws RemoteException; 

} 

Interface "CommunicationImpl":

package itu.exercies.RMI.server; 

import java.rmi.RemoteException; 
import java.rmi.server.UnicastRemoteObject; 

public class CommunicationImpl extends UnicastRemoteObject implements ICommunication { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    public CommunicationImpl() throws RemoteException { 
     super(); 

    } 

    @Override 
    public String doCommunicate(String name) throws RemoteException { 

     return "Hello this is server , whats up " +name+ " ?!\n"; 
    } 

} 

Đây là lớp học chính của tôi về máy chủ "CommunicationServer":

package itu.exercies.RMI.server; 

import java.net.MalformedURLException; 
import java.rmi.Naming; 
import java.rmi.RemoteException; 


public class CommunicationServer { 
    /** 
    * @param args 
    * @throws RemoteException 
    * @throws MalformedURLException 
    */ 
    public static void main(String[] args) throws RemoteException, MalformedURLException { 
     CommunicationImpl imp = new CommunicationImpl(); 
     Naming.rebind("CommunicationImpl", imp); 
     System.out.println("Server started..."); 
     System.out.println("Object successfully registered. It is bound to the name 'CommunicationImpl'."); 

    } 

} 

Và đây là khách hàng của tôi "CommunicationClient":

package itu.exercies.RMI.client; 

import java.net.MalformedURLException; 
import java.rmi.Naming; 
import java.rmi.NotBoundException; 
import java.rmi.RemoteException; 

import itu.exercies.RMI.server.CommunicationImpl; 

public class CommunicationClient { 
    public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException { 

     String url = new String("rmi://localhost/CommunicationImpl"); 
     CommunicationImpl comm = (CommunicationImpl)Naming.lookup(url); 
     String reply = comm.doCommunicate("Wiktor"); 
     System.out.println(reply); 


    } 

} 

Bây giờ khi tôi cố gắng chạy nó:

  1. tôi hướng đến thư mục bin của dự án của tôi sử dụng Terminal
  2. tôi chạy rmiregistry từ đó
  3. Tôi chạy Máy chủ Truyền thông của mình từ cửa sổ Thiết bị đầu cuối mới (và nó in ra các thông báo để có vẻ như hoạt động)
  4. Tôi mở cửa sổ dòng thứ ba và khi tôi thử để chạy CommunicationClient của tôi nó ném một ngoại lệ.

java itu.exercies.RMI.client.CommunicationClientException trong chủ đề "chính" java.lang.ClassCastException: $ Proxy0 không thể được đúc để itu.exercies.RMI.server.CommunicationImpl tại itu.exercies. RMI.client.CommunicationClient.main (CommunicationClient.java:14)

cho đến nay tôi đã cố gắng sửa chữa nó bằng cách tạo ra một sơ khai của đối tượng 'CommunicationImpl' sử dụng rmic nhưng bây giờ thay vì '$ Proxy0' lỗi liên quan thành 'CommunicationImpl_Stub':

Ngoại lệ trong chuỗi "chính" java.lang.ClassCastException: itu.exercies.RMI.server.CommunicationImpl_Stub không thể truyền sang itu.exercies.RMI.server.CommunicationImpl tại itu.exercies.RMI.client.CommunicationClient.main (CommunicationClient.java:14)

Tại thời điểm này tôi không có ý tưởng là tìm lỗi. Ai có thể cho tôi bất kỳ lời đề nghị?

Trả lời

18

Thay

CommunicationImpl comm = (CommunicationImpl) Naming.lookup(url); 

với

ICommunication comm = (ICommunication) Naming.lookup(url); 

CommunicationImpl là việc thực hiện máy chủ của ICommunication. Khách hàng không biết và cũng không quan tâm đến việc triển khai, chỉ có giao diện.

+2

@Booyaches Chính xác như vậy. Đối tượng CommunicationImpl là từ xa. Bạn không có nó ở địa phương. Bạn có cái gì khác mà thực hiện cùng một giao diện từ xa. Đó là giao diện từ xa. – EJP

Các vấn đề liên quan