likes
comments
collection
share

使用delay-job开源项目快速实现延迟调度业务

作者站长头像
站长
· 阅读数 21

1 背景

  • 订单创建一段时间后未支付,如何及时的关闭订单?
  • 用户注册一段时间后未完善资料,如何及时提醒?

delay-job就是专为此场景开发的轻量级分布式延迟任务调度系统,目前已在github开源。项目地址:github.com/findthinks/…

2 使用

2.1 服务端部署

个人测试服务器已安装Java8+、Mysql5.7+环境,测试过程使用root账号。

2.1.1 下载delay-job
# wget https://github.com/findthinks/delay-job/releases/download/0.6.1/delay-job-bin-0.6.1.zip
# unzip delay-job-bin-0.6.1.zip

使用delay-job开源项目快速实现延迟调度业务

2.1.2 建库表

提取解压文件下docs/db/schema_init.sql,执行建库建表。

mysql> source /root/delay-job/docs/db/schema_init.sql

使用delay-job开源项目快速实现延迟调度业务

2.1.3 修改配置

修改数据库配置信息,本文使用与默认配置一致。

# vi /root/delay-job/config/application.yaml

使用delay-job开源项目快速实现延迟调度业务

2.1.4 启动服务
# cd /root/delay-job/bin
# ./startup.sh

使用delay-job开源项目快速实现延迟调度业务 观察log/delay-job.log日志信息,确认服务正常启动,成功监听1989(http)、1990(grpc)端口。

2.2 客户端对接

delay-job触发通知支持http、grpc、kafka,本次使用http协议通知。测试使用springboot开发了一个简易的http接口,用于接受任务触发通知。为方便,客户端、服务端部署在同一台机器。

2.2.1 启动客户端

使用delay-job开源项目快速实现延迟调度业务 客户端接收延迟通知的http接口为:http://127.0.0.1:9000/recv/notify

2.2.2 注册延迟任务

手工注册一个测试任务,触发时间点为1678206771,任务触发信息通知到接口http://127.0.0.1:9000/recv/notify

curl -X 'POST' 'http://localhost:1989/api/v1/submit/job' \
  -H 'Content-Type:application/json' \
  -d'{
      "outJobNo":"job_no_000000000004",
      "triggerTime":1678206771,
      "callbackProtocol":"HTTP",
      "callbackEndpoint":"http://127.0.0.1:9000/recv/notify",
      "jobInfo":"First delay job."
    }'

使用delay-job开源项目快速实现延迟调度业务

2.2.3 触发通知

客户端任务job_no_000000000004在1678206771时间点准时收到回调通知。 使用delay-job开源项目快速实现延迟调度业务

3 总结

本文展示了如何借助delay-job开源项目,快速实现延迟调度业务,实现过程非常简单。

转载自:https://juejin.cn/post/7208472817461379133
评论
请登录