Commit 8ffb14a7 authored by shiyu's avatar shiyu

上传商品加锁

parent c9e837f8
......@@ -71,7 +71,7 @@ public class Result<T> implements Entity {
}
public static <T>Result<T> failed() {
return new Result<>(ResultCode.FAILED.getCode(), false);
return new Result<>(ResultCode.FAILED.getCode(), false, ResultCode.FAILED.getMessage());
}
public static<T> Result<T> failed(Integer code, String msg) {
......
......@@ -10,6 +10,7 @@ import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.core.util.CacheUtil;
import com.wwdz.ch.core.util.MediaUtil;
import com.wwdz.ch.core.util.PriceUtil;
import com.wwdz.ch.core.util.RedisUtils;
import com.wwdz.ch.db.dao.*;
import com.wwdz.ch.db.dao.distribution.*;
import com.wwdz.ch.db.domain.*;
......@@ -32,7 +33,6 @@ import com.wwdz.shop.api.DTO.ShopDTO;
import com.wwdz.shop.api.query.ShopQueryOption;
import com.wwdz.shop.api.service.ShopReadService;
import com.xxdxxs.utils.*;
import javafx.beans.binding.ObjectExpression;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
......@@ -43,14 +43,12 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.System;
import java.math.BigDecimal;
import java.net.URL;
import java.time.Duration;
......@@ -151,6 +149,10 @@ public class SupplierItemServiceImpl implements SupplierItemService {
@Autowired
IntroduceRecordDao introduceRecordDao;
@Autowired
RedisUtils redisUtils;
@Override
public Result findList(SupplierItemRequestDto dto) {
try {
......@@ -743,7 +745,12 @@ public class SupplierItemServiceImpl implements SupplierItemService {
@Override
public Result create(SupplierItemRequestDto dto) {
String lockKey = "create:item:" + dto.getUserId();
RLock lock = redissonClient.getLock(lockKey);
try {
if (!lock.tryLock(0,1 ,TimeUnit.SECONDS)) {
return Result.failed("操作过快,稍后再试");
}
SupplierItem supplierItem = new SupplierItem();
EntityMapper.copyAttribute(dto, supplierItem);
supplierItem.setDistributionPrice(PriceUtil.convertPriceFromStr(StringUtils.isEmpty(dto.getDistributionPrice())?"0":dto.getDistributionPrice()));
......@@ -781,6 +788,10 @@ public class SupplierItemServiceImpl implements SupplierItemService {
return Result.success(new HashMap(){{put("id", itemId);}});
} catch (Exception e) {
logger.error("上传藏品失败 error : {}", e);
} finally {
if (lock != null && lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
return Result.failed();
}
......
......@@ -16,6 +16,7 @@ import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
......@@ -42,9 +43,16 @@ public class SendMsgFromExcelJob {
@Autowired
RedissonClient redissonClient;
@Value("${spring.profiles.active}")
private String env;
// 每年8月8日晚上8点执行一次
@Scheduled(cron = "0 0 20 8 8 ?")
// @Scheduled(cron = "0 0 20 8 8 ?")
public void executeTask() {
if ("dev".equals(env)) {
logger.info(">>>>>>>>>>>>>>>>>>>>>>> dev环境, 不执行给指定类目用户发送通知任务<<<<<<<<<<<<<<<<<<<<<");
return;
}
RLock lock = redissonClient.getLock(SENDMSGFROMEXCEL_KEY);
if (!lock.tryLock()) {
logger.info("给指定类目用户发送通知任务,当前服务实例获取锁成功: {} 获取锁失败,锁被占用不执行", Thread.currentThread().getId());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment