Created
November 5, 2014 17:58
-
-
Save yupadhyay/a184b3d9bbd6abffc30b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.util.Map; | |
import org.apache.felix.scr.annotations.Activate; | |
import org.apache.felix.scr.annotations.Component; | |
import org.apache.felix.scr.annotations.Properties; | |
import org.apache.felix.scr.annotations.Property; | |
import org.apache.felix.scr.annotations.Reference; | |
import org.apache.felix.scr.annotations.Service; | |
import org.apache.sling.api.SlingHttpServletRequest; | |
import org.apache.sling.api.SlingHttpServletResponse; | |
import org.apache.sling.commons.osgi.OsgiUtil; | |
import org.apache.sling.commons.threads.ModifiableThreadPoolConfig; | |
import org.apache.sling.commons.threads.ThreadPool; | |
import org.apache.sling.commons.threads.ThreadPoolConfig.ThreadPriority; | |
import org.apache.sling.commons.threads.ThreadPoolManager; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
@Component | |
@Service | |
public class YourTestClassService { | |
private static final Logger LOGGER = LoggerFactory.getLogger(YourTestClassService.class); | |
@Reference | |
private ThreadPoolManager threadPoolManager; | |
/** your thread pool */ | |
private ThreadPool threadPool; | |
@Property(label = "minThreadPoolSize", description="Minium Thread pool size", intValue=5) | |
private static final String MINIMUM_THREAD_POOL_SIZE = "minThreadPoolSize"; | |
private int minThreadPoolSize; | |
@Property(label = "maxThreadPoolSize", description="Max Thread pool size", intValue=10) | |
private static final String MAXUIMUM_THREAD_POOL_SIZE = "maxThreadPoolSize"; | |
private int maxThreadPoolSize; | |
@Activate | |
protected final void activate(final Map<Object, Object> config) { | |
this.minThreadPoolSize = OsgiUtil.toInteger(config.get(MINIMUM_THREAD_POOL_SIZE), 5); | |
this.maxThreadPoolSize = OsgiUtil.toInteger(config.get(MAXUIMUM_THREAD_POOL_SIZE), 10); | |
ModifiableThreadPoolConfig threadPoolConfig = new ModifiableThreadPoolConfig(); | |
if(threadPoolConfig.getMinPoolSize()<this.minThreadPoolSize) { | |
threadPoolConfig.setMinPoolSize(this.minThreadPoolSize); | |
} | |
if(threadPoolConfig.getMaxPoolSize()<this.maxThreadPoolSize) { | |
threadPoolConfig.setMaxPoolSize(this.maxThreadPoolSize); | |
} | |
//You can make this configurable as well | |
threadPoolConfig.setPriority(ThreadPriority.NORM); | |
this.threadPool = threadPoolManager.create(threadPoolConfig); | |
if(threadPool == null) { | |
throw new IllegalStateException("Could not get a ThreadPool"); | |
} | |
} | |
@Deactivate | |
protected void deactivate(ComponentContext ctx) throws Exception { | |
if(threadPool != null) { | |
threadPoolManager.release(threadPool); | |
threadPoolManager = null; | |
threadPool = null; | |
} | |
} | |
public void yourMethod(){ | |
YourThreadClass your_thread_class = new YourThreadClass(); | |
Runnable YOUR_CUSTOM_THREAD = new Thread(your_thread_class); | |
this.threadPool.execute(YOUR_CUSTOM_THREAD); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment