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); }
相关文章推荐
- EJB学习(三)—消息驱动Bean
- hot code loading in nodejs
- response.getWriter().write()与out.print()的区别
- 1、让自己习惯c++
- Java的一些基本概念
- iOS 中的线程(2) --- GCD
- git简易入门(github)
- Linux-CentOS安装mysql5.6
- python中print不换行的方法
- 预测的原理及其实战(4)
- 相对路径和绝对路径
- Linux安装jdk1.8
- python的高性能web应用的开发与测试实验
- Nginx 命令
- BZOJ 2763: [JLOI2011]飞行路线(分层图最短路)
- QUT——An Easy Math Problem (三次函数求最大值)
- ubuntu apt-get 安装软件时package has no installtion candidate问题
- response.getWriter().write()与out.print()的区别(转)
- A. PawnChess
- neoclipse连接neo4j