upd: 编码规范

This commit is contained in:
2025-03-27 21:09:51 +08:00
parent 16a9d7a92e
commit 1772076ae6
26 changed files with 102 additions and 165 deletions
@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2025 Tian Jun * Copyright 2021-2025 Odboy
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2025 Tian Jun * Copyright 2021-2025 Odboy
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2025 Tian Jun * Copyright 2021-2025 Odboy
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2025 Tian Jun * Copyright 2021-2025 Odboy
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,21 +1,3 @@
/*
* <<
* Davinci
* ==
* Copyright (C) 2016 - 2019 EDP
* ==
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* >>
*
*/
package cn.odboy.mybatis.constant; package cn.odboy.mybatis.constant;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2025 Tian Jun * Copyright 2021-2025 Odboy
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2025 Tian Jun * Copyright 2021-2025 Odboy
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2025 Tian Jun * Copyright 2021-2025 Odboy
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2025 Tian Jun * Copyright 2021-2025 Odboy
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -50,7 +50,7 @@ public class EasyServiceImpl<M extends EasyMapper<T>, T> extends ServiceImpl<M,
* @param resources / * @param resources /
*/ */
public <G> int quickSave(G resources) { public <G> int quickSave(G resources) {
return this.getBaseMapper().insert(BeanUtil.copyProperties(resources, this.getEntityClass())); return getBaseMapper().insert(BeanUtil.copyProperties(resources, this.getEntityClass()));
} }
/** /**
@@ -59,7 +59,7 @@ public class EasyServiceImpl<M extends EasyMapper<T>, T> extends ServiceImpl<M,
* @param resources / * @param resources /
*/ */
public <G> int quickSaveBatch(List<G> resources) { public <G> int quickSaveBatch(List<G> resources) {
return this.getBaseMapper().insertBatchSomeColumn(BeanUtil.copyToList(resources, this.getEntityClass())); return getBaseMapper().insertBatchSomeColumn(BeanUtil.copyToList(resources, this.getEntityClass()));
} }
/** /**
@@ -68,7 +68,7 @@ public class EasyServiceImpl<M extends EasyMapper<T>, T> extends ServiceImpl<M,
* @param resources / * @param resources /
*/ */
public <G> int quickModifyById(G resources) { public <G> int quickModifyById(G resources) {
return this.getBaseMapper().updateById(BeanUtil.copyProperties(resources, this.getEntityClass())); return getBaseMapper().updateById(BeanUtil.copyProperties(resources, this.getEntityClass()));
} }
/** /**
@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2025 Tian Jun * Copyright 2021-2025 Odboy
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2025 Tian Jun * Copyright 2021-2025 Odboy
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2025 Tian Jun * Copyright 2021-2025 Odboy
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2025 Tian Jun * Copyright 2021-2025 Odboy
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2025 Tian Jun * Copyright 2021-2025 Odboy
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2025 Tian Jun * Copyright 2021-2025 Odboy
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2025 Tian Jun * Copyright 2021-2025 Odboy
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,18 +1,3 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.util; package cn.odboy.util;
import cn.odboy.mybatis.constant.DataTypeEnum; import cn.odboy.mybatis.constant.DataTypeEnum;
@@ -1,5 +1,5 @@
/* /*
* Copyright 2021-2025 Tian Jun * Copyright 2021-2025 Odboy
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -1,33 +0,0 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.websocket;
import lombok.Data;
/**
* @author ZhangHouYing
* @date 2019-08-10 9:55
*/
@Data
public class WebSocketMsg {
private String msg;
private WebSocketMsgTypeEnum msgType;
public WebSocketMsg(String msg, WebSocketMsgTypeEnum msgType) {
this.msg = msg;
this.msgType = msgType;
}
}
@@ -1,39 +0,0 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.websocket;
/**
* @author ZhangHouYing
* @date 2019-08-10 9:56
*/
public enum WebSocketMsgTypeEnum {
/**
* 连接
*/
CONNECT,
/**
* 关闭
*/
CLOSE,
/**
* 信息
*/
INFO,
/**
* 错误
*/
ERROR
}
@@ -1,4 +1,4 @@
package cn.odboy.websocket; package cn.odboy.websocket.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@@ -1,4 +1,4 @@
package cn.odboy.websocket; package cn.odboy.websocket.config;
import io.undertow.server.DefaultByteBufferPool; import io.undertow.server.DefaultByteBufferPool;
import io.undertow.websockets.jsr.WebSocketDeploymentInfo; import io.undertow.websockets.jsr.WebSocketDeploymentInfo;
@@ -0,0 +1,21 @@
package cn.odboy.websocket.constant;
public enum WebSocketMsgTypeEnum {
/**
* 连接
*/
CONNECT,
/**
* 关闭
*/
CLOSE,
/**
* 信息
*/
INFO,
/**
* 错误
*/
ERROR
}
@@ -0,0 +1,29 @@
package cn.odboy.websocket.context;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class WebSocketClientManager {
/**
* concurrent包的线程安全Map,用来存放每个客户端对应的MyWebSocket对象。(分布式必出问题)
*/
private static final Map<String, WebSocketServer> CLIENT = new ConcurrentHashMap<>();
public static void addClient(String sid, WebSocketServer webSocketServer) {
// 如果存在就先删除一个,防止重复推送消息
CLIENT.remove(sid);
CLIENT.put(sid, webSocketServer);
}
public static void removeClient(String sid) {
CLIENT.remove(sid);
}
public static Collection<WebSocketServer> getAllClient() {
return CLIENT.values();
}
public static WebSocketServer getClientBySid(String sid) {
return CLIENT.get(sid);
}
}
@@ -1,22 +1,9 @@
/* package cn.odboy.websocket.context;
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.odboy.websocket;
import cn.odboy.websocket.dto.WebSocketMsgDto;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.websocket.*; import javax.websocket.*;
@@ -27,18 +14,11 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/**
* @author ZhangHouYing
* @date 2019-08-10 15:46
*/
@ServerEndpoint("/webSocket/{sid}")
@Slf4j @Slf4j
@Component @Component
@ServerEndpoint("/webSocket/{sid}")
public class WebSocketServer { public class WebSocketServer {
/**
* concurrent包的线程安全Map,用来存放每个客户端对应的MyWebSocket对象。(分布式必出问题)
*/
private static final Map<String, WebSocketServer> CLIENT = new ConcurrentHashMap<>();
/** /**
* 与某个客户端的连接会话,需要通过它来给客户端发送数据 * 与某个客户端的连接会话,需要通过它来给客户端发送数据
*/ */
@@ -54,10 +34,8 @@ public class WebSocketServer {
@OnOpen @OnOpen
public void onOpen(Session session, @PathParam("sid") String sid) { public void onOpen(Session session, @PathParam("sid") String sid) {
this.session = session; this.session = session;
// 如果存在就先删除一个,防止重复推送消息
CLIENT.remove(sid);
CLIENT.put(sid, this);
this.sid = sid; this.sid = sid;
WebSocketClientManager.addClient(sid, this);
} }
/** /**
@@ -65,7 +43,7 @@ public class WebSocketServer {
*/ */
@OnClose @OnClose
public void onClose() { public void onClose() {
CLIENT.remove(this.sid); WebSocketClientManager.removeClient(this.sid);
} }
/** /**
@@ -75,7 +53,7 @@ public class WebSocketServer {
*/ */
@OnMessage @OnMessage
public void onMessage(String message, Session session) { public void onMessage(String message, Session session) {
log.info("收到来{}的信息:{}", sid, message); log.info("收到来 sid={} 的信息: {}", sid, message);
sendToAll(message); sendToAll(message);
} }
@@ -85,40 +63,39 @@ public class WebSocketServer {
* @param message / * @param message /
*/ */
private static void sendToAll(String message) { private static void sendToAll(String message) {
// for (WebSocketServer item : WebSocketClientManager.getAllClient()) {
for (WebSocketServer item : CLIENT.values()) {
try { try {
item.sendMessage(message); item.innerSendMessage(message);
} catch (IOException e) { } catch (IOException e) {
log.error("发送消息给{}失败", item.sid, e); log.error("发送消息给 sid={} 失败", item.sid, e);
} }
} }
} }
@OnError @OnError
public void onError(Session session, Throwable error) { public void onError(Session session, Throwable error) {
log.error("发生错误"); log.error("WebSocket sid={} 发生错误", this.sid, error);
error.printStackTrace(); error.printStackTrace();
} }
/** /**
* 实现服务器主动推送 * 实现服务器主动推送
*/ */
private void sendMessage(String message) throws IOException { private void innerSendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message); this.session.getBasicRemote().sendText(message);
} }
/** /**
* 群发自定义消息 * 群发自定义消息
*/ */
public static void sendInfo(WebSocketMsg webSocketMsg, @PathParam("sid") String sid) throws IOException { public static void sendMessage(WebSocketMsgDto webSocketMsgDto, @PathParam("sid") String sid) throws IOException {
String message = JSON.toJSONString(webSocketMsg); String message = JSON.toJSONString(webSocketMsgDto);
log.info("推送消息到{},推送内容:{}", sid, message); log.info("推送消息到{},推送内容:{}", sid, message);
try { try {
if (sid == null) { if (sid == null) {
sendToAll(message); sendToAll(message);
} else { } else {
CLIENT.get(sid).sendMessage(message); WebSocketClientManager.getClientBySid(sid).innerSendMessage(message);
} }
} catch (IOException ignored) { } catch (IOException ignored) {
} }
@@ -0,0 +1,15 @@
package cn.odboy.websocket.dto;
import cn.odboy.websocket.constant.WebSocketMsgTypeEnum;
import lombok.Data;
@Data
public class WebSocketMsgDto {
private String msg;
private WebSocketMsgTypeEnum msgType;
public WebSocketMsgDto(String msg, WebSocketMsgTypeEnum msgType) {
this.msg = msg;
this.msgType = msgType;
}
}