package com.plannaplan.services; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ScheduledFuture; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.support.CronTrigger; import org.springframework.stereotype.Service; @Service public class EventService { @Autowired private ExchangeService exchangeService; public static final int FIRST_TOUR_SCHEDULE = 0; public static final int SECOND_TOUR_SCHEDULE = 1; private ThreadPoolTaskScheduler scheduler; private Map> jobsMap = new HashMap<>(); @Scheduled(cron = "0 24 17 * * *") public void collectGroupLosses() { System.out.println("Checking for groups"); } @Scheduled(cron = "0 0 0 * * *") public void performExchangeService() { System.out.println("Performing Exchange"); this.exchangeService.performExchange(); } /** * Schedule provided task to perform * * @param taskId static filed of this class that represents to what event * we want to assign task * @param task runnable class that perform task in implemented run method * @param cronTrigger CronTrigger instance with date to perform */ public void scheduleTask(int taskId, Runnable task, CronTrigger cronTrigger) { ScheduledFuture scheduledTask = jobsMap.get(taskId); if (scheduledTask != null) { scheduledTask.cancel(true); jobsMap.put(taskId, null); } scheduledTask = this.scheduler.schedule(task, cronTrigger); jobsMap.put(taskId, scheduledTask); } @PostConstruct public void initialize() { this.scheduler = new ThreadPoolTaskScheduler(); this.scheduler.initialize(); } }