com.bfd.harpc.heartbeat.HeartBeatManager.java Source code

Java tutorial

Introduction

Here is the source code for com.bfd.harpc.heartbeat.HeartBeatManager.java

Source

/**
 * Copyright (C) 2015 Baifendian Corporation
 *
 * Licensed 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 com.bfd.harpc.heartbeat;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bfd.harpc.common.ServerNode;
import com.bfd.harpc.loadbalance.common.DynamicHostSet;
import com.bfd.harpc.monitor.NamedThreadFactory;

/**
 * heartbeat?
 * <p>
 * 
 * @author : dsfan
 * @date : 2015-6-17
 */
public class HeartBeatManager<T> {

    /** LOGGER */
    private final Logger LOGGER = LoggerFactory.getLogger(getClass());

    /** {@link ScheduledExecutorService} */
    private final ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1,
            new NamedThreadFactory("Harpc-Heartbeat", true));

    /** {@link DynamicHostSet} */
    private final DynamicHostSet dynamicHostSet;

    /**  */
    private ScheduledFuture<?> heatbeatTimer;

    /** 0? */
    private final int heartbeat;

    /**  */
    private final int heartbeatTimeout;

    /** ? */
    private final int times;

    /** ? */
    private final int interval;

    /**  */
    private final GenericKeyedObjectPool<ServerNode, T> pool;

    /**
     * @param dynamicHostSet
     * @param heartbeat
     * @param heartbeatTimeout
     * @param times
     * @param interval
     * @param pool
     */
    public HeartBeatManager(DynamicHostSet dynamicHostSet, int heartbeat, int heartbeatTimeout, int times,
            int interval, GenericKeyedObjectPool<ServerNode, T> pool) {
        this.dynamicHostSet = dynamicHostSet;
        this.heartbeat = heartbeat;
        this.heartbeatTimeout = heartbeatTimeout;
        this.times = times;
        this.interval = interval;
        this.pool = pool;
    }

    /**
     * ?heartbeat
     * <p>
     */
    public void startHeatbeatTimer() {
        stopHeartbeatTimer();
        if (heartbeat > 0) {
            heatbeatTimer = scheduled.scheduleWithFixedDelay(
                    new HeartBeatTask<T>(dynamicHostSet, times, interval, heartbeatTimeout, pool), heartbeat,
                    heartbeat, TimeUnit.MILLISECONDS);
        }
    }

    /**
     * ?heartbeat
     * <p>
     */
    public void stopHeartbeatTimer() {
        try {
            ScheduledFuture<?> timer = heatbeatTimer;
            if (timer != null && !timer.isCancelled()) {
                timer.cancel(true);
            }
        } catch (Throwable t) {
            LOGGER.warn(t.getMessage(), t);
        } finally {
            heatbeatTimer = null;
        }
    }
}