下面列出了com.google.common.util.concurrent.MoreExecutors#getExitingScheduledExecutorService ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public ThrottledAsyncChecker(final Timer timer,
final long minMsBetweenChecks,
final long diskCheckTimeout,
final ExecutorService executorService) {
this.timer = timer;
this.minMsBetweenChecks = minMsBetweenChecks;
this.diskCheckTimeout = diskCheckTimeout;
this.executorService = MoreExecutors.listeningDecorator(executorService);
this.checksInProgress = new HashMap<>();
this.completedChecks = new WeakHashMap<>();
if (this.diskCheckTimeout > 0) {
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new
ScheduledThreadPoolExecutor(1);
this.scheduledExecutorService = MoreExecutors
.getExitingScheduledExecutorService(scheduledThreadPoolExecutor);
} else {
this.scheduledExecutorService = null;
}
}
@Provides
@Singleton
public HeartbeatManager provideHeartBeat(EventBus eventBus, Clock clock) {
HeartbeatManager heartbeatManager =
new HeartbeatManager(
new ConcurrentHashMap<>(),
MoreExecutors.getExitingScheduledExecutorService(
(ScheduledThreadPoolExecutor)
Executors.newScheduledThreadPool(
1,
new ThreadFactoryBuilder().setNameFormat("heartbeat-manager-%d").build())),
eventBus,
clock,
heartbeatConfiguration);
eventBus.register(heartbeatManager);
return heartbeatManager;
}
@Provides
@Singleton
public WorkerManager provideWorkerManager(
PartitionWorkerFactory factory, EventBus eventBus, Metrics metrics, PartitionPolicy policy) {
List<PartitionWorker> partitionWorkers = new ArrayList<>();
for (int i = 0; i < workersConfiguration.getNumberOfWorkers(); i++) {
partitionWorkers.add(factory.get(i));
}
WorkerManager manager =
new WorkerManager(
partitionWorkers,
factory,
MoreExecutors.getExitingScheduledExecutorService(
(ScheduledThreadPoolExecutor)
Executors.newScheduledThreadPool(
1, new ThreadFactoryBuilder().setNameFormat("worker-manager-%d").build())),
new ConcurrentHashMap<>(),
policy,
metrics);
eventBus.register(manager);
return manager;
}
@Override
public void initialize(Map<String, Object> config) {
command = (String[]) config.get(COMMAND);
processTimeoutMills = (int) config.get(PROCESS_TIMEOUT_MILLS);
Map<String, Object> processorConfig = (Map<String, Object>) config.get(PROCESS_CONFIG);
ShellContext shellContext = (ShellContext) config.get(SHELL_CONTEXT);
List<String> outputStreams = (List<String>) config.get(OUTPUT_STREAMS);
Map<String, String> envMap = (Map<String, String>) config.get(SHELL_ENVIRONMENT);
String className = (String) config.get(MULTILANG_SERIALIZER);
shellProcess = new ShellProcess(command);
if(className != null)
shellProcess.setSerializerClassName(className);
shellProcess.setEnv(envMap);
//subprocesses must send their pid first thing
Long subpid = shellProcess.launch(processorConfig, shellContext, outputStreams);
LOG.info("Launched subprocess with pid " + subpid);
LOG.info("Start checking heartbeat...");
setHeartbeat();
heartBeatExecutorService = MoreExecutors.getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1));
heartBeatExecutorService.scheduleAtFixedRate(new HeartbeatTimerTask(this), 1, 1, TimeUnit.SECONDS);
}
/**
* Schedule profilers with a SchedulerExecutorService
*
* @param profilers Collection of profilers to schedule
* @param arguments
*/
private static void scheduleProfilers(Collection<Profiler> profilers, Arguments arguments) {
// We need to convert to an ExitingScheduledExecutorService so the JVM shuts down
// when the main thread finishes
ScheduledExecutorService scheduledExecutorService = MoreExecutors.getExitingScheduledExecutorService(
(ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(profilers.size(), new ProfilerThreadFactory()));
Map<String, ScheduledFuture<?>> runningProfilers = new HashMap<>(profilers.size());
Map<String, Profiler> activeProfilers = new HashMap<>(profilers.size());
for (Profiler profiler : profilers) {
activeProfilers.put(profiler.getClass().getSimpleName(), profiler);
ProfilerWorkerThread worker = new ProfilerWorkerThread(profiler, errors);
ScheduledFuture future = scheduledExecutorService.scheduleAtFixedRate(worker, EXECUTOR_DELAY, profiler.getPeriod(), profiler.getTimeUnit());
runningProfilers.put(profiler.getClass().getSimpleName(), future);
}
if (arguments.httpServerEnabled) {
ProfilerServer.startServer(runningProfilers, activeProfilers, arguments.httpPort, isRunning, errors);
}
}
public DnsSrvWatcher<T> build() {
checkState(polling ^ dnsSrvWatcherFactory != null, "specify either polling or custom trigger");
DnsSrvWatcherFactory<T> watcherFactory;
if (polling) {
final ScheduledExecutorService executor =
scheduledExecutorService != null
? scheduledExecutorService
: MoreExecutors.getExitingScheduledExecutorService(
new ScheduledThreadPoolExecutor(
1, new ThreadFactoryBuilder().setNameFormat("dns-lookup-%d").build()),
0, SECONDS);
watcherFactory =
cnf -> new PollingDnsSrvWatcher<>(cnf, executor, pollingInterval, pollingIntervalUnit);
} else {
watcherFactory = requireNonNull(dnsSrvWatcherFactory, "dnsSrvWatcherFactory");
}
final ChangeNotifierFactory<T> changeNotifierFactory =
fqdn -> new ServiceResolvingChangeNotifier<>(
resolver, fqdn, resultTransformer, errorHandler);
return watcherFactory.create(changeNotifierFactory);
}
void startExecutor() {
scanTimes = new ConcurrentHashMap<>();
executorService =
MoreExecutors.getExitingScheduledExecutorService(
(ScheduledThreadPoolExecutor)
Executors.newScheduledThreadPool(
workersConfiguration.getThreadPoolSize(),
new ThreadFactoryBuilder()
.setNameFormat("partition-worker-" + index + "-%s")
.build()));
log.info("New ExecutorService has been created.");
}
public void schedule(Set<? extends SamplingSchedulable> schedulables) {
ScheduledExecutorService scheduledExecutorService = MoreExecutors.getExitingScheduledExecutorService(
(ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(schedulables.size(), new SchedulerThreadFactory()), 0, TimeUnit.MILLISECONDS);
for (SamplingSchedulable samplingSchedulable : schedulables) {
System.out.println("scheduling "+ samplingSchedulable.getClass()+ " with an interval of "+ samplingSchedulable.getInterval() + " ms");
try {
scheduledExecutorService.scheduleAtFixedRate(samplingSchedulable, 0, samplingSchedulable.getInterval(), samplingSchedulable.getTimeUnit());
} catch (Exception e) {
e.printStackTrace();
}
}
}
public DefaultProxyEndpointManager(IntSupplier checkInterval) {
this.healthCheckInterval = checkInterval;
this.scheduled = MoreExecutors.getExitingScheduledExecutorService(
new ScheduledThreadPoolExecutor(1, XpipeThreadFactory.create("ProxyEndpointManager")),
THREAD_POOL_TIME_OUT, TimeUnit.SECONDS);
this.healthChecker = new DefaultProxyEndpointHealthChecker(scheduled);
start();
}
@Bean(name = GLOBAL_SCHEDULED)
public ScheduledExecutorService getScheduled() {
int corePoolSize = Math.min(OsUtils.getCpuCount(), 4);
return MoreExecutors.getExitingScheduledExecutorService(
new ScheduledThreadPoolExecutor(corePoolSize, XpipeThreadFactory.create(GLOBAL_SCHEDULED)),
1, TimeUnit.SECONDS
);
}
@PostConstruct
public void cleaner() {
ScheduledExecutorService scheduled = MoreExecutors.getExitingScheduledExecutorService(
new ScheduledThreadPoolExecutor(1, XpipeThreadFactory.create("DefaultTunnelManager")),
THREAD_POOL_TIME_OUT, TimeUnit.SECONDS);
future = scheduled.scheduleWithFixedDelay(
new AbstractExceptionLogTask() {
@Override
protected void doRun() throws Exception {
doClean();
}
}, 10, 10, TimeUnit.MINUTES);
}
@Bean(name = REDIS_COMMAND_EXECUTOR)
public ScheduledExecutorService getRedisCommandExecutor() {
int corePoolSize = OsUtils.getCpuCount();
if (corePoolSize > maxScheduledCorePoolSize) {
corePoolSize = maxScheduledCorePoolSize;
}
return MoreExecutors.getExitingScheduledExecutorService(
new ScheduledThreadPoolExecutor(corePoolSize, XpipeThreadFactory.create(REDIS_COMMAND_EXECUTOR)),
THREAD_POOL_TIME_OUT, TimeUnit.SECONDS
);
}
@PostConstruct
public void scheduledCheckSentEmails() {
logger.info("[scheduledCheckSentEmails] [post construct] begin");
ScheduledExecutorService scheduled = MoreExecutors.getExitingScheduledExecutorService(
new ScheduledThreadPoolExecutor(1, XpipeThreadFactory.create(getClass().getSimpleName() + "-")),
THREAD_POOL_TIME_OUT, TimeUnit.SECONDS
);
start(scheduled);
}
@Bean(name = SCHEDULED_EXECUTOR)
public ScheduledExecutorService getScheduledExecutorService() {
int corePoolSize = OsUtils.getCpuCount();
if (corePoolSize > maxScheduledCorePoolSize) {
corePoolSize = maxScheduledCorePoolSize;
}
return MoreExecutors.getExitingScheduledExecutorService(
new ScheduledThreadPoolExecutor(corePoolSize, XpipeThreadFactory.create(SCHEDULED_EXECUTOR)),
THREAD_POOL_TIME_OUT, TimeUnit.SECONDS
);
}
@Before
public void setup() throws IOException {
MockProfiler1 profiler1 = new MockProfiler1(new HashSet<String>());
MockProfiler2 profiler2 = new MockProfiler2(new HashSet<String>());
activeProfilers = new HashMap<>();
activeProfilers.put("MockProfiler1", profiler1);
activeProfilers.put("MockProfiler2", profiler2);
port = 8080;
isRunning = new AtomicReference<>(true);
errors = new ArrayList<>();
errors.add("example error");
Map<String, ScheduledFuture<?>> runningProfilers = new HashMap<>();
ProfilerWorkerThread worker1 = new ProfilerWorkerThread(profiler1, errors);
ProfilerWorkerThread worker2 = new ProfilerWorkerThread(profiler2, errors);
ScheduledExecutorService scheduledExecutorService = MoreExecutors.getExitingScheduledExecutorService(
(ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(2, new ProfilerThreadFactory()));
ScheduledFuture future1 = scheduledExecutorService.scheduleAtFixedRate(worker1, 0, profiler1.getPeriod(), profiler1.getTimeUnit());
ScheduledFuture future2 = scheduledExecutorService.scheduleAtFixedRate(worker2, 0, profiler2.getPeriod(), profiler2.getTimeUnit());
runningProfilers.put("MockProfiler1", future1);
runningProfilers.put("MockProfiler2", future2);
ProfilerServer.startServer(runningProfilers, activeProfilers, port, isRunning, errors);
client = HttpClients.createDefault();
}
private static ListeningScheduledExecutorService defaultExecutorService() {
final int clientCount = clientCounter.incrementAndGet();
final ThreadFactory threadFactory = new ThreadFactoryBuilder()
.setNameFormat("helios-client-" + clientCount + "-thread-%d")
.build();
final ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(4, threadFactory);
final ScheduledExecutorService exitingExecutor =
MoreExecutors.getExitingScheduledExecutorService(stpe, 0, SECONDS);
return MoreExecutors.listeningDecorator(exitingExecutor);
}
public TaskMonitor(final JobId jobId, final FlapController flapController,
final StatusUpdater statusUpdater) {
this.jobId = jobId;
this.flapController = flapController;
this.statusUpdater = statusUpdater;
final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
// Let core threads time out to avoid unnecessarily keeping a flapping state check thread alive
// for the majority of tasks that do not flap.
executor.setKeepAliveTime(5, SECONDS);
executor.allowCoreThreadTimeOut(true);
this.scheduler = MoreExecutors.getExitingScheduledExecutorService(executor, 0, SECONDS);
}
public void prepare(Map stormConf, TopologyContext context, final OutputCollector collector) {
Object maxPending = stormConf.get(Config.TOPOLOGY_SHELLBOLT_MAX_PENDING);
if (maxPending != null) {
this._pendingWrites = new LinkedBlockingQueue(((Number) maxPending).intValue());
}
_rand = new Random();
_collector = collector;
_context = context;
workerTimeoutMills = 1000 * RT.intCast(stormConf.get(Config.SUPERVISOR_WORKER_TIMEOUT_SECS));
_process = new ShellProcess(_command);
// subprocesses must send their pid first thing
Number subpid = _process.launch(stormConf, context);
LOG.info("Launched subprocess with pid " + subpid);
// reader
_readerThread = new Thread(new BoltReaderRunnable());
_readerThread.start();
_writerThread = new Thread(new BoltWriterRunnable());
_writerThread.start();
heartBeatExecutorService = MoreExecutors.getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1));
heartBeatExecutorService.scheduleAtFixedRate(new BoltHeartbeatTimerTask(this), 1, 1, TimeUnit.SECONDS);
LOG.info("Start checking heartbeat...");
setHeartbeat();
}
@Override
public HelixTaskResult handleMessage() throws InterruptedException {
String messageSubType = this._message.getMsgSubType();
Preconditions.checkArgument(
messageSubType.equalsIgnoreCase(HelixMessageSubTypes.APPLICATION_MASTER_SHUTDOWN.toString()),
String.format("Unknown %s message subtype: %s", GobblinHelixConstants.SHUTDOWN_MESSAGE_TYPE, messageSubType));
HelixTaskResult result = new HelixTaskResult();
if (stopStatus.isStopInProgress()) {
result.setSuccess(true);
return result;
}
log.info("Handling message " + HelixMessageSubTypes.APPLICATION_MASTER_SHUTDOWN.toString());
ScheduledExecutorService shutdownMessageHandlingCompletionWatcher =
MoreExecutors.getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1));
// Schedule the task for watching on the removal of the shutdown message, which indicates that
// the message has been successfully processed and it's safe to disconnect the HelixManager.
// This is a hacky way of watching for the completion of processing the shutdown message and
// should be replaced by a fix to https://issues.apache.org/jira/browse/HELIX-611.
shutdownMessageHandlingCompletionWatcher.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
HelixManager helixManager = _notificationContext.getManager();
HelixDataAccessor helixDataAccessor = helixManager.getHelixDataAccessor();
HelixProperty helixProperty = helixDataAccessor
.getProperty(_message.getKey(helixDataAccessor.keyBuilder(), helixManager.getInstanceName()));
// The absence of the shutdown message indicates it has been removed
if (helixProperty == null) {
eventBus.post(new ClusterManagerShutdownRequest());
}
}
}, 0, 1, TimeUnit.SECONDS);
result.setSuccess(true);
return result;
}
public void open(Map stormConf, TopologyContext context, SpoutOutputCollector collector) {
_collector = collector;
_context = context;
workerTimeoutMills = 1000 * RT.intCast(stormConf.get(Config.SUPERVISOR_WORKER_TIMEOUT_SECS));
_process = new ShellProcess(_command);
Number subPid = _process.launch(stormConf, context);
LOG.info("Launched subprocess with pid " + subPid);
heartBeatExecutorService = MoreExecutors.getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1));
}