Skip to content

Commit

Permalink
feat: [功能]秒级任务触发 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeswalker23 committed May 13, 2021
1 parent 2d8acad commit 53362d9
Show file tree
Hide file tree
Showing 18 changed files with 281 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ A widget for helping investors manage funds.
- [x] 简单邮件
- [x] 视图模板邮件
- [ ] 网页通知
- [ ] 秒级任务触发

## 优化项:
### 通用向
Expand Down
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,13 @@ public interface VirtualUserDao {
* @param virtualUser 待插入数据
*/
void insert(@Param("virtualUser") VirtualUserModel virtualUser);

/**
* 根据 mailbox 查询
*
* @param mailbox 邮箱
* @return VirtualUserModel
*/
@Select("select * from virtual_user where mailbox=#{mailbox} limit 1")
VirtualUserModel selectByMailbox(@Param("mailbox") String mailbox);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import io.walkers.planes.fundhelper.entity.model.WebsiteNoticeModel;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* Dao of {@link WebsiteNoticeModel}
Expand All @@ -18,4 +21,12 @@ public interface WebsiteNoticeDao {
* @param websiteNotice 待插入数据
*/
void insert(@Param("websiteNotice") WebsiteNoticeModel websiteNotice);

/**
* 查询未激活数据
*
* @return List
*/
@Select("select * from website_notice where active_date <= DATE_FORMAT(now(), 'yyyy-MM-dd 23:59:59')")
List<WebsiteNoticeModel> selectActivatedWebsiteNotices();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ public class VirtualUserModel implements Serializable {
* 更新时间
*/
private java.util.Date updateDate;
/**
* 邮箱
*/
private String mailbox;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.walkers.planes.fundhelper.entity.model;

import io.walkers.planes.fundhelper.service.task.ModelAggregation;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

/**
* 网页通知模型
Expand All @@ -16,7 +18,7 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class WebsiteNoticeModel implements Serializable {
public class WebsiteNoticeModel implements Serializable, ModelAggregation {
/**
* 主键
*/
Expand Down Expand Up @@ -47,4 +49,13 @@ public class WebsiteNoticeModel implements Serializable {
* 更新时间
*/
private java.util.Date updateDate;
/**
* 激活时间
*/
private java.util.Date activeDate;

@Override
public Date activeDate() {
return this.activeDate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.walkers.planes.fundhelper.schedule;

import io.walkers.planes.fundhelper.dao.WebsiteNoticeDao;
import io.walkers.planes.fundhelper.entity.model.WebsiteNoticeModel;
import io.walkers.planes.fundhelper.service.task.impl.DefaultDelayTaskHandler;
import io.walkers.planes.fundhelper.service.task.impl.DefaultDirectTaskHandler;
import io.walkers.planes.fundhelper.service.task.processor.TaskProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
* 定时任务:激活任务(每日执行)
*
* @author planeswalker23
*/
@Slf4j
@Component
public class DailyTaskProcessorSchedule {

@Resource
private DefaultDirectTaskHandler defaultDirectTaskHandler;
@Resource
private DefaultDelayTaskHandler defaultDelayTaskHandler;
@Resource
private WebsiteNoticeDao websiteNoticeDao;

/**
* 触发待激活任务
* 触发时机:每天00:00
*/
@Scheduled(cron = "1 * * * * ?")
public void dailyActiveTask() {
List<WebsiteNoticeModel> data = websiteNoticeDao.selectActivatedWebsiteNotices();
TaskProcessor.process(data, defaultDirectTaskHandler, defaultDelayTaskHandler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.Data;

import java.util.Date;

/**
* 通知信息实体类
*
Expand All @@ -26,4 +28,8 @@ public class NoticeMessage {
* {@link NoticeMethod}
*/
private String noticeMethod;
/**
* 激活时间
*/
private Date activeDate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public Boolean notice(NoticeMessage noticeMessage) {
.virtualUserId(userByMailbox.getId())
.readStatus(Boolean.FALSE)
.build();
websiteNotice.setActiveDate(noticeMessage.getActiveDate());
websiteNoticeDao.insert(websiteNotice);
log.info("网页通知成功 -> 内容: {}", JacksonUtil.toJson(noticeMessage));
return Boolean.TRUE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.walkers.planes.fundhelper.service.task;

import java.util.Date;

/**
* 满足激活条件的接口
*
* @author planeswalker23
*/
public interface ActivateCondition {

/**
* 是否满足条件
*
* @return Boolean
*/
Boolean match();

/**
* 获取激活时间
* @return Date
*/
Date activeDate();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.walkers.planes.fundhelper.service.task;

import io.walkers.planes.fundhelper.util.TimeUtil;

import java.util.Date;

/**
* 需要具备秒级触发能力的模型需要实现该接口
*
* @author planeswalker23
*/
public interface ModelAggregation extends ActivateCondition {

/**
* 获取激活时间
*
* @return Date
*/
@Override
Date activeDate();

/**
* 是否满足条件
*
* @return Boolean
*/
@Override
default Boolean match() {
Date startOfDay = TimeUtil.startOfDay(this.activeDate());
// 应已激活(今天第一秒大于激活时间)
return startOfDay.after(this.activeDate());
}

/**
* 执行任务
*
* @param taskHandler 任务执行器
*/
default void handle(TaskHandler taskHandler) {
taskHandler.handle();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.walkers.planes.fundhelper.service.task;

/**
* 任务执行器接口
*
* @author planeswalker23
*/
public interface TaskHandler {

/**
* 执行任务
*
* @return Boolean
*/
Boolean handle();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.walkers.planes.fundhelper.service.task.impl;

import io.walkers.planes.fundhelper.service.task.TaskHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
* 默认延迟任务执行器
*
* @author planeswalker23
*/
@Slf4j
@Component
public class DefaultDelayTaskHandler implements TaskHandler {
@Override
public Boolean handle() {
log.info("延迟任务执行器开始执行");
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.walkers.planes.fundhelper.service.task.impl;

import io.walkers.planes.fundhelper.service.task.TaskHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
* 默认直接任务执行器
*
* @author planeswalker23
*/
@Slf4j
@Component
public class DefaultDirectTaskHandler implements TaskHandler {
@Override
public Boolean handle() {
log.info("直接任务执行器开始执行");
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.walkers.planes.fundhelper.service.task.processor;

import io.walkers.planes.fundhelper.entity.model.WebsiteNoticeModel;
import io.walkers.planes.fundhelper.service.task.TaskHandler;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
* 默认任务模板处理器
*
* @author planeswalker23
*/
public class TaskProcessor {

/**
* 执行处理方法
*
* @return Boolean
*/
public static void process(List<WebsiteNoticeModel> data, TaskHandler directTaskHandler, TaskHandler delayTaskHandler) {
if (CollectionUtils.isEmpty(data)) {
return;
}
data.forEach(model -> {
// 应已激活的数据:直接任务执行器
// 即将激活的数据:插入 Redis 过期键,监听过期事件
TaskHandler taskHandler = model.match() ? directTaskHandler : delayTaskHandler;
model.handle(taskHandler);
});
}
}
18 changes: 18 additions & 0 deletions src/main/java/io/walkers/planes/fundhelper/util/TimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneOffset;
import java.util.Calendar;

/**
* @author planeswalker23
Expand Down Expand Up @@ -70,4 +71,21 @@ public static java.sql.Date formerDate(String dateType) {
currentDate = currentDate.minus(period);
return new java.sql.Date(currentDate.atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli());
}

/**
* 获取传入日期的 00:00:00 时间
* @param date 日期
* @return java.util.Date
*/
public static java.util.Date startOfDay(java.util.Date date) {
if (date == null) {
throw new FundHelperException("入参 date 为空,默认获取当天的时间");
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
}
4 changes: 2 additions & 2 deletions src/main/resources/mapper/WebstieNoticeMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.walkers.planes.fundhelper.dao.WebsiteNoticeDao">
<insert id="insert" parameterType="io.walkers.planes.fundhelper.entity.model.WebsiteNoticeModel" useGeneratedKeys="true" keyProperty="id">
insert into website_notice (virtual_user_id, title, content, read_status, create_date, update_date)
values (#{websiteNotice.virtualUserId}, #{websiteNotice.title}, #{websiteNotice.content}, #{websiteNotice.readStatus}, now(), now())
insert into website_notice (virtual_user_id, title, content, read_status, create_date, update_date, active_date)
values (#{websiteNotice.virtualUserId}, #{websiteNotice.title}, #{websiteNotice.content}, #{websiteNotice.readStatus}, now(), now(), #{websiteNotice.activeDate})
</insert>
</mapper>
Loading

0 comments on commit 53362d9

Please sign in to comment.