2013-02-20 37 views
0

爲什麼我得到這個錯誤java.net.SocketException異常:殘破的管道smpp.logica庫

20:43:40,798 ERROR Tx:809 - java.net.SocketException: Broken pipe 
java.net.SocketException: Broken pipe 
    at java.net.SocketOutputStream.socketWrite0(Native Method) 
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92) 
    at java.net.SocketOutputStream.write(SocketOutputStream.java:136) 
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65) 
    at java.io.BufferedOutputStream.write(BufferedOutputStream.java:109) 
    at com.logica.smpp.TCPIPConnection.send(TCPIPConnection.java:353) 
    at com.logica.smpp.Transmitter.send(Transmitter.java:79) 
    at com.logica.smpp.Session.send(Session.java:993) 
    at com.logica.smpp.Session.send(Session.java:1048) 
    at com.logica.smpp.Session.enquireLink(Session.java:789) 
    at com.logica.smpp.Tx.kirimEnquireLink(Tx.java:795) 
    at com.logica.smpp.Tx.access$0(Tx.java:777) 
    at com.logica.smpp.Tx$1.run(Tx.java:120) 
    at java.util.TimerThread.mainLoop(Timer.java:512) 
    at java.util.TimerThread.run(Timer.java:462) 

..I'm使用Open SMPP邏庫創建SMSC client..for幾個小時它的工作,但那麼錯誤來out..when我試圖發送查詢鏈接,,我創建會話是syncronize和連接的類型是收發這裏有一段代碼,我用綁定和enqury鏈接

private void bind() 
    { 
     debug.enter(this, "SMPPTest.bind()"); 
     try { 

      if (bound) { 
       System.out.println("Already bound, unbind first."); 
       return; 
      } 

      BindRequest request = null; 
      BindResponse response = null; 
      String syncMode = (asynchronous ? "a" : "s"); 

      // type of the session 
      syncMode = getParam("Asynchronous/Synchronnous Session? (a/s)", 
           syncMode); 
      if (syncMode.compareToIgnoreCase("a")==0) { 
       asynchronous = true; 
      } else if (syncMode.compareToIgnoreCase("s")==0) { 
       asynchronous = false; 
      } else { 
       System.out.println("Invalid mode async/sync, expected a or s, got " 
            + syncMode +". Operation canceled."); 
       return; 
      } 

      // input values 
      bindOption = getParam("Transmitter/Receiver/Transciever (t/r/tr)", 
            bindOption); 

      if (bindOption.compareToIgnoreCase("t")==0) { 
       request = new BindTransmitter(); 
      } else if (bindOption.compareToIgnoreCase("r")==0) { 
       request = new BindReceiver(); 
      } else if (bindOption.compareToIgnoreCase("tr")==0) { 
       request = new BindTransciever(); 
      } else { 
       System.out.println("Invalid bind mode, expected t, r or tr, got " + 
            bindOption + ". Operation canceled."); 
       return; 
      } 

      ipAddress = getParam("IP address of SMSC", ipAddress); 
      port = getParam("Port number", port); 

      TCPIPConnection connection = new TCPIPConnection(ipAddress, port); 
      connection.setReceiveTimeout(20*1000); 
      session = new Session(connection); 

      systemId = getParam("Your system ID", systemId); 
      password = getParam("Your password", password); 

      // set values 
      request.setSystemId(systemId); 
      request.setPassword(password); 
      request.setSystemType(systemType); 
      request.setInterfaceVersion((byte)0x34); 
      request.setAddressRange(addressRange); 

      // send the request 
      System.out.println("Bind request " + request.debugString()); 
      if (asynchronous) { 
       pduListener = new SMPPTestPDUEventListener(session); 
       response = session.bind(request,pduListener); 
      } else { 
       response = session.bind(request); 
      } 
      System.out.println("Bind response " + response.debugString()); 
      if (response.getCommandStatus() == Data.ESME_ROK) { 
       System.out.println("CommandID "+response.getCommandId()); 
       bound = true; 
      } 

     } catch (Exception e) { 
      event.write(e,""); 
      debug.write("Bind operation failed. " + e); 
      System.out.println("Bind operation failed. " + e); 
     } finally { 
      debug.exit(this); 
     } 
    } 

詢價鏈接代碼是

