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