public void reconnect ( boolean callFromQueue ) throws InterruptedException { Set < MDC . MDCCloseable > contextEntries = null ; Map < String , String > previousContext = null ; { ConcurrentMap < String , String > contextMap = api . getContextMap ( ) ; if ( callFromQueue && contextMap != null ) { previousContext = MDC . getCopyOfContextMap ( ) ; contextEntries = contextMap . entrySet ( ) . stream ( ) . map ( ( entry ) - > MDC . putCloseable ( entry . getKey ( ) , entry . getValue ( ) ) ) . collect ( Collectors . toSet ( ) ) ; } } if ( shutdown ) { api . setStatus ( JDA . Status . SHUTDOWN ) ; api . getEventManager ( ) . handle ( new ShutdownEvent ( api , OffsetDateTime . now ( ) , 1000 ) ) ; return ; } String message = "" ; if ( callFromQueue ) message = String . format ( "Queue is attempting to reconnect a shard...%s " , shardInfo != null ? " Shard: " + shardInfo . getShardString ( ) : "" ) ; LOG . debug ( "{}Attempting to reconnect in {}s" , message , reconnectTimeoutS ) ; while ( shouldReconnect ) { api . setStatus ( JDA . Status . WAITING_TO_RECONNECT ) ; int delay = reconnectTimeoutS ; Thread . sleep ( delay * 1000 ) ; handleIdentifyRateLimit = false ; api . setStatus ( JDA . Status . ATTEMPTING_TO_RECONNECT ) ; LOG . debug ( "Attempting to reconnect!" ) ; try { connect ( ) ; break ; } catch ( RejectedExecutionException ex ) { // JDA has already been shutdown so we can stop here api . setStatus ( JDA . Status . SHUTDOWN ) ; api . getEventManager ( ) . handle ( new ShutdownEvent ( api , OffsetDateTime . now ( ) , 1000 ) ) ; return ; } catch ( RuntimeException ex ) { reconnectTimeoutS = Math . min ( reconnectTimeoutS << 1 , api . getMaxReconnectDelay ( ) ) ; LOG . warn ( "Reconnect failed! Next attempt in {}s" , reconnectTimeoutS ) ; } } if ( contextEntries != null ) contextEntries . forEach ( MDC . MDCCloseable :: close ) ; if ( previousContext != null ) previousContext . forEach ( MDC :: put ) ; }
This method is used to start the reconnect of the JDA instance . It is public for access from SessionReconnectQueue extensions .