package de.otto.edison.jobtrigger.trigger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * A service that is using all discovered job definitions to schedule triggering of jobs.
 *
 * @author Guido Steinacker
 * @since 05.09.15
 */
@Service
public class JobScheduler {

    @Autowired
    private ThreadPoolTaskScheduler scheduler;

    public void updateTriggers(final List<JobTrigger> jobTriggers) {
        stopAllTriggers();
        scheduler.initialize();
        for (final JobTrigger jobTrigger : jobTriggers) {
            scheduler.schedule(
                    jobTrigger.getRunnable(),
                    jobTrigger.getTrigger());
        }
    }

    public void stopAllTriggers() {
        scheduler.destroy();
    }
}

@@ -37,16 +37,269 @@
 igger;%0A%0A
+import static com.google.common.collect.ImmutableSet.copyOf;%0Aimport static org.slf4j.LoggerFactory.getLogger;%0A%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.concurrent.ScheduledFuture;%0A%0Aimport org.slf4j.Logger;%0A
 import o
@@ -482,30 +482,93 @@
 %0Aimport 
-java.util.List
+com.google.common.collect.Sets;%0Aimport com.google.common.collect.Sets.SetView
 ;%0A%0A/**%0A 
@@ -817,101 +817,344 @@
 er;%0A
-%0A    public void updateTriggers(final List%3CJobTrigger%3E jobTriggers) %7B%0A        stopAll
+    %0A    private Map%3CJobTrigger, ScheduledFuture%3E taskMap = new HashMap%3CJobTrigger, ScheduledFuture%3E();%0A%0A    private static final Logger LOG = getLogger(JobScheduler.class);%0A%0A    public void updateTriggers(final List%3CJobTrigger%3E jobTriggers) %7B%0A    %09SetView%3CJobTrigger%3E deletedTasks = Sets.difference(taskMap.keySet(), copyOf(job
 Triggers
 ();%0A
@@ -1153,75 +1153,229 @@
 gers
-(
+)
 );%0A    
-    scheduler.initialize
+%09SetView%3CJobTrigger%3E newTasks = Sets.difference(copyOf(jobTriggers), taskMap.keySet
 ()
+)
 ;%0A    
-    for (final Job
+%09deletedTasks.forEach(e-%3EstopTrigger(e)); %0A    %09newTasks.forEach(e-%3EstartTrigger(e));%0A    %7D%0A%0A%09private void start
 Trigger
- j
+(J
 obTr
@@ -1383,10 +1383,8 @@
 gger
- :
  job
@@ -1382,33 +1382,32 @@
 igger jobTrigger
-s
 ) %7B%0A            
@@ -1406,210 +1406,477 @@
     
-    scheduler.schedule(%0A                    jobTrigger.getRunnable(),%0A                    jobTrigger.getTrigger());%0A        %7D%0A    %7D%0A%0A    public void stopAllTriggers() %7B%0A        scheduler.destroy();%0A    
+LOG.info(%22Start JobTrigger %22 + jobTrigger);%0A%09%09ScheduledFuture%3C?%3E task = scheduler.schedule(jobTrigger.getRunnable(), jobTrigger.getTrigger());%0A%09%09taskMap.put(jobTrigger, task);%0A%09%7D%0A%0A%09private void stopTrigger(JobTrigger jobTrigger) %7B%0A        LOG.info(%22Stop JobTrigger %22 + jobTrigger);%0A%09%09taskMap.get(jobTrigger).cancel(false);%0A%09%09taskMap.remove(jobTrigger);%0A%09%7D%0A%0A%09public void stopAllTriggers() %7B%0A%09%09taskMap.values().stream().forEach(e-%3Ee.cancel(false));%0A%09%09taskMap.clear();%09%09%0A%09
 %7D%0A%7D%0A

Update JobScheduler.java