2015-10-23 70 views
0

如果服務器未啓動,我想向用戶顯示錯誤消息。 當用戶啓動程序時。 下面我提到了我用來連接到服務器的應用程序的我的ServerConnector單例類。handeling java RMI連接拒絕主機異常

public class ServerConnector { 



private static ServerConnector serverConnector; 
    private RemoteFactory remoteFactory; 

    private ServerConnector() throws NotBoundException, MalformedURLException, 
      RemoteException { 
     remoteFactory = (RemoteFactory) Naming 
       .lookup("rmi://localhost:5050/RoomReservation"); 
    } 

    public static ServerConnector getServerConnector() 
      throws NotBoundException, MalformedURLException, RemoteException { 
     if (serverConnector == null) { 
      serverConnector = new ServerConnector(); 
     } 
     return serverConnector; 
    } 

    public CustomerController getCustomerController() throws RemoteException { 
     return remoteFactory.getCustomerController(); 
    } 


} 

有沒有什麼辦法在加載程序的GUI之前檢查服務器是否啓動?

謝謝。

+1

使用try catch塊設置標誌。並使用它來更新您的GUI以顯示錯誤消息。 – shanmuga

回答

1
  • Catch java.rmi.ConnectException。這意味着註冊表尚未啓動。

  • Catch NotBoundException。這意味着服務器尚未綁定到註冊表。