Commit ffe12602 authored by shiyubin's avatar shiyubin 🌼

crc高位补零,ByteBuf异常处理

parent 979cc658
# 下发控制6x6大屏 # 下发控制6x6大屏
#### 启动:
​ NettyApplication.java 主程序入口,运行即启动
```
//NettyApplication.java 文件路径: /src/main/java/com/gemho/netty/NettyApplication.java
public static void main(String[] args) {
SpringApplication.run(NettyApplication.class, args);
new nettyServer(9999); // 设备连接端口,可以根据自身情况进行修改
}
```
​ application.yml文件中
```
// application.yml 文件路径: /src/main/resource/application.yml
server:
port: 9998 // http请求端口,可以根据自身情况进行修改
```
#### 服务端: #### 服务端:
​ 监听端口: 9999 ​ 监听端口: 9999
...@@ -18,13 +46,17 @@ ...@@ -18,13 +46,17 @@
| 参数名称 | 参数值 | | 参数名称 | 参数值 |
| -------- | -------------------------------------------- | | -------- | -------------------------------------------- |
| key | 313233343536373839383736353433(hex string) | | key | 864333042029678(string) |
| code | 03 (string) | | code | 03 (string) |
| x | 54(hex string) | | x | 54(string) |
| y | 5 (hex string) | | y | 5 (string) |
| font | 30 (string) | | font | 30 (string) |
| content | 温度 (string) | | content | 温度 (string) |
完整接口格式:
http://localhost:9998/display?key=864333042029678&code=01&x=12&y=15&font=31&content=温 度:33.5℃
##### 清屏指令 ##### 清屏指令
...@@ -32,7 +64,8 @@ ...@@ -32,7 +64,8 @@
##### url: http://192.168.1.49:9998/clear ##### url: http://192.168.1.49:9998/clear
| 参数名称 | 参数值 | | 参数名称 | 参数值 |
| -------- | -------------------------------------------- | | -------- | ------------------------- |
| key | 313233343536373839383736353433(hex string) | | key | 864333042029678(string) |
| code | 03(string) | | code | 03(string) |
完整接口格式:http://localhost:9998/clear?key=864333042029678&code=01
...@@ -4,16 +4,6 @@ ...@@ -4,16 +4,6 @@
<facet type="Spring" name="Spring"> <facet type="Spring" name="Spring">
<configuration /> <configuration />
</facet> </facet>
<facet type="jpa" name="JPA">
<configuration>
<setting name="validation-enabled" value="true" />
<setting name="provider-name" value="Hibernate" />
<datasource-mapping>
<factory-entry name="entityManagerFactory" />
</datasource-mapping>
<naming-strategy-map />
</configuration>
</facet>
<facet type="web" name="Web"> <facet type="web" name="Web">
<configuration> <configuration>
<webroots /> <webroots />
...@@ -33,7 +23,7 @@ ...@@ -33,7 +23,7 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" /> <excludeFolder url="file://$MODULE_DIR$/target" />
</content> </content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.3.3.RELEASE" level="project" /> <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.3.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.3.3.RELEASE" level="project" /> <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.3.3.RELEASE" level="project" />
......
...@@ -3,8 +3,10 @@ package com.gemho.netty; ...@@ -3,8 +3,10 @@ package com.gemho.netty;
import com.gemho.netty.controller.nettyServer; import com.gemho.netty.controller.nettyServer;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
@SpringBootApplication @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class NettyApplication { public class NettyApplication {
public static void main(String[] args) { public static void main(String[] args) {
......
package com.gemho.netty.controller; package com.gemho.netty.controller;
import cn.hutool.json.JSONUtil; import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import com.gemho.netty.model.ResultModel;
import com.gemho.netty.util.CRC16Util; import com.gemho.netty.util.CRC16Util;
import com.gemho.netty.util.MapUtils; import com.gemho.netty.util.MapUtils;
import com.gemho.netty.util.ParamUtil; import com.gemho.netty.util.ParamUtil;
...@@ -8,58 +10,75 @@ import com.gemho.netty.util.StringUtil; ...@@ -8,58 +10,75 @@ import com.gemho.netty.util.StringUtil;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.LinkedHashMap; import java.net.URLEncoder;
import java.util.Map;
@RestController @RestController
public class SocketController { public class SocketController {
/**
private static String CLEAR_CODE = "0000000400000650"; *
* @param key 313233343536373839383736353433 16进制DTU码
private static String DISPLAY_CODE = "0001"; * @param code 01
* @return String
// key:313233343536373839383736353433 16进制DTU码 */
// code: 03 @GetMapping("/clear")
@RequestMapping("/clear")
public String clearDiaplay(String key, String code) { public String clearDiaplay(String key, String code) {
String CLEAR_CODE = "0000000400006050";
code = code + CLEAR_CODE; code = code + CLEAR_CODE;
int i = CRC16Util.calcCrc16(StringUtil.hexString2Bytes(code)); int i = CRC16Util.calcCrc16(StringUtil.hexString2Bytes(code));
System.out.println("=============" + StringUtil.encodeHEX(i)); code = (code + StringUtil.hightAddZero(StringUtil.encodeHEX(i))).toUpperCase();
System.out.println("=============" + StringUtil.hexString2Bytes((code + StringUtil.encodeHEX(i)).toUpperCase()));
code = (code + StringUtil.encodeHEX(i)).toUpperCase();
// 内容封装 // 内容封装
ByteBuf bufff = Unpooled.buffer(); ByteBuf bufff = Unpooled.buffer();
// bufff.clear(); // bufff.clear();
System.out.println("============send:" + code);
bufff.writeBytes(StringUtil.hexString2Bytes(code)); bufff.writeBytes(StringUtil.hexString2Bytes(code));
// 获取通道 // 获取通道
Channel mapChannel = MapUtils.getMapChannel(key); Channel mapChannel = MapUtils.getMapChannel(key);
bufff.retain();
mapChannel.writeAndFlush(bufff); mapChannel.writeAndFlush(bufff);
// bufff.retain();
// 返回失败指令时使用
if (!ParamUtil.findMapContainerKey("clear" + mapChannel.remoteAddress().toString())) {
ResultModel model = new ResultModel();
model.setCount(1);
model.setHexCodeByte(StringUtil.hexString2Bytes(code));
ParamUtil.addMap("clear" + mapChannel.remoteAddress().toString(), model);
}
return "success"; return "success";
} }
// 01 00 01 00 0E 36 05 30 CE C2 B6 C8 B7 38
// key:313233343536373839383736353433 16进制DTU码 /**
// code: 03 *
// x: 54 16进制 * @param key 313233343536373839383736353433 16进制DTU码
// y: 5 16禁止 * @param code 01
// font: 30字体大小 * @param x 54 16进制
// content: 温度 * @param y 5 16禁止
@RequestMapping("/display") * @param font 31字体大小
public String display(String key, String code, Integer x, Integer y, String font, String content) { * @param content 温度
* @return String
*/
@GetMapping("/display")
public String display(String key, String code, Integer x, Integer y, String font,String content) {
try{ try{
String DISPLAY_CODE = "0001";
// 指令处理 // 指令处理
String xHex = Integer.toHexString(x).length() == 1? "0"+ Integer.toHexString(x) : Integer.toHexString(x); String xHex = Integer.toHexString(x).length() == 1? "0"+ Integer.toHexString(x) : Integer.toHexString(x);
String yHex = Integer.toHexString(y).length() == 1? "0"+ Integer.toHexString(y) : Integer.toHexString(y); String yHex = Integer.toHexString(y).length() == 1? "0"+ Integer.toHexString(y) : Integer.toHexString(y);
String contentByte = StringUtil.enUnicode(content); String contentByte = StringUtil.enUnicode(content);
byte[] contentB = StringUtil.hexString2Bytes(contentByte); byte[] contentB = StringUtil.hexString2Bytes(contentByte);
String lenHex = StringUtil.encodeHEX(contentB.length + 10); String lenHex = StringUtil.encodeHEX(contentB.length + 3);
lenHex = StringUtil.hightAddZero(lenHex); lenHex = StringUtil.hightAddZero(lenHex);
code = code + DISPLAY_CODE + lenHex + xHex + yHex + font + StringUtil.bytesToHexString(contentB); code = code + DISPLAY_CODE + lenHex + xHex + yHex + font + StringUtil.bytesToHexString(contentB);
code = code + StringUtil.encodeHEX(CRC16Util.calcCrc16(StringUtil.hexString2Bytes(code))); code = code + StringUtil.hightAddZero(StringUtil.encodeHEX(CRC16Util.calcCrc16(StringUtil.hexString2Bytes(code))));
System.out.println("下发指令==============" + code.toUpperCase()); System.out.println("下发指令==============" + code.toUpperCase());
// 数据封装 // 数据封装
ByteBuf bufff = Unpooled.buffer(); ByteBuf bufff = Unpooled.buffer();
...@@ -67,11 +86,14 @@ public class SocketController { ...@@ -67,11 +86,14 @@ public class SocketController {
// 获取通道 // 获取通道
Channel mapChannel = MapUtils.getMapChannel(key); Channel mapChannel = MapUtils.getMapChannel(key);
mapChannel.writeAndFlush(bufff); mapChannel.writeAndFlush(bufff);
// 执行失败使用
if (ParamUtil.findMap(mapChannel.remoteAddress()) == null) { // bufff.retain();
Map<Integer, ByteBuf> map = new LinkedHashMap<>(); // 返回失败指令时使用
map.put(1, bufff.retain()); if (!ParamUtil.findMapContainerKey("display" + mapChannel.remoteAddress().toString())) {
ParamUtil.addMap(mapChannel.remoteAddress(), map); ResultModel model = new ResultModel();
model.setCount(1);
model.setHexCodeByte(StringUtil.hexString2Bytes(code));
ParamUtil.addMap("display" + mapChannel.remoteAddress().toString(), model);
} }
return "success"; return "success";
}catch(Exception e) { }catch(Exception e) {
...@@ -79,5 +101,13 @@ public class SocketController { ...@@ -79,5 +101,13 @@ public class SocketController {
return null; return null;
} }
} }
@GetMapping("/test")
public void test(String key) throws Exception{
System.out.println(key + "=======================");
String body = HttpUtil.createGet("http://localhost:9998/display?key=864333042029678&code=01&x=0&y=12&font=31&content=温 度:33.5℃").execute().body();
// Thread.sleep(500);
String body1 = HttpUtil.createGet("http://localhost:9998/display?key=864333042029678&code=01&x=0&y=24&font=31&content=湿 度:33.5%RH").execute().body();
// Thread.sleep(500);
String body2 = HttpUtil.createGet("http://localhost:9998/display?key=864333042029678&code=01&x=0&y=36&font=31&content=CO2:450ppm").execute().body();
}
} }
package com.gemho.netty.controller; package com.gemho.netty.controller;
import cn.hutool.json.JSONUtil; import com.gemho.netty.model.ResultModel;
import com.gemho.netty.util.*; import com.gemho.netty.util.*;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.Map;
import java.util.Set;
public class nettyHandler extends ChannelInboundHandlerAdapter { public class nettyHandler extends ChannelInboundHandlerAdapter {
@Override @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception { public void channelActive(ChannelHandlerContext ctx) {
System.out.println("当前连接信息:" + ctx.channel().remoteAddress()); System.out.println("当前连接信息:" + ctx.channel().remoteAddress());
} }
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
/*System.out.println(msg.toString() + "========================"); System.out.println("返回指令===========" + msg.toString());
if (msg.toString().length() == 8) {
System.out.println("=====================1");
msg = StringUtil.bytesToHexString(msg.toString().getBytes());
}else {
System.out.println("=====================2");
msg = msg.toString();
}*/
System.out.println("=================" + StringUtil.bytesToHexString(StringUtil.hexString2Bytes(StringUtil.splitString(msg.toString()))));
if (msg.toString().length() == 30) { if (msg.toString().length() == 30) {
MapUtils.addMap(msg.toString(), ctx.channel()); String IMEICode = new String(StringUtil.hexString2Bytes(msg.toString()));
System.out.println("连接的IMEI码:" + IMEICode);
if (MapUtils.judgeChannel(IMEICode)) {
// 有重复的IMEI连接
MapUtils.replaceChannel(IMEICode, ctx.channel());
} else {
// 无重复连接
MapUtils.addMap(IMEICode, ctx.channel());
}
}else { }else {
byte[] bys = StringUtil.hexString2Bytes(msg.toString()); byte[] bys = StringUtil.hexString2Bytes(msg.toString());
int i = CRC16Util.calcCrc16(bys, 0, 6); int i = CRC16Util.calcCrc16(bys, 0, 6);
...@@ -39,34 +38,20 @@ public class nettyHandler extends ChannelInboundHandlerAdapter { ...@@ -39,34 +38,20 @@ public class nettyHandler extends ChannelInboundHandlerAdapter {
dest,:byte目的数组(截取后存放的数组) dest,:byte目的数组(截取后存放的数组)
destPos:截取后存放的数组起始位置(0位置有效) destPos:截取后存放的数组起始位置(0位置有效)
length:截取的数据长度*/ length:截取的数据长度*/
byte[] newBys = new byte[2]; byte[] newBys = new byte[1];
System.arraycopy(bys, 3, newBys, 0, 2); System.arraycopy(bys, 5, newBys, 0, 1);
if ("0001".equals(StringUtil.bytesToHexString(newBys))) { System.out.println("==============result:" + StringUtil.bytesToHexString(newBys));
System.out.println("正常. 大屏接收到数据"); if ("00".equals(StringUtil.bytesToHexString(newBys))) {
System.out.println("正常:大屏接收到数据");
} else { } else {
// 执行失败 byte[] typeByte = new byte[2];
Map<Integer, ByteBuf> map = ParamUtil.findMap(ctx.channel().remoteAddress()); System.arraycopy(bys, 1, typeByte, 0, 2);
if (map != null) { System.out.println("0000:清屏异常==0001:屏幕显示异常============返回异常信息指令:" + StringUtil.bytesToHexString(typeByte));
Set<Integer> integers = map.keySet(); if ("0001".equals(StringUtil.bytesToHexString(typeByte))) {
for (Integer key : integers) { this.processResult("display" + ctx.channel().remoteAddress().toString(), ctx);
for (int j = 0; j < 4 - key; j++) { } else {
if (key == 3) { this.processResult("clear" + ctx.channel().remoteAddress().toString(), ctx);
ParamUtil.removeMap(ctx.channel().remoteAddress());
System.out.println(ctx.channel().remoteAddress() + "错误. 大屏未接收到数据");
return ;
}else {
ByteBuf byteBuf = map.get(key);
System.out.println("发送数据==============" + JSONUtil.toJsonStr(byteBuf));
ctx.channel().write(byteBuf.retain());
map.put(key+1, map.get(key).retain());
map.remove(ctx.channel().remoteAddress());
ParamUtil.updateMap(ctx.channel().remoteAddress(), map);
System.out.println("repeat......." + key);
}
}
}
} }
// System.out.println(ctx.channel().remoteAddress() + "错误. 大屏未接收到数据");
} }
}else { }else {
System.out.println("CRC校验失败!"); System.out.println("CRC校验失败!");
...@@ -77,15 +62,45 @@ public class nettyHandler extends ChannelInboundHandlerAdapter { ...@@ -77,15 +62,45 @@ public class nettyHandler extends ChannelInboundHandlerAdapter {
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("异常信息:" + cause.getMessage()); System.out.println("异常信息:" + cause.getMessage());
ctx.channel().closeFuture().sync();
} }
// 心跳处理 /**
*
* @param ctx 连接通道
* @param evt 事件
*/
@Override @Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
} }
/**
*
* @param Key clear-address
* @param ctx 通道
* @throws Exception 异常
*/
private void processResult(String Key, ChannelHandlerContext ctx) throws Exception{
ResultModel map = ParamUtil.findMap(Key);
if (map != null) {
int count = map.getCount();
for (int j = 0; j < 4 - map.getCount(); j++) {
if (count == 3) {
System.out.println(Key + "错误:大屏未接收到数据, 连接关闭!");
ParamUtil.removeMap(Key);
ctx.channel().close().sync();
return;
} else {
byte[] hexCodeByte = map.getHexCodeByte();
ByteBuf byteBuf = Unpooled.buffer();
byteBuf.writeBytes(hexCodeByte);
ctx.writeAndFlush(byteBuf);
count += 1;
System.out.println("repeat send......." + count);
}
}
}
}
} }
...@@ -18,7 +18,7 @@ public class nettyServer { ...@@ -18,7 +18,7 @@ public class nettyServer {
public void start(int port) { private void start(int port) {
System.out.println("启动成功, 当前监听: " + port); System.out.println("启动成功, 当前监听: " + port);
ServerBootstrap b = new ServerBootstrap(); ServerBootstrap b = new ServerBootstrap();
NioEventLoopGroup group = new NioEventLoopGroup(); NioEventLoopGroup group = new NioEventLoopGroup();
...@@ -26,8 +26,7 @@ public class nettyServer { ...@@ -26,8 +26,7 @@ public class nettyServer {
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() { .childHandler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) public void initChannel(SocketChannel ch) {
throws Exception {
System.out.println("initChannel ch:" + ch); System.out.println("initChannel ch:" + ch);
ch.pipeline() ch.pipeline()
.addLast("decoder", new DecodeUtil()) .addLast("decoder", new DecodeUtil())
......
package com.gemho.netty.model;
public class ResultModel {
private int count;
private byte[] hexCodeByte;
public ResultModel() {
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public byte[] getHexCodeByte() {
return hexCodeByte;
}
public void setHexCodeByte(byte[] hexCodeByte) {
this.hexCodeByte = hexCodeByte;
}
}
...@@ -7,18 +7,9 @@ import io.netty.handler.codec.ByteToMessageDecoder; ...@@ -7,18 +7,9 @@ import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.List; import java.util.List;
public class DecodeUtil extends ByteToMessageDecoder { public class DecodeUtil extends ByteToMessageDecoder {
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception { protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
/*//创建字节数组,buffer.readableBytes可读字节长度
byte[] b = new byte[buffer.readableBytes()];
//复制内容到字节数组b
buffer.readBytes(b);
//字节数组转字符串
String str = new String(b);
System.out.println("======decode:" + str);
out.add(bytesToHexString(b));*/
String HEXES = "0123456789ABCDEF"; String HEXES = "0123456789ABCDEF";
byte[] req = new byte[buffer.readableBytes()]; byte[] req = new byte[buffer.readableBytes()];
buffer.readBytes(req); buffer.readBytes(req);
...@@ -32,32 +23,4 @@ public class DecodeUtil extends ByteToMessageDecoder { ...@@ -32,32 +23,4 @@ public class DecodeUtil extends ByteToMessageDecoder {
out.add(hex.toString()); out.add(hex.toString());
} }
public String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
public static String toHexString1(byte[] b) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; ++i) {
buffer.append(toHexString1(b[i]));
}
return buffer.toString();
}
public static String toHexString1(byte b) {
String s = Integer.toHexString(b & 0xFF);
if (s.length() == 1) {
return "0" + s;
} else {
return s;
}
}
} }
...@@ -13,18 +13,29 @@ public class MapUtils { ...@@ -13,18 +13,29 @@ public class MapUtils {
private static Map<String, Channel> map = new LinkedHashMap<>(); private static Map<String, Channel> map = new LinkedHashMap<>();
/**
* 添加建立的连接
* @param key 标识
* @param channel 通道
*/
public static void addMap(String key, Channel channel) { public static void addMap(String key, Channel channel) {
lock.lock(); lock.lock();
map.put(key, channel); map.put(key, channel);
lock.unlock(); lock.unlock();
} }
public static void removeMap(String key){
/*public static void removeMap(String key){
lock.lock(); lock.lock();
map.remove(key); map.remove(key);
lock.unlock(); lock.unlock();
} }*/
/**
* 根据IMEI码,查询管道
* @param key 标识
* @return Channel
*/
public static Channel getMapChannel(String key) { public static Channel getMapChannel(String key) {
try{ try{
lock.lock(); lock.lock();
...@@ -34,4 +45,29 @@ public class MapUtils { ...@@ -34,4 +45,29 @@ public class MapUtils {
} }
} }
/**
* 查询是否重复连接
* @param key 标识
* @return channel
*/
public static boolean judgeChannel(String key) {
try{
lock.lock();
return map.containsKey(key);
}finally {
lock.unlock();
}
}
/**
* 替换掉当前IMEI码的通道
* @param key 标识
* @param channel 通道
*/
public static void replaceChannel(String key , Channel channel) {
lock.lock();
map.replace(key, channel);
lock.unlock();
}
} }
package com.gemho.netty.util; package com.gemho.netty.util;
import io.netty.buffer.ByteBuf; import com.gemho.netty.model.ResultModel;
import java.net.SocketAddress;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
...@@ -11,27 +9,27 @@ public class ParamUtil { ...@@ -11,27 +9,27 @@ public class ParamUtil {
private static ReentrantLock lock = new ReentrantLock(); private static ReentrantLock lock = new ReentrantLock();
private static Map<SocketAddress, Map<Integer, ByteBuf>> PARAM_MAP = new LinkedHashMap<>(); private static Map<String, ResultModel> PARAM_MAP = new LinkedHashMap<>();
public static void addMap(SocketAddress key, Map<Integer, ByteBuf> map) { public static void addMap(String key, ResultModel model) {
lock.lock(); lock.lock();
PARAM_MAP.put(key, map); PARAM_MAP.put(key, model);
lock.unlock(); lock.unlock();
} }
public static void updateMap(SocketAddress key, Map<Integer, ByteBuf> map) { /* public static void updateMap(String key, ResultModel model) {
lock.lock(); lock.lock();
PARAM_MAP.replace(key, map); PARAM_MAP.replace(key, model);
lock.unlock(); lock.unlock();
} }*/
public static void removeMap(SocketAddress key) { public static void removeMap(String key) {
lock.lock(); lock.lock();
PARAM_MAP.remove(key); PARAM_MAP.remove(key);
lock.unlock(); lock.unlock();
} }
public static Map<Integer, ByteBuf> findMap(SocketAddress key) { public static ResultModel findMap(String key) {
lock.lock(); lock.lock();
try{ try{
return PARAM_MAP.get(key); return PARAM_MAP.get(key);
...@@ -43,4 +41,14 @@ public class ParamUtil { ...@@ -43,4 +41,14 @@ public class ParamUtil {
} }
} }
public static boolean findMapContainerKey(String key) {
try{
lock.lock();
return PARAM_MAP.containsKey(key);
}finally {
lock.unlock();
}
}
} }
server: server:
port: 9998 port: 9998
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/postgres
username: postgres
password: red___
server: server:
port: 9998 port: 9998
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/postgres
username: postgres
password: red___
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