Skip to content

Instantly share code, notes, and snippets.

@zeroows
Created June 11, 2015 08:47
Show Gist options
  • Save zeroows/5ec3472f0f0bf3c6b1ba to your computer and use it in GitHub Desktop.
Save zeroows/5ec3472f0f0bf3c6b1ba to your computer and use it in GitHub Desktop.
To Utilize the same Executor in java
package tk.aalkhodiry.concurrent.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.concurrent.*;
public class CompletionServiceTaskScheduler<T> implements CompletionService<T>, TaskScheduler {
private static final Logger LOG = LoggerFactory.getLogger(CompletionServiceTaskScheduler.class);
private TaskScheduler taskScheduler;
private CompletionService<T> completionService;
@PostConstruct
public void initializeCompletionServiceTaskScheduler() {
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
final int availableProcessors = Runtime.getRuntime().availableProcessors();
threadPoolTaskScheduler.setThreadNamePrefix("MySTE-");
threadPoolTaskScheduler.setPoolSize(availableProcessors);
threadPoolTaskScheduler.setErrorHandler(org.springframework.scheduling.support.TaskUtils.LOG_AND_PROPAGATE_ERROR_HANDLER);
threadPoolTaskScheduler.initialize();
completionService = new ExecutorCompletionService<>(threadPoolTaskScheduler.getScheduledExecutor());
taskScheduler = threadPoolTaskScheduler;
LOG.trace(String.format("Initializing CompletionService and TaskScheduler is done and the Available Processors in this system: %s", availableProcessors));
}
public Future<T> submit(Callable<T> task) {
return completionService.submit(task);
}
public Future<T> submit(Runnable task, T result) {
return completionService.submit(task, result);
}
public Future<T> take() throws InterruptedException {
return completionService.take();
}
public Future<T> poll() {
return completionService.poll();
}
public Future<T> poll(long timeout, TimeUnit unit) throws InterruptedException {
return completionService.poll(timeout, unit);
}
public ScheduledFuture schedule(Runnable runnable, Trigger trigger) {
return taskScheduler.schedule(delegate(runnable), trigger);
}
public ScheduledFuture schedule(Runnable runnable, Date date) {
return taskScheduler.schedule(delegate(runnable), date);
}
public ScheduledFuture scheduleAtFixedRate(Runnable runnable, Date date, long l) {
return taskScheduler.scheduleAtFixedRate(delegate(runnable), date, l);
}
public ScheduledFuture scheduleAtFixedRate(Runnable runnable, long l) {
return taskScheduler.scheduleAtFixedRate(delegate(runnable), l);
}
public ScheduledFuture scheduleWithFixedDelay(Runnable runnable, Date date, long l) {
return taskScheduler.scheduleWithFixedDelay(delegate(runnable), date, l);
}
public ScheduledFuture scheduleWithFixedDelay(Runnable runnable, long l) {
return taskScheduler.scheduleWithFixedDelay(delegate(runnable), l);
}
protected Runnable delegate(final Runnable runnable) {
return new Runnable() {
@SuppressWarnings({"unchecked"})
public void run() {
completionService.submit(runnable, null);
}
};
}
public TaskScheduler getTaskScheduler() {
return taskScheduler;
}
public CompletionService<T> getCompletionService() {
return completionService;
}
}
@zeroows
Copy link
Author

zeroows commented Jun 11, 2015

To use it in Spring boot:

/**
     * Defining Scheduler And Task Scheduler
     */
    @Configuration
    @EnableScheduling
    public static class MySchedulerConfig implements SchedulingConfigurer{
        private static final Logger LOG = LoggerFactory.getLogger(MySchedulerConfig.class);

        private static CompletionServiceTaskScheduler completionServiceTaskScheduler = new CompletionServiceTaskScheduler();

        @Bean
        protected CompletionServiceTaskScheduler taskSchedulerCompletionService() {

            return completionServiceTaskScheduler;
        }

        @Override
        public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
            taskRegistrar.setTaskScheduler(completionServiceTaskScheduler.getTaskScheduler());
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment