add:增加统一外部调用服务
This commit is contained in:
@@ -32,13 +32,16 @@ public class TableDataInfo<T> implements Serializable {
|
||||
/**
|
||||
* 消息状态码
|
||||
*/
|
||||
private int code;
|
||||
private String code;
|
||||
|
||||
/**
|
||||
* 消息内容
|
||||
*/
|
||||
private String msg;
|
||||
|
||||
/**
|
||||
* 反馈数据
|
||||
*/
|
||||
private Object data;
|
||||
/**
|
||||
* 分页
|
||||
*
|
||||
@@ -52,7 +55,7 @@ public class TableDataInfo<T> implements Serializable {
|
||||
|
||||
public static <T> TableDataInfo<T> build(IPage<T> page) {
|
||||
TableDataInfo<T> rspData = new TableDataInfo<>();
|
||||
rspData.setCode(HttpStatus.HTTP_OK);
|
||||
rspData.setCode(String.valueOf(HttpStatus.HTTP_OK));
|
||||
rspData.setMsg("查询成功");
|
||||
rspData.setContent(page.getRecords());
|
||||
rspData.setTotalElements(page.getTotal());
|
||||
@@ -61,7 +64,7 @@ public class TableDataInfo<T> implements Serializable {
|
||||
|
||||
public static <T> TableDataInfo<T> build(List<T> list) {
|
||||
TableDataInfo<T> rspData = new TableDataInfo<>();
|
||||
rspData.setCode(HttpStatus.HTTP_OK);
|
||||
rspData.setCode(String.valueOf(HttpStatus.HTTP_OK));
|
||||
rspData.setMsg("查询成功");
|
||||
rspData.setContent(list);
|
||||
rspData.setTotalElements(list.size());
|
||||
@@ -70,9 +73,17 @@ public class TableDataInfo<T> implements Serializable {
|
||||
|
||||
public static <T> TableDataInfo<T> build() {
|
||||
TableDataInfo<T> rspData = new TableDataInfo<>();
|
||||
rspData.setCode(HttpStatus.HTTP_OK);
|
||||
rspData.setCode(String.valueOf(HttpStatus.HTTP_OK));
|
||||
rspData.setMsg("查询成功");
|
||||
return rspData;
|
||||
}
|
||||
|
||||
public static <T> TableDataInfo<T> buildJson(Object result) {
|
||||
TableDataInfo<T> rspData = new TableDataInfo<>();
|
||||
rspData.setCode(String.valueOf(HttpStatus.HTTP_OK));
|
||||
rspData.setData(result);
|
||||
rspData.setMsg("操作成功");
|
||||
return rspData;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.nl.common.domain.interfaces;
|
||||
|
||||
public interface CallBack {
|
||||
/**
|
||||
* 回调执行方法
|
||||
*/
|
||||
void executor();
|
||||
|
||||
/**
|
||||
* 本回调任务名称
|
||||
* @return /
|
||||
*/
|
||||
default String getCallBackName() {
|
||||
return Thread.currentThread().getId() + ":" + this.getClass().getName();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.nl.common.domain.interfaces;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2022/12/14 8:40 下午
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface LConsumer<X,Y,Z> {
|
||||
|
||||
void accept(X x,Y y,Z z);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.nl.common.domain.interfaces;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@FunctionalInterface
|
||||
public
|
||||
interface LockProcess {
|
||||
void process() throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.nl.common.domain.interfaces;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@FunctionalInterface
|
||||
public
|
||||
interface ReturnLockProcess<T> {
|
||||
T process() throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.nl.common.utils;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.common.domain.interfaces.LockProcess;
|
||||
import org.nl.common.domain.interfaces.ReturnLockProcess;
|
||||
import org.nl.common.exception.BadRequestException;
|
||||
import org.nl.config.SpringContextHolder;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2023/3/27 10:30
|
||||
*/
|
||||
@Slf4j
|
||||
public class RedissonUtils {
|
||||
/**
|
||||
*
|
||||
* @param process 业务代码
|
||||
* @param key
|
||||
* @param seconds 尝试获取锁的等待时间,允许为空
|
||||
*/
|
||||
@SneakyThrows
|
||||
public static void lock(LockProcess process, String key, Integer seconds){
|
||||
RedissonClient redissonClient = SpringContextHolder.getBean(RedissonClient.class);
|
||||
RLock lock = redissonClient.getLock(key);
|
||||
boolean isLock;
|
||||
if (seconds == null){
|
||||
isLock = lock.tryLock();
|
||||
}else {
|
||||
isLock = lock.tryLock(seconds, TimeUnit.SECONDS);
|
||||
}
|
||||
try {
|
||||
if (isLock){
|
||||
process.process();
|
||||
} else {
|
||||
throw new BadRequestException("当前业务:"+key+"正在执行请稍后再试");
|
||||
}
|
||||
}catch (Exception ex){
|
||||
log.error("key:"+ex.getMessage());
|
||||
throw ex;
|
||||
}finally {
|
||||
if (lock.isHeldByCurrentThread() && lock.isLocked()){
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public static<T> T lockAndReturn(ReturnLockProcess<T> process, String key, Integer seconds){
|
||||
RedissonClient redissonClient = SpringContextHolder.getBean(RedissonClient.class);
|
||||
RLock lock = redissonClient.getLock(key);
|
||||
boolean isLock;
|
||||
if (seconds == null){
|
||||
isLock = lock.tryLock();
|
||||
}else {
|
||||
isLock = lock.tryLock(seconds, TimeUnit.SECONDS);
|
||||
}
|
||||
try {
|
||||
if (isLock){
|
||||
T result = process.process();
|
||||
return result;
|
||||
} else {
|
||||
throw new BadRequestException("当前业务 key:"+key+"正在执行请稍后再试");
|
||||
}
|
||||
}finally {
|
||||
if (isLock){
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user