コード例 #1
0
public abstract class BaseThriftPushCommand<T, K> extends HystrixCommand<T> {
  protected final TCNode tcnode;
  protected final List<K> list;

  protected BaseThriftPushCommand(TCNode tcnode, List<K> list) {
    super(setter);
    this.tcnode = tcnode;
    this.list = list;
  }

  private static final HystrixCommandGroupKey groupKey =
      HystrixCommandGroupKey.Factory.asKey("PushGroup");
  private static final HystrixCommandKey commandKey = HystrixCommandKey.Factory.asKey("node");
  private static final HystrixThreadPoolKey threadPoolKey =
      HystrixThreadPoolKey.Factory.asKey("T-ThriftPushGroup");

  private static final HystrixCommandProperties.Setter commandProperties =
      HystrixCommandProperties.Setter()
          .withExecutionTimeoutInMilliseconds(5000)
          .withFallbackEnabled(true);
  private static final HystrixThreadPoolProperties.Setter threadPoolProperties =
      HystrixThreadPoolProperties.Setter().withCoreSize(1).withMaxQueueSize(2);
  public static final HystrixCommand.Setter setter =
      HystrixCommand.Setter.withGroupKey(groupKey)
          .andCommandKey(commandKey)
          .andThreadPoolKey(threadPoolKey)
          .andCommandPropertiesDefaults(commandProperties)
          .andThreadPoolPropertiesDefaults(threadPoolProperties);

  public T push() {
    this.logPush(tcnode);
    return this.execute();
  }

  protected abstract void logPush(TCNode tcnode);
}
コード例 #2
0
public class ThriftPingCommand extends HystrixCommand<Integer> {
  private static final HystrixCommandGroupKey groupKey =
      HystrixCommandGroupKey.Factory.asKey("ThriftPingGroup");
  private static final HystrixCommandKey commandKey = HystrixCommandKey.Factory.asKey("ping");
  private static final HystrixThreadPoolKey threadPoolKey =
      HystrixThreadPoolKey.Factory.asKey("T-ThriftPingGroup");

  private static final HystrixCommandProperties.Setter commandProperties =
      HystrixCommandProperties.Setter()
          .withExecutionTimeoutInMilliseconds(5000)
          .withFallbackEnabled(true);
  private static final HystrixThreadPoolProperties.Setter threadPoolProperties =
      HystrixThreadPoolProperties.Setter().withCoreSize(4).withMaxQueueSize(10);
  private static final HystrixCommand.Setter setter =
      HystrixCommand.Setter.withGroupKey(groupKey)
          .andCommandKey(commandKey)
          .andThreadPoolKey(threadPoolKey)
          .andCommandPropertiesDefaults(commandProperties)
          .andThreadPoolPropertiesDefaults(threadPoolProperties);

  private final TSNode tsnode;

  public ThriftPingCommand(TSNode tsnode) {
    super(setter);
    this.tsnode = tsnode;
  }

  @Loggable
  public int ping(TSNode tsnode) {
    return this.execute();
  }

  public int ping() {
    return this.ping(this.tsnode);
  }

  @Override
  protected Integer run() throws Exception {
    String host = this.tsnode.getHost();
    int port = this.tsnode.getPort();

    TSocket transport = new TSocket(host, port, 1000);
    TProtocol protocol = new TBinaryProtocol(transport);
    PoolAble.Client client = new PoolAble.Client(protocol);
    transport.open();
    int vNodes = -1;
    try {
      vNodes = client.ping();
    } finally {
      if (transport.isOpen()) {
        transport.close();
      }
    }
    return vNodes;
  }

  @Override
  protected Integer getFallback() {
    return this.getFallback(this.tsnode);
  }

  @Loggable(value = Loggable.WARN)
  protected Integer getFallback(TSNode tsnode) {
    return -1;
  }
}