Skip to content

Commit

Permalink
[add] 实现定时发布
Browse files Browse the repository at this point in the history
  • Loading branch information
ZYNORl committed Jan 11, 2024
1 parent 0977fb6 commit a4e5c8e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public class PublishPostReqBO {
* 是否为定时发布
*/
private Boolean isTiming;
/**
* 发布时间
*/
private Long publishTime;
/**
* 首次开始编写帖子的时间
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package top.zynorl.petplanet.post.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -16,6 +17,7 @@
* @Date 2024/01/02
**/
@RestController
@Slf4j
@RequestMapping("/post")
public class PostController {
@Autowired
Expand All @@ -26,10 +28,11 @@ public class PostController {
public R<Boolean> publish(@RequestBody PublishPostReq publishPostReq){
// TODO 获取登录人信息,并将userId添加里面
PublishPostReqBO publishPostReqBO = postControllerConverter.toPublishPostReqBO(publishPostReq);
Boolean isOk = postService.publishPost(publishPostReqBO);
if(isOk){
try {
postService.publishPost(publishPostReqBO);
return R.ok(true);
}else {
}catch (Exception e){
log.error("PostController发布失败:"+e.getMessage());
return R.no("发布失败");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package top.zynorl.petplanet.post.scheduler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.zynorl.petplanet.post.common.pojo.bo.req.PublishPostReqBO;
import top.zynorl.petplanet.post.transaction.PostPublishTransactionalService;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* Created by zynorl on 2024/1/11 12:57
*/
@Service
public class PostPublishScheduler {

@Autowired
private PostPublishTransactionalService postPublishTransactionalService;
private ScheduledExecutorService scheduler;

private void init() {
// 初始化一个定长线程池
scheduler = Executors.newScheduledThreadPool(3);
}

// 假设这是一个服务方法,接收前端传递的发布时间(LocalDateTime类型)
public void schedulePublication(PublishPostReqBO publishPostReqBO) {
init();
// 计算从当前时间到目标发布时间的时间差
LocalDateTime publishTime = Instant.ofEpochMilli(publishPostReqBO.getPublishTime())
.atZone(ZoneId.systemDefault())
.toLocalDateTime();
long delay = Duration.between(LocalDateTime.now(), publishTime).toMillis();

// 如果延迟时间为负数,说明发布时间已过,则立即执行
if (delay < 0) {
postPublishTransactionalService.doProcess(publishPostReqBO);
} else {
// 使用ScheduledExecutorService安排任务
scheduler.schedule(() -> postPublishTransactionalService.doProcess(publishPostReqBO), delay, TimeUnit.MILLISECONDS);
}
shutdown();
}


private void shutdown() {
// 当不再需要定时任务时,确保关闭scheduler
if (scheduler != null && !scheduler.isShutdown()) {
scheduler.shutdown();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public interface PostService {
* @param publishPostReqBO
* @return
*/
Boolean publishPost(PublishPostReqBO publishPostReqBO);
void publishPost(PublishPostReqBO publishPostReqBO);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.zynorl.petplanet.post.common.pojo.bo.req.PublishPostReqBO;
import top.zynorl.petplanet.post.scheduler.PostPublishScheduler;
import top.zynorl.petplanet.post.service.PostService;
import top.zynorl.petplanet.post.transaction.PostPublishTransactionalService;

Expand All @@ -17,11 +18,11 @@
public class PostServiceImpl implements PostService {

@Autowired
private PostPublishTransactionalService postPublishTransactionalService;
private PostPublishScheduler postPublishScheduler;

@Override
public Boolean publishPost(PublishPostReqBO publishPostReqBO) {
return postPublishTransactionalService.doProcess(publishPostReqBO);
public void publishPost(PublishPostReqBO publishPostReqBO) {
postPublishScheduler.schedulePublication(publishPostReqBO);
}

}

0 comments on commit a4e5c8e

Please sign in to comment.