/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.dubbo.remoting.exchange.support.header;

import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.remoting.Channel;
import org.apache.dubbo.remoting.Client;

/**
 * ReconnectTimerTask
 */
public class ReconnectTimerTask extends AbstractTimerTask {

    private static final Logger logger = LoggerFactory.getLogger(ReconnectTimerTask.class);

    private final int heartbeatTimeout;

    ReconnectTimerTask(ChannelProvider channelProvider, Long heartbeatTimeoutTick, int heartbeatTimeout1) {
        super(channelProvider, heartbeatTimeoutTick);
        this.heartbeatTimeout = heartbeatTimeout1;
    }

    @Override
    protected void doTask(Channel channel) {
        try {
            Long lastRead = lastRead(channel);
            Long now = now();
            if (lastRead != null && now - lastRead > heartbeatTimeout) {
                logger.warn("Close channel " + channel + ", because heartbeat read idle time out: "
                        + heartbeatTimeout + "ms");
                if (channel instanceof Client) {
                    try {
                        ((Client) channel).reconnect();
                    } catch (Exception e) {
                        //do nothing
                    }
                } else {
                    channel.close();
                }
            }
        } catch (Throwable t) {
            logger.warn("Exception when reconnect to remote channel " + channel.getRemoteAddress(), t);
        }
    }
}

@@ -1543,34 +1543,16 @@
 nnel) %7B%0A
-        try %7B%0A    
         
@@ -1590,28 +1590,24 @@
 l);%0A        
-    
 Long now = n
@@ -1608,28 +1608,24 @@
 ow = now();%0A
-    
         if (
@@ -1677,24 +1677,95 @@
 tTimeout) %7B%0A
+            if (channel instanceof Client) %7B%0A                try %7B%0A    
             
@@ -1785,12 +1785,26 @@
 rn(%22
-Clos
+Reconnect to remot
 e ch
@@ -1820,16 +1820,35 @@
  channel
+.getRemoteAddress()
  + %22, be
@@ -1909,16 +1909,20 @@
         
+   
+ 
 + heartb
@@ -1961,285 +1961,336 @@
     
-if (channel instanceof Client) %7B%0A                    try %7B%0A                        ((Client) channel).reconnect();%0A                    %7D catch (Exception e) %7B%0A                        //do nothing%0A                    %7D%0A                %7D else %7B%0A                    channel.close(
+    ((Client) channel).reconnect();%0A                %7D catch (Throwable t) %7B%0A                    // do nothing%0A                %7D%0A            %7D else %7B%0A                try %7B%0A                    logger.warn(%22Close channel %22 + channel + %22, because heartbeat read idle time out: %22%0A                            + heartbeatTimeout + %22ms%22
 );%0A 
@@ -2304,30 +2304,45 @@
         
-%7D%0A
     
+channel.close();%0A
         
 %7D%0A      
@@ -2325,34 +2325,32 @@
 lose();%0A        
-%7D%0A
         %7D catch 
@@ -2369,32 +2369,40 @@
 ) %7B%0A            
+ 
+       
 logger.warn(%22Exc
@@ -2417,26 +2417,12 @@
 hen 
-reconnect to remot
+clos
 e ch
@@ -2442,32 +2442,45 @@
 nnel
-.getRemoteAddress(), t);
+, t);%0A                %7D%0A            %7D
 %0A   

optimize ReconnectTimerTask's log output (#3162)