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.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.lookout.step;
import com.alipay.lookout.api.*;
import com.alipay.lookout.api.composite.MixinMetric;
import com.alipay.lookout.common.LookoutConstants;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
/**
* Created by [email protected] on 2017/4/3.
*/
public class MeasurableScheduler extends ThreadPoolExecutor implements ScheduledExecutorService,
ScheduledService {
private static final AtomicLong sequencer = new AtomicLong(0);
private final Counter activeCount;
private final Timer taskExecutionTime;
private final Timer taskExecutionDelay;
private final Counter skipped;
Id id;
Registry registry;
public MeasurableScheduler(final Registry registry, final String name, int poolSize) {
super(poolSize, poolSize, 0, TimeUnit.NANOSECONDS, new DelayQueue(),
newThreadFactory(name), new AbortPolicy());
this.registry = registry;
Id mixinMetricId = registry.createId("lookout.scheduler." + name).withTag(
LookoutConstants.TAG_PRIORITY_KEY, PRIORITY.LOW.name());
MixinMetric mixinMetric = registry.mixinMetric(mixinMetricId);
mixinMetric.gauge("queueSize", new Gauge() {
@Override
public Integer value() {
return MeasurableScheduler.super.getQueue().size();
}
});
activeCount = mixinMetric.counter("activeThreads");
taskExecutionTime = mixinMetric.timer("taskExecutionTime");
taskExecutionDelay = mixinMetric.timer("taskExecutionDelay");
skipped = mixinMetric.counter("skipped");
this.id = mixinMetricId;
}
private static ThreadFactory newThreadFactory(final String id) {
return new ThreadFactory() {
private final AtomicInteger next = new AtomicInteger();
@Override
public Thread newThread(Runnable r) {
final String name = "lookout-" + id + "-" + next.getAndIncrement();
final Thread t = new Thread(r, name);
t.setDaemon(true);
return t;
}
};
}
@Override
public ScheduledFuture> schedule(Runnable command, long delay, TimeUnit unit) {
if (command == null || unit == null)
throw new NullPointerException();
RunnableScheduledFuture> t = decorateTask(
command,
new MeasurableScheduler.ScheduledFutureTask(command, null, triggerTime(delay,
unit)));
delayedExecute(t);
return t;
}
@Override
public ScheduledFuture schedule(Callable callable, long delay, TimeUnit unit) {
if (callable == null || unit == null)
throw new NullPointerException();
RunnableScheduledFuture t = new MeasurableScheduler.ScheduledFutureTask(callable,
triggerTime(delay, unit));
delayedExecute(t);
return t;
}
public ScheduledFuture> scheduleAtFixedRateSkipIfLong(Runnable command, long initialDelay,
long period, TimeUnit unit) {
if (command == null || unit == null)
throw new NullPointerException();
if (period <= 0)
throw new IllegalArgumentException();
RunnableScheduledFuture> t = decorateTask(
command,
new MeasurableScheduler.ScheduledFutureTask