2020-12-21 17:12:10 +01:00
|
|
|
package com.plannaplan.services;
|
|
|
|
|
2021-01-04 18:10:14 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.concurrent.ScheduledFuture;
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
2021-01-13 14:42:13 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2020-12-21 17:12:10 +01:00
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
2021-01-04 18:10:14 +01:00
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
|
|
|
import org.springframework.scheduling.support.CronTrigger;
|
2020-12-21 17:12:10 +01:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2021-01-15 15:54:17 +01:00
|
|
|
/**
|
|
|
|
* Service to manage app events
|
|
|
|
*/
|
2020-12-21 17:12:10 +01:00
|
|
|
@Service
|
|
|
|
public class EventService {
|
|
|
|
|
2021-01-13 14:42:13 +01:00
|
|
|
@Autowired
|
|
|
|
private ExchangeService exchangeService;
|
|
|
|
|
2021-01-04 18:10:14 +01:00
|
|
|
public static final int FIRST_TOUR_SCHEDULE = 0;
|
|
|
|
public static final int SECOND_TOUR_SCHEDULE = 1;
|
|
|
|
|
|
|
|
private ThreadPoolTaskScheduler scheduler;
|
|
|
|
private Map<Integer, ScheduledFuture<?>> jobsMap = new HashMap<>();
|
|
|
|
|
|
|
|
@Scheduled(cron = "0 24 17 * * *")
|
2020-12-21 17:12:10 +01:00
|
|
|
public void collectGroupLosses() {
|
|
|
|
System.out.println("Checking for groups");
|
|
|
|
}
|
2021-01-03 18:06:25 +01:00
|
|
|
|
2021-01-15 15:54:17 +01:00
|
|
|
/**
|
|
|
|
* perfroms checks for matching exchanges daily
|
|
|
|
*/
|
2021-01-13 14:42:13 +01:00
|
|
|
@Scheduled(cron = "0 0 0 * * *")
|
|
|
|
public void performExchangeService() {
|
2021-01-13 16:52:18 +01:00
|
|
|
System.out.println("Performing Exchange");
|
|
|
|
this.exchangeService.performExchange();
|
2021-01-13 14:42:13 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 11:14:10 +01:00
|
|
|
/**
|
|
|
|
* Schedule provided task to perform
|
2021-01-13 14:42:13 +01:00
|
|
|
*
|
2021-01-05 11:14:10 +01:00
|
|
|
* @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
|
|
|
|
*/
|
2021-01-04 18:10:14 +01:00
|
|
|
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);
|
2021-01-03 18:06:25 +01:00
|
|
|
}
|
|
|
|
|
2021-01-15 15:54:17 +01:00
|
|
|
/**
|
|
|
|
* init resources needed for dynamicly creating new tasks (needed to set tours
|
|
|
|
* end events)
|
|
|
|
*/
|
2021-01-04 18:10:14 +01:00
|
|
|
@PostConstruct
|
|
|
|
public void initialize() {
|
|
|
|
this.scheduler = new ThreadPoolTaskScheduler();
|
|
|
|
this.scheduler.initialize();
|
2021-01-03 18:06:25 +01:00
|
|
|
}
|
2020-12-21 17:12:10 +01:00
|
|
|
}
|