예제 #1
0
  public ServiceObserver(
      ServiceObserverConfiguration serviceConfig,
      ExecutorService executor,
      Class<D> dataClass,
      Class<A> aggregateClass,
      Class<S> summaryClass,
      ContentParser<D> contentParser,
      ObserverNamingStrategy ons) {
    this.serviceConfig = serviceConfig;
    this.executor = executor;
    this.observers = new ConcurrentHashMap<String, Observer<D>>();
    this.dataClass = dataClass;
    this.aggregateClass = aggregateClass;
    this.summaryClass = summaryClass;
    this.contentParser = contentParser;
    if (ons == null) {
      this.ons = ObserverNamingStrategy.DEFAULT;
    } else {
      this.ons = ons;
    }
    this.snapshotAllTimer =
        Metrics.newTimer(
            new MetricName(
                "com.mfizz.observer.ServiceObservers",
                serviceConfig.getName(),
                "snapshot-all-time"),
            TimeUnit.MILLISECONDS,
            TimeUnit.MILLISECONDS);
    this.snapshotAllAttemptedCounter = new AtomicLong();
    this.snapshotAllCompletedCounter = new AtomicLong();
    this.lastSnapshotAllResult = new AtomicReference<SnapshotAllResult>();

    // this.lastSnapshotAllTimestamp = new AtomicLong(0);
    // this.lastSnapshotsCompletedCounter = new AtomicInteger();
    // this.lastSnapshotsFailedCounter = new AtomicInteger();

    this.groups = new TreeSet<String>();
    this.summary = new ConcurrentHashMap<String, SummaryGroup<S>>();
    this.snapshots = new ConcurrentHashMap<String, TimeSeries<ObserveAggregateSnapshot<A>>>();

    // always make sure "current" is added to set of vars to track
    // "current" always tracked for all
    this.serviceConfig.getPeriods().add(SummaryPeriod.parse("current"));

    //
    // create high performance http client that can be reused by all observers
    //
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

    PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
    // increase max total connection to 200
    cm.setMaxTotal(200);
    // increase default max connection per route to 20
    cm.setDefaultMaxPerRoute(20);

    this.httpclient = new DefaultHttpClient(cm);
    this.httpclient.setHttpRequestRetryHandler(
        new HttpRequestRetryHandler() {
          @Override
          public boolean retryRequest(
              IOException exception, int executionCount, HttpContext context) {
            // always return false -- we never want a request retried
            return false;
          }
        });
    this.httpclient
        .getParams()
        .setIntParameter(
            CoreConnectionPNames.SO_TIMEOUT, new Integer((int) serviceConfig.getSocketTimeout()))
        .setIntParameter(
            CoreConnectionPNames.CONNECTION_TIMEOUT,
            new Integer((int) serviceConfig.getConnectionTimeout()))
        .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
        .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
        .setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false)
        .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false);
  }