简体   繁体   中英

How to auto-reconnect to XMPP server?

I have an application which is supposed to keep permanent connection to a XMPP server. But every 2 or 3 days, it disconnects, and can't reconnect automatically with ReconnectionManager as it is supposed to do.

What could I do to see what is happening and/or to correct this ?

2018-09-20 07:06:10.887 -  WARN - o.j.smack.AbstractXMPPConnection         : Connection closed with error
java.net.SocketException: Connexion terminée par expiration du délai d'attente (Read failed)
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
        at java.net.SocketInputStream.read(SocketInputStream.java:171)
        at java.net.SocketInputStream.read(SocketInputStream.java:141)
        at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
        at sun.security.ssl.InputRecord.read(InputRecord.java:503)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
        at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:940)
        at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
        at java.io.BufferedReader.fill(BufferedReader.java:161)
        at java.io.BufferedReader.read1(BufferedReader.java:212)
        at java.io.BufferedReader.read(BufferedReader.java:286)
        at org.xmlpull.mxp1.MXParser.fillBuf(MXParser.java:2992)
        at org.xmlpull.mxp1.MXParser.more(MXParser.java:3046)
        at org.xmlpull.mxp1.MXParser.nextImpl(MXParser.java:1144)
        at org.xmlpull.mxp1.MXParser.next(MXParser.java:1093)
        at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1177)
        at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$300(XMPPTCPConnection.java:956)
        at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:971)
        at java.lang.Thread.run(Thread.java:748)

Here is my Connection conf (Spring java-style config) :

  @Bean("xmppConnection")
  public XmppConnectionFactoryBean xmppConnectionFactoryBean() {

    XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
    configBuilder.setUsernameAndPassword(appProperties.getXmpp().getUsername(), appProperties.getXmpp().getPassword());
    configBuilder.setServiceName(appProperties.getXmpp().getServiceName());
    configBuilder.setHost(appProperties.getXmpp().getHost());
    int port = appProperties.getXmpp().getPort();
    if (port != 0) {
      configBuilder.setPort(port);
    }
    int timeout = appProperties.getXmpp().getTimeout();
    if (timeout != 0) {
      configBuilder.setConnectTimeout(timeout);
    }
    configBuilder.setDebuggerEnabled(appProperties.getXmpp().isDebuggerEnabled());
    XMPPTCPConnectionConfiguration config = configBuilder.build();

    XmppConnectionFactoryBean connectionFactoryBean = new XmppConnectionFactoryBean();
    connectionFactoryBean.setConnectionConfiguration(config);
    connectionFactoryBean.setSubscriptionMode(null);

    return connectionFactoryBean;
  }

And here is where the connection is used in Spring Integration conf :

<int-xmpp:inbound-channel-adapter id="xmppInboundAdapter" channel="xmppInbound" xmpp-connection="xmppConnection" auto-startup="true" />

UPDATE : To audit the issue, I've just added a listener to xmppConnection :

  @Bean("xmppConnection")
  public XMPPConnection xmppConnection(XmppConnectionFactoryBean xmppConnectionFactoryBean) throws Exception {
    XMPPConnection xmppConnection = xmppConnectionFactoryBean.getObject();
    xmppConnection.addConnectionListener(new ConnectionListener() {

      @Override
      public void reconnectionSuccessful() {
        logger.info("Successfully reconnected to the XMPP server.");
      }

      @Override
      public void reconnectionFailed(Exception arg0) {
        logger.info("Failed to reconnect to the XMPP server.");
      }

      @Override
      public void reconnectingIn(int seconds) {
        logger.info("Reconnecting in " + seconds + " seconds.");
      }

      @Override
      public void connectionClosedOnError(Exception arg0) {
        logger.error("Connection to XMPP server was lost.");
      }

      @Override
      public void connectionClosed() {
        logger.info("XMPP connection was closed.");
      }

      @Override
      public void connected(XMPPConnection connection) {
        logger.info("Connected to XMPP server.");        
      }

      @Override
      public void authenticated(XMPPConnection connection, boolean resumed) {
        logger.info("Authenticated to XMPP server.");        
      }
    });

    return xmppConnection;

  }

UPDATE 2 : now with the audit activated, I can see :

2018-10-03 07:29:39.442 - ERROR - f.e.r.l.i.xmpp.config.XmppManagerConfig  : Connection to XMPP server was lost.

but never "Reconnecting in" or "Failed to reconnect".

Hi I hope you are using latest version of smack 4.3.0

By default Smack will try to reconnect the connection in case it was abruptly disconnected. The reconnection manager will try to immediately reconnect to the server and increase the delay between attempts as successive reconnections keep failing.

In case you want to force a reconnection while the reconnection manager is waiting for the next reconnection, you can just use AbstractXMPPConnection#connect() and a new attempt will be made. If the manual attempt also failed then the reconnection manager will still continue the reconnection job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM