news 2026/4/23 19:22:42

Xxl-Job实现订单30分钟未支付自动取消

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Xxl-Job实现订单30分钟未支付自动取消

数据库

配置文件

xxl:job:admin:addresses:http://localhost:8080/xxl-job-admin/accessToken:default_tokentimeout:30cookie:XXL_JOB_LOGIN_IDENTITY=executor:appname:springboot3address:ip:port:9989logPath:logretentiondays:10

配置xxlJob任务参数

importlombok.Data;importlombok.ToString;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.PropertySource;importorg.springframework.stereotype.Component;@Component@PropertySource("classpath:xxl-job-info.properties")@ConfigurationProperties(prefix="xxl-job-info")@Data@ToStringpublicclassXxlJobInfoVO{privateStringjobGroup;privateStringjobDesc;privateStringauthor;privateStringscheduleType;privateStringglueType;privateStringexecutorHandler;privateStringexecutorRouteStrategy;privateStringmisfireStrategy;privateStringexecutorBlockStrategy;privateStringexecutorTimeout;privateStringexecutorFailRetryCount;privateStringglueRemark;privateStringtriggerStatus;}

xxl-job-info.properties

# \u5B9A\u65F6\u5668\u7EC4#这里需要根据自己的执行器改xxl-job-info.jobGroup=5# \u63CF\u8FF0xxl-job-info.jobDesc=\u667A\u6167\u533B\u517B\u5EB7\u8BA2\u5355\u5B9A\u65F6\u53D6\u6D88\u652F\u4ED8# \u521B\u5EFA\u8005xxl-job-info.author=stx# \u5B9A\u65F6\u5668\u7C7B\u578Bxxl-job-info.scheduleType=CRON# glue\u7C7B\u578Bxxl-job-info.glueType=BEAN# \u6267\u884C\u5668\u4EFB\u52A1handlerxxl-job-info.executorHandler=cancelPay# \u6267\u884C\u5668\u8DEF\u7531\u7B56\u7565xxl-job-info.executorRouteStrategy=FIRST# \u8C03\u5EA6\u8FC7\u671F\u7B56\u7565xxl-job-info.misfireStrategy=DO_NOTHING# \u963B\u585E\u5904\u7406\u7B56\u7565xxl-job-info.executorBlockStrategy=SERIAL_EXECUTION# \u4EFB\u52A1\u6267\u884C\u8D85\u65F6\u65F6\u95F4\uFF0C\u5355\u4F4D\u79D2xxl-job-info.executorTimeout=0# \u5931\u8D25\u91CD\u8BD5\u6B21\u6570xxl-job-info.executorFailRetryCount=0# GLUE\u5907\u6CE8xxl-job-info.glueRemark=GLUE\u4EE3\u7801\u521D\u59CB\u5316# \u8C03\u5EA6\u72B6\u6001\uFF1A0-\u505C\u6B62\uFF0C1-\u8FD0\u884Cxxl-job-info.triggerStatus=1

CancelPayUtil

