parent
d2e6438ba9
commit
4e43a43a7c
@ -0,0 +1,98 @@
|
||||
package com.luoo.music.config;
|
||||
|
||||
import com.luoo.music.dao.JournalDao;
|
||||
import com.luoo.music.pojo.Journal;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
/**
|
||||
* @author locust
|
||||
*/
|
||||
@Component
|
||||
public class StartupRunner implements ApplicationRunner {
|
||||
|
||||
public static Map<String, ScheduledFuture<?>> taskMap = new HashMap<>();
|
||||
|
||||
@Autowired
|
||||
private JournalDao journalDao;
|
||||
@Autowired
|
||||
private ThreadPoolTaskScheduler threadPoolTaskScheduler;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
// 定时发布未发布
|
||||
List<Journal> journals = journalDao.findByIsScheduledAndIsPublish("1", "0");
|
||||
for (Journal journal : journals) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
LocalDateTime pubTime = journal.getPubTime();
|
||||
Date date = Date.from(pubTime.atZone(ZoneId.systemDefault()).toInstant());
|
||||
int comparisonResult = pubTime.compareTo(now);
|
||||
// 发布时间在当前时间之后
|
||||
if (comparisonResult > 0) {
|
||||
StartRunnable startRunnable = new StartRunnable(journal.getId(), journal.getPubTime());
|
||||
ScheduledFuture<?> schedule = threadPoolTaskScheduler.schedule(startRunnable, date);
|
||||
taskMap.put(journal.getId(), schedule);
|
||||
} else {
|
||||
journalDao.updateIsPublishById(journal.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class StartRunnable implements Runnable {
|
||||
|
||||
private String id;
|
||||
private LocalDateTime pubTime;
|
||||
|
||||
public StartRunnable(String id, LocalDateTime pubTime) {
|
||||
this.id = id;
|
||||
this.pubTime = pubTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
journalDao.updatePubById(id, pubTime);
|
||||
taskMap.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
private class StopRunnable implements Runnable {
|
||||
|
||||
private String id;
|
||||
|
||||
public StopRunnable(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
ScheduledFuture<?> scheduledFuture = taskMap.get(id);
|
||||
if (scheduledFuture != null) {
|
||||
boolean cancel = scheduledFuture.cancel(true);
|
||||
if (cancel) {
|
||||
taskMap.remove(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
|
||||
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
|
||||
threadPoolTaskScheduler.setPoolSize(10);
|
||||
threadPoolTaskScheduler.setRemoveOnCancelPolicy(true);
|
||||
threadPoolTaskScheduler.setThreadNamePrefix("TrainTaskThreadPool-");
|
||||
return threadPoolTaskScheduler;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue