diff --git a/Socket/pom.xml b/Socket/pom.xml new file mode 100644 index 0000000..be92b00 --- /dev/null +++ b/Socket/pom.xml @@ -0,0 +1,27 @@ + + + + JavaBasiceDemo + com.wyl.example + 1.0-SNAPSHOT + + 4.0.0 + + Socket + + + org.apache.commons + commons-pool2 + 2.6.2 + compile + + + + + 8 + 8 + + + \ No newline at end of file diff --git a/Socket/src/main/java/com/wyl/socket/client/clienttextnet.java b/Socket/src/main/java/com/wyl/socket/client/clienttextnet.java new file mode 100644 index 0000000..0dc773c --- /dev/null +++ b/Socket/src/main/java/com/wyl/socket/client/clienttextnet.java @@ -0,0 +1,47 @@ +package com.wyl.socket.client; + +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; +import java.text.SimpleDateFormat; + +/** + * 客户端 + * + * @author 刘正 + * + */ +public class clienttextnet { + public static String _pattern = "yyyy-MM-dd HH:mm:ss SSS"; + public static SimpleDateFormat format = new SimpleDateFormat(_pattern); + + public static void main(String[] args) { + try { + while (true) { + // 与服务端建立连接 + Socket socket = new Socket("127.0.0.1", 9999); + // 获得输出流,给服务端发送信息 + OutputStream dout = socket.getOutputStream(); + String str = "数据传输------"; + dout.write(str.getBytes()); + // 通过shutdownOutput高速服务器已经发送完数据,后续只能接受数据 + socket.shutdownOutput(); + + // 接收服务端发送的消息 + InputStream din = socket.getInputStream(); + byte[] outPut = new byte[4096]; + while (din.read(outPut) > 0) { + String result = new String(outPut); + System.out.println("服务端反回的的消息是:" + result); + } + din.close(); + dout.close(); + socket.close(); + Thread.sleep(3000); + } + } catch (Exception e) { + e.printStackTrace(); + } + + } +} \ No newline at end of file diff --git a/Socket/src/main/java/com/wyl/socket/client/pool/ConnectionPoolFactory.java b/Socket/src/main/java/com/wyl/socket/client/pool/ConnectionPoolFactory.java new file mode 100644 index 0000000..f5c8b40 --- /dev/null +++ b/Socket/src/main/java/com/wyl/socket/client/pool/ConnectionPoolFactory.java @@ -0,0 +1,68 @@ +package com.wyl.socket.client.pool; + +import org.apache.commons.pool2.impl.GenericObjectPool; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; + +import java.net.Socket; + +/** + * 连接池工厂 + * + * @author admin + */ +public class ConnectionPoolFactory { + + private GenericObjectPool pool = null; + + private static ConnectionPoolFactory instance = null; + + public static ConnectionPoolFactory getInstance() { + + if (instance == null) { + synchronized (ConnectionPoolFactory.class) { + if (instance == null) { + GenericObjectPoolConfig config = new GenericObjectPoolConfig(); + config.setMaxIdle(500); + config.setMaxWaitMillis(30000); + config.setMinEvictableIdleTimeMillis(1800000); + config.setMinIdle(50); + config.setTestOnBorrow(false); + config.setTestOnCreate(false); + config.setTestOnReturn(false); + config.setTestWhileIdle(true); + config.setTimeBetweenEvictionRunsMillis(10000); + config.setMaxTotal(1500); + config.setNumTestsPerEvictionRun(1); + config.setLifo(true); + String hosts = "127.0.0.1:9999"; + instance = new ConnectionPoolFactory(config, hosts); + } + } + } + + return instance; + } + + private ConnectionPoolFactory(GenericObjectPoolConfig config, String hosts) { + SocketConnectionFactory factory = new SocketConnectionFactory(hosts); + pool = new GenericObjectPool(factory, config); + } + + public Socket getConnection() throws Exception { + return pool.borrowObject(); + } + + public void releaseConnection(Socket socket) { + try { + pool.returnObject(socket); + } catch (Throwable e) { + if (socket != null) { + try { + socket.close(); + } catch (Exception ex) { + e.printStackTrace(); + } + } + } + } +} \ No newline at end of file diff --git a/Socket/src/main/java/com/wyl/socket/client/pool/SocketConnectionFactory.java b/Socket/src/main/java/com/wyl/socket/client/pool/SocketConnectionFactory.java new file mode 100644 index 0000000..e887738 --- /dev/null +++ b/Socket/src/main/java/com/wyl/socket/client/pool/SocketConnectionFactory.java @@ -0,0 +1,102 @@ +package com.wyl.socket.client.pool; + +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.pool2.BasePooledObjectFactory; +import org.apache.commons.pool2.PooledObject; +import org.apache.commons.pool2.impl.DefaultPooledObject; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; + +/** + * Socket连接创建工厂 + * + * @author admin + */ +@Slf4j +public class SocketConnectionFactory extends BasePooledObjectFactory { + + + private List socketAddress = null; + + private final AtomicLong atomicLongCount; + + public SocketConnectionFactory(String hosts) { + + socketAddress = new ArrayList(); + + String[] hostsAdd = hosts.split(";"); + if (hostsAdd.length > 0) { + for (String tmpHost : hostsAdd) { + String[] dataStrings = tmpHost.split(":"); + InetSocketAddress address = new InetSocketAddress(dataStrings[0], Integer.parseInt(dataStrings[1])); + socketAddress.add(address); + } + } + + atomicLongCount = new AtomicLong(); + } + + private InetSocketAddress getSocketAddress() { + int index = (int) (atomicLongCount.getAndIncrement() % socketAddress.size()); + log.info("调用C服务器地址:" + socketAddress.get(index).getHostName()); + return socketAddress.get(index); + } + + @Override + public void destroyObject(PooledObject p) throws Exception { + Socket socket = p.getObject(); + log.info("销毁Socket:" + socket); + if (socket != null) { + socket.close(); + } + } + + @Override + public boolean validateObject(PooledObject p) { + + Socket socket = p.getObject(); + if (socket != null) { + if (!socket.isConnected()) { + return false; + } + if (socket.isClosed()) { + return false; + } + + try { + OutputStream outputStream = socket.getOutputStream(); + outputStream.write(1); + outputStream.flush(); + int read = socket.getInputStream().read(); + return true; + } catch (IOException e) { + e.printStackTrace(); + return false; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + return false; + } + + @Override + public Socket create() throws Exception { + Socket socket = new Socket(); + socket.connect(getSocketAddress()); + return socket; + } + + @Override + public PooledObject wrap(Socket obj) { + return new DefaultPooledObject(obj); + } + +} \ No newline at end of file diff --git a/Socket/src/main/java/com/wyl/socket/service/servernettext.java b/Socket/src/main/java/com/wyl/socket/service/servernettext.java new file mode 100644 index 0000000..f394e96 --- /dev/null +++ b/Socket/src/main/java/com/wyl/socket/service/servernettext.java @@ -0,0 +1,54 @@ +package com.wyl.socket.service; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * 服务端 + * @author 刘正 + * + */ +public class servernettext { + public static String _pattern = "yyyy-MM-dd HH:mm:ss SSS"; + public static SimpleDateFormat format = new SimpleDateFormat(_pattern); + // 设置超时间 + public static int _sec = 0; + public static void main(String[] args) { + try { + //监听指定的端口 + ServerSocket server=new ServerSocket(9999); + while (true) { + //建立连接 + Socket soket=server.accept(); + System.out.println(format.format(new Date())); + System.out.println("建立了链接\n"); + //接收客户端消息(从socket中获取输入流,并建立缓冲区进行读取) + InputStream din=soket.getInputStream(); + System.out.println("客户端ip地址是:"+soket.getInetAddress()); + System.out.println("客户端端口号是:"+soket.getPort()); + System.out.println("本地端口号是:"+soket.getLocalPort()); + byte[] outPut=new byte[4096]; + while (din.read(outPut)>0) { + //注意指定编码格式,发送方和接收方一定要统一,建议使用UTF-8 + String result=new String(outPut); + System.out.println("客户端的消息是:"+result); + } + + //给客户端发送消息 + OutputStream dout=soket.getOutputStream(); + dout.write("已收到你发来的消息!!".getBytes()); + + din.close(); + dout.close(); + soket.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/Socket/src/test/java/com/wyl/socker/test/PoolTest.java b/Socket/src/test/java/com/wyl/socker/test/PoolTest.java new file mode 100644 index 0000000..4369180 --- /dev/null +++ b/Socket/src/test/java/com/wyl/socker/test/PoolTest.java @@ -0,0 +1,20 @@ +package com.wyl.socker.test; + +import com.wyl.socket.client.pool.ConnectionPoolFactory; + +import java.io.OutputStream; +import java.net.Socket; +import java.nio.charset.StandardCharsets; + +public class PoolTest { + public static void main(String[] args) throws Exception { + ConnectionPoolFactory instance = ConnectionPoolFactory.getInstance(); + for (int i = 0; i < 1; i++) { + Socket connection = instance.getConnection(); + OutputStream outputStream = connection.getOutputStream(); + outputStream.write("123".getBytes(StandardCharsets.UTF_8)); + outputStream.flush(); + } + Thread.sleep(100000000); + } +} diff --git a/chain-of-responsibility/src/main/java/com/wyl/chain/Handler.java b/chain-of-responsibility/src/main/java/com/wyl/chain/Handler.java index 4ba136c..e330c5f 100644 --- a/chain-of-responsibility/src/main/java/com/wyl/chain/Handler.java +++ b/chain-of-responsibility/src/main/java/com/wyl/chain/Handler.java @@ -2,19 +2,21 @@ package com.wyl.chain; /** * 责任链设计模式 + * + * @author wangyl + * @version V1.0 * @ClassName: Handler - * @Date: 2022/4/24 9:43 下午 - * @author wangyl - * @version V1.0 + * @Date: 2022/4/24 9:43 下午 */ public interface Handler { /** * 处理数据的借口 + * * @param date * @return java.lang.Boolean * @Date 2022/4/24 9:43 下午 * @Author wangyl - * @Version V1.0 + * @Version V1.0 */ - Boolean handleData(String date); + void handleData(String date); } diff --git a/chain-of-responsibility/src/main/java/com/wyl/chain/HandlerChain.java b/chain-of-responsibility/src/main/java/com/wyl/chain/HandlerChain.java index e4eba53..f330b8e 100644 --- a/chain-of-responsibility/src/main/java/com/wyl/chain/HandlerChain.java +++ b/chain-of-responsibility/src/main/java/com/wyl/chain/HandlerChain.java @@ -11,13 +11,8 @@ public class HandlerChain { } public void handle(String data) { - for (Handler handler : handlers){ - Boolean aBoolean = handler.handleData(data); - if (!aBoolean){ - System.out.println("数据处理失败结束"); - break; - } - + for (Handler handler : handlers) { + handler.handleData(data); } } } diff --git a/chain-of-responsibility/src/main/java/com/wyl/chain/handler/FirstHandler.java b/chain-of-responsibility/src/main/java/com/wyl/chain/handler/FirstHandler.java index 8eae952..b148eef 100644 --- a/chain-of-responsibility/src/main/java/com/wyl/chain/handler/FirstHandler.java +++ b/chain-of-responsibility/src/main/java/com/wyl/chain/handler/FirstHandler.java @@ -4,24 +4,16 @@ import com.wyl.chain.Handler; /** * 第一个处理 - * @ClassName: FirstHandler - * @Date: 2022/4/24 9:59 下午 + * * @author wangyl * @version V1.0 + * @ClassName: FirstHandler + * @Date: 2022/4/24 9:59 下午 */ public class FirstHandler implements Handler { @Override - public Boolean handleData(String date) { - if (date.startsWith("First")) { - System.out.println("第一处理器开始处理。。。"); - if (date.length() < 6) { - throw new RuntimeException("数据长度不够"); - } - System.out.println("第一处理器处理完毕。。。"); - return true; - } - System.out.println("无需第一处理"); - return false; + public void handleData(String date) { + } } diff --git a/chain-of-responsibility/src/main/java/com/wyl/chain/handler/SecondHandler.java b/chain-of-responsibility/src/main/java/com/wyl/chain/handler/SecondHandler.java index 7dc0ca5..913b3ce 100644 --- a/chain-of-responsibility/src/main/java/com/wyl/chain/handler/SecondHandler.java +++ b/chain-of-responsibility/src/main/java/com/wyl/chain/handler/SecondHandler.java @@ -17,17 +17,8 @@ import java.util.concurrent.RejectedExecutionException; */ public class SecondHandler implements Handler { @Override - public Boolean handleData(String date) { - if (date.endsWith("Second")) { - System.out.println("第二处理器开始处理。。。"); - if (date.length() < 10) { - throw new RuntimeException("第二处理数据长度不够"); - } - System.out.println("第二处理器处理完成。。。"); - return true; - } - System.out.println("无需第二处理"); - return false; + public void handleData(String date) { + } public static void main(String[] args) { diff --git a/chain-of-responsibility/src/main/java/com/wyl/filter/Filter.java b/chain-of-responsibility/src/main/java/com/wyl/filter/Filter.java new file mode 100644 index 0000000..af74303 --- /dev/null +++ b/chain-of-responsibility/src/main/java/com/wyl/filter/Filter.java @@ -0,0 +1,5 @@ +package com.wyl.filter; + +public interface Filter { + String doFilter(String text, FilterChain chain); +} \ No newline at end of file diff --git a/chain-of-responsibility/src/main/java/com/wyl/filter/FilterChain.java b/chain-of-responsibility/src/main/java/com/wyl/filter/FilterChain.java new file mode 100644 index 0000000..ebd442b --- /dev/null +++ b/chain-of-responsibility/src/main/java/com/wyl/filter/FilterChain.java @@ -0,0 +1,26 @@ +package com.wyl.filter; + +import java.util.ArrayList; +import java.util.List; + +public class FilterChain { + List fs = new ArrayList(); + int index = 0; + + + public FilterChain addFilter(Filter f) { + fs.add(f); + return this; + } + + public String doFilter(String text, FilterChain chain) { + if (index == fs.size()) { + return text; + } + Filter f = fs.get(index); + index++; + return f.doFilter(text, chain); + } + + +} \ No newline at end of file diff --git a/chain-of-responsibility/src/main/java/com/wyl/filter/HTMLFilter.java b/chain-of-responsibility/src/main/java/com/wyl/filter/HTMLFilter.java new file mode 100644 index 0000000..f6315db --- /dev/null +++ b/chain-of-responsibility/src/main/java/com/wyl/filter/HTMLFilter.java @@ -0,0 +1,11 @@ +package com.wyl.filter; + +public class HTMLFilter implements Filter { + + + @Override + public String doFilter(String text, FilterChain chain) { + text = text + "HTMLFilter"; + return chain.doFilter(text, chain); + } +} \ No newline at end of file diff --git a/chain-of-responsibility/src/main/java/com/wyl/filter/SensitiveFilter.java b/chain-of-responsibility/src/main/java/com/wyl/filter/SensitiveFilter.java new file mode 100644 index 0000000..4abf79b --- /dev/null +++ b/chain-of-responsibility/src/main/java/com/wyl/filter/SensitiveFilter.java @@ -0,0 +1,11 @@ +package com.wyl.filter; + +public class SensitiveFilter implements Filter { + + + @Override + public String doFilter(String text, FilterChain chain) { + text = text + "SensitiveFilter"; + return chain.doFilter(text, chain); + } +} \ No newline at end of file diff --git a/chain-of-responsibility/src/test/java/com/wyl/filter/Test.java b/chain-of-responsibility/src/test/java/com/wyl/filter/Test.java new file mode 100644 index 0000000..cc20d2e --- /dev/null +++ b/chain-of-responsibility/src/test/java/com/wyl/filter/Test.java @@ -0,0 +1,16 @@ +package com.wyl.filter; + +public class Test { + + /** + * @param args + */ + public static void main(String[] args) { + String msg = ""; + FilterChain fc = new FilterChain(); + fc.addFilter(new HTMLFilter()).addFilter(new SensitiveFilter()); + String s = fc.doFilter(msg, fc); + System.out.println(s); + } + +} \ No newline at end of file diff --git a/concurrent/src/test/java/concurrent/completableFuture/completableFutureTest.java b/concurrent/src/test/java/concurrent/completableFuture/completableFutureTest.java new file mode 100644 index 0000000..a17ca15 --- /dev/null +++ b/concurrent/src/test/java/concurrent/completableFuture/completableFutureTest.java @@ -0,0 +1,127 @@ +package concurrent.completableFuture; + +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +@Slf4j +public class completableFutureTest { + + /** + * 有返回值的异步任务 + */ + @Test + public void supplyAsyncGet() throws ExecutionException, InterruptedException { + CompletableFuture supplyAsyncFuture = CompletableFuture.supplyAsync(() -> { + log.info("executing supplyAsync task ..."); + return "this is supplyAsync"; + }); + String s = supplyAsyncFuture.get(); + System.out.println(s); + } + + @Test + public void supplyAsyncGetTimeOut() throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture supplyAsyncFuture = CompletableFuture.supplyAsync(() -> { + log.info("executing supplyAsync task ..."); + return "this is supplyAsync"; + }); + String s = supplyAsyncFuture.get(10L, TimeUnit.MILLISECONDS); + System.out.println(s); + } + + @Test + public void supplyAsyncJoin() { + CompletableFuture supplyAsyncFuture = CompletableFuture.supplyAsync(() -> { + log.info("executing supplyAsync task ..."); + return "this is supplyAsync"; + }); + String s = supplyAsyncFuture.join(); + System.out.println(s); + } + + + /** + * 没有返回值的异步任务 + */ + public void runAsyncGet() throws ExecutionException, InterruptedException { + CompletableFuture runAsyncFuture = CompletableFuture.runAsync(() -> { + log.info("executing runAsync task ..."); + }); + runAsyncFuture.get(); + } + + public void runAsyncGetTimeOut() throws ExecutionException, InterruptedException, TimeoutException { + CompletableFuture runAsyncFuture = CompletableFuture.runAsync(() -> { + log.info("executing runAsync task ..."); + }); + runAsyncFuture.get(10L, TimeUnit.MILLISECONDS); + } + + public void runAsyncJoin() { + CompletableFuture runAsyncFuture = CompletableFuture.runAsync(() -> { + log.info("executing runAsync task ..."); + }); + runAsyncFuture.join(); + } + + /** + * 多任务并行 等待都完成 + */ + @Test + public void allOf() throws ExecutionException, InterruptedException { + CompletableFuture cf11 = CompletableFuture.supplyAsync(() -> { + log.info("executing supplyAsync task cf11 ..."); + try { + TimeUnit.SECONDS.sleep(10L); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return "this is supplyAsync"; + }); + CompletableFuture cf12 = CompletableFuture.supplyAsync(() -> { + log.info("executing supplyAsync task cf12 ..."); + return "this is supplyAsync1"; + }); + CompletableFuture allOfFuture = CompletableFuture.allOf(cf11, cf12); + String s = cf12.get(); + String s1 = cf11.get(); + System.out.println(s); + System.out.println(s1); +// allOfFuture.get(); +// String s = cf12.get(); +// String s1 = cf11.get(); +// System.out.println(s); +// System.out.println(s1); + } + + /** + * 多任务并行 一个完成就返回完成的 + */ + @Test + public void test() throws ExecutionException, InterruptedException { + CompletableFuture cf21 = CompletableFuture.supplyAsync(() -> { + log.info("executing supplyAsync task cf21 ..."); + try { + TimeUnit.SECONDS.sleep(1L); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return "this is supplyAsync cf21"; + }); + CompletableFuture cf22 = CompletableFuture.supplyAsync(() -> { + log.info("executing supplyAsync task cf22 ..."); + return "this is supplyAsync cf22"; + }); + CompletableFuture anyOfFuture = CompletableFuture.anyOf(cf21, cf22); + log.info("{}", anyOfFuture.get()); // 输出结果:this is supplyAsync cf21或cf22 + String join = cf21.join(); + String join1 = cf22.join(); + System.out.println(join); + System.out.println(join1); + } +} diff --git a/concurrent/src/test/java/concurrent/hashmap/HashMapTest.java b/concurrent/src/test/java/concurrent/hashmap/HashMapTest.java index d3729a0..28486ac 100644 --- a/concurrent/src/test/java/concurrent/hashmap/HashMapTest.java +++ b/concurrent/src/test/java/concurrent/hashmap/HashMapTest.java @@ -3,9 +3,6 @@ package concurrent.hashmap; import org.junit.jupiter.api.Test; -import java.util.concurrent.DelayQueue; -import java.util.concurrent.Delayed; -import java.util.concurrent.TimeUnit; public class HashMapTest { static final int MAXIMUM_CAPACITY = 1 << 30; diff --git a/concurrent/src/test/java/concurrent/model/ModelTest.java b/concurrent/src/test/java/concurrent/model/ModelTest.java index cd55b00..e4c89ec 100644 --- a/concurrent/src/test/java/concurrent/model/ModelTest.java +++ b/concurrent/src/test/java/concurrent/model/ModelTest.java @@ -2,7 +2,10 @@ package concurrent.model; import org.junit.jupiter.api.Test; +import java.lang.ref.WeakReference; +import java.util.HashMap; import java.util.LinkedList; +import java.util.Map; import java.util.Queue; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.LinkedBlockingDeque; @@ -11,10 +14,10 @@ public class ModelTest { @Test public void ArrayBlockingQueueModelTest() throws InterruptedException { ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(5000); - for (int i = 0; i < 1000; i++){ + for (int i = 0; i < 1000; i++) { new QueueProducer(arrayBlockingQueue, i).start(); } - for (int i = 0; i < 200000; i++){ + for (int i = 0; i < 200000; i++) { new QueueConsumer(arrayBlockingQueue, i).start(); } @@ -24,10 +27,10 @@ public class ModelTest { @Test public void LinkedBlockingQueueModelTest() throws InterruptedException { LinkedBlockingDeque arrayBlockingQueue = new LinkedBlockingDeque<>(1000); - for (int i = 0; i < 1000; i++){ + for (int i = 0; i < 1000; i++) { new QueueProducer(arrayBlockingQueue, i).start(); } - for (int i = 0; i < 200000; i++){ + for (int i = 0; i < 200000; i++) { new QueueConsumer(arrayBlockingQueue, i).start(); } @@ -39,12 +42,50 @@ public class ModelTest { Object o = new Object(); Queue queue = new LinkedList(); Integer maxSize = 100; - for (int i = 0; i < 15; i++){ + for (int i = 0; i < 15; i++) { new WaitProducer(maxSize, o, queue).start(); } - for (int i = 0; i < 2; i++){ + for (int i = 0; i < 2; i++) { new WaitConsumer(o, queue).start(); } Thread.sleep(10000000); } + + @Test + public void test() throws InterruptedException { + Map, WeakReference> map = new HashMap<>(8); + // 注意这里~ + WeakReference key = new WeakReference<>(1); + WeakReference value = new WeakReference<>(2); + map.put(key, value); + System.out.println("put success"); + Thread.sleep(1000); + System.gc(); + System.out.println("get " + map.get(key).get()); + } + + @Test + public void test2() throws InterruptedException { + Map, WeakReference> map = new HashMap<>(8); + WeakReference key = new WeakReference<>(1); + WeakReference value = new WeakReference<>(777); + map.put(key,value); + System.out.println("put success"); + Thread.sleep(1000); + System.gc(); + System.out.println("get " + map.get(key).get()); + } + + @Test + public void test3() throws InterruptedException { + Map, Integer> map = new HashMap<>(8); + WeakReference key = new WeakReference<>(1); + Integer value = 777; + map.put(key,value); + System.out.println("put success"); + Thread.sleep(1000); + System.gc(); + System.out.println("get " + map.get(key)); + } + } diff --git a/file-read/src/main/java/generator/domain/AbtestExperimentAlgorithm.java b/file-read/src/main/java/generator/domain/AbtestExperimentAlgorithm.java new file mode 100644 index 0000000..756ebc1 --- /dev/null +++ b/file-read/src/main/java/generator/domain/AbtestExperimentAlgorithm.java @@ -0,0 +1,359 @@ +package generator.domain; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import java.io.Serializable; +import java.time.LocalDateTime; + +/** + * 算法实验记录表 + * @TableName abtest_experiment_algorithm + */ +@TableName(value ="abtest_experiment_algorithm") +public class AbtestExperimentAlgorithm implements Serializable { + /** + * + */ + @TableId(type = IdType.AUTO) + private Integer id; + + /** + * 参数列表,json格式的字符串,如[{key:value},] + */ + private String parametersList; + + /** + * 分流模式 + */ + private String shuntType; + + /** + * 版本号 + */ + private Integer version; + + /** + * abtest_experiment_group表中的id,关联实验状态信息 + */ + private Integer abtestExperimentGroupId; + + /** + * 实验细节描述 + */ + private String detailsDescription; + + /** + * 分流字段 + */ + private String shuntKey; + + /** + * 分流比例 + */ + private Integer shuntProportion; + + /** + * 更新时间 + */ + private LocalDateTime updateTime; + + /** + * 创建时间 + */ + private LocalDateTime createTime; + + /** + * 名字 + */ + private String employeeName; + + /** + * 工号 + */ + private String employeeWorkCode; + + /** + * 已删除 1 未删除 0 + */ + private Integer deleted; + + /** + * etl更新时间 + */ + private LocalDateTime beidouEtlTime; + + @TableField(exist = false) + private static final long serialVersionUID = 1L; + + /** + * + */ + public Integer getId() { + return id; + } + + /** + * + */ + public void setId(Integer id) { + this.id = id; + } + + /** + * 参数列表,json格式的字符串,如[{key:value},] + */ + public String getParametersList() { + return parametersList; + } + + /** + * 参数列表,json格式的字符串,如[{key:value},] + */ + public void setParametersList(String parametersList) { + this.parametersList = parametersList; + } + + /** + * 分流模式 + */ + public String getShuntType() { + return shuntType; + } + + /** + * 分流模式 + */ + public void setShuntType(String shuntType) { + this.shuntType = shuntType; + } + + /** + * 版本号 + */ + public Integer getVersion() { + return version; + } + + /** + * 版本号 + */ + public void setVersion(Integer version) { + this.version = version; + } + + /** + * abtest_experiment_group表中的id,关联实验状态信息 + */ + public Integer getAbtestExperimentGroupId() { + return abtestExperimentGroupId; + } + + /** + * abtest_experiment_group表中的id,关联实验状态信息 + */ + public void setAbtestExperimentGroupId(Integer abtestExperimentGroupId) { + this.abtestExperimentGroupId = abtestExperimentGroupId; + } + + /** + * 实验细节描述 + */ + public String getDetailsDescription() { + return detailsDescription; + } + + /** + * 实验细节描述 + */ + public void setDetailsDescription(String detailsDescription) { + this.detailsDescription = detailsDescription; + } + + /** + * 分流字段 + */ + public String getShuntKey() { + return shuntKey; + } + + /** + * 分流字段 + */ + public void setShuntKey(String shuntKey) { + this.shuntKey = shuntKey; + } + + /** + * 分流比例 + */ + public Integer getShuntProportion() { + return shuntProportion; + } + + /** + * 分流比例 + */ + public void setShuntProportion(Integer shuntProportion) { + this.shuntProportion = shuntProportion; + } + + /** + * 更新时间 + */ + public LocalDateTime getUpdateTime() { + return updateTime; + } + + /** + * 更新时间 + */ + public void setUpdateTime(LocalDateTime updateTime) { + this.updateTime = updateTime; + } + + /** + * 创建时间 + */ + public LocalDateTime getCreateTime() { + return createTime; + } + + /** + * 创建时间 + */ + public void setCreateTime(LocalDateTime createTime) { + this.createTime = createTime; + } + + /** + * 名字 + */ + public String getEmployeeName() { + return employeeName; + } + + /** + * 名字 + */ + public void setEmployeeName(String employeeName) { + this.employeeName = employeeName; + } + + /** + * 工号 + */ + public String getEmployeeWorkCode() { + return employeeWorkCode; + } + + /** + * 工号 + */ + public void setEmployeeWorkCode(String employeeWorkCode) { + this.employeeWorkCode = employeeWorkCode; + } + + /** + * 已删除 1 未删除 0 + */ + public Integer getDeleted() { + return deleted; + } + + /** + * 已删除 1 未删除 0 + */ + public void setDeleted(Integer deleted) { + this.deleted = deleted; + } + + /** + * etl更新时间 + */ + public LocalDateTime getBeidouEtlTime() { + return beidouEtlTime; + } + + /** + * etl更新时间 + */ + public void setBeidouEtlTime(LocalDateTime beidouEtlTime) { + this.beidouEtlTime = beidouEtlTime; + } + + @Override + public boolean equals(Object that) { + if (this == that) { + return true; + } + if (that == null) { + return false; + } + if (getClass() != that.getClass()) { + return false; + } + AbtestExperimentAlgorithm other = (AbtestExperimentAlgorithm) that; + return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) + && (this.getParametersList() == null ? other.getParametersList() == null : this.getParametersList().equals(other.getParametersList())) + && (this.getShuntType() == null ? other.getShuntType() == null : this.getShuntType().equals(other.getShuntType())) + && (this.getVersion() == null ? other.getVersion() == null : this.getVersion().equals(other.getVersion())) + && (this.getAbtestExperimentGroupId() == null ? other.getAbtestExperimentGroupId() == null : this.getAbtestExperimentGroupId().equals(other.getAbtestExperimentGroupId())) + && (this.getDetailsDescription() == null ? other.getDetailsDescription() == null : this.getDetailsDescription().equals(other.getDetailsDescription())) + && (this.getShuntKey() == null ? other.getShuntKey() == null : this.getShuntKey().equals(other.getShuntKey())) + && (this.getShuntProportion() == null ? other.getShuntProportion() == null : this.getShuntProportion().equals(other.getShuntProportion())) + && (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime())) + && (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime())) + && (this.getEmployeeName() == null ? other.getEmployeeName() == null : this.getEmployeeName().equals(other.getEmployeeName())) + && (this.getEmployeeWorkCode() == null ? other.getEmployeeWorkCode() == null : this.getEmployeeWorkCode().equals(other.getEmployeeWorkCode())) + && (this.getDeleted() == null ? other.getDeleted() == null : this.getDeleted().equals(other.getDeleted())) + && (this.getBeidouEtlTime() == null ? other.getBeidouEtlTime() == null : this.getBeidouEtlTime().equals(other.getBeidouEtlTime())); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); + result = prime * result + ((getParametersList() == null) ? 0 : getParametersList().hashCode()); + result = prime * result + ((getShuntType() == null) ? 0 : getShuntType().hashCode()); + result = prime * result + ((getVersion() == null) ? 0 : getVersion().hashCode()); + result = prime * result + ((getAbtestExperimentGroupId() == null) ? 0 : getAbtestExperimentGroupId().hashCode()); + result = prime * result + ((getDetailsDescription() == null) ? 0 : getDetailsDescription().hashCode()); + result = prime * result + ((getShuntKey() == null) ? 0 : getShuntKey().hashCode()); + result = prime * result + ((getShuntProportion() == null) ? 0 : getShuntProportion().hashCode()); + result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode()); + result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode()); + result = prime * result + ((getEmployeeName() == null) ? 0 : getEmployeeName().hashCode()); + result = prime * result + ((getEmployeeWorkCode() == null) ? 0 : getEmployeeWorkCode().hashCode()); + result = prime * result + ((getDeleted() == null) ? 0 : getDeleted().hashCode()); + result = prime * result + ((getBeidouEtlTime() == null) ? 0 : getBeidouEtlTime().hashCode()); + return result; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()); + sb.append(" ["); + sb.append("Hash = ").append(hashCode()); + sb.append(", id=").append(id); + sb.append(", parametersList=").append(parametersList); + sb.append(", shuntType=").append(shuntType); + sb.append(", version=").append(version); + sb.append(", abtestExperimentGroupId=").append(abtestExperimentGroupId); + sb.append(", detailsDescription=").append(detailsDescription); + sb.append(", shuntKey=").append(shuntKey); + sb.append(", shuntProportion=").append(shuntProportion); + sb.append(", updateTime=").append(updateTime); + sb.append(", createTime=").append(createTime); + sb.append(", employeeName=").append(employeeName); + sb.append(", employeeWorkCode=").append(employeeWorkCode); + sb.append(", deleted=").append(deleted); + sb.append(", beidouEtlTime=").append(beidouEtlTime); + sb.append(", serialVersionUID=").append(serialVersionUID); + sb.append("]"); + return sb.toString(); + } +} \ No newline at end of file diff --git a/file-read/src/main/java/generator/mapper/AbtestExperimentAlgorithmMapper.java b/file-read/src/main/java/generator/mapper/AbtestExperimentAlgorithmMapper.java new file mode 100644 index 0000000..5721c87 --- /dev/null +++ b/file-read/src/main/java/generator/mapper/AbtestExperimentAlgorithmMapper.java @@ -0,0 +1,18 @@ +package generator.mapper; + +import generator.domain.AbtestExperimentAlgorithm; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; + +/** +* @author wyl +* @description 针对表【abtest_experiment_algorithm(算法实验记录表)】的数据库操作Mapper +* @createDate 2023-05-26 10:46:15 +* @Entity generator.domain.AbtestExperimentAlgorithm +*/ +public interface AbtestExperimentAlgorithmMapper extends BaseMapper { + +} + + + + diff --git a/file-read/src/main/java/generator/service/AbtestExperimentAlgorithmService.java b/file-read/src/main/java/generator/service/AbtestExperimentAlgorithmService.java new file mode 100644 index 0000000..2b3e4a3 --- /dev/null +++ b/file-read/src/main/java/generator/service/AbtestExperimentAlgorithmService.java @@ -0,0 +1,13 @@ +package generator.service; + +import generator.domain.AbtestExperimentAlgorithm; +import com.baomidou.mybatisplus.extension.service.IService; + +/** +* @author wyl +* @description 针对表【abtest_experiment_algorithm(算法实验记录表)】的数据库操作Service +* @createDate 2023-05-26 10:46:15 +*/ +public interface AbtestExperimentAlgorithmService extends IService { + +} diff --git a/file-read/src/main/java/generator/service/impl/AbtestExperimentAlgorithmServiceImpl.java b/file-read/src/main/java/generator/service/impl/AbtestExperimentAlgorithmServiceImpl.java new file mode 100644 index 0000000..bf7290f --- /dev/null +++ b/file-read/src/main/java/generator/service/impl/AbtestExperimentAlgorithmServiceImpl.java @@ -0,0 +1,22 @@ +package generator.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import generator.domain.AbtestExperimentAlgorithm; +import generator.service.AbtestExperimentAlgorithmService; +import generator.mapper.AbtestExperimentAlgorithmMapper; +import org.springframework.stereotype.Service; + +/** +* @author wyl +* @description 针对表【abtest_experiment_algorithm(算法实验记录表)】的数据库操作Service实现 +* @createDate 2023-05-26 10:46:15 +*/ +@Service +public class AbtestExperimentAlgorithmServiceImpl extends ServiceImpl + implements AbtestExperimentAlgorithmService{ + +} + + + + diff --git a/file-read/src/main/resources/mapper/AbtestExperimentAlgorithmMapper.xml b/file-read/src/main/resources/mapper/AbtestExperimentAlgorithmMapper.xml new file mode 100644 index 0000000..63d42aa --- /dev/null +++ b/file-read/src/main/resources/mapper/AbtestExperimentAlgorithmMapper.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + id,parameters_list,shunt_type, + version,abtest_experiment_group_id,details_description, + shunt_key,shunt_proportion,update_time, + create_time,employee_name,employee_work_code, + deleted,beidou_etl_time + + diff --git a/javacv/pom.xml b/javacv/pom.xml index c60922d..21f807a 100644 --- a/javacv/pom.xml +++ b/javacv/pom.xml @@ -12,15 +12,17 @@ javacv - 8 - 8 + 11 + 11 - org.bytedeco - javacv-platform - 1.5.8 + org.opencv + opencv + 470 + system + ${project.basedir}/src/main/resources/opencv-470.jar \ No newline at end of file diff --git a/javacv/src/main/java/com/wyl/javacv/DemoApplicationTests.java b/javacv/src/main/java/com/wyl/javacv/DemoApplicationTests.java new file mode 100644 index 0000000..be3d63a --- /dev/null +++ b/javacv/src/main/java/com/wyl/javacv/DemoApplicationTests.java @@ -0,0 +1,49 @@ +package com.wyl.javacv; + + +import org.opencv.core.CvType; +import org.opencv.core.Mat; +import org.opencv.core.MatOfByte; +import org.opencv.highgui.HighGui; +import org.opencv.imgcodecs.Imgcodecs; +import org.opencv.imgproc.Imgproc; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.net.URL; + + +public class DemoApplicationTests { + /** + * @return + * @Description + * @Param + * @Author zhangsan + * @Date 2020.09.05 9:43 + **/ + public static void main(String[] args) throws Exception { + // 解决awt报错问题 + // 加载动态库 + URL url = ClassLoader.getSystemResource("opencv_java470.dll"); + System.load(url.getPath()); + // 读取图像 +// Mat image = Imgcodecs.imread("D:\\1.jpg"); + File file = new File("D:/1.jpg"); + InputStream fos = new FileInputStream(file); + Mat image = Imgcodecs.imdecode(new MatOfByte(fos.readAllBytes()), Imgcodecs.IMREAD_COLOR); + if (image.empty()) { + throw new Exception("image is empty"); + } + HighGui.imshow("Original Image", image); + + // 创建输出单通道图像 + Mat grayImage = new Mat(image.rows(), image.cols(), CvType.CV_8SC1); + // 进行图像色彩空间转换 + Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_RGB2GRAY); + + HighGui.imshow("Processed Image", grayImage); + Imgcodecs.imwrite("D:/hello.jpg", grayImage); + HighGui.waitKey(); + } +} \ No newline at end of file diff --git a/javacv/src/main/java/com/wyl/javacv/ImageToVideoExample.java b/javacv/src/main/java/com/wyl/javacv/ImageToVideoExample.java deleted file mode 100644 index af1eb61..0000000 --- a/javacv/src/main/java/com/wyl/javacv/ImageToVideoExample.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.wyl.javacv; - -import org.opencv.core.Mat; -import org.opencv.core.Size; -import org.opencv.videoio.VideoWriter; - -import java.io.File; - -import static org.opencv.imgcodecs.Imgcodecs.imread; - -public class ImageToVideoExample { - - public static void main(String[] args) { - // 图像文件夹路径 - String imageFolderPath = "/Users/wyl/project/java/wyl/JavaBasiceDemo/javacv/src/main/resources/"; - - // 获取图像文件列表 - File[] imageFiles = new File(imageFolderPath).listFiles(); - - // 设置视频编码器参数 - int fourcc = VideoWriter.fourcc('X', 'V', 'I', 'D'); // 使用XVID编码器 - double fps = 24; // 视频帧率 - Mat image = imread(imageFiles[0].getAbsolutePath()); - Size frameSize = new Size(image.cols(), image.rows()); // 视频帧尺寸 - - // 创建视频写入器 - VideoWriter writer = new VideoWriter("/Users/wyl/project/java/wyl/JavaBasiceDemo/javacv/src/main/resources/output.avi", fourcc, fps, frameSize, true); - - // 逐个读取图像并写入视频 - for (File imageFile : imageFiles) { - // 加载图像 - image = imread(imageFile.getAbsolutePath()); - - // 检查图像是否加载成功 - if (image.empty()) { - System.out.println("无法加载图像:" + imageFile.getName()); - System.exit(1); - } - - // 写入视频 - writer.write(image); - - // 释放内存 - image.release(); - } - - // 释放视频写入器 - writer.release(); - - System.out.println("视频合成完成!"); - } -} diff --git a/javacv/src/main/java/com/wyl/javacv/JavaCVExample.java b/javacv/src/main/java/com/wyl/javacv/JavaCVExample.java deleted file mode 100644 index 59b0e34..0000000 --- a/javacv/src/main/java/com/wyl/javacv/JavaCVExample.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.wyl.javacv; - - -import org.bytedeco.opencv.opencv_core.Mat; - -import static org.bytedeco.opencv.global.opencv_highgui.imshow; -import static org.bytedeco.opencv.global.opencv_highgui.waitKey; -import static org.bytedeco.opencv.global.opencv_imgcodecs.imread; -import static org.bytedeco.opencv.global.opencv_imgcodecs.imwrite; - -public class JavaCVExample { - - public static void main(String[] args) { - // 加载图像 - Mat image = imread("/Users/wyl/project/java/wyl/JavaBasiceDemo/javacv/src/main/resources/OIP.jpg"); - - // 检查图像是否加载成功 - if (image.empty()) { - System.out.println("无法加载图像"); - System.exit(1); - } - - // 显示图像 -// imshow("图像窗口", image); -// waitKey(0); - - // 保存图像 - imwrite("/Users/wyl/project/java/wyl/JavaBasiceDemo/javacv/src/main/resources/example_output.jpg", image); - - // 释放内存 - image.release(); - } -} diff --git a/javacv/src/main/resources/opencv_java470.dll b/javacv/src/main/resources/opencv_java470.dll new file mode 100644 index 0000000..b3c1b15 Binary files /dev/null and b/javacv/src/main/resources/opencv_java470.dll differ diff --git a/jna/pom.xml b/jna/pom.xml new file mode 100644 index 0000000..ec326a2 --- /dev/null +++ b/jna/pom.xml @@ -0,0 +1,19 @@ + + + + JavaBasiceDemo + com.wyl.example + 1.0-SNAPSHOT + + 4.0.0 + + jna + + + 11 + 11 + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index c7c053d..687d10b 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,8 @@ DelayQueue javacv mqtt + Socket + jna pom diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 7d91e6a..30006b0 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -14,6 +14,7 @@ spring-boot-common spring-boot-rabbitmq spring-boot-mybatis + spring-boot-wxrobot spring-boot diff --git a/spring-boot/spring-boot-wxrobot/pom.xml b/spring-boot/spring-boot-wxrobot/pom.xml new file mode 100644 index 0000000..a9ad723 --- /dev/null +++ b/spring-boot/spring-boot-wxrobot/pom.xml @@ -0,0 +1,35 @@ + + + + spring-boot + com.wyl.example + 1.0-SNAPSHOT + ../pom.xml + + 4.0.0 + + spring-boot-wxrobot + + + 8 + 8 + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + com.alibaba + fastjson + 1.2.83 + + + + \ No newline at end of file diff --git a/spring-boot/spring-boot-wxrobot/src/main/java/com/wyl/spring/wxrobot/IBSApplication.java b/spring-boot/spring-boot-wxrobot/src/main/java/com/wyl/spring/wxrobot/IBSApplication.java new file mode 100644 index 0000000..f5c6779 --- /dev/null +++ b/spring-boot/spring-boot-wxrobot/src/main/java/com/wyl/spring/wxrobot/IBSApplication.java @@ -0,0 +1,14 @@ +package com.wyl.spring.wxrobot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class IBSApplication { + + public static void main(String[] args) { + SpringApplication.run(IBSApplication.class, args); + } + +} + diff --git a/spring-boot/spring-boot-wxrobot/src/main/java/com/wyl/spring/wxrobot/controller/RobotMessage.java b/spring-boot/spring-boot-wxrobot/src/main/java/com/wyl/spring/wxrobot/controller/RobotMessage.java new file mode 100644 index 0000000..21d2214 --- /dev/null +++ b/spring-boot/spring-boot-wxrobot/src/main/java/com/wyl/spring/wxrobot/controller/RobotMessage.java @@ -0,0 +1,33 @@ +package com.wyl.spring.wxrobot.controller; + +import com.alibaba.fastjson.JSONObject; +import com.wyl.spring.wxrobot.entity.param.WxRobotMessageParam; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author: wangyl + * @date: 2023/2/6 + * @description: + */ +@RestController +@RequestMapping("/") +@Slf4j +@RequiredArgsConstructor(onConstructor = @__(@Autowired)) +public class RobotMessage { + + @PostMapping("/getMessageData") + public String getMessageData(@RequestBody WxRobotMessageParam skuListPageReq) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("code", 200); + jsonObject.put("message", "成功"); + jsonObject.put("data", "123"); + System.out.println(jsonObject.toJSONString()); + return jsonObject.toJSONString(); + } +} diff --git a/spring-boot/spring-boot-wxrobot/src/main/java/com/wyl/spring/wxrobot/entity/param/WxRobotMessageParam.java b/spring-boot/spring-boot-wxrobot/src/main/java/com/wyl/spring/wxrobot/entity/param/WxRobotMessageParam.java new file mode 100644 index 0000000..7887608 --- /dev/null +++ b/spring-boot/spring-boot-wxrobot/src/main/java/com/wyl/spring/wxrobot/entity/param/WxRobotMessageParam.java @@ -0,0 +1,22 @@ +package com.wyl.spring.wxrobot.entity.param; + +import lombok.Data; + +/** + * @author: wangyl + * @date: 2023/2/6 + * @description: 接收消息参数 + */ +@Data +public class WxRobotMessageParam { + /** + * 消息内容 + */ + private String text; + ThreadLocal t = new ThreadLocal<>(); + + public String getText() { + t.set("123"); + return text; + } +} diff --git a/spring-boot/spring-boot-wxrobot/src/main/resources/application.properties b/spring-boot/spring-boot-wxrobot/src/main/resources/application.properties new file mode 100644 index 0000000..a7d5980 --- /dev/null +++ b/spring-boot/spring-boot-wxrobot/src/main/resources/application.properties @@ -0,0 +1 @@ +server.port=65006 \ No newline at end of file