package org.marsik.bugautomation;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.servlet.ServletException;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.marsik.bugautomation.jobs.BugzillaRefreshJob;
import org.marsik.bugautomation.jobs.RefreshRulesJob;
import org.marsik.bugautomation.jobs.TrelloRefreshJob;
import org.marsik.bugautomation.server.RestServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {
    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    private ScheduledExecutorService scheduler;
    private ExecutorService threadPool;

    @Inject
    RefreshRulesJob refreshRulesJob;

    @Inject
    TrelloRefreshJob trelloRefreshJob;

    @Inject
    BugzillaRefreshJob bugzillaRefreshJob;

    @PostConstruct
    public void create() {
        scheduler = Executors.newSingleThreadScheduledExecutor();
        threadPool = Executors.newCachedThreadPool();

        scheduler.scheduleWithFixedDelay(() -> threadPool.submit(refreshRulesJob),
                0, 30, TimeUnit.SECONDS);
        scheduler.scheduleWithFixedDelay(() -> threadPool.submit(trelloRefreshJob),
                0, 120, TimeUnit.SECONDS);
        scheduler.scheduleWithFixedDelay(() -> threadPool.submit(bugzillaRefreshJob),
                0, 300, TimeUnit.SECONDS);
    }

    @PreDestroy
    public void tearDown() {
        scheduler.shutdown();
        threadPool.shutdown();
    }

    @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
    public static void main(String[] args) {
        final Weld w = new Weld();
        final WeldContainer wc = w.initialize();

        // Make sure the app is instantiated
        Main bean = wc.instance().select(Main.class).get();
        bean.toString();

        // Start REST server
        try {
            RestServer.build(8080, wc.getBeanManager());
        } catch (ServletException ex) {
            logger.error("Server failed", ex);
        }
    }
}

@@ -158,32 +158,89 @@
 xecutorService;%0A
+import java.util.concurrent.ScheduledThreadPoolExecutor;%0A
 import java.util
@@ -984,48 +984,8 @@
 ler;
-%0A    private ExecutorService threadPool;
 %0A%0A  
@@ -1224,78 +1224,14 @@
 newS
-ingleThreadScheduledExecutor();%0A        threadPool = Executors.newCach
+chedul
 edTh
@@ -1239,16 +1239,17 @@
 eadPool(
+3
 );%0A%0A    
@@ -1289,32 +1289,8 @@
 lay(
-() -%3E threadPool.submit(
 refr
@@ -1300,17 +1300,16 @@
 RulesJob
-)
 ,%0A      
@@ -1389,32 +1389,8 @@
 lay(
-() -%3E threadPool.submit(
 trel
@@ -1393,33 +1393,32 @@
 trelloRefreshJob
-)
 ,%0A              
@@ -1491,32 +1491,8 @@
 lay(
-() -%3E threadPool.submit(
 bugz
@@ -1505,17 +1505,16 @@
 freshJob
-)
 ,%0A      
@@ -1636,39 +1636,8 @@
 ();%0A
-        threadPool.shutdown();%0A
     

Use fixed scheduling thread pool to prevent concurrent execution of stalled jobs