本文目录导读:

在Java中实现“催办”功能,通常涉及业务逻辑层(判断催办条件)、调度层(定时任务或异步处理)以及通知层(发送消息),以下是一个完整的实现思路和代码案例,适用于工作流、审批系统等场景。
催办功能的典型场景
- 待办审批超时(如超过24小时未处理)
- 任务到期前提醒(如提前1小时)
- 手动触发催办(用户点击“催办”按钮)
核心实现方案
实体设计(数据库表)
-- 待办任务表
CREATE TABLE task (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
assignee VARCHAR(100) COMMENT '处理人',
status VARCHAR(20) COMMENT '状态: PENDING/COMPLETED',
created_time DATETIME,
deadline_time DATETIME COMMENT '截止时间',
last_remind_time DATETIME COMMENT '上次催办时间'
);
主业务代码(Java)
// 催办服务接口
public interface UrgeService {
/**
* 手动催办指定任务
*/
void urgeTask(Long taskId);
/**
* 自动催办所有超时任务
*/
void autoUrge();
/**
* 发送催办通知
*/
void sendUrgeNotification(Task task);
}
// 实现类
@Service
public class UrgeServiceImpl implements UrgeService {
@Autowired
private TaskMapper taskMapper;
@Autowired
private NotificationService notificationService;
/**
* 手动催办单个任务
*/
@Override
public void urgeTask(Long taskId) {
Task task = taskMapper.selectById(taskId);
if (task == null) {
throw new BusinessException("任务不存在");
}
if ("COMPLETED".equals(task.getStatus())) {
throw new BusinessException("任务已完成,无需催办");
}
// 更新催办时间
task.setLastRemindTime(new Date());
taskMapper.updateById(task);
// 发送催办通知
sendUrgeNotification(task);
}
/**
* 自动催办:扫描所有超时任务
*/
@Override
public void autoUrge() {
// 查询所有超时的待办任务(截止时间<当前时间 且 未完成)
List<Task> overdueTasks = taskMapper.selectOverdueTasks(new Date());
for (Task task : overdueTasks) {
// 防止重复催办(上次催办时间在1小时内不再催办)
if (task.getLastRemindTime() != null &&
task.getLastRemindTime().after(DateUtils.addHours(new Date(), -1))) {
continue;
}
// 发送催办通知
sendUrgeNotification(task);
// 更新催办时间
task.setLastRemindTime(new Date());
taskMapper.updateById(task);
}
}
/**
* 发送催办通知(统一封装)
*/
@Override
public void sendUrgeNotification(Task task) {
// 构建通知内容
String message = String.format("【催办提醒】任务[%s]已超时,请尽快处理!",
task.getId());
// 根据处理人发送通知(邮箱/短信/系统消息等)
notificationService.send(
task.getAssignee(),
NotificationType.URGE,
message
);
}
}
MyBatis Mapper 查询超时任务
@Mapper
public interface TaskMapper {
/**
* 查询所有超时的待办任务
*/
@Select("SELECT * FROM task WHERE status = 'PENDING' AND deadline_time < #{now}")
List<Task> selectOverdueTasks(@Param("now") Date now);
}
定时调度实现(自动催办)
使用 Spring 的 @Scheduled 注解实现定时扫描:
@Component
public class UrgeScheduler {
@Autowired
private UrgeService urgeService;
/**
* 每30分钟执行一次自动催办
*/
@Scheduled(fixedDelay = 30 * 60 * 1000) // 30分钟
// 或使用 cron 表达式:@Scheduled(cron = "0 */30 * * * ?")
public void scheduleAutoUrge() {
try {
log.info("开始自动催办...");
urgeService.autoUrge();
log.info("自动催办结束");
} catch (Exception e) {
log.error("自动催办异常", e);
}
}
}
需要在 Spring Boot 启动类或配置类上添加 @EnableScheduling:
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
手动催办(Controller 接口)
@RestController
@RequestMapping("/api/urge")
public class UrgeController {
@Autowired
private UrgeService urgeService;
/**
* 手动催办任务
*/
@PostMapping("/task/{taskId}")
public Result<Void> urgeTask(@PathVariable Long taskId) {
urgeService.urgeTask(taskId);
return Result.success("催办成功");
}
}
通知服务(简化示例)
@Service
public class NotificationService {
/**
* 发送通知
*/
public void send(String recipient, NotificationType type, String content) {
// 实际可根据类型选择不同渠道
// 1. 邮件发送(如 JavaMailSender)
// 2. 短信发送(如阿里云 SMS)
// 3. 系统站内信(存入通知表)
// 4. 即时通讯(企业微信/钉钉机器人)
switch (type) {
case URGE:
// 优先发送邮件 + 系统站内信
sendInternalMessage(recipient, content);
sendEmail(recipient, content);
break;
default:
break;
}
}
private void sendEmail(String to, String content) {
// 邮件实现代码...
}
private void sendInternalMessage(String userId, String content) {
// 插入消息表...
}
}
扩展与优化建议
| 优化方向 | 实现方式 |
|---|---|
| 防止重复催办 | 记录上次催办时间,设定最小间隔(如1小时) |
| 催办次数限制 | 设定最大催办次数(如3次后不再催办) |
| 升级催办 | 超长时间未处理,通知上级领导 |
| 异步处理 | 使用 MQ 或线程池异步发送通知,避免阻塞定时任务 |
| 催办模板 | 不同业务场景使用不同模板(如审批催办、付款催办) |
完整时序图(自动催办)
时间轴:
Scheduler UrgeService TaskMapper NotificationService
| | | |
|-- @Scheduled ----> | | |
| |-- selectOverdueTasks() ----> |
| | |<-- 超时任务列表 --- |
| | | |
| |---- for each task: | |
| | 发送催办通知 ---------------> |
| | 更新 lastRemindTime |
| | | |
催办功能的核心是:
- 定义超时规则(超时时间、催办间隔、最大催办次数)
- 定时扫描:Spring
@Scheduled定期检查超时任务 - 通知分发:支持多种渠道(邮件、短信、站内信)
- 防重复与限流:记录上次催办时间,避免骚扰
代码可以直接集成到 Spring Boot 项目中,满足大部分 Java 催办需求。