@Component//@RestControllerpublicclassCancelPayUtil{@Value("${xxl.job.cookie}")privateStringcookie;@Value("${xxl.job.admin.addresses}")privateStringxxlJobAdminAddr;@AutowiredprivateXxlJobInfoVOxxlJobInfoVO;//@GetMapping("/removeCancelJob")publicBooleanremoveCancelJob(StringjobId){RestTemplaterestTemplate=newRestTemplate();HttpHeadersheaders=newHttpHeaders();//设置为form方式headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);headers.add("Cookie",cookie);MultiValueMap<String,String>map=newLinkedMultiValueMap<String,String>();map.add("id",jobId);HttpEntity<MultiValueMap<String,String>>requestb=newHttpEntity<MultiValueMap<String,String>>(map,headers);ResponseEntity<String>response=restTemplate.postForEntity(xxlJobAdminAddr+"/jobinfo/remove",requestb,String.class);StringregionString=response.getBody();//获取请求体JSONObjectjsonObject=JSONObject.parseObject(regionString,JSONObject.class);//将请求体转化为json格式Stringcode=jsonObject.getString("code");if(!("200".equals(code))){thrownewIllegalArgumentException("xxl-job定时任务删除失败");}returntrue;}//@GetMapping("/uploadCancelJob")publicStringuploadCancelJob(Stringcron,StringorderId){RestTemplaterestTemplate=newRestTemplate();HttpHeadersheaders=newHttpHeaders();//设置为form方式headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);headers.add("Cookie",cookie);MultiValueMap<String,String>map=newLinkedMultiValueMap<String,String>();map.add("jobGroup",xxlJobInfoVO.getJobGroup());map.add("jobDesc",xxlJobInfoVO.getJobDesc());map.add("author",xxlJobInfoVO.getAuthor());map.add("alarmEmail","");map.add("scheduleType",xxlJobInfoVO.getScheduleType());map.add("scheduleConf",cron);map.add("cronGen_display",cron);map.add("schedule_conf_CRON",cron);map.add("schedule_conf_FIX_RATE","");map.add("schedule_conf_FIX_DELAY","");map.add("glueType",xxlJobInfoVO.getGlueType());map.add("executorHandler",xxlJobInfoVO.getExecutorHandler());map.add("executorParam",orderId);map.add("executorRouteStrategy",xxlJobInfoVO.getExecutorRouteStrategy());map.add("childJobId","");map.add("misfireStrategy",xxlJobInfoVO.getMisfireStrategy());map.add("executorBlockStrategy",xxlJobInfoVO.getExecutorBlockStrategy());map.add("executorTimeout",xxlJobInfoVO.getExecutorTimeout());map.add("executorFailRetryCount",xxlJobInfoVO.getExecutorFailRetryCount());map.add("glueRemark",xxlJobInfoVO.getGlueRemark());map.add("glueSource","");map.add("triggerStatus",xxlJobInfoVO.getTriggerStatus());HttpEntity<MultiValueMap<String,String>>requestb=newHttpEntity<MultiValueMap<String,String>>(map,headers);ResponseEntity<String>response=restTemplate.postForEntity(xxlJobAdminAddr+"/jobinfo/add",requestb,String.class);StringregionString=response.getBody();//获取请求体JSONObjectjsonObject=JSONObject.parseObject(regionString,JSONObject.class);//将请求体转化为json格式Stringcode=jsonObject.getString("code");StringjobId=jsonObject.getString("content");if(!("200".equals(code))){thrownewIllegalArgumentException("xxl-job定时任务创建失败");}returnjobId;}}

DateUtils

