Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.alibaba.tmq.client.remoting.timer.ClientHeartBeatTimer Maven / Gradle / Ivy
package com.alibaba.tmq.client.remoting.timer;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.alibaba.tmq.client.TMQFactory;
import com.alibaba.tmq.client.context.ClientContext;
import com.alibaba.tmq.client.system.consumer.executer.ConsumerExecuter;
import com.alibaba.tmq.client.system.consumer.listener.MessageListener;
import com.alibaba.tmq.client.system.producer.executer.NormalProducerExecuter;
import com.alibaba.tmq.client.system.producer.executer.TransactionProducerExecuter;
import com.alibaba.tmq.client.util.StringUtil;
import com.alibaba.tmq.common.constants.Constants;
import com.alibaba.tmq.common.domain.ConsumerKey;
import com.alibaba.tmq.common.util.ListUtil;
/**
* 心跳定时器
* @author tianyao.myc
*
*/
public class ClientHeartBeatTimer extends TimerTask implements Constants {
private static final Log logger = LogFactory.getLog(ClientHeartBeatTimer.class);
@Override
public void run() {
try {
List serverListForProducer = ClientContext.zookeeper.getServerListForProducer();
if(ListUtil.isEmpty(serverListForProducer)) {
logger.warn("[ClientHeartBeatTimer]: serverListForProducer is empty, clientConfig:" + ClientContext.clientConfig);
} else {
if (serverListForProducer.size() == 1 && serverListForProducer.get(0).equals(FAKE_SERVER)) {
logger.warn("[ClientHeartBeatTimer]: receive fake server, isolated!!!");
serverListForProducer.clear();
}
/** 更新服务端地址列表缓存 */
ClientContext.clientRemoting.setCachedServerListForProducer(serverListForProducer);
checkHeartBeatForProducer(serverListForProducer);
}
List serverListForConsumer = ClientContext.zookeeper.getServerListForConsumer();
if(ListUtil.isEmpty(serverListForConsumer)) {
logger.warn("[ClientHeartBeatTimer]: serverListForConsumer is empty, clientConfig:" + ClientContext.clientConfig);
} else {
if (serverListForConsumer.size() == 1 && serverListForConsumer.get(0).equals(FAKE_SERVER)) {
logger.warn("[ClientHeartBeatTimer]: receive fake server, isolated!!!");
serverListForConsumer.clear();
}
/** 更新服务端地址列表缓存 */
ClientContext.clientRemoting.setCachedServerListForConsumer(serverListForConsumer);
checkHeartBeatForConsumer(serverListForConsumer);
}
if(StringUtil.isNotBlank(ClientContext.clientConfig.getBackupDomainName())) {
List backupServerListForProducer = ClientContext.zookeeper.getbackupServerListForProducer();
if(ListUtil.isEmpty(backupServerListForProducer)) {
logger.warn("[ClientHeartBeatTimer]: backupServerListForProducer is empty, clientConfig:" + ClientContext.clientConfig);
} else {
if (backupServerListForProducer.size() == 1 && backupServerListForProducer.get(0).equals(FAKE_SERVER)) {
logger.warn("[ClientHeartBeatTimer]: receive fake server, isolated!!!");
backupServerListForProducer.clear();
}
/** 更新服务端地址列表缓存 */
ClientContext.clientRemoting.setCachedBackupServerListForProducer(backupServerListForProducer);
checkHeartBeatForProducer(backupServerListForProducer);
}
List backupServerListForConsumer = ClientContext.zookeeper.getbackupServerListForConsumer();
if(ListUtil.isEmpty(backupServerListForConsumer)) {
logger.warn("[ClientHeartBeatTimer]: backupServerListForConsumer is empty, clientConfig:" + ClientContext.clientConfig);
} else {
if (backupServerListForConsumer.size() == 1 && backupServerListForConsumer.get(0).equals(FAKE_SERVER)) {
logger.warn("[ClientHeartBeatTimer]: receive fake server, isolated!!!");
backupServerListForConsumer.clear();
}
/** 更新服务端地址列表缓存 */
ClientContext.clientRemoting.setCachedBackupServerListForConsumer(backupServerListForConsumer);
checkHeartBeatForConsumer(backupServerListForConsumer);
}
}
} catch (Throwable e) {
logger.error("[ClientHeartBeatTimer]: run error, clientConfig:" + ClientContext.clientConfig, e);
}
}
/**
* 检查心跳
* serverList
*/
private void checkHeartBeat(List serverList) {
for(String server : serverList) {
try {
//检查生产者心跳
checkProducerHeartBeat(server);
//检查消费者心跳
checkConsumerHeartBeat(server);
} catch (Throwable e) {
logger.error("[ClientHeartBeatTimer]: checkHeartBeat error"
+ ", clientConfig:" + ClientContext.clientConfig
+ ", server:" + server, e);
}
}
}
/**
* 检查心跳
* serverList
*/
private void checkHeartBeatForProducer(List serverList) {
for(String server : serverList) {
try {
//检查生产者心跳
checkProducerHeartBeat(server);
} catch (Throwable e) {
logger.error("[ClientHeartBeatTimer]: checkHeartBeat error"
+ ", clientConfig:" + ClientContext.clientConfig
+ ", server:" + server, e);
}
}
}
/**
* 检查心跳
* serverList
*/
private void checkHeartBeatForConsumer(List serverList) {
for(String server : serverList) {
try {
//检查消费者心跳
checkConsumerHeartBeat(server);
} catch (Throwable e) {
logger.error("[ClientHeartBeatTimer]: checkHeartBeat error"
+ ", clientConfig:" + ClientContext.clientConfig
+ ", server:" + server, e);
}
}
}
/**
* 检查生产者心跳
* server
*/
private void checkProducerHeartBeat(String server) {
//拿到普通Producer映射表
ConcurrentHashMap> normalProducerTable = TMQFactory.getNormalproducertable();
//检查每一个普通生产者心跳
checkNormalProducerHeartBeat(server, normalProducerTable, "normalProducerTable");
//拿到事务Producer映射表
ConcurrentHashMap> transactionProducerTable = TMQFactory.getTransactionproducertable();
//检查每一个事务生产者心跳
checkTransactionProducerHeartBeat(server, transactionProducerTable, "transactionProducerTable");
}
/**
* 检查每一个普通生产者心跳
* server
* producerTable
* source
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private void checkNormalProducerHeartBeat(String server,
ConcurrentHashMap> producerTable, String source) {
Iterator iterator = producerTable.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry entry = (Map.Entry)iterator.next();
String producerId = (String)entry.getKey();
ConcurrentHashMap producerMap = (ConcurrentHashMap)entry.getValue();
Iterator producerIterator = producerMap.entrySet().iterator();
while(producerIterator.hasNext()) {
Map.Entry producerEntry = (Map.Entry)producerIterator.next();
String instanceName = (String)producerEntry.getKey();
try {
ClientContext.clientRemoting.connectServer(instanceName, server, ROLE_TYPE_PRODUCER, producerId, NULL, NULL);
} catch (Throwable e) {
logger.error("[ClientHeartBeatTimer]: checkNormalProducerHeartBeat connectServer error"
+ ", server:" + server
+ ", clientConfig:" + ClientContext.clientConfig
+ ", producerId:" + producerId
+ ", instanceName:" + instanceName, e);
}
}
}
}
/**
* 检查每一个事务生产者心跳
* server
* producerTable
* source
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private void checkTransactionProducerHeartBeat(String server,
ConcurrentHashMap> producerTable, String source) {
Iterator iterator = producerTable.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry entry = (Map.Entry)iterator.next();
String producerId = (String)entry.getKey();
ConcurrentHashMap producerMap = (ConcurrentHashMap)entry.getValue();
Iterator producerIterator = producerMap.entrySet().iterator();
while(producerIterator.hasNext()) {
Map.Entry producerEntry = (Map.Entry)producerIterator.next();
String instanceName = (String)producerEntry.getKey();
try {
ClientContext.clientRemoting.connectServer(instanceName, server, ROLE_TYPE_PRODUCER, producerId, NULL, NULL);
} catch (Throwable e) {
logger.error("[ClientHeartBeatTimer]: checkTransactionProducerHeartBeat connectServer error"
+ ", server:" + server
+ ", clientConfig:" + ClientContext.clientConfig
+ ", producerId:" + producerId
+ ", instanceName:" + instanceName, e);
}
}
}
}
/**
* 检查消费者心跳
* server
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private void checkConsumerHeartBeat(String server) {
ConcurrentHashMap> consumerTable = TMQFactory.getConsumertable();
Iterator iterator = consumerTable.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry entry = (Map.Entry)iterator.next();
String consumerId = (String)entry.getKey();
ConcurrentHashMap consumerMap = (ConcurrentHashMap)entry.getValue();
Iterator consumerIterator = consumerMap.entrySet().iterator();
while(consumerIterator.hasNext()) {
Map.Entry consumerEntry = (Map.Entry)consumerIterator.next();
String instanceName = (String)consumerEntry.getKey();
ConsumerExecuter consumerExecuter = (ConsumerExecuter)consumerEntry.getValue();
ConcurrentHashMap listenerTable = consumerExecuter.getListenerTable();
Iterator listenerIterator = listenerTable.entrySet().iterator();
while(listenerIterator.hasNext()) {
Map.Entry listenerEntry = (Map.Entry)listenerIterator.next();
ConsumerKey consumerKey = (ConsumerKey)listenerEntry.getKey();
try {
ClientContext.clientRemoting.connectServer(instanceName, server, ROLE_TYPE_CONSUMER, consumerId, consumerKey.getTopic(), consumerKey.getTag());
} catch (Throwable e) {
logger.error("[ClientHeartBeatTimer]: connectServer error"
+ ", server:" + server
+ ", clientConfig:" + ClientContext.clientConfig
+ ", consumerId:" + consumerId
+ ", instanceName:" + instanceName
+ ", consumerKey:" + consumerKey, e);
}
}
}
}
}
}