add:acs申请任务添加控制:相同设备只能申请一次

This commit is contained in:
zhangzq
2024-12-16 17:21:10 +08:00
parent 7cd784ac83
commit 924006f3fa
2 changed files with 76 additions and 1 deletions

View File

@@ -0,0 +1,64 @@
package org.nl.common;
import org.nl.common.exception.BadRequestException;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
public class LockUtils {
private static ConcurrentHashMap<String, ReentrantLock> locks = new ConcurrentHashMap<>();
public static <T> T lock(String key, Supplier<T> supplier,Integer second) {
ReentrantLock lock;
synchronized (key.intern()){
lock = locks.get(key);
if (lock==null){
lock = new ReentrantLock();
locks.put(key, lock) ;
}
}
boolean isLock = false;
if (second !=null){
try {
isLock = lock.tryLock(second, TimeUnit.SECONDS);
}catch (Exception ex){
isLock = false;
}
}else {
isLock = lock.tryLock();
}
try {
if (isLock){
return supplier.get();
}else {
throw new BadRequestException(Thread.currentThread().getName()+"当前业务"+key+"正在执行");
}
}catch (Exception ex){
throw new BadRequestException(Thread.currentThread().getName()+"当前业务"+key+"正在执行");
}finally {
if (lock.isLocked() && lock.isHeldByCurrentThread()){
lock.unlock();
}
}
}
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(()->{
String key = String.valueOf(new Random().nextInt(5));
String result = LockUtils.lock(key, () -> {
try {
System.out.println(Thread.currentThread().getName() + "正在执行");
Thread.sleep(2000);
} catch (Exception ex) {
}
return key;
}, null);
System.out.println("执行结果"+result);
}).start();
}
}
}

View File

@@ -5,8 +5,10 @@ import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.nl.common.LockUtils;
import org.nl.common.logging.annotation.Log;
import org.nl.wms.ext.acs.service.AcsToWmsService;
import org.nl.wms.ext.acs.service.dto.to.BaseResponse;
import org.nl.wms.sch.point.service.ISchBasePointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@@ -16,6 +18,8 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.locks.ReentrantLock;
/**
* @Author: lyd
* @Description: acs请求lms接口
@@ -30,6 +34,8 @@ public class AcsToWmsController {
@Autowired
private ISchBasePointService schBasePointService;
private static ReentrantLock lock = new ReentrantLock();
@Autowired
private AcsToWmsService acsToWmsService;
@@ -40,7 +46,12 @@ public class AcsToWmsController {
//@SaCheckPermission("@el.check('schBaseTask:add')")
@SaIgnore
public ResponseEntity<Object> apply(@RequestBody JSONObject param) {
return new ResponseEntity<>(acsToWmsService.acsApply(param), HttpStatus.OK);
String device_code = param.getString("device_code");
BaseResponse response = LockUtils.lock(
device_code,
() -> acsToWmsService.acsApply(param), 2);
return new ResponseEntity<>(response, HttpStatus.OK);
}
@PostMapping("/status")