-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WFLY-3770 Graceful shutdown for EE concurrency
- Loading branch information
1 parent
4e16f37
commit c2a06d8
Showing
14 changed files
with
578 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
ee/src/main/java/org/jboss/as/ee/concurrent/ControlPointUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2014, Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 2110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.jboss.as.ee.concurrent; | ||
|
||
import org.wildfly.extension.requestcontroller.ControlPoint; | ||
|
||
import javax.enterprise.concurrent.ManagedTask; | ||
import javax.enterprise.concurrent.ManagedTaskListener; | ||
import java.util.Map; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.RejectedExecutionException; | ||
|
||
/** | ||
* Class that manages the control point for executor services | ||
* | ||
* @author Stuart Douglas | ||
*/ | ||
public class ControlPointUtils { | ||
|
||
public static Runnable doWrap(Runnable runnable,ControlPoint controlPoint) { | ||
if(controlPoint == null) { | ||
return runnable; | ||
} | ||
try { | ||
controlPoint.forceBeginRequest(); | ||
return new ControlledRunnable(runnable, controlPoint); | ||
} catch (Exception e) { | ||
throw new RejectedExecutionException(e); | ||
} | ||
} | ||
|
||
public static <T> Callable<T> doWrap(Callable<T> callable, ControlPoint controlPoint) { | ||
if(controlPoint == null) { | ||
return callable; | ||
} | ||
try { | ||
controlPoint.forceBeginRequest(); | ||
return new ControlledCallable<>(callable, controlPoint); | ||
} catch (Exception e) { | ||
throw new RejectedExecutionException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Runnable that wraps a a runnable to allow server suspend/resume to work correctly. | ||
* | ||
*/ | ||
static class ControlledRunnable implements Runnable, ManagedTask { | ||
|
||
private final Runnable runnable; | ||
private final ControlPoint controlPoint; | ||
|
||
ControlledRunnable(Runnable runnable, ControlPoint controlPoint) { | ||
this.runnable = runnable; | ||
this.controlPoint = controlPoint; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
runnable.run(); | ||
} finally { | ||
controlPoint.requestComplete(); | ||
} | ||
} | ||
@Override | ||
public Map<String, String> getExecutionProperties() { | ||
if(runnable instanceof ManagedTask) { | ||
((ManagedTask) runnable).getExecutionProperties(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public ManagedTaskListener getManagedTaskListener() { | ||
if(runnable instanceof ManagedTask) { | ||
((ManagedTask) runnable).getManagedTaskListener(); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Runnable that wraps a a runnable to allow server suspend/resume to work correctly. | ||
* | ||
*/ | ||
static class ControlledCallable<T> implements Callable<T>, ManagedTask { | ||
|
||
private final Callable<T> callable; | ||
private final ControlPoint controlPoint; | ||
|
||
ControlledCallable(Callable<T> callable, ControlPoint controlPoint) { | ||
this.callable = callable; | ||
this.controlPoint = controlPoint; | ||
} | ||
|
||
@Override | ||
public T call() throws Exception { | ||
try { | ||
return callable.call(); | ||
} finally { | ||
controlPoint.requestComplete(); | ||
} | ||
} | ||
|
||
@Override | ||
public Map<String, String> getExecutionProperties() { | ||
if(callable instanceof ManagedTask) { | ||
((ManagedTask) callable).getExecutionProperties(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public ManagedTaskListener getManagedTaskListener() { | ||
if(callable instanceof ManagedTask) { | ||
((ManagedTask) callable).getManagedTaskListener(); | ||
} | ||
return null; | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
ee/src/main/java/org/jboss/as/ee/concurrent/ManagedExecutorServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2014, Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 2110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.jboss.as.ee.concurrent; | ||
|
||
import org.glassfish.enterprise.concurrent.ContextServiceImpl; | ||
import org.glassfish.enterprise.concurrent.ManagedThreadFactoryImpl; | ||
import org.wildfly.extension.requestcontroller.ControlPoint; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.jboss.as.ee.concurrent.ControlPointUtils.doWrap; | ||
|
||
/** | ||
* @author Stuart Douglas | ||
*/ | ||
public class ManagedExecutorServiceImpl extends org.glassfish.enterprise.concurrent.ManagedExecutorServiceImpl { | ||
|
||
private final ControlPoint controlPoint; | ||
|
||
public ManagedExecutorServiceImpl(String name, ManagedThreadFactoryImpl managedThreadFactory, long hungTaskThreshold, boolean longRunningTasks, int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit keepAliveTimeUnit, long threadLifeTime, ContextServiceImpl contextService, RejectPolicy rejectPolicy, BlockingQueue<Runnable> queue, ControlPoint controlPoint) { | ||
super(name, managedThreadFactory, hungTaskThreshold, longRunningTasks, corePoolSize, maxPoolSize, keepAliveTime, keepAliveTimeUnit, threadLifeTime, contextService, rejectPolicy, queue); | ||
this.controlPoint = controlPoint; | ||
} | ||
|
||
public ManagedExecutorServiceImpl(String name, ManagedThreadFactoryImpl managedThreadFactory, long hungTaskThreshold, boolean longRunningTasks, int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit keepAliveTimeUnit, long threadLifeTime, int queueCapacity, ContextServiceImpl contextService, RejectPolicy rejectPolicy, ControlPoint controlPoint) { | ||
super(name, managedThreadFactory, hungTaskThreshold, longRunningTasks, corePoolSize, maxPoolSize, keepAliveTime, keepAliveTimeUnit, threadLifeTime, queueCapacity, contextService, rejectPolicy); | ||
this.controlPoint = controlPoint; | ||
} | ||
|
||
@Override | ||
public <T> Future<T> submit(Callable<T> task) { | ||
return super.submit(doWrap(task, controlPoint)); | ||
} | ||
|
||
@Override | ||
public <T> Future<T> submit(Runnable task, T result) { | ||
return super.submit(doWrap(task, controlPoint), result); | ||
} | ||
|
||
@Override | ||
public Future<?> submit(Runnable task) { | ||
return super.submit(doWrap(task, controlPoint)); | ||
} | ||
|
||
@Override | ||
public void execute(Runnable command) { | ||
super.execute(doWrap(command, controlPoint)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.