upd: 规范与剥离工具类

This commit is contained in:
骑着蜗牛追导弹 2024-12-06 21:10:19 +08:00
parent f8d6e56fa3
commit fd1e337334
5 changed files with 91 additions and 24 deletions

View File

@ -21,23 +21,23 @@ public class SmallMessage implements Serializable {
public static class Response implements Serializable { public static class Response implements Serializable {
private Boolean success = true; private Boolean success = true;
private String errorCode = "0"; private String errorCode = "0";
private String errorMsg = "success"; private String errorMessage = "success";
private Object data; private Object data;
public static Response bad(String errorMsg) { public static Response bad(String errorMessage) {
Response response = new Response(); Response response = new Response();
response.setSuccess(false); response.setSuccess(false);
response.setErrorCode("400"); response.setErrorCode("400");
response.setErrorMsg(errorMsg); response.setErrorMessage(errorMessage);
response.setData(null); response.setData(null);
return response; return response;
} }
public static Response ok(Object data, String errorMsg) { public static Response ok(Object data, String errorMessage) {
Response response = new Response(); Response response = new Response();
response.setSuccess(true); response.setSuccess(true);
response.setErrorCode("0"); response.setErrorCode("0");
response.setErrorMsg(errorMsg); response.setErrorMessage(errorMessage);
response.setData(data); response.setData(data);
return response; return response;
} }
@ -46,7 +46,7 @@ public class SmallMessage implements Serializable {
Response response = new Response(); Response response = new Response();
response.setSuccess(true); response.setSuccess(true);
response.setErrorCode("0"); response.setErrorCode("0");
response.setErrorMsg("success"); response.setErrorMessage("success");
response.setData(data); response.setData(data);
return response; return response;
} }

View File

@ -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();
}
}

View File

@ -1,11 +1,21 @@
package cn.odboy.config.util; package cn.odboy.config.util;
import cn.odboy.config.constant.TransferMessageType;
import cn.odboy.config.model.SmallMessage; import cn.odboy.config.model.SmallMessage;
import cn.odboy.config.model.msgtype.ConfigFileInfo;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import java.util.ArrayList;
import java.util.List;
/**
* 传输消息序列化
*
* @author odboy
* @date 2024-12-06
*/
public class MessageUtil { public class MessageUtil {
public static ByteBuf toByteBuf(Object data){ public static ByteBuf toByteBuf(Object data) {
return Unpooled.copiedBuffer(ProtostuffUtil.serializer(data)); return Unpooled.copiedBuffer(ProtostuffUtil.serializer(data));
} }
@ -15,4 +25,35 @@ public class MessageUtil {
buf.readBytes(bytes); buf.readBytes(bytes);
return ProtostuffUtil.deserializer(bytes, SmallMessage.class); 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<>();
}
} }

View File

@ -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;
}
}

View File

@ -14,15 +14,15 @@ import java.util.concurrent.ConcurrentHashMap;
*/ */
public class ProtostuffUtil { 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) { private static <T> Schema<T> getSchema(Class<T> clazz) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Schema<T> schema = (Schema<T>) cachedSchema.get(clazz); Schema<T> schema = (Schema<T>) CACHED_SCHEMA.get(clazz);
if (schema == null) { if (schema == null) {
schema = RuntimeSchema.getSchema(clazz); schema = RuntimeSchema.getSchema(clazz);
if (schema != null) { if (schema != null) {
cachedSchema.put(clazz, schema); CACHED_SCHEMA.put(clazz, schema);
} }
} }
return schema; return schema;
@ -30,9 +30,6 @@ public class ProtostuffUtil {
/** /**
* 序列化 * 序列化
*
* @param obj
* @return
*/ */
public static <T> byte[] serializer(T obj) { public static <T> byte[] serializer(T obj) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -50,10 +47,6 @@ public class ProtostuffUtil {
/** /**
* 反序列化 * 反序列化
*
* @param data
* @param clazz
* @return
*/ */
public static <T> T deserializer(byte[] data, Class<T> clazz) { public static <T> T deserializer(byte[] data, Class<T> clazz) {
try { try {