Skip to content

Commit

Permalink
Manageable executors / schedulers.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Feb 11, 2017
1 parent 0eadc7b commit 9e56124
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 4 deletions.
19 changes: 15 additions & 4 deletions rapidoid-commons/src/main/java/org/rapidoid/job/Jobs.java
Expand Up @@ -45,9 +45,9 @@ public class Jobs extends RapidoidInitializer {

private static final AtomicLong errorCounter = new AtomicLong();

private static ScheduledExecutorService SCHEDULER;
private static ScheduledThreadPoolExecutor SCHEDULER;

private static ExecutorService EXECUTOR;
private static ThreadPoolExecutor EXECUTOR;

private static final Once init = new Once();

Expand All @@ -56,8 +56,12 @@ private Jobs() {

public static synchronized ScheduledExecutorService scheduler() {
if (SCHEDULER == null) {

int threads = JOBS.sub("scheduler").entry("threads").or(64);
SCHEDULER = Executors.newScheduledThreadPool(threads, new RapidoidThreadFactory("scheduler", true));

SCHEDULER = new ScheduledThreadPoolExecutor(threads, new RapidoidThreadFactory("scheduler", true));

new ManageableExecutor("scheduler", SCHEDULER);

if (init.go()) init();
}
Expand All @@ -67,8 +71,15 @@ public static synchronized ScheduledExecutorService scheduler() {

public static synchronized Executor executor() {
if (EXECUTOR == null) {

int threads = JOBS.sub("executor").entry("threads").or(64);
EXECUTOR = Executors.newFixedThreadPool(threads, new RapidoidThreadFactory("executor", true));
int maxThreads = JOBS.sub("executor").entry("maxThreads").or(1024);
int maxQueueSize = JOBS.sub("executor").entry("maxQueueSize").or(1000000);

BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(maxQueueSize);
EXECUTOR = new ThreadPoolExecutor(threads, maxThreads, 300, TimeUnit.SECONDS, queue, new RapidoidThreadFactory("executor", true));

new ManageableExecutor("executor", EXECUTOR);

if (init.go()) init();
}
Expand Down
@@ -0,0 +1,85 @@
package org.rapidoid.job;

/*
* #%L
* rapidoid-commons
* %%
* Copyright (C) 2014 - 2017 Nikolche Mihajlovski and contributors
* %%
* Licensed 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.
* #L%
*/

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.group.AutoManageable;
import org.rapidoid.u.U;

import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

@Authors("Nikolche Mihajlovski")
@Since("5.3.0")
public class ManageableExecutor extends AutoManageable<ManageableExecutor> {

private final String name;

private final ThreadPoolExecutor executor;

public ManageableExecutor(String name, ThreadPoolExecutor executor) {
this.name = name;
this.executor = executor;
}

@Override
public List<String> overview() {
return U.list("name", "activeCount", "taskCount", "completedTaskCount",
"maximumPoolSize", "corePoolSize", "largestPoolSize");
}

public String getName() {
return name;
}

public boolean isShutdown() {
return executor.isShutdown();
}

public int getCorePoolSize() {
return executor.getCorePoolSize();
}

public int getMaximumPoolSize() {
return executor.getMaximumPoolSize();
}

public int getPoolSize() {
return executor.getPoolSize();
}

public int getActiveCount() {
return executor.getActiveCount();
}

public int getLargestPoolSize() {
return executor.getLargestPoolSize();
}

public long getTaskCount() {
return executor.getTaskCount();
}

public long getCompletedTaskCount() {
return executor.getCompletedTaskCount();
}
}
1 change: 1 addition & 0 deletions rapidoid-commons/src/main/resources/rapidoid-classes.txt
Expand Up @@ -519,6 +519,7 @@ org.rapidoid.job.CallbackExecutorJob
org.rapidoid.job.ContextPreservingJobWrapper
org.rapidoid.job.Jobs
org.rapidoid.job.JobsDSL
org.rapidoid.job.ManageableExecutor
org.rapidoid.job.PredefinedContextJobWrapper
org.rapidoid.jpa.AbstractEntity
org.rapidoid.jpa.DAO
Expand Down

0 comments on commit 9e56124

Please sign in to comment.