diff --git a/luoo_music/src/main/java/com/luoo/music/config/StartupRunner.java b/luoo_music/src/main/java/com/luoo/music/config/StartupRunner.java new file mode 100644 index 0000000..63d2e96 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/config/StartupRunner.java @@ -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> taskMap = new HashMap<>(); + + @Autowired + private JournalDao journalDao; + @Autowired + private ThreadPoolTaskScheduler threadPoolTaskScheduler; + + @Override + public void run(ApplicationArguments args) throws Exception { + // 定时发布未发布 + List 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; + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java b/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java index f879995..304557b 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java @@ -21,14 +21,21 @@ public interface JournalDao extends JpaRepository,JpaSpecificati List findByJournalNoIn(Set journalNos); + List findByIsScheduledAndIsPublish(String isScheduled, String isPublish); + List findByIdIn(Set ids); @Modifying @Transactional - @Query("UPDATE Journal j SET j.isPublish = '1', j.pubTime = :pubTime WHERE j.id = :id") + @Query("UPDATE Journal j SET j.isPublish = '1', j.isScheduled = '0', j.pubTime = :pubTime WHERE j.id = :id") int updatePubById(@Param("id") String id, @Param("pubTime") LocalDateTime pubTime); + @Modifying + @Transactional + @Query("UPDATE Journal j SET j.isPublish = '1'WHERE j.id = :id") + int updateIsPublishById(@Param("id") String id); + @Modifying @Transactional @Query("UPDATE Journal j SET j.isScheduled = '1', j.pubTime = :pubTime WHERE j.id = :id") diff --git a/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java b/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java index 81ea603..57ba644 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java @@ -191,10 +191,10 @@ public class CMSJournalService { list = new ArrayList<>(); } Tag tag = idTagMap.get(item.getTagId()); - if (StringUtils.isNotBlank(tag.getParentId())) { + // if (StringUtils.isNotBlank(tag.getParentId())) { // list.add(tag.getNameCh()); list.add(tag.getId()); - } + // } journalTagMap.put(journalId, list); } return journalTagMap;