您的位置:首页 > 其它

hystrix实用示例

2015-11-23 20:39 183 查看
HBaseUserProfileInfoGetHystrixCommand.java

public class HBaseUserProfileInfoGetHystrixCommand extends HystrixCommand<Result[]> {

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

private static byte[] tableName = Bytes.toBytes("UserProfileInfo");
static ObjectMapper objectMapper = new ObjectMapper();

private static Configuration conf;
// static HConnection hConnection = null;
private static HBasePoolManager hBasePoolManager;

static {
try {
conf = HBaseConfiguration.create();
// hConnection = HConnectionManager.createConnection(conf);
hBasePoolManager = new HBasePoolManager();
} catch (Exception ex) {
logger.error(ExceptionUtils.getFullStackTrace(ex));
}
}

List<Get> gets;

// HBasePoolManager hBasePoolManager;
public HBaseUserProfileInfoGetHystrixCommand(List<Get> gets) {
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("HBaseGroup"))
.andCommandKey(HystrixCommandKey.Factory.asKey("HBaseUserProfileInfoGetCommandKey"))
.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("HBaseUserProfileInfoGetThreadPoolKey"))
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(40))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withExecutionIsolationThreadTimeoutInMilliseconds(10000)
.withCircuitBreakerRequestVolumeThreshold(200)
)
);

this.gets = gets;
}

@Override
protected Result[] run() throws Exception {
// TODO Auto-generated method stub
Transaction catTran = null;
HTableInterface table = null;
Result[] rtn = null;
try {
catTran = Cat.newTransaction(DataProxyCatConstants.TYPE_HBase, DataProxyCatConstants.Name_ReadProfileHBase);
/*
* if(hConnection == null || hConnection.isTableEnabled(tableName)
* == false) { hConnection =
* HConnectionManager.createConnection(conf); } table =
* hConnection.getTable(tableName);
*/
table = hBasePoolManager.getConn(tableName);
rtn = table.get(gets);
catTran.setStatus(com.dianping.cat.message.Message.SUCCESS);
} catch (Exception ex) {
logger.warn(ExceptionUtils.getFullStackTrace(ex));
catTran.setStatus(ex);
} finally {
catTran.complete();
if (table != null) {
table.close();
}
}
return rtn;
}
}

[/code]
调用HBaseUserProfileInfoGetHystrixCommand
public Map<String, QualifierData> getList(String rowkv, List<String> tags, List<Integer> maxVersions) {
rowkv = rowkv.toLowerCase();
Transaction catTran = null;
Result[] results = null;
HBaseResult rtn = new HBaseResult();

try {
catTran = Cat.newTransaction(DataProxyCatConstants.TYPE_Hystrix, DataProxyCatConstants.Name_ReadProfileHBase);

List<Get> gets = new ArrayList<>();
for (int i = 0; i < tags.size(); i++) {
try {
Get get = new Get(Bytes.toBytes(rowkv));
byte[] qualifier = Bytes.toBytes(tags.get(i));
get.addColumn(family, qualifier);
int maxVersion = maxVersions.get(i);
get.setMaxVersions(maxVersion);

gets.add(get);
} catch (Exception e) {
logger.error(ExceptionUtils.getFullStackTrace(e));
}
}

HBaseUserProfileInfoGetHystrixCommand command = new HBaseUserProfileInfoGetHystrixCommand(gets);

results = command.execute();

if (results == null) {
return null;
}

for (Result result : results) {
for (KeyValue kv : result.raw()) {
String q = Bytes.toString(kv.getQualifier());
String v = Bytes.toString(kv.getValue());
long t = kv.getTimestamp();

rtn.add(rowkv, q, v, t);
}
}

catTran.setStatus(com.dianping.cat.message.Message.SUCCESS);
} catch (Exception e) {
logger.warn(ExceptionUtils.getFullStackTrace(e));
catTran.setStatus(e);
} finally {
catTran.complete();
}

return rtn.getQualifiers(rowkv);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: