From d227bd1bc4824c2d49dffa662584625b9912f5ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Wed, 17 May 2023 10:29:31 +0800 Subject: [PATCH 1/2] 'commit' --- pom.xml | 10 +- ...nder.java => HexStringSenderBySocket.java} | 47 +++++++- src/main/java/UnitTest/SocketUtilTest.java | 40 ------- src/main/java/UnitTest/TestOpenGauss.java | 41 +++++++ .../dsideal/FengHuang/Util/SocketUtil.java | 112 ------------------ 5 files changed, 90 insertions(+), 160 deletions(-) rename src/main/java/UnitTest/{HexStringSender.java => HexStringSenderBySocket.java} (59%) delete mode 100644 src/main/java/UnitTest/SocketUtilTest.java create mode 100644 src/main/java/UnitTest/TestOpenGauss.java delete mode 100644 src/main/java/com/dsideal/FengHuang/Util/SocketUtil.java diff --git a/pom.xml b/pom.xml index 05ed064..a4c964f 100644 --- a/pom.xml +++ b/pom.xml @@ -137,10 +137,11 @@ + org.postgresql postgresql - 42.2.27 + 42.6.0 @@ -285,11 +286,10 @@ kafka-clients 3.4.0 - - io.netty - netty-all - 4.1.92.Final + org.apache.mina + mina-core + 2.1.3 diff --git a/src/main/java/UnitTest/HexStringSender.java b/src/main/java/UnitTest/HexStringSenderBySocket.java similarity index 59% rename from src/main/java/UnitTest/HexStringSender.java rename to src/main/java/UnitTest/HexStringSenderBySocket.java index 28c97fa..bfde929 100644 --- a/src/main/java/UnitTest/HexStringSender.java +++ b/src/main/java/UnitTest/HexStringSenderBySocket.java @@ -1,10 +1,11 @@ package UnitTest; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; -public class HexStringSender { +public class HexStringSenderBySocket { public static String IP = "10.10.21.18"; public static int PORT = 8001; //# 开 @@ -12,7 +13,7 @@ public class HexStringSender { //关 public static String close_hex_str = "16 00 34 F5 41 11 FE 82 0D 02 6D FF 00 00 00 00 00 00 01 00 00 00"; - public static void main(String[] args) throws IOException, InterruptedException { + public static void KG() throws IOException, InterruptedException { for (int i = 1; ; i++) { Socket socket = new Socket(IP, PORT); String hex_str; @@ -21,12 +22,52 @@ public class HexStringSender { byte[] bytes = hexStringToByteArray(hex_str); OutputStream os = socket.getOutputStream(); os.write(bytes); - socket.close(); + InputStream in = socket.getInputStream(); + byte[] buffer = new byte[1024]; + int len = in.read(buffer); + + byte[] resp = new byte[len]; + System.arraycopy(buffer, 0, resp, 0, len); + + System.out.println("response: " + bytesToHexString(resp)); + + socket.close(); Thread.sleep(2000); } } + public static void getSbList() throws IOException { + String hex_str="08 00 34 F5 41 11 FE 81"; + Socket socket = new Socket(IP, PORT); + byte[] bytes = hexStringToByteArray(hex_str); + OutputStream os = socket.getOutputStream(); + os.write(bytes); + + InputStream in = socket.getInputStream(); + byte[] buffer = new byte[1024]; + int len = in.read(buffer); + + byte[] resp = new byte[len]; + System.arraycopy(buffer, 0, resp, 0, len); + + System.out.println("response: " + bytesToHexString(resp)); + + socket.close(); + } + public static void main(String[] args) throws IOException, InterruptedException { + //KG(); + getSbList(); + } + public static String bytesToHexString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + for (byte b : bytes) { + sb.append(String.format("%02X", b)); + sb.append(" "); + } + return sb.toString(); + } + /** * 16进制表示的字符串转换为字节数组 * diff --git a/src/main/java/UnitTest/SocketUtilTest.java b/src/main/java/UnitTest/SocketUtilTest.java deleted file mode 100644 index a2eaafd..0000000 --- a/src/main/java/UnitTest/SocketUtilTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package UnitTest; - -import com.dsideal.FengHuang.Util.SocketUtil; - -public class SocketUtilTest { - - public static void Open(String host, int port, String sb_id) throws Exception { - //开 - String hexData = "16 00 34 F5 41 11 FE 82 0D 02 ? 00 00 00 00 00 00 01 00 00 01".replace("?", sb_id).replace(" ", ""); - SocketUtil sender = new SocketUtil(host, port); - sender.sendHexData(hexData); - } - - public static void Close(String host, int port, String sb_id) throws Exception { - //关 - String hexData = "16 00 34 F5 41 11 FE 82 0D 02 ? 00 00 00 00 00 00 01 00 00 00".replace("?", sb_id).replace(" ", ""); - SocketUtil sender = new SocketUtil(host, port); - sender.sendHexData(hexData); - } - - public static void getList(String host, int port) throws Exception { - //String gateWay = "1141f534"; - String hexData = "08 00 34 f5 41 11 FE 81"; - hexData=hexData.replace(" ",""); - SocketUtil sender = new SocketUtil(host, port); - sender.sendHexData(hexData); - } - - public static void main(String[] args) throws Exception { - String HOST = "10.10.21.18"; - int PORT = 8001; - String sb_id = "6D FF"; - for (int i = 1; ; i++) { - if (i % 2 == 1) Open(HOST, PORT, sb_id); - else Close(HOST, PORT, sb_id); - Thread.sleep(1000); - } -// getList(HOST,PORT); - } -} diff --git a/src/main/java/UnitTest/TestOpenGauss.java b/src/main/java/UnitTest/TestOpenGauss.java new file mode 100644 index 0000000..50519ca --- /dev/null +++ b/src/main/java/UnitTest/TestOpenGauss.java @@ -0,0 +1,41 @@ +package UnitTest; + +import com.jfinal.plugin.activerecord.ActiveRecordPlugin; +import com.jfinal.plugin.activerecord.Db; +import com.jfinal.plugin.activerecord.Record; +import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect; +import com.jfinal.plugin.druid.DruidPlugin; + +import java.util.ArrayList; +import java.util.List; + +public class TestOpenGauss { + + public static void main(String[] args) { + //读取库 + DruidPlugin druid = new DruidPlugin("jdbc:postgresql://10.10.14.62:5432/dsideal_db", "test", + "Test@123", "org.postgresql.Driver"); + druid.start(); + + ActiveRecordPlugin arp = new ActiveRecordPlugin(druid); + arp.setDialect(new PostgreSqlDialect()); + arp.start(); + + String sql = "truncate table t1"; + Db.update(sql); + + List aList = new ArrayList<>(); + for (int i = 1; i <= 20; i++) { + Record record = new Record(); + record.set("id", i); + record.set("name", "黄海" + i); + aList.add(record); + } + Db.batchSave("t1", aList, 100); + + sql = "select * from t1"; + List list = Db.find(sql); + System.out.println(list); + } +} + diff --git a/src/main/java/com/dsideal/FengHuang/Util/SocketUtil.java b/src/main/java/com/dsideal/FengHuang/Util/SocketUtil.java deleted file mode 100644 index 966cbcb..0000000 --- a/src/main/java/com/dsideal/FengHuang/Util/SocketUtil.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.dsideal.FengHuang.Util; - -import io.netty.bootstrap.Bootstrap; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; -import io.netty.channel.*; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.channel.socket.SocketChannel; -import io.netty.channel.socket.nio.NioSocketChannel; -import io.netty.handler.codec.MessageToByteEncoder; - -import java.nio.charset.StandardCharsets; - -public class SocketUtil { - private final String host; - private final int port; - - public SocketUtil(String host, int port) { - this.host = host; - this.port = port; - } - - public void sendHexData(String hexData) throws Exception { - EventLoopGroup workerGroup = new NioEventLoopGroup(); - - try { - Bootstrap b = new Bootstrap(); - b.group(workerGroup); - b.channel(NioSocketChannel.class); - b.option(ChannelOption.SO_KEEPALIVE, true); - b.handler(new ChannelInitializer() { - @Override - public void initChannel(SocketChannel ch) { - ChannelPipeline p = ch.pipeline(); - p.addLast(new HexDataEncoder()); - p.addLast(new HexDataSenderHandler(hexData)); - p.addLast(new ClientHandler()); // 添加客户端 Handler - } - }); - - // Start the client. - ChannelFuture f = b.connect(host, port).sync(); - // Wait until the connection is closed. - //f.channel().closeFuture().sync(); - //手动关闭 - f.channel().close(); - } finally { - workerGroup.shutdownGracefully(); - } - } - - class HexDataSenderHandler extends ChannelInboundHandlerAdapter { - private final String hexData; - - public HexDataSenderHandler(String hexData) { - this.hexData = hexData; - } - - @Override - public void channelActive(ChannelHandlerContext ctx) { - byte[] bytes = hexStringToByteArray(hexData); - ByteBuf buffer = Unpooled.buffer(bytes.length); - buffer.writeBytes(bytes); - ctx.writeAndFlush(buffer); - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { - cause.printStackTrace(); - ctx.close(); - } - - private byte[] hexStringToByteArray(String hexData) { - int len = hexData.length(); - byte[] data = new byte[len / 2]; - for (int i = 0; i < len; i += 2) { - data[i / 2] = (byte) ((Character.digit(hexData.charAt(i), 16) << 4) + - Character.digit(hexData.charAt(i + 1), 16)); - } - return data; - } - - - } - - static class ClientHandler extends ChannelInboundHandlerAdapter { - - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - // 处理服务器响应数据 - byte[] bytes = (byte[]) msg; - String response = new String(bytes, StandardCharsets.US_ASCII); - System.out.println("Server response: " + response); - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { - cause.printStackTrace(); - ctx.close(); - } - } - - static class HexDataEncoder extends MessageToByteEncoder { - @Override - protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { - byte[] bytes = new byte[msg.readableBytes()]; - msg.getBytes(msg.readerIndex(), bytes); - out.writeBytes(bytes); - } - } - -} From fe0ee394bf7a6f71a1e7a71c7f7ff89096f79f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Wed, 17 May 2023 10:30:11 +0800 Subject: [PATCH 2/2] 'commit' --- src/main/java/UnitTest/TestOpenGauss.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/UnitTest/TestOpenGauss.java b/src/main/java/UnitTest/TestOpenGauss.java index 50519ca..7d5b4e5 100644 --- a/src/main/java/UnitTest/TestOpenGauss.java +++ b/src/main/java/UnitTest/TestOpenGauss.java @@ -10,7 +10,6 @@ import java.util.ArrayList; import java.util.List; public class TestOpenGauss { - public static void main(String[] args) { //读取库 DruidPlugin druid = new DruidPlugin("jdbc:postgresql://10.10.14.62:5432/dsideal_db", "test", @@ -25,7 +24,7 @@ public class TestOpenGauss { Db.update(sql); List aList = new ArrayList<>(); - for (int i = 1; i <= 20; i++) { + for (int i = 1; i <= 1000; i++) { Record record = new Record(); record.set("id", i); record.set("name", "黄海" + i);