当SMSC出现网络故障或电源故障时,Java SMPP API会检测到断开连接

时间:2018-01-30 22:14:09

标签: java smpp

我的Java项目(客户端)使用Java SMPP API连接SMSC。我正在对项目进行断线测试。当我关闭SMSC模拟器时,客户端可以检测到连接丢失,但是如果我在SMSC模拟器上关闭网络,则客户端无法检测到断开连接。

即使我检查connection.isBound()connection.getState(),两者都表明连接仍然存在。我的怀疑是当网络出现故障或电源故障时,连接会话仍处于打开状态。

以下是我的代码建立连接:

 connection = new Connection(config.getSmscHostname(), config.getSmscPort(),true /*async=true*/);
 connection.addObserver(this);
 connection.autoAckMessages(true);
 connection.autoAckLink(true);
 connection.bind(config.getBindType(), config.getSystemId(), config.getSecret(), 
 config.getSystemType(), config.getSourceTON(),
                    config.getSourceNPI(), config.getShortcode());

我已经覆盖了ConnectionObserver方法update(Connection source, SMPPPacket packet),但它仅在SMSC模拟器关闭时才有效。

    /**
     * Called for all events <b>other</b> than packet reception. This method is
     * called for all events generated by the API framework <i>except </i> that
     * of a packet received. The {@link #packetReceived}method is called in
     * that case. The <code>update</code> method is mostly used for control
     * events, such as signifying the exit of the receiver thread or notifying
     * of error conditions.
     *
     * @param source
     *            the Connection which received the packet.
     * @param event
     *            the SMPP event type.
     * @see ie.omk.smpp.event.SMPPEvent#getType
     */
    @Override
    public void update(Connection source, SMPPEvent event){
        if(event.getType() == SMPPEvent.RECEIVER_EXIT
                && ((ReceiverExitEvent) event).getReason() == ReceiverExitEvent.EXCEPTION) {
            //We have lost the connection to the SMSC. 
            interruptAndTermintateExecution(new SmscException(SmscException.Reason.LOST_CONNECTION, "SMSC is down, lost connection."));
            logger.error("SMSC is down, lost connection...");
//            eaDown.emit("Smsc is down, lost connection");// notifies Health Link
        }

无论如何在客户端检测到它?请帮忙!谢谢!

1 个答案:

答案 0 :(得分:0)

我不熟悉此API,但是,您可以尝试使用PING或使用任何其他命令来通过终端命令确定主机要查找的内容。

这可以通过编程方式完成,如下所示。

这个过程的作用是......

  1. 通过操作系统的运行终端执行命令
  2. 挂钩已执行流程的I / O流
  3. 使用这些流来确定操作是否成功。
  4. 即使Ping在您的情况下没有用处,还有许多其他命令行技术可用于确定远程主机是否可用,只需更改您希望执行的参数,并使用它的I / O Streams以同样的方式根据输出验证连接。

    希望这会有所帮助:)

    if (os.toLowerCase().contains("win")) {
        args = new String[] { "cmd", "/c", "PING" + host };
    } else {
        args = new String[] { "bash", "-c", "PING" + host };
    }
    ProcessBuilder pipeline = new ProcessBuilder(args);
    pipeline.redirectError(Redirect.PIPE);
    pipeline.redirectInput(Redirect.PIPE);
    pipeline.redirectOutput(Redirect.PIPE);
    pipeline.start();
    
    
    //Gets the I/O Streams of the process we just executed
    bufferedReader = new BufferedReader(new InputStreamReader(pipeline.getInputStream()));
    bufferedEReader = new BufferedReader(new InputStreamReader(pipeline.getErrorStream()));
    bufferedWriter = new BufferedWriter(new OutputStreamWriter(pipeline.getOutputStream()));
    
    
    //Reading the streams
    String line = null;
    while (streaming) {
        try {
            if (bufferedReader.ready()) {
                line = bufferedReader.readLine();
                if (line.contains("A SUCCESS MESSAGE")) {
                      //We have our success message, do what you need
                }
                bufferedWriter.flush();
            }
        } catch (Exception e) {
    
        }
     }