publicclassDateUtils{publicstaticStringgetLocalDateTimeToCron(longminutes){LocalDateTimelocalDateTime=LocalDateTime.now();LocalDateTimeplusMinLocalDateTime=localDateTime.plusMinutes(minutes);StringcornExpress=plusMinLocalDateTime.format(DateTimeFormatter.ofPattern("ss mm HH dd MM ? yyyy"));returncornExpress;}publicstaticStringgetDateToCron(Longtime){// 示例/* Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.MINUTE, 30); SimpleDateFormat sdf = new SimpleDateFormat("ss mm HH dd MM ? yyyy"); String cronExpression = sdf.format(calendar.getTime()); System.out.println("Corn表达式:" + cronExpression); */SimpleDateFormatsdf=newSimpleDateFormat("ss mm HH dd MM ? yyyy");StringcronExpression=sdf.format(time);returncronExpression;}}

订单取消逻辑

@Slf4j@ComponentpublicclassOrderTimeOutJob{@ResourceprivateIOrderInfoServiceorderInfoService;@XxlJob("cancelPay")publicvoidjob(){log.info(">>>>>>>>进入xxl-job定时任务执行方法");StringorderNumber=XxlJobHelper.getJobParam();orderInfoService.update(Wrappers.<OrderInfo>lambdaUpdate().eq(OrderInfo::getOrderNumber,orderNumber).set(OrderInfo::getPayStatus,5));}}

定时任务创建逻辑

@RestController@RequestMapping("/orderInfo")@Slf4jpublicclassOrderInfoController{@ResourceprivateIOrderInfoServiceorderInfoService;@ResourceprivateCancelPayUtilcancelPayUtil;@ResourceprivateIOrderJobServiceorderJobService;@PostMapping("/saveOrderInfo")@Transactional(rollbackFor=Exception.class)publicBooleansaveOrderInfo(StringorderNumber){OrderInfoorderInfo=newOrderInfo();orderInfo.setOrderNumber(orderNumber);booleansaveResultBoolean=orderInfoService.save(orderInfo);StringlocalDateTimeToCron=DateUtils.getLocalDateTimeToCron(2);StringjobId=cancelPayUtil.uploadCancelJob(localDateTimeToCron,orderNumber);BooleanjobIsExist=jobId.isEmpty()||jobId.equals("")?false:true;if(jobIsExist){OrderJoborderJob=newOrderJob();orderJob.setOrderNumber(orderNumber);orderJob.setJobId(jobId);orderJobService.save(orderJob);}log.info(String.format("订单号: %s,订单取消时间: %s",orderNumber,localDateTimeToCron));returnsaveResultBoolean&&jobIsExist;}}

定时任务取消逻辑

@RestController@RequestMapping("/payInfo")@Slf4jpublicclassPayController{@ResourceprivateCancelPayUtilcancelPayUtil;@ResourceprivateIOrderJobServiceorderJobService;@RequestMapping("/payInfo")publicBooleanpayInfo(StringorderNumber){//业务逻辑...//支付成功,取消定时任务LambdaQueryWrapper<OrderJob>jobLambdaQueryWrapper=newLambdaQueryWrapper<OrderJob>().eq(OrderJob::getOrderNumber,orderNumber);OrderJoborderJob=orderJobService.getOne(jobLambdaQueryWrapper);returncancelPayUtil.removeCancelJob(orderJob.getJobId());}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 6:30:07

Napi::ArrayBuffer

ArrayBufferNapi::ArrayBuffer类继承自Napi::Object类。Napi::ArrayBuffer类对应于 JavaScript 中的ArrayBuffer类。方法New分配一个具有指定长度的新Napi::ArrayBuffer实例。static Napi::ArrayBuffer Napi::ArrayBuffer::New(napi_env env, size_t byteLength);[in] env&…

作者头像 李华
网站建设 2026/4/23 6:24:46

AsyncContext

AsyncContextNapi::AsyncWorker类可能并非适用于所有场景。当使用其他任何异步机制时&#xff0c;引入新的Napi::AsyncContext类是必要的&#xff0c;以确保运行时能正确跟踪异步操作。Napi::AsyncContext类可传递给Napi::Function::MakeCallback()方法&#xff0c;以正确恢复正…

作者头像 李华
网站建设 2026/4/23 6:25:30

近视防控:“防“在前,“控“在后

近年来&#xff0c;儿童青少年近视率居高不下&#xff0c;已成为影响国民健康的重要公共卫生问题。“每天户外活动2小时”“减少连续近距离用眼时间”&#xff0c;这些主流的防控建议虽有充分的理论支撑&#xff0c;却在沉重的学业压力下屡屡碰壁。事实上&#xff0c;科学的近视…

作者头像 李华
网站建设 2026/4/22 8:41:47

LIS2DW12三轴加速度传感器原理图设计,已量产(加速度传感器)

目录 1、核心传感电路:LIS2DW12 的引脚逻辑 2、电源部分:低噪 + 低功耗的双重保障 3、接口细节:I2C 上拉与扩展预留 4、容易忽略的细节 最近在做一款低功耗便携监测设备,选传感模块时一眼盯上了 ST 的 LIS2DW12—— 这颗三轴加速度计的参数简直是为便携场景量身定做:1…

作者头像 李华
网站建设 2026/4/23 7:48:39

Yandex竞价实战指南:5个让ROI翻倍的深度优化技巧

在俄罗斯及东欧市场&#xff0c;Yandex Direct作为核心广告投放平台&#xff0c;其竞价系统优化直接决定企业获客成本与转化效率。据2025年数字营销效果调研报告显示&#xff0c;采用系统化优化策略的广告主平均CTR提升47%&#xff0c;CPA降低32%。本文将拆解从基础设置到高阶策…

作者头像 李华