| | |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.UUID; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | @Component |
| | |
| | | */ |
| | | public void applyInTaskAsync(String barcode, Integer sourceStaNo, Integer locType1,Double weight) { |
| | | String requestKey = RedisKeyType.ASYNC_WMS_IN_TASK_REQUEST.key + barcode + "_" + sourceStaNo; |
| | | String requestVersionKey = RedisKeyType.ASYNC_WMS_IN_TASK_REQUEST_VERSION.key + barcode + "_" + sourceStaNo; |
| | | String responseKey = RedisKeyType.ASYNC_WMS_IN_TASK_RESPONSE.key + barcode + "_" + sourceStaNo; |
| | | |
| | | // 检查是否已有请求在进行中 |
| | |
| | | return; // 已有请求在进行中,跳过 |
| | | } |
| | | |
| | | // 标记请求进行中,设置60秒超时 |
| | | redisUtil.set(requestKey, "processing", 60); |
| | | String requestId = UUID.randomUUID().toString(); |
| | | // 标记请求进行中,记录当前最新请求版本,旧请求晚到时不允许覆盖新结果 |
| | | redisUtil.set(requestKey, requestId, 120); |
| | | redisUtil.set(requestVersionKey, requestId, 600); |
| | | redisUtil.del(responseKey); |
| | | |
| | | // 提交异步任务 |
| | | new Thread(() -> { |
| | | try { |
| | | String response = applyInTask(barcode, sourceStaNo, locType1,weight); |
| | | Object latestRequestVersion = redisUtil.get(requestVersionKey); |
| | | if (latestRequestVersion == null || !requestId.equals(latestRequestVersion.toString())) { |
| | | News.info("忽略过期异步WMS入库响应,barcode={},stationId={},requestId={}", barcode, sourceStaNo, requestId); |
| | | return; |
| | | } |
| | | if (response != null) { |
| | | // 存储响应结果,设置60秒超时 |
| | | redisUtil.set(responseKey, response, 60); |
| | |
| | | } |
| | | } catch (Exception e) { |
| | | News.error("异步WMS入库请求异常,barcode={},stationId={},error={},weight={}", barcode, sourceStaNo, e.getMessage(),weight); |
| | | redisUtil.set(responseKey, "ERROR:" + e.getMessage(), 10); |
| | | Object latestRequestVersion = redisUtil.get(requestVersionKey); |
| | | if (latestRequestVersion != null && requestId.equals(latestRequestVersion.toString())) { |
| | | redisUtil.set(responseKey, "ERROR:" + e.getMessage(), 10); |
| | | } |
| | | } finally { |
| | | // 清除请求进行中标记 |
| | | redisUtil.del(requestKey); |
| | | Object currentRequest = redisUtil.get(requestKey); |
| | | if (currentRequest != null && requestId.equals(currentRequest.toString())) { |
| | | redisUtil.del(requestKey); |
| | | } |
| | | } |
| | | }).start(); |
| | | } |
| | |
| | | return redisUtil.get(requestKey) != null; |
| | | } |
| | | |
| | | public void clearAsyncInTaskCache(String barcode, Integer stationId) { |
| | | String cacheSuffix = barcode + "_" + stationId; |
| | | redisUtil.del( |
| | | RedisKeyType.ASYNC_WMS_IN_TASK_REQUEST.key + cacheSuffix, |
| | | RedisKeyType.ASYNC_WMS_IN_TASK_REQUEST_VERSION.key + cacheSuffix, |
| | | RedisKeyType.ASYNC_WMS_IN_TASK_RESPONSE.key + cacheSuffix |
| | | ); |
| | | } |
| | | |
| | | // 申请任务重新分配库位 |
| | | public synchronized String applyReassignTaskLocNo(Integer taskNo, Integer stationId) { |
| | | String wmsUrl = null; |