private void kirimEnquireLink() 
    { 
    try 
    { 
     log.info("Send enquireLink!"); 
     EnquireLink request = new EnquireLink(); 
     EnquireLinkResp response = new EnquireLinkResp(); 
//  synchronized (session) { 
//  session.enquireLink(request); 
//  } 
     if(asynchronous) 
     { 
      session.enquireLink(request); 
     }else 
     { 
      response = session.enquireLink(request); 
      System.out.println("Enquire Link Response "+request.debugString()); 
     } 

    } 
    catch (Exception e) 
    { 
     bound = false; 
    // unbind(); 
     log.error(e, e); 
    } 
    } 

我打電話詢問每一個環節10秒,,任何想法,爲什麼

回答

1

你所面對的問題是,從未有保險這方面,將始終可用,也沒有會話。許多不同的外部原因可能會導致ESME和SMSC之間的鏈接失效。我的建議是,嘗試抓住enquire_link操作和提交操作,評估異常並採取行動。

我已經成功地實現遞歸方法調用來處理這個問題如下

/** 
* Connect to ESME and submit a message, if binding process fails, reattempt 
* to reconnect and submit. 
*/ 
public void connect() { 

    try { 
     //Create connection 
     BindRequest request = null; 
     request = new BindTransciever(); 
     connection = new TCPIPConnection("localhost", 17632); 
     connection.setReceiveTimeout(20 * 1000); 
     session = new Session(connection); 
     //Prepare request 
     request.setSystemId("pavel"); 
     request.setPassword("wpsd"); 
     request.setSystemType("CMT"); 
     request.setInterfaceVersion((byte) 0x34); 
     request.setAddressRange(new AddressRange()); 
     pduListener = new SMPPTestPDUEventListener(session); 

     //Session binding process, if it fails, we are thrown to the catch section 
     //with a BrokenPipe (IOException) 
     session.bind(request, pduListener); 

     //Prepare message 
     SubmitSM msg = new SubmitSM(); 
     // set values 
     msg.setDestAddr("04234143939"); 
     msg.setShortMessage("hello"); 
     msg.assignSequenceNumber(true); 

     //Send to our custom made submitMessage method that reattempts if failure 
     submitMessage(msg); 

    } catch (Exception ex){ 
      //Analyze what type of exception was 
      if (ex instanceof IOException || ex instanceof SocketException){ 
       //IOException relate to the brokenpipe issue you are facing 
       //you need to close existing sessions and connections 
       //restablish session 
       if (this.connection!=null){ 
        this.connection.close(); 
       } 
       //This is a recursive call, I encourage you to elaborate 
       //a little bit this method implementing a counter so you 
       //don't end up in an infinite loop 
       this.connect(); 
      } else { 
       //LOG whatever other exception thrown 
      } 
    } 
} 

    /** 
* Submit message to SMSC, if it fails because of a connection issue, reattempt 
* @param message 
*/ 
private void submitMessage(SubmitSM message){ 
     try{ 
      session.submit(message); 
     } catch (Exception ex){ 
      //Analyze what type of exception was 
      if (ex instanceof IOException || ex instanceof SocketException){ 
       //IOException relate to the brokenpipe issue you are facing 
       //you need to close existing sessions and connections 
       //restablish session and try to submit again 
       if (this.connection!=null){ 
        this.connection.close(); 
       } 
       //Call a rebind method 
       this.bind(); 
       //This is a recursive call, I encourage you to elaborate 
       //a little bit this method implementing a counter so you 
       //don't end up in an infinite loop 
       this.submitMessage(message); 
      } else { 
       //LOG whatever other exception thrown 
      } 
     } 
} 

做同樣與enquire_link,試捕,IOException異常時重新綁定,並重新嘗試。爲了避免遞歸調用期間的無限循環,不要忘記添加一個couter和最多的嘗試次數。

你不需要enquire_link每10秒鐘。大多數提供商會告訴您需要多長時間才能完成,標準爲10分鐘。

+0

我看到謝謝......這是有道理的......爲什麼我總是得到例外......我已經做了你所說的......趕上例外並嘗試重新連接......好吧,我發送查詢鏈接每10秒鐘,因爲提供商要求,所以,謝謝 – dmh 2013-02-22 05:53:58

相關問題