upd: 规范与剥离工具类
This commit is contained in:
parent
f8d6e56fa3
commit
fd1e337334
|
@ -21,23 +21,23 @@ public class SmallMessage implements Serializable {
|
|||
public static class Response implements Serializable {
|
||||
private Boolean success = true;
|
||||
private String errorCode = "0";
|
||||
private String errorMsg = "success";
|
||||
private String errorMessage = "success";
|
||||
private Object data;
|
||||
|
||||
public static Response bad(String errorMsg) {
|
||||
public static Response bad(String errorMessage) {
|
||||
Response response = new Response();
|
||||
response.setSuccess(false);
|
||||
response.setErrorCode("400");
|
||||
response.setErrorMsg(errorMsg);
|
||||
response.setErrorMessage(errorMessage);
|
||||
response.setData(null);
|
||||
return response;
|
||||
}
|
||||
|
||||
public static Response ok(Object data, String errorMsg) {
|
||||
public static Response ok(Object data, String errorMessage) {
|
||||
Response response = new Response();
|
||||
response.setSuccess(true);
|
||||
response.setErrorCode("0");
|
||||
response.setErrorMsg(errorMsg);
|
||||
response.setErrorMessage(errorMessage);
|
||||
response.setData(data);
|
||||
return response;
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ public class SmallMessage implements Serializable {
|
|||
Response response = new Response();
|
||||
response.setSuccess(true);
|
||||
response.setErrorCode("0");
|
||||
response.setErrorMsg("success");
|
||||
response.setErrorMessage("success");
|
||||
response.setData(data);
|
||||
return response;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package cn.odboy.config.util;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelId;
|
||||
/**
|
||||
* 统一操作Channel
|
||||
* @author odboy
|
||||
* @date 2024-12-06
|
||||
*/
|
||||
public class ChannelUtil {
|
||||
public static String getId(ChannelHandlerContext ctx){
|
||||
return ctx.channel().id().asShortText();
|
||||
}
|
||||
|
||||
public static String getId(ChannelId channelId){
|
||||
return channelId.asShortText();
|
||||
}
|
||||
}
|
|
@ -1,9 +1,19 @@
|
|||
package cn.odboy.config.util;
|
||||
|
||||
import cn.odboy.config.constant.TransferMessageType;
|
||||
import cn.odboy.config.model.SmallMessage;
|
||||
import cn.odboy.config.model.msgtype.ConfigFileInfo;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 传输消息序列化
|
||||
*
|
||||
* @author odboy
|
||||
* @date 2024-12-06
|
||||
*/
|
||||
public class MessageUtil {
|
||||
public static ByteBuf toByteBuf(Object data) {
|
||||
return Unpooled.copiedBuffer(ProtostuffUtil.serializer(data));
|
||||
|
@ -15,4 +25,35 @@ public class MessageUtil {
|
|||
buf.readBytes(bytes);
|
||||
return ProtostuffUtil.deserializer(bytes, SmallMessage.class);
|
||||
}
|
||||
|
||||
// ================ 以下为很糙的自定义方法
|
||||
public static ByteBuf toRegisterBad(String errorMessage) {
|
||||
return toByteBuf(
|
||||
new SmallMessage(TransferMessageType.REGISTER, SmallMessage.Response.bad(errorMessage)));
|
||||
}
|
||||
|
||||
public static ByteBuf toRegisterOk(Object data) {
|
||||
return toByteBuf(
|
||||
new SmallMessage(TransferMessageType.REGISTER, SmallMessage.Response.ok(data)));
|
||||
}
|
||||
|
||||
public static ByteBuf toPushConfigBad(String errorMessage) {
|
||||
return toByteBuf(
|
||||
new SmallMessage(TransferMessageType.PUSH_CONFIG, SmallMessage.Response.bad(errorMessage)));
|
||||
}
|
||||
|
||||
public static ByteBuf toPushConfigOk(Object data) {
|
||||
return toByteBuf(
|
||||
new SmallMessage(TransferMessageType.PUSH_CONFIG, SmallMessage.Response.ok(data)));
|
||||
}
|
||||
|
||||
public static List<ConfigFileInfo> toConfigFileInfoList(Object o) {
|
||||
if (o instanceof List) {
|
||||
List<?> list = (List<?>) o;
|
||||
if (!list.isEmpty() && list.get(0) instanceof ConfigFileInfo) {
|
||||
return (List<ConfigFileInfo>) list;
|
||||
}
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package cn.odboy.config.util;
|
||||
|
||||
/**
|
||||
* 配置命名空间 名称
|
||||
*
|
||||
* @author odboy
|
||||
* @date 2024-12-06
|
||||
*/
|
||||
public class PropertyNameUtil {
|
||||
private static final String DEFAULT_PREFIX = "kenaito";
|
||||
|
||||
public static String get(String fileName) {
|
||||
return DEFAULT_PREFIX + "_" + fileName;
|
||||
}
|
||||
}
|
|
@ -14,15 +14,15 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||
*/
|
||||
public class ProtostuffUtil {
|
||||
|
||||
private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>();
|
||||
private static final Map<Class<?>, Schema<?>> CACHED_SCHEMA = new ConcurrentHashMap<Class<?>, Schema<?>>();
|
||||
|
||||
private static <T> Schema<T> getSchema(Class<T> clazz) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Schema<T> schema = (Schema<T>) cachedSchema.get(clazz);
|
||||
Schema<T> schema = (Schema<T>) CACHED_SCHEMA.get(clazz);
|
||||
if (schema == null) {
|
||||
schema = RuntimeSchema.getSchema(clazz);
|
||||
if (schema != null) {
|
||||
cachedSchema.put(clazz, schema);
|
||||
CACHED_SCHEMA.put(clazz, schema);
|
||||
}
|
||||
}
|
||||
return schema;
|
||||
|
@ -30,9 +30,6 @@ public class ProtostuffUtil {
|
|||
|
||||
/**
|
||||
* 序列化
|
||||
*
|
||||
* @param obj
|
||||
* @return
|
||||
*/
|
||||
public static <T> byte[] serializer(T obj) {
|
||||
@SuppressWarnings("unchecked")
|
||||
|
@ -50,10 +47,6 @@ public class ProtostuffUtil {
|
|||
|
||||
/**
|
||||
* 反序列化
|
||||
*
|
||||
* @param data
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
public static <T> T deserializer(byte[] data, Class<T> clazz) {
|
||||
try {
|
||||
|
|
Loading…
Reference in New Issue