定时发布,期刊列表返回全部标签

main
JH 10 months ago
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;
}
}

@ -21,14 +21,21 @@ public interface JournalDao extends JpaRepository<Journal,String>,JpaSpecificati
List<Journal> findByJournalNoIn(Set<String> journalNos); List<Journal> findByJournalNoIn(Set<String> journalNos);
List<Journal> findByIsScheduledAndIsPublish(String isScheduled, String isPublish);
List<Journal> findByIdIn(Set<String> ids); List<Journal> findByIdIn(Set<String> ids);
@Modifying @Modifying
@Transactional @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, int updatePubById(@Param("id") String id,
@Param("pubTime") LocalDateTime pubTime); @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 @Modifying
@Transactional @Transactional
@Query("UPDATE Journal j SET j.isScheduled = '1', j.pubTime = :pubTime WHERE j.id = :id") @Query("UPDATE Journal j SET j.isScheduled = '1', j.pubTime = :pubTime WHERE j.id = :id")

@ -191,10 +191,10 @@ public class CMSJournalService {
list = new ArrayList<>(); list = new ArrayList<>();
} }
Tag tag = idTagMap.get(item.getTagId()); Tag tag = idTagMap.get(item.getTagId());
if (StringUtils.isNotBlank(tag.getParentId())) { // if (StringUtils.isNotBlank(tag.getParentId())) {
// list.add(tag.getNameCh()); // list.add(tag.getNameCh());
list.add(tag.getId()); list.add(tag.getId());
} // }
journalTagMap.put(journalId, list); journalTagMap.put(journalId, list);
} }
return journalTagMap; return journalTagMap;

Loading…
Cancel
Save