異常的定位與解決

本例是java rmi的一個實例,是java網絡編程精簡裏面的:java

服務器:SmipleServer編程


import javax.naming.*;

public class SimpleServer{
  public static void main( String args[] ){
    try{
       HelloService service1 = new HelloServiceImpl("service1");
       HelloService service2 = new HelloServiceImpl("service2");
       System.setProperty( "java.rmi.server.hostname",   "127.0.0.1");
       Context namingContext=new InitialContext();
       
       namingContext.rebind( "rmi://127.0.0.1:8000/HelloService1", service1 );
       namingContext.rebind( "rmi:HelloService2", service2 );
/*     
       namingContext.rebind( "rmi://localhost:8000/HelloService1", service1 );
       namingContext.rebind( "rmi://localhost:8000/HelloService1", service2 );
*/   
       System.out.println( "服務器註冊了兩個HelloService對象" );
    }catch(Exception e){
       e.printStackTrace();
    }
  }
}

服務器

 

接口:HelloService網絡

import java.util.Date;
import java.rmi.*;
public interface HelloService extends Remote{
  public String echo(String msg) throws RemoteException;
  public Date getTime() throws RemoteException;
}

socket

 

具體服務:HelloServiceImpltcp

import java.util.Date;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class HelloServiceImpl extends UnicastRemoteObject implements HelloService{
  private String name;
  public HelloServiceImpl(String name)throws RemoteException{
    this.name=name;
  }
  public String echo(String msg)throws RemoteException{
    System.out.println(name+":調用echo()方法");
    return "echo:"+msg +" from "+name;
  }
  public Date getTime()throws RemoteException{
    System.out.println(name+":調用getTime()方法");
    return new Date();
  }
}


運行服務器:SimpleServerthis

輸出的異常信息爲:url

javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
    java.net.ConnectException: Connection refused: connect]
    at com.sun.jndi.rmi.registry.RegistryContext.rebind(RegistryContext.java:142)
    at com.sun.jndi.toolkit.url.GenericURLContext.rebind(GenericURLContext.java:231)
    at javax.naming.InitialContext.rebind(InitialContext.java:408)
    at com.net.rmi.hello.SimpleServer.main(SimpleServer.java:13)
Caused by: java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
    java.net.ConnectException: Connection refused: connect
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
    at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
    at com.sun.jndi.rmi.registry.RegistryContext.rebind(RegistryContext.java:140)
    ... 3 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:519)
    at java.net.Socket.connect(Socket.java:469)
    at java.net.Socket.<init>(Socket.java:366)
    at java.net.Socket.<init>(Socket.java:180)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)
    ... 8 more

.net

 

爲了定位異常,咱們要把異常翻譯過來,翻譯成類天然語言,使得咱們可以易於接受和理解:翻譯

javax.naming.ServiceUnavailableException

根據JDK的描述,該異常在javax.naming包裏面,異常的繼承結構爲:

java.lang.Exception

javax.naming.NamingException

javax.naming.ServiceUnavailableException

爲了瞭解這個類,咱們先要了解其父:

javax.naming.NamingException:

此異常是 Context 和 DirContext 接口中的操做拋出的全部異常的超類。失敗的特性由子類的名稱描述。

此異常捕獲指出操做失敗處的信息,好比解析最後進行到的位置。

這就是咱們可以看懂的信息了,其餘的咱們暫時不知道它在說什麼東西,暫且無論它。

javax.naming.ServiceUnavailableException:

當試圖與目錄或命名服務通訊,而該服務不可用時,拋出此異常。該服務可能由於各類緣由而不可用。例如,服務器可能太忙而沒法爲請求提供服務,

或者服務器可能沒有爲向某些請求提供服務而註冊等等。

咱們看異常信息的下一句話:

[Root exception is java.rmi.ConnectException: Connection refused to host: 127.0.0.1;

nested exception is: java.net.ConnectException: Connection refused: connect]

這個翻譯過來就是最根本的或者說最原始的異常是java.rmi.ConnectException.相關的描述是鏈接被拒絕。而這個異常中嵌套的異常是

java.net.ConnectException 鏈接被拒絕。

java.rmi.ConnectException的父類爲java.rmi.RemoteException

RemoteException 是許多與通訊相關的異常的通用超類,這些異常可能會在執行遠程方法調用期間發生。

遠程接口(擴展 java.rmi.Remote 的接口)的每一個方法必須在其 throws 子句中列出 RemoteException

java.rmi.Connection:

若是拒絕遠程主機對鏈接的遠程方法調用,則拋出 ConnectException

相關文章
相關標籤/搜索