1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
@Component public class DelayTaskProcessor { private static final Logger log = LoggerFactory.getLogger(DelayTaskProcessor.class); @Autowired private DelayTaskManagementService delayTaskManagementService; @Autowired private BusinessService businessService;
public void processDelayTask(String taskJson) { try { DelayTask task = JSON.parseObject(taskJson, DelayTask.class); log.info("开始处理延时任务: taskId={}, type={}", task.getTaskId(), task.getTaskType()); if (!"PENDING".equals(task.getStatus())) { log.warn("任务状态不是待执行: taskId={}, status={}", task.getTaskId(), task.getStatus()); return; } updateTaskStatus(task.getTaskId(), "PROCESSING"); boolean success = processTaskByType(task); if (success) { updateTaskStatus(task.getTaskId(), "COMPLETED"); log.info("延时任务处理完成: taskId={}", task.getTaskId()); } else { handleTaskFailure(task); } } catch (Exception e) { log.error("处理延时任务失败: taskJson={}", taskJson, e); } }
private boolean processTaskByType(DelayTask task) { try { switch (task.getTaskType()) { case "ORDER_TIMEOUT": return processOrderTimeoutTask(task); case "PAYMENT_TIMEOUT": return processPaymentTimeoutTask(task); case "NOTIFICATION_DELAY": return processNotificationDelayTask(task); case "DATA_CLEANUP": return processDataCleanupTask(task); case "REPORT_GENERATION": return processReportGenerationTask(task); default: log.warn("未知任务类型: taskId={}, type={}", task.getTaskId(), task.getTaskType()); return false; } } catch (Exception e) { log.error("处理任务失败: taskId={}, type={}", task.getTaskId(), task.getTaskType(), e); return false; } }
private boolean processOrderTimeoutTask(DelayTask task) { try { OrderTimeoutData data = JSON.parseObject(task.getTaskData(), OrderTimeoutData.class); OrderStatus status = businessService.getOrderStatus(data.getOrderId()); if (status == OrderStatus.PENDING) { businessService.cancelOrder(data.getOrderId(), "订单超时自动取消"); log.info("订单超时取消成功: orderId={}", data.getOrderId()); return true; } else { log.info("订单状态已变更,无需处理: orderId={}, status={}", data.getOrderId(), status); return true; } } catch (Exception e) { log.error("处理订单超时任务失败: taskId={}", task.getTaskId(), e); return false; } }
private boolean processPaymentTimeoutTask(DelayTask task) { try { PaymentTimeoutData data = JSON.parseObject(task.getTaskData(), PaymentTimeoutData.class); PaymentStatus status = businessService.getPaymentStatus(data.getPaymentId()); if (status == PaymentStatus.PENDING) { businessService.cancelPayment(data.getPaymentId(), "支付超时自动取消"); log.info("支付超时取消成功: paymentId={}", data.getPaymentId()); return true; } else { log.info("支付状态已变更,无需处理: paymentId={}, status={}", data.getPaymentId(), status); return true; } } catch (Exception e) { log.error("处理支付超时任务失败: taskId={}", task.getTaskId(), e); return false; } }
private boolean processNotificationDelayTask(DelayTask task) { try { NotificationData data = JSON.parseObject(task.getTaskData(), NotificationData.class); NotificationResult result = businessService.sendNotification(data); if (result.isSuccess()) { log.info("延时通知发送成功: taskId={}", task.getTaskId()); return true; } else { log.error("延时通知发送失败: taskId={}, error={}", task.getTaskId(), result.getMessage()); return false; } } catch (Exception e) { log.error("处理延时通知任务失败: taskId={}", task.getTaskId(), e); return false; } }
private boolean processDataCleanupTask(DelayTask task) { try { DataCleanupData data = JSON.parseObject(task.getTaskData(), DataCleanupData.class); CleanupResult result = businessService.cleanupData(data); if (result.isSuccess()) { log.info("数据清理完成: taskId={}, cleanedCount={}", task.getTaskId(), result.getCleanedCount()); return true; } else { log.error("数据清理失败: taskId={}, error={}", task.getTaskId(), result.getMessage()); return false; } } catch (Exception e) { log.error("处理数据清理任务失败: taskId={}", task.getTaskId(), e); return false; } }
private boolean processReportGenerationTask(DelayTask task) { try { ReportData data = JSON.parseObject(task.getTaskData(), ReportData.class); ReportResult result = businessService.generateReport(data); if (result.isSuccess()) { log.info("报表生成完成: taskId={}, reportId={}", task.getTaskId(), result.getReportId()); return true; } else { log.error("报表生成失败: taskId={}, error={}", task.getTaskId(), result.getMessage()); return false; } } catch (Exception e) { log.error("处理报表生成任务失败: taskId={}", task.getTaskId(), e); return false; } }
private void handleTaskFailure(DelayTask task) { try { task.setRetryCount(task.getRetryCount() + 1); if (task.getRetryCount() < 3) { long retryDelay = calculateRetryDelay(task.getRetryCount()); delayTaskManagementService.addDelayTask(task, retryDelay); log.info("任务重试: taskId={}, retryCount={}, retryDelay={}ms", task.getTaskId(), task.getRetryCount(), retryDelay); } else { updateTaskStatus(task.getTaskId(), "FAILED"); log.error("任务重试次数超限: taskId={}", task.getTaskId()); } } catch (Exception e) { log.error("处理任务失败异常: taskId={}", task.getTaskId(), e); } }
private long calculateRetryDelay(int retryCount) { return (long) Math.pow(2, retryCount) * 60000; }
private void updateTaskStatus(String taskId, String status) { try { DelayTask task = delayTaskManagementService.getTaskInfo(taskId); if (task != null) { task.setStatus(status); } } catch (Exception e) { log.error("更新任务状态失败: taskId={}, status={}", taskId, status, e); } } }
|