package com.plannaplan.services; import java.sql.Date; import java.time.LocalDate; import java.util.Calendar; import java.util.HashMap; import java.util.Map; import java.util.TimeZone; import java.util.concurrent.ScheduledFuture; import javax.annotation.PostConstruct; import com.plannaplan.entities.AppConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.scheduling.TaskScheduler; // 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 { public static final int FIRST_TOUR_SCHEDULE = 0; public static final int SECOND_TOUR_SCHEDULE = 1; // @Autowired // private EmailService emailService; private ThreadPoolTaskScheduler scheduler; private Map> jobsMap = new HashMap<>(); // @Autowired // private ConfiguratorService configService; @Scheduled(cron = "0 24 17 * * *") public void collectGroupLosses() { System.out.println("Checking for groups"); } // @Scheduled(cron = "#{eventService.getFirstTourValue()}") // public void performAcceptAction() { // System.out.println("Checking for hahaha"); // } // new CronTrigger("0 0 0 * * ?", // TimeZone.getTimeZone(TimeZone.getDefault().getID())) 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); } // @Bean // public String getFirstTourValue() { // final AppConfig config = this.configService.getCurrentConfig(); // final LocalDate tourEnd = config.getFirstTourEnd().toLocalDate(); // final String cronExp = "0 0 0 " + tourEnd.getDayOfMonth() + " " + // tourEnd.getMonthValue() + " " // + tourEnd.getYear(); // return cronExp; // } // @Bean // public String getSecondTourValue() { // return "0 6 18 * * *"; // } @PostConstruct public void initialize() { this.scheduler = new ThreadPoolTaskScheduler(); this.scheduler.initialize(); } }