Skip to content

Commit

Permalink
Merge pull request #61 from ingwarsw/own_executors_service
Browse files Browse the repository at this point in the history
Using own executor service
  • Loading branch information
stephenc authored Jun 17, 2019
2 parents 0df3c15 + 38c2c5a commit df46ae7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
</scm>

<properties>
<revision>2.4.2</revision>
<revision>2.5.0</revision>
<changelist>-SNAPSHOT</changelist>
<jenkins.version>2.60.3</jenkins.version>
<jenkins.version>2.164.3</jenkins.version>
<java.level>8</java.level>
<no-test-jar>false</no-test-jar>
</properties>
Expand Down
36 changes: 31 additions & 5 deletions src/main/java/jenkins/scm/api/SCMEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,23 @@
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.ExtensionList;
import hudson.init.Terminator;
import hudson.model.Cause;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.security.ACL;
import hudson.util.ClassLoaderSanityThreadFactory;
import hudson.util.DaemonThreadFactory;
import hudson.util.NamingThreadFactory;
import java.util.Date;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.servlet.http.HttpServletRequest;
import jenkins.util.Timer;
import jenkins.security.ImpersonatingScheduledExecutorService;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -126,6 +131,11 @@ public abstract class SCMEvent<P> {
@CheckForNull
private final String origin;

/**
* The scheduled executor thread pool. This is initialized lazily since it may be never needed.
*/
private static ScheduledExecutorService executorService;

/**
* Constructor to use when the timestamp is available from the external SCM.
*
Expand Down Expand Up @@ -198,10 +208,25 @@ protected SCMEvent(SCMEvent<P> copy) {
* @return a {@link ScheduledExecutorService}.
*/
@NonNull
protected static ScheduledExecutorService executorService() {
// Future-proofing, if we find out that events are drowning Timer then we may need to move them to their
// own dedicated ScheduledExecutorService thread pool
return Timer.get();
protected static synchronized ScheduledExecutorService executorService() {
if (executorService == null) {
// corePoolSize is set to 10, but will only be created if needed.
// ScheduledThreadPoolExecutor "acts as a fixed-sized pool using corePoolSize threads"
executorService = new ImpersonatingScheduledExecutorService(new ScheduledThreadPoolExecutor(10, new NamingThreadFactory(new ClassLoaderSanityThreadFactory(new DaemonThreadFactory()), "SCMEvent")), ACL.SYSTEM);
}
return executorService;
}


/**
* Shutdown the timer and throw it away.
*/
@Terminator
public synchronized void closeExecutorService() {
if (executorService != null) {
executorService.shutdownNow();
executorService = null;
}
}

/**
Expand Down Expand Up @@ -454,6 +479,7 @@ public Dispatcher(E event) {
}

protected abstract void log(SCMEventListener l, Throwable e);

protected abstract void fire(SCMEventListener l, E event);

@Override
Expand Down

0 comments on commit df46ae7

Please sign in to comment.