diff --git a/.gitignore b/.gitignore index 966499f4..27a9502e 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,11 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* *.iml -*.idea/ \ No newline at end of file +*.idea/ + +out/ +classes/ +target/ +build/ + +.DS_Store diff --git a/01jvm/AnalysisForList.java b/01jvm/AnalysisForList.java new file mode 100644 index 00000000..86eb5b24 --- /dev/null +++ b/01jvm/AnalysisForList.java @@ -0,0 +1,24 @@ +public class AnalysisForList { + + private int[] array = new int[] {1,2,3}; + + public void testFor() { + for (int i : array) { + System.out.println(i); + } + } + + public void testForIndex() { + for (int i=0;i " + url.toExternalForm()); + } + + // 扩展类加载器 + printClassloader("扩展类加载器",JvmClassLoaderPrintPath.class.getClassLoader().getParent()); + + // 应用类加载器 + printClassloader("应用类加载器",JvmClassLoaderPrintPath.class.getClassLoader()); + + } + + private static void printClassloader(String name, ClassLoader classLoader) { + System.out.println(); + if (null != classLoader) { + System.out.println(name + " Classloader -> " + classLoader.toString()); + printURLForClassloader(classLoader); + } else { + System.out.println(name + " Classloader -> null"); + } + } + + private static void printURLForClassloader(ClassLoader classLoader) { + Object ucp = insightField(classLoader,"ucp"); + Object path = insightField(ucp,"path"); + List paths = (List) path; + for (Object p : paths) { + System.out.println(" ===> " + p.toString()); + } + } + + private static Object insightField(Object obj, String fName) { + Field f = null; + try { + if (obj instanceof URLClassLoader) { + f = URLClassLoader.class.getDeclaredField(fName); + } else { + f = obj.getClass().getDeclaredField(fName); + } + f.setAccessible(true); + return f.get(obj); + } + catch (Exception ex) { + ex.printStackTrace(); + } + return null; + } + + +} diff --git a/01jvm/README.md b/01jvm/README.md new file mode 100644 index 00000000..51c885e3 --- /dev/null +++ b/01jvm/README.md @@ -0,0 +1,149 @@ +# 第1周作业 + + +参见 我的教室 -> 本周作业 + +## 作业内容 + + +> Week01 作业题目: + +1.(必做)自己写一个简单的 HelloNum.java,里面需要涉及基本类型,四则运行,if 和 for,然后自己分析一下对应的字节码,有问题群里讨论。 + +2.(必做)自定义一个 Classloader,加载一个 Hello.xlass 文件,执行 hello 方法,此文件内容是一个 Hello.class 文件所有字节(x=255-x)处理后的文件。文件群里提供。 + +3.(必做)画一张图,展示 Xmx、Xms、Xmn、Meta、DirectMemory、Xss 这些内存参数的关系。 + +4.(选做)检查一下自己维护的业务系统的 JVM 参数配置,用 jstat 和 jstack、jmap 查看一下详情,并且自己独立分析一下大概情况,思考有没有不合理的地方,如何改进。 + +注意:如果没有线上系统,可以自己 run 一个 web/java 项目。 + +5.(选做)本机使用 G1 GC 启动一个程序,仿照课上案例分析一下 JVM 情况。 + + + +## 操作步骤 + + +### 作业1(必做) + +1. 编写代码, 根据自己的意愿随意编写, 可参考: [HelloNum.java](./HelloNum.java) +2. 编译代码, 执行命令: `javac -g HelloNum.java` +3. 查看反编译的代码。 + - 3.1 可以安装并使用idea的jclasslib插件, 选中 [HelloNum.java](./HelloNum.java) 文件, 选择 `View --> Show Bytecode With jclasslib` 即可。 + - 3.2 或者直接通过命令行工具 javap, 执行命令: `javap -c -v -p -l HelloNum.class` +4. 分析相关的字节码。【此步骤需要各位同学自己进行分析】 + + +### 作业2(必做) + +1. 打开 Spring 官网: https://spring.io/ +2. 找到 Projects --> Spring Initializr: https://start.spring.io/ +3. 填写项目信息, 生成 maven 项目; 下载并解压。 +4. Idea或者Eclipse从已有的Source导入Maven项目。 +5. 从课件资料中找到资源 Hello.xlass 文件并复制到 src/main/resources 目录。 +6. 编写代码,实现 findClass 方法,以及对应的解码方法 +7. 编写main方法,调用 loadClass 方法; +8. 创建实例,以及调用方法 +9. 执行. + +具体代码可参考: [XlassLoader.java](./XlassLoader.java) + + +### 作业3(必做) + +对应的图片需要各位同学自己绘制,可以部分参考PPT课件。 + +提示: + +- Xms 设置堆内存的初始值 +- Xmx 设置堆内存的最大值 +- Xmn 设置堆内存中的年轻代的最大值 +- Meta 区不属于堆内存, 归属为非堆 +- DirectMemory 直接内存, 属于 JVM 内存中开辟出来的本地内存空间。 +- Xss设置的是单个线程栈的最大空间; + +JVM进程空间中的内存一般来说包括以下这些部分: + +- 堆内存(Xms ~ Xmx) = 年轻代(~Xmn) + 老年代 +- 非堆 = Meta + CodeCache + ... +- Native内存 = 直接内存 + Native + ... +- 栈内存 = n * Xss + +另外,注意区分规范与实现的区别, 需要根据具体实现以及版本, 才能确定。 一般来说,我们的目的是为了排查故障和诊断问题,大致弄清楚这些参数和空间的关系即可。 具体设置时还需要留一些冗余量。 + + +### 4.(选做) + +这个是具体案例分析, 请各位同学自己分析。 + +比如我们一个生产系统应用的启动参数为: + +``` +JAVA_OPTS=-Xmx200g -Xms200g -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -XX:ZCollectionInterval=30 -XX:ZAllocationSpikeTolerance=5 -XX:ReservedCodeCacheSize=2g -XX:InitialCodeCacheSize=2g -XX:ConcGCThreads=8 -XX:ParallelGCThreads=16 +``` + +另一个系统的启动参数为: + +``` +JAVA_OPTS=-Xmx4g -Xms4g -XX:+UseG1GC -XX:MaxGCPauseMillis=50 +``` + +具体如何设置, 需要考虑的因素包括: + +- 系统容量: 业务规模, 并发, 成本预算; 需要兼顾性能与成本; +- 延迟要求: 最坏情况下能接受多少时间的延迟尖刺。 +- 吞吐量: 根据业务特征来确定, 比如, 网关, 大数据底层平台, 批处理作业系统, 在线实时应用, 他们最重要的需求不一样。 +- 系统架构: 比如拆分为小内存更多节点, 还是大内存少量节点。 +- 其他... + + +### 5.(选做) + +例如使用以下命令: + +``` +# 编译 +javac -g GCLogAnalysis.java +# JDK8 启动程序 +java -Xmx2g -Xms2g -XX:+UseG1GC -verbose:gc -XX:+PrintGCDateStamps -XX:+PrintGCDetails -Xloggc:gc.log GCLogAnalysis +``` + +尝试使用课程中介绍的各种工具JDK命令行和图形工具来进行分析。 + +其中 [GCLogAnalysis.java](./GCLogAnalysis.java) 文件也可以从课件资料zip中找到. + +## 几个命令用法 +### 1、十六进制方式查看文件 +`hexdump -C Hello.class` +输出:`00000000 ca fe ba be 00 00 00 34 00 1c 0a 00 06 00 0e 09` + +可以看到magic number: `cafe babe`, +以及`00 00 00 34`,十六进制34=十进制3*16+4=52,这是jdk8,如果是jdk11则是55,十六进制37. + +### 2、Base64方式编码文件 +`base64 Hello.class` +### 3、显示JVM默认参数 +``` +java -XX:+PrintFlagsFinal -version + +java -XX:+PrintFlagsFinal -version | grep -F " Use" | grep -F "GC " + +java -XX:+PrintFlagsFinal -version | grep MaxNewSize + +``` + +### 4、切换不同jdk +``` +jenv shell 1.8 +jenv shell 11 +``` +显示所有jdk +``` +jenv versions +``` + +## 更多资料 + +更多中英文的技术文章和参考资料: + diff --git a/01jvm/TestAddUrl.java b/01jvm/TestAddUrl.java new file mode 100644 index 00000000..b4c3dc2e --- /dev/null +++ b/01jvm/TestAddUrl.java @@ -0,0 +1,22 @@ + +import java.io.File; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; + +public class TestAddUrl { + + public static void main(String[] args) throws Exception { + URLClassLoader classLoader = (URLClassLoader) TestAddUrl.class.getClassLoader(); + String dir = "./lib"; + Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); + method.setAccessible(true); + method.invoke(classLoader, new File(dir).getAbsoluteFile().toURL()); + + Class klass = Class.forName("HelloKimmking",true, classLoader); + Object obj = klass.newInstance(); + Method hello = klass.getDeclaredMethod("hello"); + hello.invoke(obj); + } + +} diff --git a/01jvm/XlassLoader.java b/01jvm/XlassLoader.java new file mode 100644 index 00000000..719b6dc5 --- /dev/null +++ b/01jvm/XlassLoader.java @@ -0,0 +1,75 @@ +import java.io.Closeable; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Method; + +/* + 第一周作业: + 2.(必做)自定义一个 Classloader,加载一个 lib.Hello.xlass 文件,执行 hello 方法,此文件内容是一个 lib.Hello.class 文件所有字节(x=255-x)处理后的文件。文件群里提供。 + */ +public class XlassLoader extends ClassLoader { + + public static void main(String[] args) throws Exception { + // 相关参数 + final String packageName = "lib"; + final String className = "Hello"; + final String methodName = "hello"; + // 创建类加载器 + ClassLoader classLoader = new XlassLoader(); + // 加载相应的类 + Class clazz = classLoader.loadClass(packageName + "." + className); + // 看看里面有些什么方法 + for (Method m : clazz.getDeclaredMethods()) { + System.out.println(clazz.getSimpleName() + "." + m.getName()); + } + // 创建对象 + Object instance = clazz.getDeclaredConstructor().newInstance(); + // 调用实例方法 + Method method = clazz.getMethod(methodName); + method.invoke(instance); + } + + @Override + protected Class findClass(String name) throws ClassNotFoundException { + // 如果支持包名, 则需要进行路径转换 + String resourcePath = name.replace(".", "/"); + // 文件后缀 + final String suffix = ".xlass"; + // 获取输入流 + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(resourcePath + suffix); + try { + // 读取数据 + int length = inputStream.available(); + byte[] byteArray = new byte[length]; + inputStream.read(byteArray); + // 转换 + byte[] classBytes = decode(byteArray); + // 通知底层定义这个类 + return defineClass(name, classBytes, 0, classBytes.length); + } catch (IOException e) { + throw new ClassNotFoundException(name, e); + } finally { + close(inputStream); + } + } + + // 解码 + private static byte[] decode(byte[] byteArray) { + byte[] targetArray = new byte[byteArray.length]; + for (int i = 0; i < byteArray.length; i++) { + targetArray[i] = (byte) (255 - byteArray[i]); + } + return targetArray; + } + + // 关闭 + private static void close(Closeable res) { + if (null != res) { + try { + res.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} diff --git a/01jvm/jvm/HelloClassLoader.java b/01jvm/jvm/HelloClassLoader.java new file mode 100644 index 00000000..51d906f1 --- /dev/null +++ b/01jvm/jvm/HelloClassLoader.java @@ -0,0 +1,22 @@ +package jvm; + +import java.util.Base64; + +public class HelloClassLoader extends ClassLoader { + + public static void main(String[] args) throws Exception { + + new HelloClassLoader().findClass("lib.Hello").newInstance(); + } + + @Override + protected Class findClass(String name) throws ClassNotFoundException { + String helloBase64 = "yv66vgAAADQAHwoABwAQCQARABIIABMKABQAFQgAFgcAFwcAGAEABjxpbml0PgEAAygpVgEABENvZGUBAA9MaW5lTnVtYmVyVGFibGUBAAVoZWxsbwEACDxjbGluaXQ+AQAKU291cmNlRmlsZQEACkhlbGxvLmphdmEMAAgACQcAGQwAGgAbAQAdSGVsbG8gY2xhc3Mgc2F5IGhlbGxvIG1ldGhvZC4HABwMAB0AHgEAGEhlbGxvIENsYXNzIEluaXRpYWxpemVkIQEACWxpYi9IZWxsbwEAEGphdmEvbGFuZy9PYmplY3QBABBqYXZhL2xhbmcvU3lzdGVtAQADb3V0AQAVTGphdmEvaW8vUHJpbnRTdHJlYW07AQATamF2YS9pby9QcmludFN0cmVhbQEAB3ByaW50bG4BABUoTGphdmEvbGFuZy9TdHJpbmc7KVYAIQAGAAcAAAAAAAMAAQAIAAkAAQAKAAAAHQABAAEAAAAFKrcAAbEAAAABAAsAAAAGAAEAAAADAAEADAAJAAEACgAAACUAAgABAAAACbIAAhIDtgAEsQAAAAEACwAAAAoAAgAAAAgACAAJAAgADQAJAAEACgAAACUAAgAAAAAACbIAAhIFtgAEsQAAAAEACwAAAAoAAgAAAAUACAAGAAEADgAAAAIADw==+AQAKU291cmNlRmlsZQEACkhlbGxvLmphdmEMAAgACQcAGQwAGgAbAQAdSGVsbG8gY2xhc3Mgc2F5IGhlbGxvIG1ldGhvZC4HABwMAB0AHgEAGEhlbGxvIENsYXNzIEluaXRpYWxpemVkIQEACWxpYi9IZWxsbwEAEGphdmEvbGFuZy9PYmplY3QBABBqYXZhL2xhbmcvU3lzdGVtAQADb3V0AQAVTGphdmEvaW8vUHJpbnRTdHJlYW07AQATamF2YS9pby9QcmludFN0cmVhbQEAB3ByaW50bG4BABUoTGphdmEvbGFuZy9TdHJpbmc7KVYAIQAGAAcAAAAAAAMAAQAIAAkAAQAKAAAAHQABAAEAAAAFKrcAAbEAAAABAAsAAAAGAAEAAAADAAEADAAJAAEACgAAACUAAgABAAAACbIAAhIDtgAEsQAAAAEACwAAAAoAAgAAAAgACAAJAAgADQAJAAEACgAAACUAAgAAAAAACbIAAhIFtgAEsQAAAAEACwAAAAoAAgAAAAUACAAGAAEADgAAAAIADw=="; + byte[] bytes = decode(helloBase64); + return defineClass(name,bytes,0,bytes.length); + } + + public byte[] decode(String base64) { + return Base64.getDecoder().decode(base64); + } +} diff --git a/01jvm/jvm/TestMem.java b/01jvm/jvm/TestMem.java new file mode 100644 index 00000000..cb915121 --- /dev/null +++ b/01jvm/jvm/TestMem.java @@ -0,0 +1,11 @@ +package jvm; + +public class TestMem { + public static void main(String[] args) { + int[] arr1 = new int[256]; + int[][] arr2 = new int[128][2]; + int[][][] arr3 = new int[64][2][2]; + + System.out.println(); + } +} diff --git a/01jvm/lib/Hello.java b/01jvm/lib/Hello.java new file mode 100644 index 00000000..5d951d97 --- /dev/null +++ b/01jvm/lib/Hello.java @@ -0,0 +1,12 @@ +package lib; + +public class Hello { + static { + System.out.println("Hello Class Initialized!"); + } + public void hello() { + System.out.println("Hello class say hello method."); + System.gc(); // JMX MBean server + } + +} \ No newline at end of file diff --git a/01jvm/lib/Hello.xlass b/01jvm/lib/Hello.xlass new file mode 100644 index 00000000..fa659481 --- /dev/null +++ b/01jvm/lib/Hello.xlass @@ -0,0 +1 @@ +5EAÖ֩𳖑Üѕⷚߜߌߗߒ線߼߶зГаГЬ곕ЖЯ앞ЖЯ׳ГЬ֩HNMINMIN \ No newline at end of file diff --git a/01jvm/lib/HelloKimmking.java b/01jvm/lib/HelloKimmking.java new file mode 100644 index 00000000..1e970fd7 --- /dev/null +++ b/01jvm/lib/HelloKimmking.java @@ -0,0 +1,6 @@ + +public class HelloKimmking { + public void hello() { + System.out.println("hello,kimmking."); + } +} diff --git a/01jvm/out/production/01jvm/README.md b/01jvm/out/production/01jvm/README.md new file mode 100644 index 00000000..51c885e3 --- /dev/null +++ b/01jvm/out/production/01jvm/README.md @@ -0,0 +1,149 @@ +# 第1周作业 + + +参见 我的教室 -> 本周作业 + +## 作业内容 + + +> Week01 作业题目: + +1.(必做)自己写一个简单的 HelloNum.java,里面需要涉及基本类型,四则运行,if 和 for,然后自己分析一下对应的字节码,有问题群里讨论。 + +2.(必做)自定义一个 Classloader,加载一个 Hello.xlass 文件,执行 hello 方法,此文件内容是一个 Hello.class 文件所有字节(x=255-x)处理后的文件。文件群里提供。 + +3.(必做)画一张图,展示 Xmx、Xms、Xmn、Meta、DirectMemory、Xss 这些内存参数的关系。 + +4.(选做)检查一下自己维护的业务系统的 JVM 参数配置,用 jstat 和 jstack、jmap 查看一下详情,并且自己独立分析一下大概情况,思考有没有不合理的地方,如何改进。 + +注意:如果没有线上系统,可以自己 run 一个 web/java 项目。 + +5.(选做)本机使用 G1 GC 启动一个程序,仿照课上案例分析一下 JVM 情况。 + + + +## 操作步骤 + + +### 作业1(必做) + +1. 编写代码, 根据自己的意愿随意编写, 可参考: [HelloNum.java](./HelloNum.java) +2. 编译代码, 执行命令: `javac -g HelloNum.java` +3. 查看反编译的代码。 + - 3.1 可以安装并使用idea的jclasslib插件, 选中 [HelloNum.java](./HelloNum.java) 文件, 选择 `View --> Show Bytecode With jclasslib` 即可。 + - 3.2 或者直接通过命令行工具 javap, 执行命令: `javap -c -v -p -l HelloNum.class` +4. 分析相关的字节码。【此步骤需要各位同学自己进行分析】 + + +### 作业2(必做) + +1. 打开 Spring 官网: https://spring.io/ +2. 找到 Projects --> Spring Initializr: https://start.spring.io/ +3. 填写项目信息, 生成 maven 项目; 下载并解压。 +4. Idea或者Eclipse从已有的Source导入Maven项目。 +5. 从课件资料中找到资源 Hello.xlass 文件并复制到 src/main/resources 目录。 +6. 编写代码,实现 findClass 方法,以及对应的解码方法 +7. 编写main方法,调用 loadClass 方法; +8. 创建实例,以及调用方法 +9. 执行. + +具体代码可参考: [XlassLoader.java](./XlassLoader.java) + + +### 作业3(必做) + +对应的图片需要各位同学自己绘制,可以部分参考PPT课件。 + +提示: + +- Xms 设置堆内存的初始值 +- Xmx 设置堆内存的最大值 +- Xmn 设置堆内存中的年轻代的最大值 +- Meta 区不属于堆内存, 归属为非堆 +- DirectMemory 直接内存, 属于 JVM 内存中开辟出来的本地内存空间。 +- Xss设置的是单个线程栈的最大空间; + +JVM进程空间中的内存一般来说包括以下这些部分: + +- 堆内存(Xms ~ Xmx) = 年轻代(~Xmn) + 老年代 +- 非堆 = Meta + CodeCache + ... +- Native内存 = 直接内存 + Native + ... +- 栈内存 = n * Xss + +另外,注意区分规范与实现的区别, 需要根据具体实现以及版本, 才能确定。 一般来说,我们的目的是为了排查故障和诊断问题,大致弄清楚这些参数和空间的关系即可。 具体设置时还需要留一些冗余量。 + + +### 4.(选做) + +这个是具体案例分析, 请各位同学自己分析。 + +比如我们一个生产系统应用的启动参数为: + +``` +JAVA_OPTS=-Xmx200g -Xms200g -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -XX:ZCollectionInterval=30 -XX:ZAllocationSpikeTolerance=5 -XX:ReservedCodeCacheSize=2g -XX:InitialCodeCacheSize=2g -XX:ConcGCThreads=8 -XX:ParallelGCThreads=16 +``` + +另一个系统的启动参数为: + +``` +JAVA_OPTS=-Xmx4g -Xms4g -XX:+UseG1GC -XX:MaxGCPauseMillis=50 +``` + +具体如何设置, 需要考虑的因素包括: + +- 系统容量: 业务规模, 并发, 成本预算; 需要兼顾性能与成本; +- 延迟要求: 最坏情况下能接受多少时间的延迟尖刺。 +- 吞吐量: 根据业务特征来确定, 比如, 网关, 大数据底层平台, 批处理作业系统, 在线实时应用, 他们最重要的需求不一样。 +- 系统架构: 比如拆分为小内存更多节点, 还是大内存少量节点。 +- 其他... + + +### 5.(选做) + +例如使用以下命令: + +``` +# 编译 +javac -g GCLogAnalysis.java +# JDK8 启动程序 +java -Xmx2g -Xms2g -XX:+UseG1GC -verbose:gc -XX:+PrintGCDateStamps -XX:+PrintGCDetails -Xloggc:gc.log GCLogAnalysis +``` + +尝试使用课程中介绍的各种工具JDK命令行和图形工具来进行分析。 + +其中 [GCLogAnalysis.java](./GCLogAnalysis.java) 文件也可以从课件资料zip中找到. + +## 几个命令用法 +### 1、十六进制方式查看文件 +`hexdump -C Hello.class` +输出:`00000000 ca fe ba be 00 00 00 34 00 1c 0a 00 06 00 0e 09` + +可以看到magic number: `cafe babe`, +以及`00 00 00 34`,十六进制34=十进制3*16+4=52,这是jdk8,如果是jdk11则是55,十六进制37. + +### 2、Base64方式编码文件 +`base64 Hello.class` +### 3、显示JVM默认参数 +``` +java -XX:+PrintFlagsFinal -version + +java -XX:+PrintFlagsFinal -version | grep -F " Use" | grep -F "GC " + +java -XX:+PrintFlagsFinal -version | grep MaxNewSize + +``` + +### 4、切换不同jdk +``` +jenv shell 1.8 +jenv shell 11 +``` +显示所有jdk +``` +jenv versions +``` + +## 更多资料 + +更多中英文的技术文章和参考资料: + diff --git "a/01jvm/out/production/01jvm/\347\216\257\345\242\203\345\207\206\345\244\207.txt" "b/01jvm/out/production/01jvm/\347\216\257\345\242\203\345\207\206\345\244\207.txt" new file mode 100644 index 00000000..a1458b85 --- /dev/null +++ "b/01jvm/out/production/01jvm/\347\216\257\345\242\203\345\207\206\345\244\207.txt" @@ -0,0 +1,38 @@ + + +## Windows + +1.管理员身份打开powershell + +2.运行 +Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) + +3.执行choco install superbenchmarker + +4.输入 sb + +执行 sb -u http://localhost:8088/api/hello -c 20 -N 60 + +## Mac + +1.执行brew install wrk +如果显式brew update很慢,可以ctrl+C打断更新 + +2.输入 wrk + +执行 wrk -t8 -c40 -d60s http://localhost:8088/api/hello + +## 压测程序 + +1.可以从github获取 +git clone https://github.com/kimmking/atlantis +cd atlantis\gateway-server +mvn clean package +然后在target目录可以找到gateway-server-0.0.1-SNAPSHOT.jar + +2.也可以从此处下载已经编译好的: +链接:https://pan.baidu.com/s/1NbpYX4M3YKLYM1JJeIzgSQ +提取码:sp85 + +java -jar -Xmx512m -Xms512 gateway-server-0.0.1-SNAPSHOT.jar + diff --git a/01jvm/subinterface/Mapper.java b/01jvm/subinterface/Mapper.java new file mode 100644 index 00000000..b6578fb5 --- /dev/null +++ b/01jvm/subinterface/Mapper.java @@ -0,0 +1,7 @@ +package subinterface; + +public interface Mapper { + + void insert(T t); + +} diff --git a/01jvm/subinterface/User.java b/01jvm/subinterface/User.java new file mode 100644 index 00000000..2a6a541f --- /dev/null +++ b/01jvm/subinterface/User.java @@ -0,0 +1,17 @@ +package subinterface; + +public class User { + private int id; + + public User(int id) { + this.id = id; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } +} diff --git a/01jvm/subinterface/UserDAO.java b/01jvm/subinterface/UserDAO.java new file mode 100644 index 00000000..76dd2fb6 --- /dev/null +++ b/01jvm/subinterface/UserDAO.java @@ -0,0 +1,9 @@ +package subinterface; + +public class UserDAO implements UserMapper { + + @Override + public void insert(User user) { + System.out.println("Insert user id: " + user.getId()); + } +} diff --git a/01jvm/subinterface/UserMain.java b/01jvm/subinterface/UserMain.java new file mode 100644 index 00000000..4ebd24cd --- /dev/null +++ b/01jvm/subinterface/UserMain.java @@ -0,0 +1,10 @@ +package subinterface; + +public class UserMain { + + public static void main(String[] args) { + UserDAO dao = new UserDAO(); + dao.insert(new User(123)); + } + +} diff --git a/01jvm/subinterface/UserMapper.java b/01jvm/subinterface/UserMapper.java new file mode 100644 index 00000000..573c0abc --- /dev/null +++ b/01jvm/subinterface/UserMapper.java @@ -0,0 +1,7 @@ +package subinterface; + +public interface UserMapper extends Mapper { + + @Override + void insert(User user); +} diff --git a/02nio/GCLogAnalysis.java b/02nio/GCLogAnalysis.java new file mode 100644 index 00000000..d2570b27 --- /dev/null +++ b/02nio/GCLogAnalysis.java @@ -0,0 +1,63 @@ +import java.util.Random; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.LongAdder; +/* +演示GC日志生成与解读 +*/ +public class GCLogAnalysis { + // 随机数; 记得这里可以设置随机数种子; + private static Random random = new Random(); + public static void main(String[] args) { + // 当前毫秒时间戳 + long startMillis = System.currentTimeMillis(); + // 持续运行毫秒数; 可根据需要进行修改 + long timeoutMillis = TimeUnit.SECONDS.toMillis(1); + // 结束时间戳 + long endMillis = startMillis + timeoutMillis; + LongAdder counter = new LongAdder(); + System.out.println("正在执行..."); + // 缓存一部分对象; 进入老年代 + int cacheSize = 2000; + Object[] cachedGarbage = new Object[cacheSize]; + // 在此时间范围内,持续循环 + while (System.currentTimeMillis() < endMillis) { + // 生成垃圾对象 + Object garbage = generateGarbage(100*1024); + counter.increment(); + int randomIndex = random.nextInt(2 * cacheSize); + if (randomIndex < cacheSize) { + cachedGarbage[randomIndex] = garbage; + } + } + System.out.println("执行结束!共生成对象次数:" + counter.longValue()); + } + + // 生成对象 + private static Object generateGarbage(int max) { + int randomSize = random.nextInt(max); + int type = randomSize % 4; + Object result = null; + switch (type) { + case 0: + result = new int[randomSize]; + break; + case 1: + result = new byte[randomSize]; + break; + case 2: + result = new double[randomSize]; + break; + default: + StringBuilder builder = new StringBuilder(); + String randomString = "randomString-Anything"; + while (builder.length() < randomSize) { + builder.append(randomString); + builder.append(max); + builder.append(randomSize); + } + result = builder.toString(); + break; + } + return result; + } +} diff --git a/02nio/README.md b/02nio/README.md new file mode 100644 index 00000000..79e9f518 --- /dev/null +++ b/02nio/README.md @@ -0,0 +1,35 @@ +# 第2周作业 + + +## 作业内容 + +> Week02 作业题目: + +1. (选做)使用 [GCLogAnalysis.java](./GCLogAnalysis.java) 自己演练一遍 串行/并行/CMS/G1 的案例。 +2. (选做)使用压测工具(wrk 或 sb),演练 gateway-server-0.0.1-SNAPSHOT.jar 示例。 +3. (选做)如果自己本地有可以运行的项目,可以按照 2 的方式进行演练。 +4. (必做)根据上述自己对于 1 和 2 的演示,写一段对于不同 GC 和堆内存的总结,提交到 GitHub。 +5. (选做)运行课上的例子,以及 Netty 的例子,分析相关现象。 +6. (必做)写一段代码,使用 HttpClient 或 OkHttp 访问 http://localhost:8801 ,代码提交到 GitHub + + + +## 操作步骤 + + +### 第二周-作业6.(必做) + +1. 打开 Spring 官网: https://spring.io/ +2. 找到 Projects --> Spring Initializr: https://start.spring.io/ +3. 填写项目信息, 生成 maven 项目; 下载并解压。 +4. Idea或者Eclipse从已有的Source导入Maven项目。 +5. 搜索依赖, 推荐 mvnrepository: https://mvnrepository.com/ +6. 搜索 OkHttp 或者 HttpClient,然后在 pom.xml 之中增加对应的依赖。 +7. 使用OkHttp + - 7.1 查找OkHttp官网: https://square.github.io/okhttp/ + - 7.2 参照官方示例编写代码: [OkHttpUtils.java](https://github.com/renfufei/JAVA-000/blob/main/Week_02/homework02/src/main/java/com/renfufei/homework02/OkHttpUtils.java) +8. 使用HttpClient + - 8.1 查找官网: http://hc.apache.org/ + - 8.2 参照官方示例编写代码: [HttpClientHelper.java](https://github.com/renfufei/JAVA-000/blob/main/Week_02/homework02/src/main/java/com/renfufei/homework02/HttpClientHelper.java) + - 8.3 执行如果报错, 根据提示,增加 commons-logging 或者其他日志依赖。 +9. 执行与测试. diff --git a/02nio/nio01/dependency-reduced-pom.xml b/02nio/nio01/dependency-reduced-pom.xml new file mode 100644 index 00000000..1646aa28 --- /dev/null +++ b/02nio/nio01/dependency-reduced-pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + java0.nio01 + nio01 + 1.0 + + + + maven-compiler-plugin + + ${maven.compile.source} + ${maven.compile.target} + + + + maven-shade-plugin + 3.2.0 + + + + shade + + + + + + ${app.main.class} + ${maven.compile.source} + ${maven.compile.target} + + + + + + + + + + + Main + 8 + 8 + + diff --git a/02nio/nio01/pom.xml b/02nio/nio01/pom.xml index 0fc8ec7e..4fb902dd 100644 --- a/02nio/nio01/pom.xml +++ b/02nio/nio01/pom.xml @@ -7,18 +7,69 @@ java0.nio01 nio01 1.0 + + + Main + 8 + 8 + + org.apache.maven.plugins maven-compiler-plugin - 8 - 8 + ${maven.compile.source} + ${maven.compile.target} + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.0 + + + + shade + + + + + + ${app.main.class} + ${maven.compile.source} + ${maven.compile.target} + + + + + + + + + + + io.netty + netty-all + 4.1.51.Final + + + com.squareup.okhttp3 + okhttp + 3.12.0 + + + org.apache.httpcomponents + httpclient + 4.5.5 + + + + \ No newline at end of file diff --git a/02nio/nio01/src/main/java/Main.java b/02nio/nio01/src/main/java/Main.java new file mode 100644 index 00000000..b07dca92 --- /dev/null +++ b/02nio/nio01/src/main/java/Main.java @@ -0,0 +1,34 @@ +import java0.nio01.HttpServer01; +import java0.nio01.HttpServer02; +import java0.nio01.HttpServer03; +import java0.nio01.netty.NettyHttpServer; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +public class Main { + + public static void main(String[] args) { + Map map = new HashMap<>(); + map.put("1", HttpServer01.class); + map.put("2", HttpServer02.class); + map.put("3", HttpServer03.class); + map.put("8", NettyHttpServer.class); + + String id = (null == args || args.length == 0) ? "1" : args[0]; + Class clazz = map.get(id); + if( null == clazz ) { + System.out.println("No class for id: " + id); + } + + try { + Method method = clazz.getDeclaredMethod("main", new Class[]{String[].class}); + method.invoke(null, new Object[]{new String[]{}}); + } catch (Exception e) { + e.printStackTrace(); + } + + } + +} diff --git a/02nio/nio01/src/main/java/java0/nio01/HttpClientDemo.java b/02nio/nio01/src/main/java/java0/nio01/HttpClientDemo.java new file mode 100644 index 00000000..43f5c9ed --- /dev/null +++ b/02nio/nio01/src/main/java/java0/nio01/HttpClientDemo.java @@ -0,0 +1,51 @@ +package java0.nio01; + +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.protocol.HTTP; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; + +public class HttpClientDemo { + + public static void main(String[] args) throws IOException { + + byte[] bytes = getBody1( "http://localhost:8801"); + System.out.println(new String(bytes)); + + } + + private static byte[] getBody1(String url){ + HttpGet httpGet = new HttpGet(url); + httpGet.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE); + CloseableHttpClient httpClient = null; + CloseableHttpResponse response = null; + try { + httpClient = HttpClients.createDefault(); + response = httpClient.execute(httpGet); + HttpEntity entity = response.getEntity(); + // System.out.println(EntityUtils.toString(entity)); + return EntityUtils.toByteArray(entity); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + // 释放资源 + if (response != null) { + response.close(); + } + if (httpClient != null) { + httpClient.close(); + } + httpGet.releaseConnection(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return null; + } +} diff --git a/02nio/nio01/src/main/java/java0/nio01/HttpServer01.java b/02nio/nio01/src/main/java/java0/nio01/HttpServer01.java index c9cc060f..bed0f8a4 100644 --- a/02nio/nio01/src/main/java/java0/nio01/HttpServer01.java +++ b/02nio/nio01/src/main/java/java0/nio01/HttpServer01.java @@ -5,6 +5,7 @@ import java.net.ServerSocket; import java.net.Socket; +// 单线程的socket程序 public class HttpServer01 { public static void main(String[] args) throws IOException{ ServerSocket serverSocket = new ServerSocket(8801); @@ -20,17 +21,17 @@ public static void main(String[] args) throws IOException{ private static void service(Socket socket) { try { - Thread.sleep(20); PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true); printWriter.println("HTTP/1.1 200 OK"); printWriter.println("Content-Type:text/html;charset=utf-8"); - String body = "hello,nio"; + String body = "hello,nio1"; printWriter.println("Content-Length:" + body.getBytes().length); printWriter.println(); printWriter.write(body); + printWriter.flush(); printWriter.close(); socket.close(); - } catch (IOException | InterruptedException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/02nio/nio01/src/main/java/java0/nio01/HttpServer02.java b/02nio/nio01/src/main/java/java0/nio01/HttpServer02.java index 9b51cf29..298814ac 100644 --- a/02nio/nio01/src/main/java/java0/nio01/HttpServer02.java +++ b/02nio/nio01/src/main/java/java0/nio01/HttpServer02.java @@ -5,6 +5,7 @@ import java.net.ServerSocket; import java.net.Socket; +// 每个请求一个线程 public class HttpServer02 { public static void main(String[] args) throws IOException{ ServerSocket serverSocket = new ServerSocket(8802); @@ -22,17 +23,16 @@ public static void main(String[] args) throws IOException{ private static void service(Socket socket) { try { - Thread.sleep(20); PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true); printWriter.println("HTTP/1.1 200 OK"); printWriter.println("Content-Type:text/html;charset=utf-8"); - String body = "hello,nio"; + String body = "hello,nio2"; printWriter.println("Content-Length:" + body.getBytes().length); printWriter.println(); printWriter.write(body); printWriter.close(); socket.close(); - } catch (IOException | InterruptedException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/02nio/nio01/src/main/java/java0/nio01/HttpServer03.java b/02nio/nio01/src/main/java/java0/nio01/HttpServer03.java index fad136a8..bd9b4acb 100644 --- a/02nio/nio01/src/main/java/java0/nio01/HttpServer03.java +++ b/02nio/nio01/src/main/java/java0/nio01/HttpServer03.java @@ -7,9 +7,11 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +// 创建了一个固定大小的线程池处理请求 public class HttpServer03 { public static void main(String[] args) throws IOException{ - ExecutorService executorService = Executors.newFixedThreadPool(40); + ExecutorService executorService = Executors.newFixedThreadPool( + Runtime.getRuntime().availableProcessors() * 4); final ServerSocket serverSocket = new ServerSocket(8803); while (true) { try { @@ -23,17 +25,16 @@ public static void main(String[] args) throws IOException{ private static void service(Socket socket) { try { - Thread.sleep(20); PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true); printWriter.println("HTTP/1.1 200 OK"); printWriter.println("Content-Type:text/html;charset=utf-8"); - String body = "hello,nio"; + String body = "hello,nio3"; printWriter.println("Content-Length:" + body.getBytes().length); printWriter.println(); printWriter.write(body); printWriter.close(); socket.close(); - } catch (IOException | InterruptedException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/02nio/nio01/src/main/java/java0/nio01/OKHttpClientDemo.java b/02nio/nio01/src/main/java/java0/nio01/OKHttpClientDemo.java new file mode 100644 index 00000000..98678754 --- /dev/null +++ b/02nio/nio01/src/main/java/java0/nio01/OKHttpClientDemo.java @@ -0,0 +1,35 @@ +package java0.nio01; + +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +import java.io.IOException; + +public class OKHttpClientDemo { + + private static OkHttpClient client = new OkHttpClient(); + public static void main(String[] args) throws IOException { + + getBody1(client, "http://localhost:8801"); + client = null; + } + + private static void getBody1(OkHttpClient client, String url){ + + Request request = new Request.Builder() + .get() + .url(url) + .build(); + //String body = "test"; + try { + Response response = client.newCall(request).execute(); + String responseData = response.body().string(); + System.out.println(responseData); + } catch (IOException e) { + e.printStackTrace(); + }finally { + client = null; + } + } +} diff --git a/02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java b/02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java new file mode 100644 index 00000000..81f31d2e --- /dev/null +++ b/02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java @@ -0,0 +1,81 @@ +package java0.nio01.netty; + +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpUtil; +import io.netty.util.ReferenceCountUtil; + +import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; +import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE; +import static io.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT; +import static io.netty.handler.codec.http.HttpResponseStatus.OK; +import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; + +public class HttpHandler extends ChannelInboundHandlerAdapter { + + @Override + public void channelReadComplete(ChannelHandlerContext ctx) { + ctx.flush(); + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + try { + //logger.info("channelRead流量接口请求开始,时间为{}", startTime); + FullHttpRequest fullRequest = (FullHttpRequest) msg; + String uri = fullRequest.uri(); + //logger.info("接收到的请求url为{}", uri); + if (uri.contains("/test")) { + handlerTest(fullRequest, ctx, "hello,kimmking"); + } else { + handlerTest(fullRequest, ctx, "hello,others"); + } + + } catch(Exception e) { + e.printStackTrace(); + } finally { + ReferenceCountUtil.release(msg); + } + } + + private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx, String body) { + FullHttpResponse response = null; + try { + String value = body; // 对接上次作业的httpclient或者okhttp请求另一个url的响应数据 + +// httpGet ... http://localhost:8801 +// 返回的响应,"hello,nio"; +// value = reponse.... + + response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(value.getBytes("UTF-8"))); + response.headers().set("Content-Type", "application/json"); + response.headers().setInt("Content-Length", response.content().readableBytes()); + + } catch (Exception e) { + System.out.println("处理出错:"+e.getMessage()); + response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT); + } finally { + if (fullRequest != null) { + if (!HttpUtil.isKeepAlive(fullRequest)) { + ctx.write(response).addListener(ChannelFutureListener.CLOSE); + } else { + response.headers().set(CONNECTION, KEEP_ALIVE); + ctx.write(response); + } + ctx.flush(); + } + } + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + cause.printStackTrace(); + ctx.close(); + } + +} diff --git a/02nio/nio01/src/main/java/java0/nio01/netty/HttpInitializer.java b/02nio/nio01/src/main/java/java0/nio01/netty/HttpInitializer.java new file mode 100644 index 00000000..ff4ee0be --- /dev/null +++ b/02nio/nio01/src/main/java/java0/nio01/netty/HttpInitializer.java @@ -0,0 +1,19 @@ +package java0.nio01.netty; + +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.socket.SocketChannel; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpServerCodec; + +public class HttpInitializer extends ChannelInitializer { + + @Override + public void initChannel(SocketChannel ch) { + ChannelPipeline p = ch.pipeline(); + p.addLast(new HttpServerCodec()); + //p.addLast(new HttpServerExpectContinueHandler()); + p.addLast(new HttpObjectAggregator(1024 * 1024)); + p.addLast(new HttpHandler()); + } +} diff --git a/02nio/nio01/src/main/java/java0/nio01/netty/NettyHttpServer.java b/02nio/nio01/src/main/java/java0/nio01/netty/NettyHttpServer.java new file mode 100644 index 00000000..487d7283 --- /dev/null +++ b/02nio/nio01/src/main/java/java0/nio01/netty/NettyHttpServer.java @@ -0,0 +1,48 @@ +package java0.nio01.netty; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.PooledByteBufAllocator; +import io.netty.channel.Channel; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.epoll.EpollChannelOption; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.logging.LogLevel; +import io.netty.handler.logging.LoggingHandler; + +public class NettyHttpServer { + public static void main(String[] args) throws InterruptedException { + + int port = 8808; + + EventLoopGroup bossGroup = new NioEventLoopGroup(2); + EventLoopGroup workerGroup = new NioEventLoopGroup(16); + + try { + ServerBootstrap b = new ServerBootstrap(); + b.option(ChannelOption.SO_BACKLOG, 128) + .childOption(ChannelOption.TCP_NODELAY, true) + .childOption(ChannelOption.SO_KEEPALIVE, true) + .childOption(ChannelOption.SO_REUSEADDR, true) + .childOption(ChannelOption.SO_RCVBUF, 32 * 1024) + .childOption(ChannelOption.SO_SNDBUF, 32 * 1024) + .childOption(EpollChannelOption.SO_REUSEPORT, true) + .childOption(ChannelOption.SO_KEEPALIVE, true) + .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); + + b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) + .handler(new LoggingHandler(LogLevel.INFO)) + .childHandler(new HttpInitializer()); + + Channel ch = b.bind(port).sync().channel(); + System.out.println("开启netty http服务器,监听地址和端口为 http://127.0.0.1:" + port + '/'); + ch.closeFuture().sync(); + } finally { + bossGroup.shutdownGracefully(); + workerGroup.shutdownGracefully(); + } + + + } +} diff --git a/02nio/nio02/pom.xml b/02nio/nio02/pom.xml index 6cbbeffd..c10848f8 100644 --- a/02nio/nio02/pom.xml +++ b/02nio/nio02/pom.xml @@ -29,7 +29,7 @@ io.netty netty-all - 4.1.45.Final + 4.1.104.Final @@ -52,6 +52,11 @@ httpasyncclient 4.1.4 + + + org.projectlombok + lombok + sn [" + sn.getNextNum() + "]"); + } + sn.getThreadLocal().remove(); + } + } +} diff --git a/03concurrency/0301/src/main/java/java0/conc0303/tool/CountDownLatchDemo.java b/03concurrency/0301/src/main/java/java0/conc0303/tool/CountDownLatchDemo.java new file mode 100644 index 00000000..b93731be --- /dev/null +++ b/03concurrency/0301/src/main/java/java0/conc0303/tool/CountDownLatchDemo.java @@ -0,0 +1,33 @@ +package java0.conc0303.tool; + +import java.util.concurrent.CountDownLatch; + +public class CountDownLatchDemo { + public static void main(String[] args) throws InterruptedException { + CountDownLatch countDownLatch = new CountDownLatch(5); + for(int i=0;i<5;i++){ + new Thread(new readNum(i,countDownLatch)).start(); + } + countDownLatch.await(); // 注意跟CyclicBarrier不同,这里在主线程await + System.out.println("==>各个子线程执行结束。。。。"); + System.out.println("==>主线程执行结束。。。。"); + } + + static class readNum implements Runnable{ + private int id; + private CountDownLatch latch; + public readNum(int id,CountDownLatch latch){ + this.id = id; + this.latch = latch; + } + @Override + public void run() { + synchronized (this){ + System.out.println("id:"+id+","+Thread.currentThread().getName()); + //latch.countDown(); + System.out.println("线程组任务"+id+"结束,其他任务继续"); + latch.countDown(); + } + } + } +} \ No newline at end of file diff --git a/03concurrency/0301/src/main/java/java0/conc0303/tool/CountDownLatchDemo2.java b/03concurrency/0301/src/main/java/java0/conc0303/tool/CountDownLatchDemo2.java new file mode 100644 index 00000000..62a1007a --- /dev/null +++ b/03concurrency/0301/src/main/java/java0/conc0303/tool/CountDownLatchDemo2.java @@ -0,0 +1,41 @@ +package java0.conc0303.tool; + + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +public class CountDownLatchDemo2 { + + private final static int threadCount = 200; + + public static void main(String[] args) throws Exception { + + ExecutorService exec = Executors.newCachedThreadPool(); + + final CountDownLatch countDownLatch = new CountDownLatch(threadCount); + + for (int i = 0; i < threadCount; i++) { + final int threadNum = i; + exec.execute(() -> { + try { + test(threadNum); + //countDownLatch.countDown(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + countDownLatch.countDown(); + } + }); + } + countDownLatch.await(); + System.out.println("==>所有程序员完成任务,项目顺利上线!"); + //exec.shutdown(); + } + + private static void test(int threadNum) throws Exception { + Thread.sleep(100); + System.out.println(String.format("程序员[%d]完成任务。。。", threadNum)); + Thread.sleep(100); + } +} \ No newline at end of file diff --git a/03concurrency/0301/src/main/java/java0/conc0303/tool/CyclicBarrierDemo.java b/03concurrency/0301/src/main/java/java0/conc0303/tool/CyclicBarrierDemo.java new file mode 100644 index 00000000..628d980d --- /dev/null +++ b/03concurrency/0301/src/main/java/java0/conc0303/tool/CyclicBarrierDemo.java @@ -0,0 +1,51 @@ +package java0.conc0303.tool; + +import java.util.concurrent.CyclicBarrier; + +public class CyclicBarrierDemo { + public static void main(String[] args) throws InterruptedException { + CyclicBarrier cyclicBarrier = new CyclicBarrier(5, new Runnable() { + @Override + public void run() { + System.out.println("回调>>"+Thread.currentThread().getName()); + System.out.println("回调>>线程组执行结束"); + System.out.println("==>各个子线程执行结束。。。。"); + } + }); + for (int i = 0; i < 5; i++) { + new Thread(new readNum(i,cyclicBarrier)).start(); + } + + // ==>>> + + + System.out.println("==>主线程执行结束。。。。"); + + //CyclicBarrier 可以重复利用, + // 这个是CountDownLatch做不到的 +// for (int i = 11; i < 16; i++) { +// new Thread(new readNum(i,cyclicBarrier)).start(); +// } + } + static class readNum implements Runnable{ + private int id; + private CyclicBarrier cyc; + public readNum(int id,CyclicBarrier cyc){ + this.id = id; + this.cyc = cyc; + } + @Override + public void run() { + synchronized (this){ + System.out.println("id:"+id+","+Thread.currentThread().getName()); + try { + cyc.await(); + System.out.println("线程组任务" + id + "结束,其他任务继续"); + //cyc.await(); // 注意跟CountDownLatch不同,这里在子线程await + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } +} \ No newline at end of file diff --git a/03concurrency/0301/src/main/java/java0/conc0303/tool/CyclicBarrierDemo2.java b/03concurrency/0301/src/main/java/java0/conc0303/tool/CyclicBarrierDemo2.java new file mode 100644 index 00000000..33e88260 --- /dev/null +++ b/03concurrency/0301/src/main/java/java0/conc0303/tool/CyclicBarrierDemo2.java @@ -0,0 +1,49 @@ +package java0.conc0303.tool; + +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; + +public class CyclicBarrierDemo2 { + public static void main(String[] args) { + int N = 4; + CyclicBarrier barrier = new CyclicBarrier(N); + + for(int i=0;i CyclicBarrier重用"); + + for(int i=0;i { + try { + semaphore.acquire(3); // 获取全部许可,退化成串行执行 + test(threadNum); + semaphore.release(3); // 释放多个许可 + } catch (Exception e) { + e.printStackTrace(); + } + }); + } + exec.shutdown(); + } + + private static void test(int threadNum) throws Exception { + System.out.println("id:"+threadNum+","+Thread.currentThread().getName()); + Thread.sleep(1000); + } + } diff --git a/03concurrency/0301/src/main/java/java0/conc0303/tool/SemaphoreDemo3.java b/03concurrency/0301/src/main/java/java0/conc0303/tool/SemaphoreDemo3.java new file mode 100644 index 00000000..3a02dc78 --- /dev/null +++ b/03concurrency/0301/src/main/java/java0/conc0303/tool/SemaphoreDemo3.java @@ -0,0 +1,115 @@ + +package java0.conc0303.tool; + +import java.util.concurrent.Semaphore; + +public class SemaphoreDemo3 { + + public static void main(String[] args) { + // 启动线程 + for (int i = 0; i <= 10; i++) { + // 生产者 + new Thread(new Producer()).start(); + // 消费者 + new Thread(new Consumer()).start(); + } + } + + + // 仓库 + static Warehouse buffer = new Warehouse(); + + static class Producer implements Runnable { + + static int num = 1; + + @Override + public void run() { + int n = num++; + while (true) { + try { + buffer.put(n); + System.out.println(">" + n); + // 速度较快。休息10毫秒 + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } + + + static class Consumer implements Runnable { + @Override + public void run() { + while (true) { + try { + System.out.println("<" + buffer.take()); + // 速度较慢,休息1000毫秒 + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } + + + static class Warehouse { + // 非满锁 + final Semaphore notFull = new Semaphore(10); + // 非空锁 + final Semaphore notEmpty = new Semaphore(0); + // 核心锁 + final Semaphore mutex = new Semaphore(1); + // 库存容量 + final Object[] items = new Object[10]; + int putptr, takeptr, count; + + + /** + * 放库存 + * + * @param obj + * @throws InterruptedException + */ + public void put(Object obj) throws InterruptedException { + notFull.acquire(); + mutex.acquire(); + items[putptr] = obj; + try { + if (++putptr == items.length) { + putptr = 0; + ++count; + } + } finally { + mutex.release(); + notEmpty.release(); + } + } + + /** + * 取库存 + * + * @return + * @throws InterruptedException + */ + public Object take() throws InterruptedException { + notEmpty.acquire(); + mutex.acquire(); + Object obj = items[takeptr]; + try { + if (++takeptr == items.length) { + takeptr = 0; + --count; + } + return obj; + } finally { + mutex.release(); + notFull.release(); + } + } + } + +} diff --git a/03concurrency/README.md b/03concurrency/README.md new file mode 100644 index 00000000..d2c34792 --- /dev/null +++ b/03concurrency/README.md @@ -0,0 +1,45 @@ +# 第3周作业 + + +## 作业内容 + +Week03 作业题目(周四): + +基础代码可以 fork: https://github.com/JavaCourse00/JavaCourseCodes + +02nio/nio02 文件夹下 + +实现以后,代码提交到 Github。 + +1.(必做)整合你上次作业的 httpclient/okhttp; + +2.(选做)使用 netty 实现后端 http 访问(代替上一步骤) + +Week03 作业题目(周六): + +1.(必做)实现过滤器。 +2.(选做)实现路由。 + +作业提交规范: + +1. 作业不要打包 ; +2. 同学们写在 md 文件里,而不要发 Word, Excel , PDF 等 ; +3. 代码类作业需提交完整 Java 代码,不能是片段; +4. 作业按课时分目录,仅上传作业相关,笔记分开记录; +5. 画图类作业提交可直接打开的图片或 md,手画的图手机拍照上传后太大,难以查看,推荐画图(推荐 PPT、Keynote); +6. 提交记录最好要标明明确的含义(比如第几题作业)。 + +## 操作步骤 + +### 1.(必做)整合你上次作业的 httpclient/okhttp + +1. 下载老师的项目: https://github.com/JavaCourse00/JavaCourseCodes +2. 解压, 拷贝其中 `02nio` 路径下的 `nio02` 目录到第三周的作业目录下。 +3. Idea或者Eclipse打开这个Maven项目。 +4. 增加自己的包名, 以做标识。 +5. 将第二周的作业代码整合进来: [homework02](../Week_02/homework02/) 中的代码和pom.xml依赖。 +6. 将 nio01 中的 HttpServer03 代码整合进来作为后端服务,改名为 [BackendServer](https://github.com/renfufei/JAVA-000/blob/main/Week_03/nio02/src/main/java/com/renfufei/homework03/BackendServer.java), 监听 8088 端口。 +7. 找到Netty官网: https://netty.io/wiki/user-guide-for-4.x.html +8. 参照官方示例, 编写自己的过滤器 [ProxyBizFilter](https://github.com/renfufei/JAVA-000/blob/main/Week_03/nio02/src/main/java/com/renfufei/homework03/ProxyBizFilter.java); +9. 可以加入到 [HttpInboundHandler.java](https://github.com/renfufei/JAVA-000/blob/main/Week_03/nio02/src/main/java/io/github/kimmking/gateway/inbound/); 【实际上应该加入到 [HttpInboundInitializer](./nio02/src/main/java/io/github/kimmking/gateway/inbound/HttpInboundInitializer.java) 的初始化方法中】。 +10. 修改 [HttpOutboundHandler](https://github.com/renfufei/JAVA-000/blob/main/Week_03/nio02/src/main/java/io/github/kimmking/gateway/outbound/httpclient4/HttpOutboundHandler.java) 类,集成自己写的第二周的作业代码; diff --git a/04fx/Java8inAction/.gitignore b/04fx/Java8inAction/.gitignore new file mode 100644 index 00000000..f546fd89 --- /dev/null +++ b/04fx/Java8inAction/.gitignore @@ -0,0 +1,15 @@ +/ThinkingJava/ThinkingJava.iml +/.idea +*.class +target/ + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* diff --git a/04fx/Java8inAction/pom.xml b/04fx/Java8inAction/pom.xml new file mode 100644 index 00000000..0306dea0 --- /dev/null +++ b/04fx/Java8inAction/pom.xml @@ -0,0 +1,54 @@ + + + + + + io.kimmking + 1.0-SNAPSHOT + 4.0.0 + java8inaction + + java8inaction + + + 1.8 + 1.8 + UTF-8 + UTF-8 + + + + + org.openjdk.jmh + jmh-core + 1.19 + + + org.openjdk.jmh + jmh-generator-annprocess + 1.19 + + + + junit + junit + 4.13 + + + + + + + + maven-compiler-plugin + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + + diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/appa/Author.java b/04fx/Java8inAction/src/main/java/lambdasinaction/appa/Author.java new file mode 100644 index 00000000..a99235b9 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/appa/Author.java @@ -0,0 +1,13 @@ +package lambdasinaction.appa; + +import java.lang.annotation.Repeatable; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Repeatable(Authors.class) +@Retention(RetentionPolicy.RUNTIME) +public @interface Author { + + String name(); + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/appa/Authors.java b/04fx/Java8inAction/src/main/java/lambdasinaction/appa/Authors.java new file mode 100644 index 00000000..f015d102 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/appa/Authors.java @@ -0,0 +1,11 @@ +package lambdasinaction.appa; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface Authors { + + Author[] value(); + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/appa/Book.java b/04fx/Java8inAction/src/main/java/lambdasinaction/appa/Book.java new file mode 100644 index 00000000..beefd807 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/appa/Book.java @@ -0,0 +1,17 @@ +package lambdasinaction.appa; + +import java.util.Arrays; + +@Author(name = "Raoul") +@Author(name = "Mario") +@Author(name = "Alan") +public class Book { + + public static void main(String[] args) { + Author[] authors = Book.class.getAnnotationsByType(Author.class); + Arrays.asList(authors).stream().forEach(a -> { + System.out.println(a.name()); + }); + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/appc/StreamForker.java b/04fx/Java8inAction/src/main/java/lambdasinaction/appc/StreamForker.java new file mode 100644 index 00000000..d8ed124c --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/appc/StreamForker.java @@ -0,0 +1,138 @@ +package lambdasinaction.appc; + +import java.util.*; +import java.util.concurrent.*; +import java.util.function.*; +import java.util.stream.*; + +/** + * Adapted from http://mail.openjdk.java.net/pipermail/lambda-dev/2013-November/011516.html + */ +public class StreamForker { + + private final Stream stream; + private final Map, ?>> forks = new HashMap<>(); + + public StreamForker(Stream stream) { + this.stream = stream; + } + + public StreamForker fork(Object key, Function, ?> f) { + forks.put(key, f); + return this; + } + + public Results getResults() { + ForkingStreamConsumer consumer = build(); + try { + stream.sequential().forEach(consumer); + } finally { + consumer.finish(); + } + return consumer; + } + + private ForkingStreamConsumer build() { + List> queues = new ArrayList<>(); + + Map> actions = + forks.entrySet().stream().reduce( + new HashMap>(), + (map, e) -> { + map.put(e.getKey(), + getOperationResult(queues, e.getValue())); + return map; + }, + (m1, m2) -> { + m1.putAll(m2); + return m1; + }); + + return new ForkingStreamConsumer<>(queues, actions); + } + + private Future getOperationResult(List> queues, Function, ?> f) { + BlockingQueue queue = new LinkedBlockingQueue<>(); + queues.add(queue); + Spliterator spliterator = new BlockingQueueSpliterator<>(queue); + Stream source = StreamSupport.stream(spliterator, false); + return CompletableFuture.supplyAsync( () -> f.apply(source) ); + } + + public static interface Results { + public R get(Object key); + } + + private static class ForkingStreamConsumer implements Consumer, Results { + static final Object END_OF_STREAM = new Object(); + + private final List> queues; + private final Map> actions; + + ForkingStreamConsumer(List> queues, Map> actions) { + this.queues = queues; + this.actions = actions; + } + + @Override + public void accept(T t) { + queues.forEach(q -> q.add(t)); + } + + @Override + public R get(Object key) { + try { + return ((Future) actions.get(key)).get(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + void finish() { + accept((T) END_OF_STREAM); + } + } + + private static class BlockingQueueSpliterator implements Spliterator { + private final BlockingQueue q; + + BlockingQueueSpliterator(BlockingQueue q) { + this.q = q; + } + + @Override + public boolean tryAdvance(Consumer action) { + T t; + while (true) { + try { + t = q.take(); + break; + } + catch (InterruptedException e) { + } + } + + if (t != ForkingStreamConsumer.END_OF_STREAM) { + action.accept(t); + return true; + } + + return false; + } + + @Override + public Spliterator trySplit() { + return null; + } + + @Override + public long estimateSize() { + return 0; + } + + @Override + public int characteristics() { + return 0; + } + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/appc/StreamForkerExample.java b/04fx/Java8inAction/src/main/java/lambdasinaction/appc/StreamForkerExample.java new file mode 100644 index 00000000..6c8b47c4 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/appc/StreamForkerExample.java @@ -0,0 +1,38 @@ +package lambdasinaction.appc; + +import lambdasinaction.chap6.*; + +import static java.util.stream.Collectors.*; +import static lambdasinaction.chap6.Dish.menu; + +import java.util.*; +import java.util.stream.*; + +public class StreamForkerExample { + + public static void main(String[] args) throws Exception { + processMenu(); + } + + private static void processMenu() { + Stream menuStream = menu.stream(); + + StreamForker.Results results = new StreamForker(menuStream) + .fork("shortMenu", s -> s.map(Dish::getName).collect(joining(", "))) + .fork("totalCalories", s -> s.mapToInt(Dish::getCalories).sum()) + .fork("mostCaloricDish", s -> s.reduce((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2) + .get()) + .fork("dishesByType", s -> s.collect(groupingBy(Dish::getType))) + .getResults(); + + String shortMeny = results.get("shortMenu"); + int totalCalories = results.get("totalCalories"); + Dish mostCaloricDish = results.get("mostCaloricDish"); + Map> dishesByType = results.get("dishesByType"); + + System.out.println("Short menu: " + shortMeny); + System.out.println("Total calories: " + totalCalories); + System.out.println("Most caloric dish: " + mostCaloricDish); + System.out.println("Dishes by type: " + dishesByType); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/appd/InnerClass.java b/04fx/Java8inAction/src/main/java/lambdasinaction/appd/InnerClass.java new file mode 100644 index 00000000..404b7b08 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/appd/InnerClass.java @@ -0,0 +1,12 @@ +package lambdasinaction.appd; + +import java.util.function.Function; + +public class InnerClass { + Function f = new Function() { + @Override + public String apply(Object obj) { + return obj.toString(); + } + }; +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/appd/Lambda.java b/04fx/Java8inAction/src/main/java/lambdasinaction/appd/Lambda.java new file mode 100644 index 00000000..490dcc0d --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/appd/Lambda.java @@ -0,0 +1,8 @@ +package lambdasinaction.appd; + +import java.util.function.Function; + +public class Lambda { + + Function f = obj -> obj.toString(); +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap1/FilteringApples.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap1/FilteringApples.java new file mode 100644 index 00000000..51afc8cb --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap1/FilteringApples.java @@ -0,0 +1,107 @@ +package lambdasinaction.chap1; + +import java.util.*; +import java.util.function.Predicate; + +public class FilteringApples{ + + public static void main(String ... args){ + + List inventory = Arrays.asList(new Apple(80,"green"), + new Apple(155, "green"), + new Apple(120, "red")); + + // [Apple{color='green', weight=80}, Apple{color='green', weight=155}] + List greenApples = filterApples(inventory, FilteringApples::isGreenApple); + System.out.println(greenApples); + + // [Apple{color='green', weight=155}] + List heavyApples = filterApples(inventory, FilteringApples::isHeavyApple); + System.out.println(heavyApples); + + // [Apple{color='green', weight=80}, Apple{color='green', weight=155}] + List greenApples2 = filterApples(inventory, (Apple a) -> "green".equals(a.getColor())); + System.out.println(greenApples2); + + // [Apple{color='green', weight=155}] + List heavyApples2 = filterApples(inventory, (Apple a) -> a.getWeight() > 150); + System.out.println(heavyApples2); + + // [] + List weirdApples = filterApples(inventory, (Apple a) -> a.getWeight() < 80 || + "brown".equals(a.getColor())); + System.out.println(weirdApples); + } + + public static List filterGreenApples(List inventory){ + List result = new ArrayList<>(); + for (Apple apple: inventory){ + if ("green".equals(apple.getColor())) { + result.add(apple); + } + } + return result; + } + + public static List filterHeavyApples(List inventory){ + List result = new ArrayList<>(); + for (Apple apple: inventory){ + if (apple.getWeight() > 150) { + result.add(apple); + } + } + return result; + } + + public static boolean isGreenApple(Apple apple) { + return "green".equals(apple.getColor()); + } + + public static boolean isHeavyApple(Apple apple) { + return apple.getWeight() > 150; + } + + public static List filterApples(List inventory, Predicate p){ + List result = new ArrayList<>(); + for(Apple apple : inventory){ + if(p.test(apple)){ + result.add(apple); + } + } + return result; + } + + public static class Apple { + private int weight = 0; + private String color = ""; + + public Apple(int weight, String color){ + this.weight = weight; + this.color = color; + } + + public Integer getWeight() { + return weight; + } + + public void setWeight(Integer weight) { + this.weight = weight; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public String toString() { + return "Apple{" + + "color='" + color + '\'' + + ", weight=" + weight + + '}'; + } + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/Car.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/Car.java new file mode 100644 index 00000000..36f00727 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/Car.java @@ -0,0 +1,12 @@ +package lambdasinaction.chap10; + +import java.util.*; + +public class Car { + + private Optional insurance; + + public Optional getInsurance() { + return insurance; + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/Insurance.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/Insurance.java new file mode 100644 index 00000000..69409686 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/Insurance.java @@ -0,0 +1,10 @@ +package lambdasinaction.chap10; + +public class Insurance { + + private String name; + + public String getName() { + return name; + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/OperationsWithOptional.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/OperationsWithOptional.java new file mode 100644 index 00000000..da5b60d5 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/OperationsWithOptional.java @@ -0,0 +1,25 @@ +package lambdasinaction.chap10; + +import java.util.*; + +import static java.util.Optional.of; +import static java.util.Optional.empty; + +public class OperationsWithOptional { + + public static void main(String... args) { + System.out.println(max(of(3), of(5))); + System.out.println(max(empty(), of(5))); + + Optional opt1 = of(5); + //Optional opt2 = opt1.or(() -> of(4)); + +// System.out.println( +// of(5).or(() -> of(4)) +// ); + } + + public static final Optional max(Optional i, Optional j) { + return i.flatMap(a -> j.map(b -> Math.max(a, b))); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/OptionalMain.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/OptionalMain.java new file mode 100644 index 00000000..b3f094c4 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/OptionalMain.java @@ -0,0 +1,24 @@ +package lambdasinaction.chap10; + +import java.util.*; + +import static java.util.stream.Collectors.toSet; + +public class OptionalMain { + + public String getCarInsuranceName(Optional person) { + return person.flatMap(Person::getCar) + .flatMap(Car::getInsurance) + .map(Insurance::getName) + .orElse("Unknown"); + } + + public Set getCarInsuranceNames(List persons) { + return persons.stream() + .map(Person::getCar) + .map(optCar -> optCar.flatMap(Car::getInsurance)) + .map(optInsurance -> optInsurance.map(Insurance::getName).get()) +// .flatMap(Optional::stream) + .collect(toSet()); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/Person.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/Person.java new file mode 100644 index 00000000..5e84e552 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/Person.java @@ -0,0 +1,12 @@ +package lambdasinaction.chap10; + +import java.util.*; + +public class Person { + + private Optional car; + + public Optional getCar() { + return car; + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/ReadPositiveIntParam.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/ReadPositiveIntParam.java new file mode 100644 index 00000000..6b32a46f --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap10/ReadPositiveIntParam.java @@ -0,0 +1,58 @@ +package lambdasinaction.chap10; + + +import org.junit.Test; + +import java.util.*; + +import static java.util.Optional.*; +import static org.junit.Assert.assertEquals; + +public class ReadPositiveIntParam { + + @Test + public void testMap() { + Properties props = new Properties(); + props.setProperty("a", "5"); + props.setProperty("b", "true"); + props.setProperty("c", "-3"); + + assertEquals(5, readDurationImperative(props, "a")); + assertEquals(0, readDurationImperative(props, "b")); + assertEquals(0, readDurationImperative(props, "c")); + assertEquals(0, readDurationImperative(props, "d")); + + assertEquals(5, readDurationWithOptional(props, "a")); + assertEquals(0, readDurationWithOptional(props, "b")); + assertEquals(0, readDurationWithOptional(props, "c")); + assertEquals(0, readDurationWithOptional(props, "d")); + } + + public static int readDurationImperative(Properties props, String name) { + String value = props.getProperty(name); + if (value != null) { + try { + int i = Integer.parseInt(value); + if (i > 0) { + return i; + } + } catch (NumberFormatException nfe) { } + } + return 0; + } + + public static int readDurationWithOptional(Properties props, String name) { + return ofNullable(props.getProperty(name)) + .flatMap(ReadPositiveIntParam::s2i) + .filter(i -> i > 0).orElse(0); + } + + public static Optional s2i(String s) { + try { + return of(Integer.parseInt(s)); + } catch (NumberFormatException e) { + return empty(); + } + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/AsyncShop.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/AsyncShop.java new file mode 100644 index 00000000..9d537368 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/AsyncShop.java @@ -0,0 +1,42 @@ +package lambdasinaction.chap11; + +import static lambdasinaction.chap11.Util.delay; +import static lambdasinaction.chap11.Util.format; + +import java.util.Random; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Future; + +public class AsyncShop { + + private final String name; + private final Random random; + + public AsyncShop(String name) { + this.name = name; + random = new Random(name.charAt(0) * name.charAt(1) * name.charAt(2)); + } + + public Future getPrice(String product) { +/* + CompletableFuture futurePrice = new CompletableFuture<>(); + new Thread( () -> { + try { + double price = calculatePrice(product); + futurePrice.complete(price); + } catch (Exception ex) { + futurePrice.completeExceptionally(ex); + } + }).start(); + return futurePrice; +*/ + return CompletableFuture.supplyAsync(() -> calculatePrice(product)); + } + + private double calculatePrice(String product) { + delay(); + if (true) throw new RuntimeException("product not available"); + return format(random.nextDouble() * product.charAt(0) + product.charAt(1)); + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/AsyncShopClient.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/AsyncShopClient.java new file mode 100644 index 00000000..9a78f827 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/AsyncShopClient.java @@ -0,0 +1,21 @@ +package lambdasinaction.chap11; + +import java.util.concurrent.Future; + +public class AsyncShopClient { + + public static void main(String[] args) { + AsyncShop shop = new AsyncShop("BestShop"); + long start = System.nanoTime(); + Future futurePrice = shop.getPrice("myPhone"); + long incocationTime = ((System.nanoTime() - start) / 1_000_000); + System.out.println("Invocation returned after " + incocationTime + " msecs"); + try { + System.out.println("Price is " + futurePrice.get()); + } catch (Exception e) { + throw new RuntimeException(e); + } + long retrivalTime = ((System.nanoTime() - start) / 1_000_000); + System.out.println("Price returned after " + retrivalTime + " msecs"); + } +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/BestPriceFinder.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/BestPriceFinder.java new file mode 100644 index 00000000..51412e93 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/BestPriceFinder.java @@ -0,0 +1,70 @@ +package lambdasinaction.chap11; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadFactory; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class BestPriceFinder { + + private final List shops = Arrays.asList(new Shop("BestPrice"), + new Shop("LetsSaveBig"), + new Shop("MyFavoriteShop"), + new Shop("BuyItAll"), + new Shop("ShopEasy")); + + private final Executor executor = Executors.newFixedThreadPool(shops.size(), new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread t = new Thread(r); + t.setDaemon(true); + return t; + } + }); + + public List findPricesSequential(String product) { + return shops.stream() + .map(shop -> shop.getPrice(product)) + .map(Quote::parse) + .map(Discount::applyDiscount) + .collect(Collectors.toList()); + } + + public List findPricesParallel(String product) { + return shops.parallelStream() + .map(shop -> shop.getPrice(product)) + .map(Quote::parse) + .map(Discount::applyDiscount) + .collect(Collectors.toList()); + } + + public List findPricesFuture(String product) { + List> priceFutures = findPricesStream(product) + .collect(Collectors.>toList()); + + return priceFutures.stream() + .map(CompletableFuture::join) + .collect(Collectors.toList()); + } + + public Stream> findPricesStream(String product) { + return shops.stream() + .map(shop -> CompletableFuture.supplyAsync(() -> shop.getPrice(product), executor)) + .map(future -> future.thenApply(Quote::parse)) + .map(future -> future.thenCompose(quote -> CompletableFuture.supplyAsync(() -> Discount.applyDiscount(quote), executor))); + } + + public void printPricesStream(String product) { + long start = System.nanoTime(); + CompletableFuture[] futures = findPricesStream(product) + .map(f -> f.thenAccept(s -> System.out.println(s + " (done in " + ((System.nanoTime() - start) / 1_000_000) + " msecs)"))) + .toArray(size -> new CompletableFuture[size]); + CompletableFuture.allOf(futures).join(); + System.out.println("All shops have now responded in " + ((System.nanoTime() - start) / 1_000_000) + " msecs"); + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/BestPriceFinderMain.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/BestPriceFinderMain.java new file mode 100644 index 00000000..43ec8fba --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/BestPriceFinderMain.java @@ -0,0 +1,24 @@ +package lambdasinaction.chap11; + +import java.util.List; +import java.util.function.Supplier; + +public class BestPriceFinderMain { + + private static BestPriceFinder bestPriceFinder = new BestPriceFinder(); + + public static void main(String[] args) { + execute("sequential", () -> bestPriceFinder.findPricesSequential("myPhone27S")); + execute("parallel", () -> bestPriceFinder.findPricesParallel("myPhone27S")); + execute("composed CompletableFuture", () -> bestPriceFinder.findPricesFuture("myPhone27S")); + bestPriceFinder.printPricesStream("myPhone27S"); + } + + private static void execute(String msg, Supplier> s) { + long start = System.nanoTime(); + System.out.println(s.get()); + long duration = (System.nanoTime() - start) / 1_000_000; + System.out.println(msg + " done in " + duration + " msecs"); + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/Discount.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/Discount.java new file mode 100644 index 00000000..5624abd1 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/Discount.java @@ -0,0 +1,25 @@ +package lambdasinaction.chap11; + +import static lambdasinaction.chap11.Util.delay; +import static lambdasinaction.chap11.Util.format; + +public class Discount { + + public enum Code { + NONE(0), SILVER(5), GOLD(10), PLATINUM(15), DIAMOND(20); + + private final int percentage; + + Code(int percentage) { + this.percentage = percentage; + } + } + + public static String applyDiscount(Quote quote) { + return quote.getShopName() + " price is " + Discount.apply(quote.getPrice(), quote.getDiscountCode()); + } + private static double apply(double price, Code code) { + delay(); + return format(price * (100 - code.percentage) / 100); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/ExchangeService.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/ExchangeService.java new file mode 100644 index 00000000..3f98a7e3 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/ExchangeService.java @@ -0,0 +1,26 @@ +package lambdasinaction.chap11; + +import static lambdasinaction.chap11.Util.delay; + +public class ExchangeService { + + public enum Money { + USD(1.0), EUR(1.35387), GBP(1.69715), CAD(.92106), MXN(.07683); + + private final double rate; + + Money(double rate) { + this.rate = rate; + } + } + + public static double getRate(Money source, Money destination) { + return getRateWithDelay(source, destination); + } + + private static double getRateWithDelay(Money source, Money destination) { + delay(); + return destination.rate / source.rate; + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/Quote.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/Quote.java new file mode 100644 index 00000000..72dd81bf --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/Quote.java @@ -0,0 +1,34 @@ +package lambdasinaction.chap11; + +public class Quote { + + private final String shopName; + private final double price; + private final Discount.Code discountCode; + + public Quote(String shopName, double price, Discount.Code discountCode) { + this.shopName = shopName; + this.price = price; + this.discountCode = discountCode; + } + + public static Quote parse(String s) { + String[] split = s.split(":"); + String shopName = split[0]; + double price = Double.parseDouble(split[1]); + Discount.Code discountCode = Discount.Code.valueOf(split[2]); + return new Quote(shopName, price, discountCode); + } + + public String getShopName() { + return shopName; + } + + public double getPrice() { + return price; + } + + public Discount.Code getDiscountCode() { + return discountCode; + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/Shop.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/Shop.java new file mode 100644 index 00000000..bf0446fe --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/Shop.java @@ -0,0 +1,32 @@ +package lambdasinaction.chap11; + +import static lambdasinaction.chap11.Util.delay; +import static lambdasinaction.chap11.Util.format; + +import java.util.Random; + +public class Shop { + + private final String name; + private final Random random; + + public Shop(String name) { + this.name = name; + random = new Random(name.charAt(0) * name.charAt(1) * name.charAt(2)); + } + + public String getPrice(String product) { + double price = calculatePrice(product); + Discount.Code code = Discount.Code.values()[random.nextInt(Discount.Code.values().length)]; + return name + ":" + price + ":" + code; + } + + public double calculatePrice(String product) { + delay(); + return format(random.nextDouble() * product.charAt(0) + product.charAt(1)); + } + + public String getName() { + return name; + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/Util.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/Util.java new file mode 100644 index 00000000..a72c4d33 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/Util.java @@ -0,0 +1,46 @@ +package lambdasinaction.chap11; + +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.util.List; +import java.util.Locale; +import java.util.Random; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; + +public class Util { + + private static final Random RANDOM = new Random(0); + private static final DecimalFormat formatter = new DecimalFormat("#.##", new DecimalFormatSymbols(Locale.US)); + + public static void delay() { + int delay = 1000; + //int delay = 500 + RANDOM.nextInt(2000); + try { + Thread.sleep(delay); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + + public static double format(double number) { + synchronized (formatter) { + return new Double(formatter.format(number)); + } + } + + public static CompletableFuture> sequence(List> futures) { +/* + CompletableFuture allDoneFuture = + CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])); + return allDoneFuture.thenApply(v -> + futures.stream(). + map(future -> future.join()). + collect(Collectors.toList()) + ); +*/ + return CompletableFuture.supplyAsync(() -> futures.stream(). + map(future -> future.join()). + collect(Collectors.toList())); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/v1/BestPriceFinder.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/v1/BestPriceFinder.java new file mode 100644 index 00000000..1bbb6030 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/v1/BestPriceFinder.java @@ -0,0 +1,163 @@ +package lambdasinaction.chap11.v1; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ThreadFactory; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import lambdasinaction.chap11.ExchangeService; +import lambdasinaction.chap11.ExchangeService.Money; + +public class BestPriceFinder { + + private final List shops = Arrays.asList(new Shop("BestPrice"), + new Shop("LetsSaveBig"), + new Shop("MyFavoriteShop"), + new Shop("BuyItAll")/*, + new Shop("ShopEasy")*/); + + private final Executor executor = Executors.newFixedThreadPool(shops.size(), new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + Thread t = new Thread(r); + t.setDaemon(true); + return t; + } + }); + + public List findPricesSequential(String product) { + return shops.stream() + .map(shop -> shop.getName() + " price is " + shop.getPrice(product)) + .collect(Collectors.toList()); + } + + public List findPricesParallel(String product) { + return shops.parallelStream() + .map(shop -> shop.getName() + " price is " + shop.getPrice(product)) + .collect(Collectors.toList()); + } + + public List findPricesFuture(String product) { + List> priceFutures = + shops.stream() + .map(shop -> CompletableFuture.supplyAsync(() -> shop.getName() + " price is " + + shop.getPrice(product), executor)) + .collect(Collectors.toList()); + + List prices = priceFutures.stream() + .map(CompletableFuture::join) + .collect(Collectors.toList()); + return prices; + } + + public List findPricesInUSD(String product) { + List> priceFutures = new ArrayList<>(); + for (Shop shop : shops) { + // Start of Listing 10.20. + // Only the type of futurePriceInUSD has been changed to + // CompletableFuture so that it is compatible with the + // CompletableFuture::join operation below. + CompletableFuture futurePriceInUSD = + CompletableFuture.supplyAsync(() -> shop.getPrice(product)) + .thenCombine( + CompletableFuture.supplyAsync( + () -> ExchangeService.getRate(Money.EUR, Money.USD)), + (price, rate) -> price * rate + ); + priceFutures.add(futurePriceInUSD); + } + // Drawback: The shop is not accessible anymore outside the loop, + // so the getName() call below has been commented out. + List prices = priceFutures + .stream() + .map(CompletableFuture::join) + .map(price -> /*shop.getName() +*/ " price is " + price) + .collect(Collectors.toList()); + return prices; + } + + public List findPricesInUSDJava7(String product) { + ExecutorService executor = Executors.newCachedThreadPool(); + List> priceFutures = new ArrayList<>(); + for (Shop shop : shops) { + final Future futureRate = executor.submit(new Callable() { + public Double call() { + return ExchangeService.getRate(Money.EUR, Money.USD); + } + }); + Future futurePriceInUSD = executor.submit(new Callable() { + public Double call() { + try { + double priceInEUR = shop.getPrice(product); + return priceInEUR * futureRate.get(); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e.getMessage(), e); + } + } + }); + priceFutures.add(futurePriceInUSD); + } + List prices = new ArrayList<>(); + for (Future priceFuture : priceFutures) { + try { + prices.add(/*shop.getName() +*/ " price is " + priceFuture.get()); + } + catch (ExecutionException | InterruptedException e) { + e.printStackTrace(); + } + } + return prices; + } + + public List findPricesInUSD2(String product) { + List> priceFutures = new ArrayList<>(); + for (Shop shop : shops) { + // Here, an extra operation has been added so that the shop name + // is retrieved within the loop. As a result, we now deal with + // CompletableFuture instances. + CompletableFuture futurePriceInUSD = + CompletableFuture.supplyAsync(() -> shop.getPrice(product)) + .thenCombine( + CompletableFuture.supplyAsync( + () -> ExchangeService.getRate(Money.EUR, Money.USD)), + (price, rate) -> price * rate + ).thenApply(price -> shop.getName() + " price is " + price); + priceFutures.add(futurePriceInUSD); + } + List prices = priceFutures + .stream() + .map(CompletableFuture::join) + .collect(Collectors.toList()); + return prices; + } + + public List findPricesInUSD3(String product) { + // Here, the for loop has been replaced by a mapping function... + Stream> priceFuturesStream = shops + .stream() + .map(shop -> CompletableFuture + .supplyAsync(() -> shop.getPrice(product)) + .thenCombine( + CompletableFuture.supplyAsync(() -> ExchangeService.getRate(Money.EUR, Money.USD)), + (price, rate) -> price * rate) + .thenApply(price -> shop.getName() + " price is " + price)); + // However, we should gather the CompletableFutures into a List so that the asynchronous + // operations are triggered before being "joined." + List> priceFutures = priceFuturesStream.collect(Collectors.toList()); + List prices = priceFutures + .stream() + .map(CompletableFuture::join) + .collect(Collectors.toList()); + return prices; + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/v1/BestPriceFinderMain.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/v1/BestPriceFinderMain.java new file mode 100644 index 00000000..5a74161a --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/v1/BestPriceFinderMain.java @@ -0,0 +1,25 @@ +package lambdasinaction.chap11.v1; + +import java.util.List; +import java.util.function.Supplier; + +public class BestPriceFinderMain { + + private static BestPriceFinder bestPriceFinder = new BestPriceFinder(); + + public static void main(String[] args) { + execute("sequential", () -> bestPriceFinder.findPricesSequential("myPhone27S")); + execute("parallel", () -> bestPriceFinder.findPricesParallel("myPhone27S")); + execute("composed CompletableFuture", () -> bestPriceFinder.findPricesFuture("myPhone27S")); + execute("combined USD CompletableFuture", () -> bestPriceFinder.findPricesInUSD("myPhone27S")); + execute("combined USD CompletableFuture v2", () -> bestPriceFinder.findPricesInUSD2("myPhone27S")); + execute("combined USD CompletableFuture v3", () -> bestPriceFinder.findPricesInUSD3("myPhone27S")); + } + + private static void execute(String msg, Supplier> s) { + long start = System.nanoTime(); + System.out.println(s.get()); + long duration = (System.nanoTime() - start) / 1_000_000; + System.out.println(msg + " done in " + duration + " msecs"); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/v1/Shop.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/v1/Shop.java new file mode 100644 index 00000000..cd3da975 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/v1/Shop.java @@ -0,0 +1,41 @@ +package lambdasinaction.chap11.v1; + +import static lambdasinaction.chap11.Util.delay; + +import java.util.Random; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Future; + +public class Shop { + + private final String name; + private final Random random; + + public Shop(String name) { + this.name = name; + random = new Random(name.charAt(0) * name.charAt(1) * name.charAt(2)); + } + + public double getPrice(String product) { + return calculatePrice(product); + } + + private double calculatePrice(String product) { + delay(); + return random.nextDouble() * product.charAt(0) + product.charAt(1); + } + + public Future getPriceAsync(String product) { + CompletableFuture futurePrice = new CompletableFuture<>(); + new Thread( () -> { + double price = calculatePrice(product); + futurePrice.complete(price); + }).start(); + return futurePrice; + } + + public String getName() { + return name; + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/v1/ShopMain.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/v1/ShopMain.java new file mode 100644 index 00000000..ea1a19c1 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap11/v1/ShopMain.java @@ -0,0 +1,32 @@ +package lambdasinaction.chap11.v1; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +public class ShopMain { + + public static void main(String[] args) { + Shop shop = new Shop("BestShop"); + long start = System.nanoTime(); + Future futurePrice = shop.getPriceAsync("my favorite product"); + long invocationTime = ((System.nanoTime() - start) / 1_000_000); + System.out.println("Invocation returned after " + invocationTime + + " msecs"); + // Do some more tasks, like querying other shops + doSomethingElse(); + // while the price of the product is being calculated + try { + double price = futurePrice.get(); + System.out.printf("Price is %.2f%n", price); + } catch (ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + long retrievalTime = ((System.nanoTime() - start) / 1_000_000); + System.out.println("Price returned after " + retrievalTime + " msecs"); + } + + private static void doSomethingElse() { + System.out.println("Doing something else..."); + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap12/DateTimeExamples.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap12/DateTimeExamples.java new file mode 100644 index 00000000..46895cc5 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap12/DateTimeExamples.java @@ -0,0 +1,157 @@ +package lambdasinaction.chap12; + +import static java.time.temporal.TemporalAdjusters.lastDayOfMonth; +import static java.time.temporal.TemporalAdjusters.nextOrSame; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.time.DayOfWeek; +import java.time.Duration; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.Month; +import java.time.chrono.JapaneseDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.temporal.ChronoField; +import java.time.temporal.ChronoUnit; +import java.time.temporal.Temporal; +import java.time.temporal.TemporalAdjuster; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; + +public class DateTimeExamples { + + private static final ThreadLocal formatters = new ThreadLocal() { + protected DateFormat initialValue() { + return new SimpleDateFormat("dd-MMM-yyyy"); + } + }; + + public static void main(String[] args) { + useOldDate(); + useLocalDate(); + useTemporalAdjuster(); + useDateFormatter(); + } + + private static void useOldDate() { + Date date = new Date(114, 2, 18); + System.out.println(date); + + System.out.println(formatters.get().format(date)); + + Calendar calendar = Calendar.getInstance(); + calendar.set(2014, Calendar.FEBRUARY, 18); + System.out.println(calendar); + } + + private static void useLocalDate() { + LocalDate date = LocalDate.of(2014, 3, 18); + int year = date.getYear(); // 2014 + Month month = date.getMonth(); // MARCH + int day = date.getDayOfMonth(); // 18 + DayOfWeek dow = date.getDayOfWeek(); // TUESDAY + int len = date.lengthOfMonth(); // 31 (days in March) + boolean leap = date.isLeapYear(); // false (not a leap year) + System.out.println(date); + + int y = date.get(ChronoField.YEAR); + int m = date.get(ChronoField.MONTH_OF_YEAR); + int d = date.get(ChronoField.DAY_OF_MONTH); + + LocalTime time = LocalTime.of(13, 45, 20); // 13:45:20 + int hour = time.getHour(); // 13 + int minute = time.getMinute(); // 45 + int second = time.getSecond(); // 20 + System.out.println(time); + + LocalDateTime dt1 = LocalDateTime.of(2014, Month.MARCH, 18, 13, 45, 20); // 2014-03-18T13:45 + LocalDateTime dt2 = LocalDateTime.of(date, time); + LocalDateTime dt3 = date.atTime(13, 45, 20); + LocalDateTime dt4 = date.atTime(time); + LocalDateTime dt5 = time.atDate(date); + System.out.println(dt1); + + LocalDate date1 = dt1.toLocalDate(); + System.out.println(date1); + LocalTime time1 = dt1.toLocalTime(); + System.out.println(time1); + + Instant instant = Instant.ofEpochSecond(44 * 365 * 86400); + Instant now = Instant.now(); + + Duration d1 = Duration.between(LocalTime.of(13, 45, 10), time); + Duration d2 = Duration.between(instant, now); + System.out.println(d1.getSeconds()); + System.out.println(d2.getSeconds()); + + Duration threeMinutes = Duration.of(3, ChronoUnit.MINUTES); + System.out.println(threeMinutes); + + JapaneseDate japaneseDate = JapaneseDate.from(date); + System.out.println(japaneseDate); + } + + private static void useTemporalAdjuster() { + LocalDate date = LocalDate.of(2014, 3, 18); + date = date.with(nextOrSame(DayOfWeek.SUNDAY)); + System.out.println(date); + date = date.with(lastDayOfMonth()); + System.out.println(date); + + date = date.with(new NextWorkingDay()); + System.out.println(date); + date = date.with(nextOrSame(DayOfWeek.FRIDAY)); + System.out.println(date); + date = date.with(new NextWorkingDay()); + System.out.println(date); + + date = date.with(nextOrSame(DayOfWeek.FRIDAY)); + System.out.println(date); + date = date.with(temporal -> { + DayOfWeek dow = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK)); + int dayToAdd = 1; + if (dow == DayOfWeek.FRIDAY) dayToAdd = 3; + if (dow == DayOfWeek.SATURDAY) dayToAdd = 2; + return temporal.plus(dayToAdd, ChronoUnit.DAYS); + }); + System.out.println(date); + } + + private static class NextWorkingDay implements TemporalAdjuster { + @Override + public Temporal adjustInto(Temporal temporal) { + DayOfWeek dow = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK)); + int dayToAdd = 1; + if (dow == DayOfWeek.FRIDAY) dayToAdd = 3; + if (dow == DayOfWeek.SATURDAY) dayToAdd = 2; + return temporal.plus(dayToAdd, ChronoUnit.DAYS); + } + } + + private static void useDateFormatter() { + LocalDate date = LocalDate.of(2014, 3, 18); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); + DateTimeFormatter italianFormatter = DateTimeFormatter.ofPattern("d. MMMM yyyy", Locale.ITALIAN); + + System.out.println(date.format(DateTimeFormatter.ISO_LOCAL_DATE)); + System.out.println(date.format(formatter)); + System.out.println(date.format(italianFormatter)); + + DateTimeFormatter complexFormatter = new DateTimeFormatterBuilder() + .appendText(ChronoField.DAY_OF_MONTH) + .appendLiteral(". ") + .appendText(ChronoField.MONTH_OF_YEAR) + .appendLiteral(" ") + .appendText(ChronoField.YEAR) + .parseCaseInsensitive() + .toFormatter(Locale.ITALIAN); + + System.out.println(date.format(complexFormatter)); + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap13/Recursion.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap13/Recursion.java new file mode 100644 index 00000000..77f66d57 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap13/Recursion.java @@ -0,0 +1,39 @@ +package lambdasinaction.chap13; + +import java.util.stream.LongStream; + + +public class Recursion { + + public static void main(String[] args) { + System.out.println(factorialIterative(5)); + System.out.println(factorialRecursive(5)); + System.out.println(factorialStreams(5)); + System.out.println(factorialTailRecursive(5)); + } + + public static int factorialIterative(int n) { + int r = 1; + for (int i = 1; i <= n; i++) { + r*=i; + } + return r; + } + + public static long factorialRecursive(long n) { + return n == 1 ? 1 : n*factorialRecursive(n-1); + } + + public static long factorialStreams(long n){ + return LongStream.rangeClosed(1, n) + .reduce(1, (long a, long b) -> a * b); + } + + public static long factorialTailRecursive(long n) { + return factorialHelper(1, n); + } + + public static long factorialHelper(long acc, long n) { + return n == 1 ? acc : factorialHelper(acc * n, n-1); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap13/SubsetsMain.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap13/SubsetsMain.java new file mode 100644 index 00000000..02b51998 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap13/SubsetsMain.java @@ -0,0 +1,44 @@ +package lambdasinaction.chap13; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class SubsetsMain { + + public static void main(String[] args) { + List> subs = subsets(Arrays.asList(1, 4, 9)); + subs.forEach(System.out::println); + } + + public static List> subsets(List l) { + if (l.isEmpty()) { + List> ans = new ArrayList<>(); + ans.add(Collections.emptyList()); + return ans; + } + Integer first = l.get(0); + List rest = l.subList(1,l.size()); + List> subans = subsets(rest); + List> subans2 = insertAll(first, subans); + return concat(subans, subans2); + } + + public static List> insertAll(Integer first, List> lists) { + List> result = new ArrayList<>(); + for (List l : lists) { + List copyList = new ArrayList<>(); + copyList.add(first); + copyList.addAll(l); + result.add(copyList); + } + return result; + } + + static List> concat(List> a, List> b) { + List> r = new ArrayList<>(a); + r.addAll(b); + return r; + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/Combinators.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/Combinators.java new file mode 100644 index 00000000..7ae5dfd2 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/Combinators.java @@ -0,0 +1,18 @@ +package lambdasinaction.chap14; + +import java.util.function.Function; + +public class Combinators { + + public static void main(String[] args) { + System.out.println(repeat(3, (Integer x) -> 2 * x).apply(10)); + } + + static Function compose(Function g, Function f) { + return x -> g.apply(f.apply(x)); + } + + static Function repeat(int n, Function f) { + return n == 0 ? x -> x : compose(f, repeat(n - 1, f)); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/Currying.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/Currying.java new file mode 100644 index 00000000..f17ac928 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/Currying.java @@ -0,0 +1,32 @@ +package lambdasinaction.chap14; + +import java.util.function.DoubleUnaryOperator; + + +public class Currying { + + public static void main(String[] args) { + DoubleUnaryOperator convertCtoF = curriedConverter(9.0/5, 32); + DoubleUnaryOperator convertUSDtoGBP = curriedConverter(0.6, 0); + DoubleUnaryOperator convertKmtoMi = curriedConverter(0.6214, 0); + + System.out.println(convertCtoF.applyAsDouble(24)); + System.out.println(convertUSDtoGBP.applyAsDouble(100)); + System.out.println(convertKmtoMi.applyAsDouble(20)); + + DoubleUnaryOperator convertFtoC = expandedCurriedConverter(-32, 5.0/9, 0); + System.out.println(convertFtoC.applyAsDouble(98.6)); + } + + static double converter(double x, double y, double z) { + return x * y + z; + } + + static DoubleUnaryOperator curriedConverter(double y, double z) { + return (double x) -> x * y + z; + } + + static DoubleUnaryOperator expandedCurriedConverter(double w, double y, double z) { + return (double x) -> (x + w) * y + z; + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/LazyLists.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/LazyLists.java new file mode 100644 index 00000000..658e0940 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/LazyLists.java @@ -0,0 +1,129 @@ +package lambdasinaction.chap14; + +import java.util.function.Supplier; +import java.util.function.Predicate; + +public class LazyLists { + + public static void main(String[] args) { + MyList l = new MyLinkedList<>(5, new MyLinkedList<>(10, + new Empty())); + + System.out.println(l.head()); + + LazyList numbers = from(2); + int two = numbers.head(); + int three = numbers.tail().head(); + int four = numbers.tail().tail().head(); + System.out.println(two + " " + three + " " + four); + + numbers = from(2); + int prime_two = primes(numbers).head(); + int prime_three = primes(numbers).tail().head(); + int prime_five = primes(numbers).tail().tail().head(); + System.out.println(prime_two + " " + prime_three + " " + prime_five); + + // this will run until a stackoverflow occur because Java does not + // support tail call elimination + // printAll(primes(from(2))); + } + + interface MyList { + T head(); + + MyList tail(); + + default boolean isEmpty() { + return true; + } + + MyList filter(Predicate p); + } + + static class MyLinkedList implements MyList { + final T head; + final MyList tail; + + public MyLinkedList(T head, MyList tail) { + this.head = head; + this.tail = tail; + } + + public T head() { + return head; + } + + public MyList tail() { + return tail; + } + + public boolean isEmpty() { + return false; + } + + public MyList filter(Predicate p) { + return isEmpty() ? this : p.test(head()) ? new MyLinkedList<>( + head(), tail().filter(p)) : tail().filter(p); + } + } + + static class Empty implements MyList { + public T head() { + throw new UnsupportedOperationException(); + } + + public MyList tail() { + throw new UnsupportedOperationException(); + } + + public MyList filter(Predicate p) { + return this; + } + } + + static class LazyList implements MyList { + final T head; + final Supplier> tail; + + public LazyList(T head, Supplier> tail) { + this.head = head; + this.tail = tail; + } + + public T head() { + return head; + } + + public MyList tail() { + return tail.get(); + } + + public boolean isEmpty() { + return false; + } + + public MyList filter(Predicate p) { + return isEmpty() ? this : p.test(head()) ? new LazyList<>(head(), + () -> tail().filter(p)) : tail().filter(p); + } + + } + + public static LazyList from(int n) { + return new LazyList(n, () -> from(n + 1)); + } + + public static MyList primes(MyList numbers) { + return new LazyList<>(numbers.head(), () -> primes(numbers.tail() + .filter(n -> n % numbers.head() != 0))); + } + + static void printAll(MyList numbers) { + if (numbers.isEmpty()) { + return; + } + System.out.println(numbers.head()); + printAll(numbers.tail()); + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/PatternMatching.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/PatternMatching.java new file mode 100644 index 00000000..400593d4 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/PatternMatching.java @@ -0,0 +1,139 @@ +package lambdasinaction.chap14; + +import java.util.function.Function; +import java.util.function.Supplier; + +public class PatternMatching { + + public static void main(String[] args) { + simplify(); + + Expr e = new BinOp("+", new Number(5), new BinOp("*", new Number(3), new Number(4))); + Integer result = evaluate(e); + System.out.println(e + " = " + result); + } + + private static void simplify() { + TriFunction binopcase = + (opname, left, right) -> { + if ("+".equals(opname)) { + if (left instanceof Number && ((Number) left).val == 0) { + return right; + } + if (right instanceof Number && ((Number) right).val == 0) { + return left; + } + } + if ("*".equals(opname)) { + if (left instanceof Number && ((Number) left).val == 1) { + return right; + } + if (right instanceof Number && ((Number) right).val == 1) { + return left; + } + } + return new BinOp(opname, left, right); + }; + Function numcase = val -> new Number(val); + Supplier defaultcase = () -> new Number(0); + + Expr e = new BinOp("+", new Number(5), new Number(0)); + Expr match = patternMatchExpr(e, binopcase, numcase, defaultcase); + if (match instanceof Number) { + System.out.println("Number: " + match); + } else if (match instanceof BinOp) { + System.out.println("BinOp: " + match); + } + } + + private static Integer evaluate(Expr e) { + Function numcase = val -> val; + Supplier defaultcase = () -> 0; + TriFunction binopcase = + (opname, left, right) -> { + if ("+".equals(opname)) { + if (left instanceof Number && right instanceof Number) { + return ((Number) left).val + ((Number) right).val; + } + if (right instanceof Number && left instanceof BinOp) { + return ((Number) right).val + evaluate((BinOp) left); + } + if (left instanceof Number && right instanceof BinOp) { + return ((Number) left).val + evaluate((BinOp) right); + } + if (left instanceof BinOp && right instanceof BinOp) { + return evaluate((BinOp) left) + evaluate((BinOp) right); + } + } + if ("*".equals(opname)) { + if (left instanceof Number && right instanceof Number) { + return ((Number) left).val * ((Number) right).val; + } + if (right instanceof Number && left instanceof BinOp) { + return ((Number) right).val * evaluate((BinOp) left); + } + if (left instanceof Number && right instanceof BinOp) { + return ((Number) left).val * evaluate((BinOp) right); + } + if (left instanceof BinOp && right instanceof BinOp) { + return evaluate((BinOp) left) * evaluate((BinOp) right); + } + } + return defaultcase.get(); + }; + + return patternMatchExpr(e, binopcase, numcase, defaultcase); + } + + static class Expr { + } + + static class Number extends Expr { + int val; + public Number(int val) { + this.val = val; + } + + @Override + public String toString() { + return "" + val; + } + } + + static class BinOp extends Expr { + String opname; + Expr left, right; + public BinOp(String opname, Expr left, Expr right) { + this.opname = opname; + this.left = left; + this.right = right; + } + + @Override + public String toString() { + return "(" + left + " " + opname + " " + right + ")"; + } + } + + static T MyIf(boolean b, Supplier truecase, Supplier falsecase) { + return b ? truecase.get() : falsecase.get(); + } + + static interface TriFunction { + R apply(S s, T t, U u); + } + + static T patternMatchExpr(Expr e, + TriFunction binopcase, + Function numcase, Supplier defaultcase) { + + if (e instanceof BinOp) { + return binopcase.apply(((BinOp) e).opname, ((BinOp) e).left, ((BinOp) e).right); + } else if (e instanceof Number) { + return numcase.apply(((Number) e).val); + } else { + return defaultcase.get(); + } + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/PersistentTrainJourney.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/PersistentTrainJourney.java new file mode 100644 index 00000000..7e958a14 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/PersistentTrainJourney.java @@ -0,0 +1,66 @@ +package lambdasinaction.chap14; + +import java.util.function.Consumer; + +public class PersistentTrainJourney { + + public static void main(String[] args) { + TrainJourney tj1 = new TrainJourney(40, new TrainJourney(30, null)); + TrainJourney tj2 = new TrainJourney(20, new TrainJourney(50, null)); + + TrainJourney appended = append(tj1, tj2); + visit(appended, tj -> { System.out.print(tj.price + " - "); }); + System.out.println(); + + // A new TrainJourney is created without altering tj1 and tj2. + TrainJourney appended2 = append(tj1, tj2); + visit(appended2, tj -> { System.out.print(tj.price + " - "); }); + System.out.println(); + + // tj1 is altered but it's still not visible in the results. + TrainJourney linked = link(tj1, tj2); + visit(linked, tj -> { System.out.print(tj.price + " - "); }); + System.out.println(); + + // ... but here, if this code is uncommented, tj2 will be appended + // at the end of the already altered tj1. This will cause a + // StackOverflowError from the endless visit() recursive calls on + // the tj2 part of the twice altered tj1. + /*TrainJourney linked2 = link(tj1, tj2); + visit(linked2, tj -> { System.out.print(tj.price + " - "); }); + System.out.println();*/ + } + + static class TrainJourney { + public int price; + public TrainJourney onward; + + public TrainJourney(int p, TrainJourney t) { + price = p; + onward = t; + } + } + + static TrainJourney link(TrainJourney a, TrainJourney b) { + if (a == null) { + return b; + } + TrainJourney t = a; + while (t.onward != null) { + t = t.onward; + } + t.onward = b; + return a; + } + + static TrainJourney append(TrainJourney a, TrainJourney b) { + return a == null ? b : new TrainJourney(a.price, append(a.onward, b)); + } + + static void visit(TrainJourney journey, Consumer c) { + if (journey != null) { + c.accept(journey); + visit(journey.onward, c); + } + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/PersistentTree.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/PersistentTree.java new file mode 100644 index 00000000..562d4f69 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap14/PersistentTree.java @@ -0,0 +1,82 @@ +package lambdasinaction.chap14; + +public class PersistentTree { + + public static void main(String[] args) { + Tree t = new Tree("Mary", 22, + new Tree("Emily", 20, + new Tree("Alan", 50, null, null), + new Tree("Georgie", 23, null, null) + ), + new Tree("Tian", 29, + new Tree("Raoul", 23, null, null), + null + ) + ); + + // found = 23 + System.out.println(lookup("Raoul", -1, t)); + // not found = -1 + System.out.println(lookup("Jeff", -1, t)); + + Tree f = fupdate("Jeff", 80, t); + // found = 80 + System.out.println(lookup("Jeff", -1, f)); + + Tree u = update("Jim", 40, t); + // t was not altered by fupdate, so Jeff is not found = -1 + System.out.println(lookup("Jeff", -1, u)); + // found = 40 + System.out.println(lookup("Jim", -1, u)); + + Tree f2 = fupdate("Jeff", 80, t); + // found = 80 + System.out.println(lookup("Jeff", -1, f2)); + // f2 built from t altered by update() above, so Jim is still present = 40 + System.out.println(lookup("Jim", -1, f2)); + } + + static class Tree { + private String key; + private int val; + private Tree left, right; + + public Tree(String k, int v, Tree l, Tree r) { + key = k; + val = v; + left = l; + right = r; + } + } + + public static int lookup(String k, int defaultval, Tree t) { + if (t == null) + return defaultval; + if (k.equals(t.key)) + return t.val; + return lookup(k, defaultval, k.compareTo(t.key) < 0 ? t.left : t.right); + } + + public static Tree update(String k, int newval, Tree t) { + if (t == null) + t = new Tree(k, newval, null, null); + else if (k.equals(t.key)) + t.val = newval; + else if (k.compareTo(t.key) < 0) + t.left = update(k, newval, t.left); + else + t.right = update(k, newval, t.right); + return t; + } + + public static Tree fupdate(String k, int newval, Tree t) { + return (t == null) ? + new Tree(k, newval, null, null) : + k.equals(t.key) ? + new Tree(k, newval, t.left, t.right) : + k.compareTo(t.key) < 0 ? + new Tree(t.key, t.val, fupdate(k,newval, t.left), t.right) : + new Tree(t.key, t.val, t.left, fupdate(k,newval, t.right)); + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap2/FilteringApples.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap2/FilteringApples.java new file mode 100644 index 00000000..48516306 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap2/FilteringApples.java @@ -0,0 +1,144 @@ +package lambdasinaction.chap2; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class FilteringApples { + + public static void main(String... args) { + + List inventory = Arrays.asList(new Apple(80, "green"), new Apple(155, "green"), new Apple(120, "red")); + + // [Apple{color='green', weight=80}, Apple{color='green', weight=155}] + List greenApples = filterApplesByColor(inventory, "green"); + System.out.println(greenApples); + + // [Apple{color='red', weight=120}] + List redApples = filterApplesByColor(inventory, "red"); + System.out.println(redApples); + + // [Apple{color='green', weight=80}, Apple{color='green', weight=155}] + List greenApples2 = filter(inventory, new AppleColorPredicate()); + System.out.println(greenApples2); + + // [Apple{color='green', weight=155}] + List heavyApples = filter(inventory, new AppleWeightPredicate()); + System.out.println(heavyApples); + + // [] + List redAndHeavyApples = filter(inventory, new AppleRedAndHeavyPredicate()); + System.out.println(redAndHeavyApples); + + // [Apple{color='red', weight=120}] + List redApples2 = filter(inventory, new ApplePredicate() { + public boolean test(Apple a) { + return a.getColor().equals("red"); + } + }); + System.out.println(redApples2); + + } + + public static List filterGreenApples(List inventory) { + List result = new ArrayList<>(); + for (Apple apple : inventory) { + if ("green".equals(apple.getColor())) { + result.add(apple); + } + } + return result; + } + + public static List filterApplesByColor(List inventory, String color) { + List result = new ArrayList<>(); + for (Apple apple : inventory) { + if (apple.getColor().equals(color)) { + result.add(apple); + } + } + return result; + } + + public static List filterApplesByWeight(List inventory, int weight) { + List result = new ArrayList<>(); + for (Apple apple : inventory) { + if (apple.getWeight() > weight) { + result.add(apple); + } + } + return result; + } + + + public static List filter(List inventory, ApplePredicate p) { + List result = new ArrayList<>(); + for (Apple apple : inventory) { + if (p.test(apple)) { + result.add(apple); + } + } + return result; + } + + public static class Apple { + private int weight = 0; + private String color = ""; + + public Apple(int weight, String color) { + this.weight = weight; + this.color = color; + } + + public Integer getWeight() { + return weight; + } + + public void setWeight(Integer weight) { + this.weight = weight; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + @Override + public String toString() { + return "Apple{" + + "color='" + color + '\'' + + ", weight=" + weight + + '}'; + } + } + + interface ApplePredicate { + + public boolean test(Apple a); + } + + static class AppleWeightPredicate implements ApplePredicate { + @Override + public boolean test(Apple apple) { + return apple.getWeight() > 150; + } + } + + static class AppleColorPredicate implements ApplePredicate { + @Override + public boolean test(Apple apple) { + return "green".equals(apple.getColor()); + } + } + + static class AppleRedAndHeavyPredicate implements ApplePredicate { + @Override + public boolean test(Apple apple) { + return "red".equals(apple.getColor()) + && apple.getWeight() > 150; + } + } +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap2/MeaningOfThis.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap2/MeaningOfThis.java new file mode 100644 index 00000000..8d8afc52 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap2/MeaningOfThis.java @@ -0,0 +1,25 @@ +package lambdasinaction.chap2; + +public class MeaningOfThis { + + public final int value = 4; + + public void doIt() { + int value = 6; + Runnable r = new Runnable() { + public final int value = 5; + + @Override + public void run() { + int value = 10; + System.out.println(this.value); + } + }; + r.run(); + } + + public static void main(String... args) { + MeaningOfThis m = new MeaningOfThis(); + m.doIt(); // ??? + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap3/ExecuteAround.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap3/ExecuteAround.java new file mode 100644 index 00000000..5a196bfd --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap3/ExecuteAround.java @@ -0,0 +1,41 @@ +package lambdasinaction.chap3; + +import java.io.*; +public class ExecuteAround { + + public static void main(String ...args) throws IOException{ + + // method we want to refactor to make more flexible + String result = processFileLimited(); + System.out.println(result); + + System.out.println("---"); + + String oneLine = processFile(BufferedReader::readLine); + System.out.println(oneLine); + + String twoLines = processFile((BufferedReader b) -> b.readLine() + b.readLine()); + System.out.println(twoLines); + + } + + public static String processFileLimited() throws IOException { + try (BufferedReader br = + new BufferedReader(new FileReader("lambdasinaction/chap3/data.txt"))) { + return br.readLine(); + } + } + + + public static String processFile(BufferedReaderProcessor p) throws IOException { + try(BufferedReader br = new BufferedReader(new FileReader("lambdasinaction/chap3/data.txt"))){ + return p.process(br); + } + + } + + public interface BufferedReaderProcessor{ + public String process(BufferedReader b) throws IOException; + + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap3/Lambdas.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap3/Lambdas.java new file mode 100644 index 00000000..9930705d --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap3/Lambdas.java @@ -0,0 +1,79 @@ +package lambdasinaction.chap3; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + +public class Lambdas { + public static void main(String... args) { + + // Simple example + Runnable r = () -> System.out.println("Hello!"); + r.run(); + + // Filtering with lambdas + List inventory = Arrays.asList( + new Apple(80, "green"), + new Apple(155, "green"), + new Apple(120, "red")); + + // [Apple{color='green', weight=80}, Apple{color='green', weight=155}] + List greenApples = filter(inventory, (Apple a) -> "green".equals(a.getColor())); + System.out.println(greenApples); + + + Comparator c = (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()); + + // [Apple{color='green', weight=80}, Apple{color='red', weight=120}, Apple{color='green', weight=155}] + inventory.sort(c); + System.out.println(inventory); + } + + public static List filter(List inventory, ApplePredicate p) { + List result = new ArrayList<>(); + for (Apple apple : inventory) { + if (p.test(apple)) { + result.add(apple); + } + } + return result; + } + + public static class Apple { + private int weight = 0; + private String color = ""; + + public Apple(int weight, String color) { + this.weight = weight; + this.color = color; + } + + public Integer getWeight() { + return weight; + } + + public void setWeight(Integer weight) { + this.weight = weight; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public String toString() { + return "Apple{" + + "color='" + color + '\'' + + ", weight=" + weight + + '}'; + } + } + + interface ApplePredicate { + public boolean test(Apple a); + } +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap3/Sorting.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap3/Sorting.java new file mode 100644 index 00000000..41acbe25 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap3/Sorting.java @@ -0,0 +1,84 @@ +package lambdasinaction.chap3; + +import java.util.*; +import static java.util.Comparator.comparing; + +public class Sorting { + + public static void main(String...args){ + + // 1 + List inventory = new ArrayList<>(); + inventory.addAll(Arrays.asList(new Apple(80,"green"), new Apple(155, "green"), new Apple(120, "red"))); + + // [Apple{color='green', weight=80}, Apple{color='red', weight=120}, Apple{color='green', weight=155}] + inventory.sort(new AppleComparator()); + System.out.println(inventory); + + // reshuffling things a little + inventory.set(1, new Apple(30, "green")); + + // 2 + // [Apple{color='green', weight=30}, Apple{color='green', weight=80}, Apple{color='green', weight=155}] + inventory.sort(new Comparator() { + public int compare(Apple a1, Apple a2){ + return a1.getWeight().compareTo(a2.getWeight()); + }}); + System.out.println(inventory); + + // reshuffling things a little + inventory.set(1, new Apple(20, "red")); + + // 3 + // [Apple{color='red', weight=20}, Apple{color='green', weight=30}, Apple{color='green', weight=155}] + inventory.sort((a1, a2) -> a1.getWeight().compareTo(a2.getWeight())); + System.out.println(inventory); + + // reshuffling things a little + inventory.set(1, new Apple(10, "red")); + + // 4 + // [Apple{color='red', weight=10}, Apple{color='red', weight=20}, Apple{color='green', weight=155}] + inventory.sort(comparing(Apple::getWeight)); + System.out.println(inventory); + } + + public static class Apple { + private Integer weight = 0; + private String color = ""; + + public Apple(Integer weight, String color){ + this.weight = weight; + this.color = color; + } + + public Integer getWeight() { + return weight; + } + + public void setWeight(Integer weight) { + this.weight = weight; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public String toString() { + return "Apple{" + + "color='" + color + '\'' + + ", weight=" + weight + + '}'; + } + } + + static class AppleComparator implements Comparator { + public int compare(Apple a1, Apple a2){ + return a1.getWeight().compareTo(a2.getWeight()); + } + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap4/Dish.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap4/Dish.java new file mode 100644 index 00000000..96307644 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap4/Dish.java @@ -0,0 +1,51 @@ +package lambdasinaction.chap4; +import java.util.*; + +public class Dish { + + private final String name; + private final boolean vegetarian; + private final int calories; + private final Type type; + + public Dish(String name, boolean vegetarian, int calories, Type type) { + this.name = name; + this.vegetarian = vegetarian; + this.calories = calories; + this.type = type; + } + + public String getName() { + return name; + } + + public boolean isVegetarian() { + return vegetarian; + } + + public int getCalories() { + return calories; + } + + public Type getType() { + return type; + } + + public enum Type { MEAT, FISH, OTHER } + + @Override + public String toString() { + return name; + } + + public static final List menu = + Arrays.asList( new Dish("pork", false, 800, Dish.Type.MEAT), + new Dish("beef", false, 700, Dish.Type.MEAT), + new Dish("chicken", false, 400, Dish.Type.MEAT), + new Dish("french fries", true, 530, Dish.Type.OTHER), + new Dish("rice", true, 350, Dish.Type.OTHER), + new Dish("season fruit", true, 120, Dish.Type.OTHER), + new Dish("pizza", true, 550, Dish.Type.OTHER), + new Dish("prawns", false, 400, Dish.Type.FISH), + new Dish("salmon", false, 450, Dish.Type.FISH)); +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap4/StreamBasic.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap4/StreamBasic.java new file mode 100644 index 00000000..19a8c176 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap4/StreamBasic.java @@ -0,0 +1,50 @@ +package lambdasinaction.chap4; + +import java.util.*; +import java.util.stream.*; + +import static java.util.Comparator.comparing; +import static java.util.stream.Collectors.toList; + +import static lambdasinaction.chap4.Dish.menu; + +public class StreamBasic { + + public static void main(String...args){ + // Java 7 + getLowCaloricDishesNamesInJava7(Dish.menu).forEach(System.out::println); + + System.out.println("---"); + + // Java 8 + getLowCaloricDishesNamesInJava8(Dish.menu).forEach(System.out::println); + + } + + public static List getLowCaloricDishesNamesInJava7(List dishes){ + List lowCaloricDishes = new ArrayList<>(); + for(Dish d: dishes){ + if(d.getCalories() < 400){ + lowCaloricDishes.add(d); + } + } + List lowCaloricDishesName = new ArrayList<>(); + Collections.sort(lowCaloricDishes, new Comparator() { + public int compare(Dish d1, Dish d2){ + return Integer.compare(d1.getCalories(), d2.getCalories()); + } + }); + for(Dish d: lowCaloricDishes){ + lowCaloricDishesName.add(d.getName()); + } + return lowCaloricDishesName; + } + + public static List getLowCaloricDishesNamesInJava8(List dishes){ + return dishes.stream() + .filter(d -> d.getCalories() < 400) + .sorted(comparing(Dish::getCalories)) + .map(Dish::getName) + .collect(toList()); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap4/StreamVsCollection.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap4/StreamVsCollection.java new file mode 100644 index 00000000..a72a0730 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap4/StreamVsCollection.java @@ -0,0 +1,18 @@ +package lambdasinaction.chap4; + +import java.util.*; +import java.util.stream.*; +import static java.util.stream.Collectors.toList; + + +public class StreamVsCollection { + + public static void main(String...args){ + List names = Arrays.asList("Java8", "Lambdas", "In", "Action"); + Stream s = names.stream(); + s.forEach(System.out::println); + // uncommenting this line will result in an IllegalStateException + // because streams can be consumed only once + //s.forEach(System.out::println); + } +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/BuildingStreams.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/BuildingStreams.java new file mode 100644 index 00000000..15280a39 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/BuildingStreams.java @@ -0,0 +1,78 @@ +package lambdasinaction.chap5; + +import java.util.*; +import java.util.function.IntSupplier; +import java.util.stream.*; +import java.nio.charset.Charset; +import java.nio.file.*; + +public class BuildingStreams { + + public static void main(String...args) throws Exception{ + + // Stream.of + Stream stream = Stream.of("Java 8", "Lambdas", "In", "Action"); + stream.map(String::toUpperCase).forEach(System.out::println); + + // Stream.empty + Stream emptyStream = Stream.empty(); + + // Arrays.stream + int[] numbers = {2, 3, 5, 7, 11, 13}; + System.out.println(Arrays.stream(numbers).sum()); + + // Stream.iterate + Stream.iterate(0, n -> n + 2) + .limit(10) + .forEach(System.out::println); + + // fibonnaci with iterate + Stream.iterate(new int[]{0, 1}, t -> new int[]{t[1],t[0] + t[1]}) + .limit(10) + .forEach(t -> System.out.println("(" + t[0] + ", " + t[1] + ")")); + + Stream.iterate(new int[]{0, 1}, t -> new int[]{t[1],t[0] + t[1]}) + .limit(10) + . map(t -> t[0]) + .forEach(System.out::println); + + // random stream of doubles with Stream.generate + Stream.generate(Math::random) + .limit(10) + .forEach(System.out::println); + + // stream of 1s with Stream.generate + IntStream.generate(() -> 1) + .limit(5) + .forEach(System.out::println); + + IntStream.generate(new IntSupplier(){ + public int getAsInt(){ + return 2; + } + }).limit(5) + .forEach(System.out::println); + + + IntSupplier fib = new IntSupplier(){ + private int previous = 0; + private int current = 1; + public int getAsInt(){ + int nextValue = this.previous + this.current; + this.previous = this.current; + this.current = nextValue; + return this.previous; + } + }; + IntStream.generate(fib).limit(10).forEach(System.out::println); + + long uniqueWords = Files.lines(Paths.get("lambdasinaction/chap5/data.txt"), Charset.defaultCharset()) + .flatMap(line -> Arrays.stream(line.split(" "))) + .distinct() + .count(); + + System.out.println("There are " + uniqueWords + " unique words in data.txt"); + + + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Filtering.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Filtering.java new file mode 100644 index 00000000..066c8242 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Filtering.java @@ -0,0 +1,47 @@ +package lambdasinaction.chap5; +import lambdasinaction.chap4.*; + +import java.util.stream.*; +import java.util.*; +import static java.util.stream.Collectors.toList; + +import static lambdasinaction.chap4.Dish.menu; + +public class Filtering{ + + public static void main(String...args){ + + // Filtering with predicate + List vegetarianMenu = + menu.stream() + .filter(Dish::isVegetarian) + .collect(toList()); + + vegetarianMenu.forEach(System.out::println); + + // Filtering unique elements + List numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4); + numbers.stream() + .filter(i -> i % 2 == 0) + .distinct() + .forEach(System.out::println); + + // Truncating a stream + List dishesLimit3 = + menu.stream() + .filter(d -> d.getCalories() > 300) + .limit(3) + .collect(toList()); + + dishesLimit3.forEach(System.out::println); + + // Skipping elements + List dishesSkip2 = + menu.stream() + .filter(d -> d.getCalories() > 300) + .skip(2) + .collect(toList()); + + dishesSkip2.forEach(System.out::println); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Finding.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Finding.java new file mode 100644 index 00000000..acccc543 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Finding.java @@ -0,0 +1,39 @@ +package lambdasinaction.chap5; +import lambdasinaction.chap4.*; + +import java.util.stream.*; +import java.util.*; + +import static lambdasinaction.chap4.Dish.menu; + +public class Finding{ + + public static void main(String...args){ + if(isVegetarianFriendlyMenu()){ + System.out.println("Vegetarian friendly"); + } + + System.out.println(isHealthyMenu()); + System.out.println(isHealthyMenu2()); + + Optional dish = findVegetarianDish(); + dish.ifPresent(d -> System.out.println(d.getName())); + } + + private static boolean isVegetarianFriendlyMenu(){ + return menu.stream().anyMatch(Dish::isVegetarian); + } + + private static boolean isHealthyMenu(){ + return menu.stream().allMatch(d -> d.getCalories() < 1000); + } + + private static boolean isHealthyMenu2(){ + return menu.stream().noneMatch(d -> d.getCalories() >= 1000); + } + + private static Optional findVegetarianDish(){ + return menu.stream().filter(Dish::isVegetarian).findAny(); + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Laziness.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Laziness.java new file mode 100644 index 00000000..a3df328a --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Laziness.java @@ -0,0 +1,30 @@ +package lambdasinaction.chap5; + +import java.util.Arrays; +import java.util.List; + +import static java.util.stream.Collectors.toList; + +/** + * Created by raoul-gabrielurma on 14/01/2014. + */ +public class Laziness { + + public static void main(String[] args) { + List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8); + List twoEvenSquares = + numbers.stream() + .filter(n -> { + System.out.println("filtering " + n); return n % 2 == 0; + }) + .map(n -> { + System.out.println("mapping " + n); + return n * n; + }) + .limit(2) + .collect(toList()); + + } + + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Mapping.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Mapping.java new file mode 100644 index 00000000..7bf4caba --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Mapping.java @@ -0,0 +1,44 @@ +package lambdasinaction.chap5; + +import lambdasinaction.chap4.*; + +import java.util.*; +import static java.util.stream.Collectors.toList; +import static lambdasinaction.chap4.Dish.menu; + +public class Mapping{ + + public static void main(String...args){ + + // map + List dishNames = menu.stream() + .map(Dish::getName) + .collect(toList()); + System.out.println(dishNames); + + // map + List words = Arrays.asList("Hello", "World"); + List wordLengths = words.stream() + .map(String::length) + .collect(toList()); + System.out.println(wordLengths); + + // flatMap + words.stream() + .flatMap((String line) -> Arrays.stream(line.split(""))) + .distinct() + .forEach(System.out::println); + + // flatMap + List numbers1 = Arrays.asList(1,2,3,4,5); + List numbers2 = Arrays.asList(6,7,8); + List pairs = + numbers1.stream() + .flatMap((Integer i) -> numbers2.stream() + .map((Integer j) -> new int[]{i, j}) + ) + .filter(pair -> (pair[0] + pair[1]) % 3 == 0) + .collect(toList()); + pairs.forEach(pair -> System.out.println("(" + pair[0] + ", " + pair[1] + ")")); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/NumericStreams.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/NumericStreams.java new file mode 100644 index 00000000..e5f8cca8 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/NumericStreams.java @@ -0,0 +1,57 @@ +package lambdasinaction.chap5; +import lambdasinaction.chap4.*; + +import java.util.stream.*; +import java.util.*; + +import static lambdasinaction.chap4.Dish.menu; + +public class NumericStreams{ + + public static void main(String...args){ + + List numbers = Arrays.asList(3,4,5,1,2); + + Arrays.stream(numbers.toArray()).forEach(System.out::println); + int calories = menu.stream() + .mapToInt(Dish::getCalories) + .sum(); + System.out.println("Number of calories:" + calories); + + + // max and OptionalInt + OptionalInt maxCalories = menu.stream() + .mapToInt(Dish::getCalories) + .max(); + + int max; + if(maxCalories.isPresent()){ + max = maxCalories.getAsInt(); + } + else { + // we can choose a default value + max = 1; + } + System.out.println(max); + + // numeric ranges + IntStream evenNumbers = IntStream.rangeClosed(1, 100) + .filter(n -> n % 2 == 0); + + System.out.println(evenNumbers.count()); + + Stream pythagoreanTriples = + IntStream.rangeClosed(1, 100).boxed() + .flatMap(a -> IntStream.rangeClosed(a, 100) + .filter(b -> Math.sqrt(a*a + b*b) % 1 == 0).boxed() + .map(b -> new int[]{a, b, (int) Math.sqrt(a * a + b * b)})); + + pythagoreanTriples.forEach(t -> System.out.println(t[0] + ", " + t[1] + ", " + t[2])); + + } + + public static boolean isPerfectSquare(int n){ + return Math.sqrt(n) % 1 == 0; + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/PuttingIntoPractice.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/PuttingIntoPractice.java new file mode 100644 index 00000000..d0b13ae5 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/PuttingIntoPractice.java @@ -0,0 +1,90 @@ +package lambdasinaction.chap5; + +import lambdasinaction.chap5.*; + +import java.util.*; + +import static java.util.Comparator.comparing; +import static java.util.stream.Collectors.toList; + +public class PuttingIntoPractice{ + public static void main(String ...args){ + Trader raoul = new Trader("Raoul", "Cambridge"); + Trader mario = new Trader("Mario","Milan"); + Trader alan = new Trader("Alan","Cambridge"); + Trader brian = new Trader("Brian","Cambridge"); + + List transactions = Arrays.asList( + new Transaction(brian, 2011, 300), + new Transaction(raoul, 2012, 1000), + new Transaction(raoul, 2011, 400), + new Transaction(mario, 2012, 710), + new Transaction(mario, 2012, 700), + new Transaction(alan, 2012, 950) + ); + + + // Query 1: Find all transactions from year 2011 and sort them by value (small to high). + List tr2011 = transactions.stream() + .filter(transaction -> transaction.getYear() == 2011) + .sorted(comparing(Transaction::getValue)) + .collect(toList()); + System.out.println(tr2011); + + // Query 2: What are all the unique cities where the traders work? + List cities = + transactions.stream() + .map(transaction -> transaction.getTrader().getCity()) + .distinct() + .collect(toList()); + System.out.println(cities); + + // Query 3: Find all traders from Cambridge and sort them by name. + + List traders = + transactions.stream() + .map(Transaction::getTrader) + .filter(trader -> trader.getCity().equals("Cambridge")) + .distinct() + .sorted(comparing(Trader::getName)) + .collect(toList()); + System.out.println(traders); + + + // Query 4: Return a string of all traders’ names sorted alphabetically. + + String traderStr = + transactions.stream() + .map(transaction -> transaction.getTrader().getName()) + .distinct() + .sorted() + .reduce("", (n1, n2) -> n1 + n2); + System.out.println(traderStr); + + // Query 5: Are there any trader based in Milan? + + boolean milanBased = + transactions.stream() + .anyMatch(transaction -> transaction.getTrader() + .getCity() + .equals("Milan") + ); + System.out.println(milanBased); + + + // Query 6: Update all transactions so that the traders from Milan are set to Cambridge. + transactions.stream() + .map(Transaction::getTrader) + .filter(trader -> trader.getCity().equals("Milan")) + .forEach(trader -> trader.setCity("Cambridge")); + System.out.println(transactions); + + + // Query 7: What's the highest value in all the transactions? + int highestValue = + transactions.stream() + .map(Transaction::getValue) + .reduce(0, Integer::max); + System.out.println(highestValue); + } +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Reducing.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Reducing.java new file mode 100644 index 00000000..93aed73e --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Reducing.java @@ -0,0 +1,31 @@ +package lambdasinaction.chap5; +import lambdasinaction.chap4.*; + +import java.util.stream.*; +import java.util.*; + +import static lambdasinaction.chap4.Dish.menu; + +public class Reducing{ + + public static void main(String...args){ + + List numbers = Arrays.asList(3,4,5,1,2); + int sum = numbers.stream().reduce(0, (a, b) -> a + b); + System.out.println(sum); + + int sum2 = numbers.stream().reduce(0, Integer::sum); + System.out.println(sum2); + + int max = numbers.stream().reduce(0, (a, b) -> Integer.max(a, b)); + System.out.println(max); + + Optional min = numbers.stream().reduce(Integer::min); + min.ifPresent(System.out::println); + + int calories = menu.stream() + .map(Dish::getCalories) + .reduce(0, Integer::sum); + System.out.println("Number of calories:" + calories); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Trader.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Trader.java new file mode 100644 index 00000000..7487a8c6 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Trader.java @@ -0,0 +1,27 @@ +package lambdasinaction.chap5; +public class Trader{ + + private String name; + private String city; + + public Trader(String n, String c){ + this.name = n; + this.city = c; + } + + public String getName(){ + return this.name; + } + + public String getCity(){ + return this.city; + } + + public void setCity(String newCity){ + this.city = newCity; + } + + public String toString(){ + return "Trader:"+this.name + " in " + this.city; + } +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Transaction.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Transaction.java new file mode 100644 index 00000000..754e3839 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap5/Transaction.java @@ -0,0 +1,33 @@ +package lambdasinaction.chap5; + +public class Transaction{ + + private Trader trader; + private int year; + private int value; + + public Transaction(Trader trader, int year, int value) + { + this.trader = trader; + this.year = year; + this.value = value; + } + + public Trader getTrader(){ + return this.trader; + } + + public int getYear(){ + return this.year; + } + + public int getValue(){ + return this.value; + } + + public String toString(){ + return "{" + this.trader + ", " + + "year: "+this.year+", " + + "value:" + this.value +"}"; + } +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/CollectorHarness.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/CollectorHarness.java new file mode 100644 index 00000000..8370be16 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/CollectorHarness.java @@ -0,0 +1,23 @@ +package lambdasinaction.chap6; + +import java.util.function.*; + +public class CollectorHarness { + + public static void main(String[] args) { + //System.out.println("Partitioning done in: " + execute(PartitionPrimeNumbers::partitionPrimes) + " msecs"); + System.out.println("Partitioning done in: " + execute(PartitionPrimeNumbers::partitionPrimesWithCustomCollector) + " msecs" ); + } + + private static long execute(Consumer primePartitioner) { + long fastest = Long.MAX_VALUE; + for (int i = 0; i < 10; i++) { + long start = System.nanoTime(); + primePartitioner.accept(1_000_000); + long duration = (System.nanoTime() - start) / 1_000_000; + if (duration < fastest) fastest = duration; + System.out.println("done in " + duration); + } + return fastest; + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/Dish.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/Dish.java new file mode 100644 index 00000000..adba6e93 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/Dish.java @@ -0,0 +1,68 @@ +package lambdasinaction.chap6; + +import java.util.*; + +import static java.util.Arrays.asList; + +public class Dish { + + private final String name; + private final boolean vegetarian; + private final int calories; + private final Type type; + + public Dish(String name, boolean vegetarian, int calories, Type type) { + this.name = name; + this.vegetarian = vegetarian; + this.calories = calories; + this.type = type; + } + + public String getName() { + return name; + } + + public boolean isVegetarian() { + return vegetarian; + } + + public int getCalories() { + return calories; + } + + public Type getType() { + return type; + } + + public enum Type { MEAT, FISH, OTHER } + + @Override + public String toString() { + return name; + } + + public static final List menu = + asList( new Dish("pork", false, 800, Dish.Type.MEAT), + new Dish("beef", false, 700, Dish.Type.MEAT), + new Dish("chicken", false, 400, Dish.Type.MEAT), + new Dish("french fries", true, 530, Dish.Type.OTHER), + new Dish("rice", true, 350, Dish.Type.OTHER), + new Dish("season fruit", true, 120, Dish.Type.OTHER), + new Dish("pizza", true, 550, Dish.Type.OTHER), + new Dish("prawns", false, 400, Dish.Type.FISH), + new Dish("salmon", false, 450, Dish.Type.FISH)); + + public static final Map> dishTags = new HashMap<>(); + + static { + dishTags.put("pork", asList("greasy", "salty")); + dishTags.put("beef", asList("salty", "roasted")); + dishTags.put("chicken", asList("fried", "crisp")); + dishTags.put("french fries", asList("greasy", "fried")); + dishTags.put("rice", asList("light", "natural")); + dishTags.put("season fruit", asList("fresh", "natural")); + dishTags.put("pizza", asList("tasty", "salty")); + dishTags.put("prawns", asList("tasty", "roasted")); + dishTags.put("salmon", asList("delicious", "fresh")); + } +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/Grouping.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/Grouping.java new file mode 100644 index 00000000..fa981a06 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/Grouping.java @@ -0,0 +1,96 @@ +package lambdasinaction.chap6; + +import java.util.*; + +import static java.util.stream.Collectors.*; +import static lambdasinaction.chap6.Dish.dishTags; +import static lambdasinaction.chap6.Dish.menu; + +public class Grouping { + + enum CaloricLevel { DIET, NORMAL, FAT }; + + public static void main(String ... args) { + System.out.println("Dishes grouped by type: " + groupDishesByType()); + System.out.println("Dish names grouped by type: " + groupDishNamesByType()); +// System.out.println("Dish tags grouped by type: " + groupDishTagsByType()); +// System.out.println("Caloric dishes grouped by type: " + groupCaloricDishesByType()); + System.out.println("Dishes grouped by caloric level: " + groupDishesByCaloricLevel()); + System.out.println("Dishes grouped by type and caloric level: " + groupDishedByTypeAndCaloricLevel()); + System.out.println("Count dishes in groups: " + countDishesInGroups()); + System.out.println("Most caloric dishes by type: " + mostCaloricDishesByType()); + System.out.println("Most caloric dishes by type: " + mostCaloricDishesByTypeWithoutOprionals()); + System.out.println("Sum calories by type: " + sumCaloriesByType()); + System.out.println("Caloric levels by type: " + caloricLevelsByType()); + } + + private static Map> groupDishesByType() { + return menu.stream().collect(groupingBy(Dish::getType)); + } + + private static Map> groupDishNamesByType() { + return menu.stream().collect(groupingBy(Dish::getType, mapping(Dish::getName, toList()))); + } + +// private static Map> groupDishTagsByType() { +// return menu.stream().collect(groupingBy(Dish::getType, mapping(dish -> dishTags.get( dish.getName() ).stream(), toSet()))); +// } +// +// private static Map> groupCaloricDishesByType() { +//// return menu.stream().filter(dish -> dish.getCalories() > 500).collect(groupingBy(Dish::getType)); +// return menu.stream().collect(groupingBy(Dish::getType, filtering(dish -> dish.getCalories() > 500, toList()))); +// } + + private static Map> groupDishesByCaloricLevel() { + return menu.stream().collect( + groupingBy(dish -> { + if (dish.getCalories() <= 400) return CaloricLevel.DIET; + else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL; + else return CaloricLevel.FAT; + } )); + } + + private static Map>> groupDishedByTypeAndCaloricLevel() { + return menu.stream().collect( + groupingBy(Dish::getType, + groupingBy((Dish dish) -> { + if (dish.getCalories() <= 400) return CaloricLevel.DIET; + else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL; + else return CaloricLevel.FAT; + } ) + ) + ); + } + + private static Map countDishesInGroups() { + return menu.stream().collect(groupingBy(Dish::getType, counting())); + } + + private static Map> mostCaloricDishesByType() { + return menu.stream().collect( + groupingBy(Dish::getType, + reducing((Dish d1, Dish d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2))); + } + + private static Map mostCaloricDishesByTypeWithoutOprionals() { + return menu.stream().collect( + groupingBy(Dish::getType, + collectingAndThen( + reducing((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2), + Optional::get))); + } + + private static Map sumCaloriesByType() { + return menu.stream().collect(groupingBy(Dish::getType, + summingInt(Dish::getCalories))); + } + + private static Map> caloricLevelsByType() { + return menu.stream().collect( + groupingBy(Dish::getType, mapping( + dish -> { if (dish.getCalories() <= 400) return CaloricLevel.DIET; + else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL; + else return CaloricLevel.FAT; }, + toSet() ))); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/GroupingTransactions.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/GroupingTransactions.java new file mode 100644 index 00000000..ce358acb --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/GroupingTransactions.java @@ -0,0 +1,74 @@ +package lambdasinaction.chap6; + +import java.util.*; + +import static java.util.stream.Collectors.groupingBy; + +public class GroupingTransactions { + + public static List transactions = Arrays.asList( new Transaction(Currency.EUR, 1500.0), + new Transaction(Currency.USD, 2300.0), + new Transaction(Currency.GBP, 9900.0), + new Transaction(Currency.EUR, 1100.0), + new Transaction(Currency.JPY, 7800.0), + new Transaction(Currency.CHF, 6700.0), + new Transaction(Currency.EUR, 5600.0), + new Transaction(Currency.USD, 4500.0), + new Transaction(Currency.CHF, 3400.0), + new Transaction(Currency.GBP, 3200.0), + new Transaction(Currency.USD, 4600.0), + new Transaction(Currency.JPY, 5700.0), + new Transaction(Currency.EUR, 6800.0) ); + public static void main(String ... args) { + groupImperatively(); + groupFunctionally(); + + } + + private static void groupImperatively() { + Map> transactionsByCurrencies = new HashMap<>(); + for (Transaction transaction : transactions) { + Currency currency = transaction.getCurrency(); + List transactionsForCurrency = transactionsByCurrencies.get(currency); + if (transactionsForCurrency == null) { + transactionsForCurrency = new ArrayList<>(); + transactionsByCurrencies.put(currency, transactionsForCurrency); + } + transactionsForCurrency.add(transaction); + } + + System.out.println(transactionsByCurrencies); + } + + private static void groupFunctionally() { + Map> transactionsByCurrencies = transactions.stream().collect(groupingBy(Transaction::getCurrency)); + System.out.println(transactionsByCurrencies); + } + + public static class Transaction { + private final Currency currency; + private final double value; + + public Transaction(Currency currency, double value) { + this.currency = currency; + this.value = value; + } + + public Currency getCurrency() { + return currency; + } + + public double getValue() { + return value; + } + + @Override + public String toString() { + return currency + " " + value; + } + } + + public enum Currency { + EUR, USD, JPY, GBP, CHF + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/PartitionPrimeNumbers.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/PartitionPrimeNumbers.java new file mode 100644 index 00000000..c9efd614 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/PartitionPrimeNumbers.java @@ -0,0 +1,106 @@ +package lambdasinaction.chap6; + +import java.util.*; +import java.util.function.*; +import java.util.stream.*; + +import static java.util.stream.Collectors.*; +import static java.util.stream.Collector.Characteristics.*; + +public class PartitionPrimeNumbers { + + public static void main(String ... args) { + System.out.println("Numbers partitioned in prime and non-prime: " + partitionPrimes(100)); + System.out.println("Numbers partitioned in prime and non-prime: " + partitionPrimesWithCustomCollector(100)); + + } + + public static Map> partitionPrimes(int n) { + return IntStream.rangeClosed(2, n).boxed() + .collect(partitioningBy(candidate -> isPrime(candidate))); + } + + public static boolean isPrime(int candidate) { + return IntStream.rangeClosed(2, candidate-1) + .limit((long) Math.floor(Math.sqrt((double) candidate)) - 1) + .noneMatch(i -> candidate % i == 0); + } + + public static Map> partitionPrimesWithCustomCollector(int n) { + return IntStream.rangeClosed(2, n).boxed().collect(new PrimeNumbersCollector()); + } + + public static boolean isPrime(List primes, Integer candidate) { + double candidateRoot = Math.sqrt((double) candidate); + //return takeWhile(primes, i -> i <= candidateRoot).stream().noneMatch(i -> candidate % i == 0); + return primes.stream().filter(i -> i <= candidateRoot).noneMatch(i -> candidate % i == 0); + } +/* + public static List takeWhile(List list, Predicate p) { + int i = 0; + for (A item : list) { + if (!p.test(item)) { + return list.subList(0, i); + } + i++; + } + return list; + } +*/ + public static class PrimeNumbersCollector + implements Collector>, Map>> { + + @Override + public Supplier>> supplier() { + return () -> new HashMap>() {{ + put(true, new ArrayList()); + put(false, new ArrayList()); + }}; + } + + @Override + public BiConsumer>, Integer> accumulator() { + return (Map> acc, Integer candidate) -> { + acc.get( isPrime( acc.get(true), + candidate) ) + .add(candidate); + }; + } + + @Override + public BinaryOperator>> combiner() { + return (Map> map1, Map> map2) -> { + map1.get(true).addAll(map2.get(true)); + map1.get(false).addAll(map2.get(false)); + return map1; + }; + } + + @Override + public Function>, Map>> finisher() { + return i -> i; + } + + @Override + public Set characteristics() { + return Collections.unmodifiableSet(EnumSet.of(IDENTITY_FINISH)); + } + } + + public Map> partitionPrimesWithInlineCollector(int n) { + return Stream.iterate(2, i -> i + 1).limit(n) + .collect( + () -> new HashMap>() {{ + put(true, new ArrayList()); + put(false, new ArrayList()); + }}, + (acc, candidate) -> { + acc.get( isPrime(acc.get(true), candidate) ) + .add(candidate); + }, + (map1, map2) -> { + map1.get(true).addAll(map2.get(true)); + map1.get(false).addAll(map2.get(false)); + }); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/Partitioning.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/Partitioning.java new file mode 100644 index 00000000..2ec8dc79 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/Partitioning.java @@ -0,0 +1,33 @@ +package lambdasinaction.chap6; + +import java.util.*; + +import static java.util.Comparator.comparingInt; +import static java.util.stream.Collectors.*; +import static lambdasinaction.chap6.Dish.menu; + +public class Partitioning { + + public static void main(String ... args) { + System.out.println("Dishes partitioned by vegetarian: " + partitionByVegeterian()); + System.out.println("Vegetarian Dishes by type: " + vegetarianDishesByType()); + System.out.println("Most caloric dishes by vegetarian: " + mostCaloricPartitionedByVegetarian()); + } + + private static Map> partitionByVegeterian() { + return menu.stream().collect(partitioningBy(Dish::isVegetarian)); + } + + private static Map>> vegetarianDishesByType() { + return menu.stream().collect(partitioningBy(Dish::isVegetarian, groupingBy(Dish::getType))); + } + + private static Object mostCaloricPartitionedByVegetarian() { + return menu.stream().collect( + partitioningBy(Dish::isVegetarian, + collectingAndThen( + maxBy(comparingInt(Dish::getCalories)), + Optional::get))); + } +} + diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/Reducing.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/Reducing.java new file mode 100644 index 00000000..fb7cf971 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/Reducing.java @@ -0,0 +1,30 @@ +package lambdasinaction.chap6; + +import static java.util.stream.Collectors.*; +import static lambdasinaction.chap6.Dish.menu; + +public class Reducing { + + public static void main(String ... args) { + System.out.println("Total calories in menu: " + calculateTotalCalories()); + System.out.println("Total calories in menu: " + calculateTotalCaloriesWithMethodReference()); + System.out.println("Total calories in menu: " + calculateTotalCaloriesWithoutCollectors()); + System.out.println("Total calories in menu: " + calculateTotalCaloriesUsingSum()); + } + + private static int calculateTotalCalories() { + return menu.stream().collect(reducing(0, Dish::getCalories, (Integer i, Integer j) -> i + j)); + } + + private static int calculateTotalCaloriesWithMethodReference() { + return menu.stream().collect(reducing(0, Dish::getCalories, Integer::sum)); + } + + private static int calculateTotalCaloriesWithoutCollectors() { + return menu.stream().map(Dish::getCalories).reduce(Integer::sum).get(); + } + + private static int calculateTotalCaloriesUsingSum() { + return menu.stream().mapToInt(Dish::getCalories).sum(); + } +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/Summarizing.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/Summarizing.java new file mode 100644 index 00000000..06f9af6d --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/Summarizing.java @@ -0,0 +1,56 @@ +package lambdasinaction.chap6; + +import java.util.*; +import java.util.function.*; + +import static java.util.stream.Collectors.*; +import static lambdasinaction.chap6.Dish.menu; + +public class Summarizing { + + public static void main(String ... args) { + System.out.println("Nr. of dishes: " + howManyDishes()); + System.out.println("The most caloric dish is: " + findMostCaloricDish()); + System.out.println("The most caloric dish is: " + findMostCaloricDishUsingComparator()); + System.out.println("Total calories in menu: " + calculateTotalCalories()); + System.out.println("Average calories in menu: " + calculateAverageCalories()); + System.out.println("Menu statistics: " + calculateMenuStatistics()); + System.out.println("Short menu: " + getShortMenu()); + System.out.println("Short menu comma separated: " + getShortMenuCommaSeparated()); + } + + + private static long howManyDishes() { + return menu.stream().collect(counting()); + } + + private static Dish findMostCaloricDish() { + return menu.stream().collect(reducing((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2)).get(); + } + + private static Dish findMostCaloricDishUsingComparator() { + Comparator dishCaloriesComparator = Comparator.comparingInt(Dish::getCalories); + BinaryOperator moreCaloricOf = BinaryOperator.maxBy(dishCaloriesComparator); + return menu.stream().collect(reducing(moreCaloricOf)).get(); + } + + private static int calculateTotalCalories() { + return menu.stream().collect(summingInt(Dish::getCalories)); + } + + private static Double calculateAverageCalories() { + return menu.stream().collect(averagingInt(Dish::getCalories)); + } + + private static IntSummaryStatistics calculateMenuStatistics() { + return menu.stream().collect(summarizingInt(Dish::getCalories)); + } + + private static String getShortMenu() { + return menu.stream().map(Dish::getName).collect(joining()); + } + + private static String getShortMenuCommaSeparated() { + return menu.stream().map(Dish::getName).collect(joining(", ")); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/ToListCollector.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/ToListCollector.java new file mode 100644 index 00000000..0621d96a --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap6/ToListCollector.java @@ -0,0 +1,37 @@ +package lambdasinaction.chap6; + +import java.util.*; +import java.util.function.*; +import java.util.stream.Collector; +import static java.util.stream.Collector.Characteristics.*; + +public class ToListCollector implements Collector, List> { + + @Override + public Supplier> supplier() { + return () -> new ArrayList(); + } + + @Override + public BiConsumer, T> accumulator() { + return (list, item) -> list.add(item); + } + + @Override + public Function, List> finisher() { + return i -> i; + } + + @Override + public BinaryOperator> combiner() { + return (list1, list2) -> { + list1.addAll(list2); + return list1; + }; + } + + @Override + public Set characteristics() { + return Collections.unmodifiableSet(EnumSet.of(IDENTITY_FINISH, CONCURRENT)); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap7/ForkJoinSumCalculator.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap7/ForkJoinSumCalculator.java new file mode 100644 index 00000000..8f74a90a --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap7/ForkJoinSumCalculator.java @@ -0,0 +1,54 @@ +package lambdasinaction.chap7; + +import java.util.concurrent.RecursiveTask; +import java.util.concurrent.ForkJoinTask; +import java.util.stream.LongStream; + +import static lambdasinaction.chap7.ParallelStreamsHarness.FORK_JOIN_POOL; + +public class ForkJoinSumCalculator extends RecursiveTask { + + public static final long THRESHOLD = 10_000; + + private final long[] numbers; + private final int start; + private final int end; + + public ForkJoinSumCalculator(long[] numbers) { + this(numbers, 0, numbers.length); + } + + private ForkJoinSumCalculator(long[] numbers, int start, int end) { + this.numbers = numbers; + this.start = start; + this.end = end; + } + + @Override + protected Long compute() { + int length = end - start; + if (length <= THRESHOLD) { + return computeSequentially(); + } + ForkJoinSumCalculator leftTask = new ForkJoinSumCalculator(numbers, start, start + length/2); + leftTask.fork(); + ForkJoinSumCalculator rightTask = new ForkJoinSumCalculator(numbers, start + length/2, end); + Long rightResult = rightTask.compute(); + Long leftResult = leftTask.join(); + return leftResult + rightResult; + } + + private long computeSequentially() { + long sum = 0; + for (int i = start; i < end; i++) { + sum += numbers[i]; + } + return sum; + } + + public static long forkJoinSum(long n) { + long[] numbers = LongStream.rangeClosed(1, n).toArray(); + ForkJoinTask task = new ForkJoinSumCalculator(numbers); + return FORK_JOIN_POOL.invoke(task); + } +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap7/ParallelStreamBenchmark.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap7/ParallelStreamBenchmark.java new file mode 100644 index 00000000..cdd16aca --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap7/ParallelStreamBenchmark.java @@ -0,0 +1,62 @@ +package lambdasinaction.chap7; + +import java.util.concurrent.TimeUnit; +import java.util.stream.LongStream; +import java.util.stream.Stream; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; +import org.openjdk.jmh.annotations.Warmup; + +@State(Scope.Thread) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@Fork(value=2, jvmArgs={"-Xms4G", "-Xmx4G"}) +@Measurement(iterations=2) +@Warmup(iterations=3) +public class ParallelStreamBenchmark { + + private static final long N = 10_000_000L; + + @Benchmark + public long iterativeSum() { + long result = 0; + for (long i = 1L; i <= N; i++) { + result += i; + } + return result; + } + + @Benchmark + public long sequentialSum() { + return Stream.iterate( 1L, i -> i + 1 ).limit(N).reduce( 0L, Long::sum ); + } + + @Benchmark + public long parallelSum() { + return Stream.iterate(1L, i -> i + 1).limit(N).parallel().reduce( 0L, Long::sum); + } + + @Benchmark + public long rangedSum() { + return LongStream.rangeClosed( 1, N ).reduce( 0L, Long::sum ); + } + + @Benchmark + public long parallelRangedSum() { + return LongStream.rangeClosed(1, N).parallel().reduce( 0L, Long::sum); + } + + @TearDown(Level.Invocation) + public void tearDown() { + System.gc(); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap7/ParallelStreams.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap7/ParallelStreams.java new file mode 100644 index 00000000..e8b2a520 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap7/ParallelStreams.java @@ -0,0 +1,50 @@ +package lambdasinaction.chap7; + +import java.util.stream.*; + +public class ParallelStreams { + + public static long iterativeSum(long n) { + long result = 0; + for (long i = 0; i <= n; i++) { + result += i; + } + return result; + } + + public static long sequentialSum(long n) { + return Stream.iterate(1L, i -> i + 1).limit(n).reduce(Long::sum).get(); + } + + public static long parallelSum(long n) { + return Stream.iterate(1L, i -> i + 1).limit(n).parallel().reduce(Long::sum).get(); + } + + public static long rangedSum(long n) { + return LongStream.rangeClosed(1, n).reduce(Long::sum).getAsLong(); + } + + public static long parallelRangedSum(long n) { + return LongStream.rangeClosed(1, n).parallel().reduce(Long::sum).getAsLong(); + } + + public static long sideEffectSum(long n) { + Accumulator accumulator = new Accumulator(); + LongStream.rangeClosed(1, n).forEach(accumulator::add); + return accumulator.total; + } + + public static long sideEffectParallelSum(long n) { + Accumulator accumulator = new Accumulator(); + LongStream.rangeClosed(1, n).parallel().forEach(accumulator::add); + return accumulator.total; + } + + public static class Accumulator { + private long total = 0; + + public void add(long value) { + total += value; + } + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap7/ParallelStreamsHarness.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap7/ParallelStreamsHarness.java new file mode 100644 index 00000000..7d53c86f --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap7/ParallelStreamsHarness.java @@ -0,0 +1,32 @@ +package lambdasinaction.chap7; + +import java.util.concurrent.*; +import java.util.function.*; + +public class ParallelStreamsHarness { + + public static final ForkJoinPool FORK_JOIN_POOL = new ForkJoinPool(); + + public static void main(String[] args) { + System.out.println("Iterative Sum done in: " + measurePerf(ParallelStreams::iterativeSum, 10_000_000L) + " msecs"); + System.out.println("Sequential Sum done in: " + measurePerf(ParallelStreams::sequentialSum, 10_000_000L) + " msecs"); + System.out.println("Parallel forkJoinSum done in: " + measurePerf(ParallelStreams::parallelSum, 10_000_000L) + " msecs" ); + System.out.println("Range forkJoinSum done in: " + measurePerf(ParallelStreams::rangedSum, 10_000_000L) + " msecs"); + System.out.println("Parallel range forkJoinSum done in: " + measurePerf(ParallelStreams::parallelRangedSum, 10_000_000L) + " msecs" ); + System.out.println("ForkJoin sum done in: " + measurePerf(ForkJoinSumCalculator::forkJoinSum, 10_000_000L) + " msecs" ); + System.out.println("SideEffect sum done in: " + measurePerf(ParallelStreams::sideEffectSum, 10_000_000L) + " msecs" ); + System.out.println("SideEffect prallel sum done in: " + measurePerf(ParallelStreams::sideEffectParallelSum, 10_000_000L) + " msecs" ); + } + + public static long measurePerf(Function f, T input) { + long fastest = Long.MAX_VALUE; + for (int i = 0; i < 10; i++) { + long start = System.nanoTime(); + R result = f.apply(input); + long duration = (System.nanoTime() - start) / 1_000_000; + System.out.println("Result: " + result); + if (duration < fastest) fastest = duration; + } + return fastest; + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap7/WordCount.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap7/WordCount.java new file mode 100644 index 00000000..13ccce52 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap7/WordCount.java @@ -0,0 +1,116 @@ +package lambdasinaction.chap7; + +import java.util.*; +import java.util.function.*; +import java.util.stream.*; + +public class WordCount { + + public static final String SENTENCE = + " Nel mezzo del cammin di nostra vita " + + "mi ritrovai in una selva oscura" + + " che la dritta via era smarrita "; + + public static void main(String[] args) { + System.out.println("Found " + countWordsIteratively(SENTENCE) + " words"); + System.out.println("Found " + countWords(SENTENCE) + " words"); + } + + public static int countWordsIteratively(String s) { + int counter = 0; + boolean lastSpace = true; + for (char c : s.toCharArray()) { + if (Character.isWhitespace(c)) { + lastSpace = true; + } else { + if (lastSpace) counter++; + lastSpace = Character.isWhitespace(c); + } + } + return counter; + } + + public static int countWords(String s) { + //Stream stream = IntStream.range(0, s.length()) + // .mapToObj(SENTENCE::charAt).parallel(); + Spliterator spliterator = new WordCounterSpliterator(s); + Stream stream = StreamSupport.stream(spliterator, true); + + return countWords(stream); + } + + private static int countWords(Stream stream) { + WordCounter wordCounter = stream.reduce(new WordCounter(0, true), + WordCounter::accumulate, + WordCounter::combine); + return wordCounter.getCounter(); + } + + private static class WordCounter { + private final int counter; + private final boolean lastSpace; + + public WordCounter(int counter, boolean lastSpace) { + this.counter = counter; + this.lastSpace = lastSpace; + } + + public WordCounter accumulate(Character c) { + if (Character.isWhitespace(c)) { + return lastSpace ? this : new WordCounter(counter, true); + } else { + return lastSpace ? new WordCounter(counter+1, false) : this; + } + } + + public WordCounter combine(WordCounter wordCounter) { + return new WordCounter(counter + wordCounter.counter, wordCounter.lastSpace); + } + + public int getCounter() { + return counter; + } + } + + private static class WordCounterSpliterator implements Spliterator { + + private final String string; + private int currentChar = 0; + + private WordCounterSpliterator(String string) { + this.string = string; + } + + @Override + public boolean tryAdvance(Consumer action) { + action.accept(string.charAt(currentChar++)); + return currentChar < string.length(); + } + + @Override + public Spliterator trySplit() { + int currentSize = string.length() - currentChar; + if (currentSize < 10) { + return null; + } + for (int splitPos = currentSize / 2 + currentChar; splitPos < string.length(); splitPos++) { + if (Character.isWhitespace(string.charAt(splitPos))) { + Spliterator spliterator = new WordCounterSpliterator(string.substring(currentChar, splitPos)); + currentChar = splitPos; + return spliterator; + } + } + return null; + } + + @Override + public long estimateSize() { + return string.length() - currentChar; + } + + @Override + public int characteristics() { + return ORDERED + SIZED + SUBSIZED + NONNULL + IMMUTABLE; + } + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/ChainOfResponsibilityMain.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/ChainOfResponsibilityMain.java new file mode 100644 index 00000000..91d52e0a --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/ChainOfResponsibilityMain.java @@ -0,0 +1,59 @@ +package lambdasinaction.chap8; + +import java.util.function.Function; +import java.util.function.UnaryOperator; + + +public class ChainOfResponsibilityMain { + + public static void main(String[] args) { + ProcessingObject p1 = new HeaderTextProcessing(); + ProcessingObject p2 = new SpellCheckerProcessing(); + p1.setSuccessor(p2); + String result1 = p1.handle("Aren't labdas really sexy?!!"); + System.out.println(result1); + + + UnaryOperator headerProcessing = + (String text) -> "From Raoul, Mario and Alan: " + text; + UnaryOperator spellCheckerProcessing = + (String text) -> text.replaceAll("labda", "lambda"); + Function pipeline = headerProcessing.andThen(spellCheckerProcessing); + String result2 = pipeline.apply("Aren't labdas really sexy?!!"); + System.out.println(result2); + } + + static private abstract class ProcessingObject { + protected ProcessingObject successor; + + public void setSuccessor(ProcessingObject successor) { + this.successor = successor; + } + + public T handle(T input) { + T r = handleWork(input); + if (successor != null) { + return successor.handle(r); + } + return r; + } + + abstract protected T handleWork(T input); + } + + static private class HeaderTextProcessing + extends ProcessingObject { + public String handleWork(String text) { + return "From Raoul, Mario and Alan: " + text; + } + } + + static private class SpellCheckerProcessing + extends ProcessingObject { + public String handleWork(String text) { + return text.replaceAll("labda", "lambda"); + } + } +} + + diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/Debugging.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/Debugging.java new file mode 100644 index 00000000..40c4dd23 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/Debugging.java @@ -0,0 +1,30 @@ +package lambdasinaction.chap8; + + +import java.util.*; + +public class Debugging{ + public static void main(String[] args) { + List points = Arrays.asList(new Point(12, 2), null); + points.stream().map(p -> p.getX()).forEach(System.out::println); + } + + + private static class Point{ + private int x; + private int y; + + private Point(int x, int y) { + this.x = x; + this.y = y; + } + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/FactoryMain.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/FactoryMain.java new file mode 100644 index 00000000..e30d219e --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/FactoryMain.java @@ -0,0 +1,48 @@ +package lambdasinaction.chap8; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Supplier; + + +public class FactoryMain { + + public static void main(String[] args) { + Product p1 = ProductFactory.createProduct("loan"); + + Supplier loanSupplier = Loan::new; + Product p2 = loanSupplier.get(); + + Product p3 = ProductFactory.createProductLambda("loan"); + + } + + static private class ProductFactory { + public static Product createProduct(String name){ + switch(name){ + case "loan": return new Loan(); + case "stock": return new Stock(); + case "bond": return new Bond(); + default: throw new RuntimeException("No such product " + name); + } + } + + public static Product createProductLambda(String name){ + Supplier p = map.get(name); + if(p != null) return p.get(); + throw new RuntimeException("No such product " + name); + } + } + + static private interface Product {} + static private class Loan implements Product {} + static private class Stock implements Product {} + static private class Bond implements Product {} + + final static private Map> map = new HashMap<>(); + static { + map.put("loan", Loan::new); + map.put("stock", Stock::new); + map.put("bond", Bond::new); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/ObserverMain.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/ObserverMain.java new file mode 100644 index 00000000..dcd8cb50 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/ObserverMain.java @@ -0,0 +1,79 @@ +package lambdasinaction.chap8; + +import java.util.ArrayList; +import java.util.List; + + +public class ObserverMain { + + public static void main(String[] args) { + Feed f = new Feed(); + f.registerObserver(new NYTimes()); + f.registerObserver(new Guardian()); + f.registerObserver(new LeMonde()); + f.notifyObservers("The queen said her favourite book is Java 8 in Action!"); + + + Feed feedLambda = new Feed(); + + feedLambda.registerObserver((String tweet) -> { + if(tweet != null && tweet.contains("money")){ + System.out.println("Breaking news in NY! " + tweet); } + }); + feedLambda.registerObserver((String tweet) -> { + if(tweet != null && tweet.contains("queen")){ + System.out.println("Yet another news in London... " + tweet); } + }); + + feedLambda.notifyObservers("Money money money, give me money!"); + + } + + + interface Observer{ + void inform(String tweet); + } + + interface Subject{ + void registerObserver(Observer o); + void notifyObservers(String tweet); + } + + static private class NYTimes implements Observer{ + @Override + public void inform(String tweet) { + if(tweet != null && tweet.contains("money")){ + System.out.println("Breaking news in NY!" + tweet); + } + } + } + + static private class Guardian implements Observer{ + @Override + public void inform(String tweet) { + if(tweet != null && tweet.contains("queen")){ + System.out.println("Yet another news in London... " + tweet); + } + } + } + + static private class LeMonde implements Observer{ + @Override + public void inform(String tweet) { + if(tweet != null && tweet.contains("wine")){ + System.out.println("Today cheese, wine and news! " + tweet); + } + } + } + + static private class Feed implements Subject{ + private final List observers = new ArrayList<>(); + public void registerObserver(Observer o) { + this.observers.add(o); + } + public void notifyObservers(String tweet) { + observers.forEach(o -> o.inform(tweet)); + } + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/OnlineBanking.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/OnlineBanking.java new file mode 100644 index 00000000..27cbf917 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/OnlineBanking.java @@ -0,0 +1,18 @@ +package lambdasinaction.chap8; + + +abstract class OnlineBanking { + public void processCustomer(int id){ + Customer c = Database.getCustomerWithId(id); + makeCustomerHappy(c); + } + abstract void makeCustomerHappy(Customer c); + + + // dummy Customer class + static private class Customer {} + // dummy Datbase class + static private class Database{ + static Customer getCustomerWithId(int id){ return new Customer();} + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/OnlineBankingLambda.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/OnlineBankingLambda.java new file mode 100644 index 00000000..3bd9e049 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/OnlineBankingLambda.java @@ -0,0 +1,23 @@ +package lambdasinaction.chap8; + +import java.util.function.Consumer; + + +public class OnlineBankingLambda { + + public static void main(String[] args) { + new OnlineBankingLambda().processCustomer(1337, (Customer c) -> System.out.println("Hello!")); + } + + public void processCustomer(int id, Consumer makeCustomerHappy){ + Customer c = Database.getCustomerWithId(id); + makeCustomerHappy.accept(c); + } + + // dummy Customer class + static private class Customer {} + // dummy Database class + static private class Database{ + static Customer getCustomerWithId(int id){ return new Customer();} + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/Peek.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/Peek.java new file mode 100644 index 00000000..e25d68a4 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/Peek.java @@ -0,0 +1,19 @@ +package lambdasinaction.chap8; + +import java.util.List; +import java.util.stream.Stream; + +import static java.util.stream.Collectors.toList; + + +public class Peek { + + public static void main(String[] args) { + + List result = Stream.of(2, 3, 4, 5) + .peek(x -> System.out.println("taking from stream: " + x)).map(x -> x + 17) + .peek(x -> System.out.println("after map: " + x)).filter(x -> x % 2 == 0) + .peek(x -> System.out.println("after filter: " + x)).limit(3) + .peek(x -> System.out.println("after limit: " + x)).collect(toList()); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/StrategyMain.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/StrategyMain.java new file mode 100644 index 00000000..cc899aa8 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap8/StrategyMain.java @@ -0,0 +1,44 @@ +package lambdasinaction.chap8; + + +public class StrategyMain { + + public static void main(String[] args) { + // old school + Validator v1 = new Validator(new IsNumeric()); + System.out.println(v1.validate("aaaa")); + Validator v2 = new Validator(new IsAllLowerCase ()); + System.out.println(v2.validate("bbbb")); + + + // with lambdas + Validator v3 = new Validator((String s) -> s.matches("\\d+")); + System.out.println(v3.validate("aaaa")); + Validator v4 = new Validator((String s) -> s.matches("[a-z]+")); + System.out.println(v4.validate("bbbb")); + } + + interface ValidationStrategy { + public boolean execute(String s); + } + + static private class IsAllLowerCase implements ValidationStrategy { + public boolean execute(String s){ + return s.matches("[a-z]+"); + } + } + static private class IsNumeric implements ValidationStrategy { + public boolean execute(String s){ + return s.matches("\\d+"); + } + } + + static private class Validator{ + private final ValidationStrategy strategy; + public Validator(ValidationStrategy v){ + this.strategy = v; + } + public boolean validate(String s){ + return strategy.execute(s); } + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Ambiguous.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Ambiguous.java new file mode 100644 index 00000000..d007bedc --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Ambiguous.java @@ -0,0 +1,26 @@ +package lambdasinaction.chap9; + +public class Ambiguous{ + + public static void main(String... args) { + new C().hello(); + } + + static interface A{ + public default void hello() { + System.out.println("Hello from A"); + } + } + + static interface B { + public default void hello() { + System.out.println("Hello from B"); + } + } + + static class C implements B, A { + public void hello(){ + A.super.hello(); + } + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Diamond.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Diamond.java new file mode 100644 index 00000000..f0226e1d --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Diamond.java @@ -0,0 +1,23 @@ +package lambdasinaction.chap9; + +public class Diamond{ + + public static void main(String...args){ + new D().hello(); + } + + static interface A{ + public default void hello(){ + System.out.println("Hello from A"); + } + } + + static interface B extends A { } + + static interface C extends A { + } + + static class D implements B, C { + + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Drawable.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Drawable.java new file mode 100644 index 00000000..731a8772 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Drawable.java @@ -0,0 +1,8 @@ +package lambdasinaction.chap9; + +/** + * Created by raoul-gabrielurma on 15/01/2014. + */ +public interface Drawable{ + public void draw(); +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Ellipse.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Ellipse.java new file mode 100644 index 00000000..8f2d1ae0 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Ellipse.java @@ -0,0 +1,36 @@ +package lambdasinaction.chap9; + +/** + * Created by raoul-gabrielurma on 15/01/2014. + */ +public class Ellipse implements Resizable { + @Override + public int getWidth() { + return 0; + } + + @Override + public int getHeight() { + return 0; + } + + @Override + public void setWidth(int width) { + + } + + @Override + public void setHeight(int height) { + + } + + @Override + public void setAbsoluteSize(int width, int height) { + + } + + @Override + public void draw() { + + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Game.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Game.java new file mode 100644 index 00000000..0836f46c --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Game.java @@ -0,0 +1,16 @@ +package lambdasinaction.chap9; + + +import java.util.Arrays; +import java.util.List; + +public class Game { + + public static void main(String...args){ + List resizableShapes = + Arrays.asList(new Square(), + new Triangle(), new Ellipse()); + Utils.paint(resizableShapes); + } +} + diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Intro.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Intro.java new file mode 100644 index 00000000..d540a705 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Intro.java @@ -0,0 +1,17 @@ +package lambdasinaction.chap9; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + +public class Intro{ + + public static void main(String...args){ + + List numbers = Arrays.asList(3, 5, 1, 2, 6); + // sort is a default method + // naturalOrder is a static method + numbers.sort(Comparator.naturalOrder()); + System.out.println(numbers); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Letter.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Letter.java new file mode 100644 index 00000000..2ff65193 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Letter.java @@ -0,0 +1,28 @@ +package lambdasinaction.chap9; + +import java.util.function.Function; + +public class Letter{ + public static String addHeader(String text){ + return "From Raoul, Mario and Alan:" + text; + } + + public static String addFooter(String text){ + return text + "Kind regards"; + } + + public static String checkSpelling(String text){ + return text.replaceAll("C\\+\\+", "**Censored**"); + } + + + public static void main(String...args){ + Function addHeader = Letter::addHeader; + Function transformationPipeline + = addHeader.andThen(Letter::checkSpelling) + .andThen(Letter::addFooter); + + System.out.println(transformationPipeline.apply("C++ stay away from me!")); + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/MostSpecific.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/MostSpecific.java new file mode 100644 index 00000000..2a845e72 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/MostSpecific.java @@ -0,0 +1,38 @@ +package lambdasinaction.chap9; + +public class MostSpecific{ + + public static void main(String... args) { + new C().hello(); + new E().hello(); + new G().hello(); + } + + static interface A{ + public default void hello() { + System.out.println("Hello from A"); + } + } + + static interface B extends A{ + public default void hello() { + System.out.println("Hello from B"); + } + } + + static class C implements B, A {} + + static class D implements A{} + + static class E extends D implements B, A{} + + static class F implements B, A { + public void hello() { + System.out.println("Hello from F"); + } + } + + static class G extends F implements B, A{} + +} + diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/README b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/README new file mode 100644 index 00000000..bdbebd1f --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/README @@ -0,0 +1,37 @@ +To try out the scenario described in section 8.1 on Evolving APIs you need to do the following: + +compile individual files as follows from the directory src/main/java: + +javac lambdasinaction/chap8/Resizable.java +javac lambdasinaction/chap8/Ellipse.java +javac lambdasinaction/chap8/Utils.java +javac lambdasinaction/chap8/Game.java + +You can run the application and everything will work: + +java lambdasinaction/chap8/Game + +You can now modify the interface Resizable and add the method "setRelativeSize". +Compile and run, no problem: + +javac lambdasinaction/chap8/Resizable.java + +Now modify Utils to use the new setRelativeSize method available on all kinds of Resizable. +Just uncomment the appropriate the line in Utils, compile, run, and you'll have a surprise! + +Exception in thread "main" java.lang.AbstractMethodError: lambdasinaction.chap8.Square.setRelativeSize(II)V + +Note also that recompiling the whole application will fail because Ellipse doesn't implement +the new method setRelativeSize: + +javac lambdasinaction/chap8/Ellipse.java +lambdasinaction/chap7/Ellipse.java:6: error: Ellipse is not abstract and does not override abstract method setRelativeSize(int,int) in Resizable +public class Ellipse implements Resizable { + ^ +1 error + +The problem can be fixed by ensuring that setRelativeSize is a default method: + +public default void setRelativeSize(int widthFactor, int heightFactor){ + // a default implementation +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Resizable.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Resizable.java new file mode 100644 index 00000000..c197b2bb --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Resizable.java @@ -0,0 +1,12 @@ +package lambdasinaction.chap9; + +public interface Resizable extends Drawable{ + public int getWidth(); + public int getHeight(); + public void setWidth(int width); + public void setHeight(int height); + public void setAbsoluteSize(int width, int height); + //TODO: uncomment, read the README for instructions + //public void setRelativeSize(int widthFactor, int heightFactor); +} + diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Square.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Square.java new file mode 100644 index 00000000..11c2397f --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Square.java @@ -0,0 +1,36 @@ +package lambdasinaction.chap9; + +/** + * Created by raoul-gabrielurma on 15/01/2014. + */ +public class Square implements Resizable { + @Override + public int getWidth() { + return 0; + } + + @Override + public int getHeight() { + return 0; + } + + @Override + public void setWidth(int width) { + + } + + @Override + public void setHeight(int height) { + + } + + @Override + public void setAbsoluteSize(int width, int height) { + + } + + @Override + public void draw() { + + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Triangle.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Triangle.java new file mode 100644 index 00000000..ab98fa72 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Triangle.java @@ -0,0 +1,36 @@ +package lambdasinaction.chap9; + +/** + * Created by raoul-gabrielurma on 15/01/2014. + */ +public class Triangle implements Resizable { + @Override + public int getWidth() { + return 0; + } + + @Override + public int getHeight() { + return 0; + } + + @Override + public void setWidth(int width) { + + } + + @Override + public void setHeight(int height) { + + } + + @Override + public void setAbsoluteSize(int width, int height) { + + } + + @Override + public void draw() { + + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Utils.java b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Utils.java new file mode 100644 index 00000000..9e2af4bd --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/chap9/Utils.java @@ -0,0 +1,13 @@ +package lambdasinaction.chap9; + +import java.util.List; + +public class Utils{ + public static void paint(List l){ + l.forEach(r -> { r.setAbsoluteSize(42, 42); }); + + //TODO: uncomment, read the README for instructions + //l.forEach(r -> { r.setRelativeSize(2, 2); }); + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/Grouping.java b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/Grouping.java new file mode 100644 index 00000000..b1472cf0 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/Grouping.java @@ -0,0 +1,79 @@ +/* + * Copyright 2005 JBoss Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package lambdasinaction.dsl; + +import lambdasinaction.chap6.Dish; + +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collector; + +import static java.util.stream.Collectors.groupingBy; +import static lambdasinaction.chap6.Dish.menu; +import static lambdasinaction.dsl.Grouping.GroupingBuilder.groupOn; + +public class Grouping { + + enum CaloricLevel { DIET, NORMAL, FAT }; + + public static void main(String ... args) { + System.out.println("Dishes grouped by type and caloric level: " + groupDishedByTypeAndCaloricLevel2()); + System.out.println("Dishes grouped by type and caloric level: " + groupDishedByTypeAndCaloricLevel3()); + } + + private static CaloricLevel getCaloricLevel( Dish dish ) { + if (dish.getCalories() <= 400) return CaloricLevel.DIET; + else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL; + else return CaloricLevel.FAT; + } + + private static Map>> groupDishedByTypeAndCaloricLevel2() { + return menu.stream().collect( + twoLevelGroupingBy(Dish::getType, dish -> getCaloricLevel( dish ) ) + ); + } + + public static Collector>>> twoLevelGroupingBy(Function f1, Function f2) { + return groupingBy(f1, groupingBy(f2)); + } + + private static Map>> groupDishedByTypeAndCaloricLevel3() { + Collector>>> c = groupOn( ( Dish dish ) -> getCaloricLevel( dish ) ).after( Dish::getType ).get(); + return menu.stream().collect( c ); + } + + public static class GroupingBuilder { + private final Collector> collector; + + public GroupingBuilder( Collector> collector ) { + this.collector = collector; + } + + public Collector> get() { + return collector; + } + + public GroupingBuilder, J> after(Function classifier) { + return new GroupingBuilder<>( groupingBy( classifier, collector ) ); + } + + public static GroupingBuilder, K> groupOn(Function classifier) { + return new GroupingBuilder<>( groupingBy( classifier ) ); + } + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/LambdaOrderBuilder.java b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/LambdaOrderBuilder.java new file mode 100644 index 00000000..6746c3b1 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/LambdaOrderBuilder.java @@ -0,0 +1,83 @@ +/* + * Copyright 2005 JBoss Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package lambdasinaction.dsl; + +import lambdasinaction.dsl.model.Order; +import lambdasinaction.dsl.model.Stock; +import lambdasinaction.dsl.model.Trade; + +import java.util.function.Consumer; + +public class LambdaOrderBuilder { + + private Order order = new Order(); + + public static Order order(Consumer consumer) { + LambdaOrderBuilder builder = new LambdaOrderBuilder(); + consumer.accept( builder ); + return builder.order; + } + + public void forCustomer(String customer) { + order.setCustomer( customer ); + } + + public void buy(Consumer consumer) { + trade( consumer, Trade.Type.BUY ); + } + + public void sell(Consumer consumer) { + trade( consumer, Trade.Type.SELL ); + } + + private void trade( Consumer consumer, Trade.Type type ) { + TradeBuilder builder = new TradeBuilder(); + builder.trade.setType( type ); + consumer.accept( builder ); + order.addTrade( builder.trade ); + } + + public static class TradeBuilder { + private Trade trade = new Trade(); + + public void quantity(int quantity) { + trade.setQuantity( quantity ); + } + + public void price(double price) { + trade.setPrice( price ); + } + + public void stock(Consumer consumer) { + StockBuilder builder = new StockBuilder(); + consumer.accept( builder ); + trade.setStock( builder.stock ); + } + } + + public static class StockBuilder { + private Stock stock = new Stock(); + + public void symbol(String symbol) { + stock.setSymbol( symbol ); + } + + public void market(String market) { + stock.setMarket( market ); + } + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/Main.java b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/Main.java new file mode 100644 index 00000000..b50683e5 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/Main.java @@ -0,0 +1,98 @@ +/* + * Copyright 2005 JBoss Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package lambdasinaction.dsl; + +import lambdasinaction.dsl.model.Order; +import lambdasinaction.dsl.model.Stock; +import lambdasinaction.dsl.model.Trade; + +import static lambdasinaction.dsl.MethodChainingOrderBuilder.forCustomer; +import static lambdasinaction.dsl.NestedFunctionOrderBuilder.*; + +public class Main { + + public void plain() { + Order order = new Order(); + order.setCustomer( "BigBank" ); + + Trade trade1 = new Trade(); + trade1.setType( Trade.Type.BUY ); + + Stock stock1 = new Stock(); + stock1.setSymbol( "IBM" ); + stock1.setMarket( "NYSE" ); + + trade1.setStock( stock1 ); + trade1.setPrice( 125.00 ); + trade1.setQuantity( 80 ); + order.addTrade( trade1 ); + + Trade trade2 = new Trade(); + trade2.setType( Trade.Type.BUY ); + + Stock stock2 = new Stock(); + stock2.setSymbol( "GOOGLE" ); + stock2.setMarket( "NASDAQ" ); + + trade2.setStock( stock2 ); + trade2.setPrice( 375.00 ); + trade2.setQuantity( 50 ); + order.addTrade( trade2 ); + } + + public void methodChaining() { + Order order = forCustomer( "BigBank" ) + .buy( 80 ).stock( "IBM" ).on( "NYSE" ).at( 125.00 ) + .sell( 50 ).stock( "GOOGLE" ).on( "NASDAQ" ).at( 375.00 ) + .end(); + + } + + public void nestedFunction() { + Order order = order("BigBank", + buy(80, + stock( "IBM", on( "NYSE" ) ), + at(125.00)), + sell(50, + stock("GOOGLE", on("NASDAQ")), + at(375.00)) + ); + } + + public void lambda() { + Order order = LambdaOrderBuilder.order( o -> { + o.forCustomer( "BigBank" ); + o.buy( t -> { + t.quantity( 80 ); + t.price( 125.00 ); + t.stock( s -> { + s.symbol( "IBM" ); + s.market( "NYSE" ); + } ); + }); + o.sell( t -> { + t.quantity( 50 ); + t.price( 375.00 ); + t.stock( s -> { + s.symbol( "GOOGLE" ); + s.market( "NASDAQ" ); + } ); + }); + } ); + } + +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/MethodChainingOrderBuilder.java b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/MethodChainingOrderBuilder.java new file mode 100644 index 00000000..7d771ec5 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/MethodChainingOrderBuilder.java @@ -0,0 +1,99 @@ +/* + * Copyright 2005 JBoss Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package lambdasinaction.dsl; + +import lambdasinaction.dsl.model.Order; +import lambdasinaction.dsl.model.Stock; +import lambdasinaction.dsl.model.Trade; + +public class MethodChainingOrderBuilder { + + public final Order order = new Order(); + + private MethodChainingOrderBuilder(String customer) { + order.setCustomer( customer ); + } + + public static MethodChainingOrderBuilder forCustomer( String customer ) { + return new MethodChainingOrderBuilder(customer); + } + + public Order end() { + return order; + } + + public TradeBuilder buy(int quantity) { + return new TradeBuilder( this, Trade.Type.BUY, quantity ); + } + + public TradeBuilder sell(int quantity) { + return new TradeBuilder( this, Trade.Type.SELL, quantity ); + } + + private MethodChainingOrderBuilder addTrade(Trade trade) { + order.addTrade( trade ); + return this; + } + + public static class TradeBuilder { + private final MethodChainingOrderBuilder builder; + public final Trade trade = new Trade(); + + private TradeBuilder(MethodChainingOrderBuilder builder, Trade.Type type, int quantity) { + this.builder = builder; + trade.setType( type ); + trade.setQuantity( quantity ); + } + + public StockBuilder stock(String symbol) { + return new StockBuilder( builder, trade, symbol ); + } + } + + public static class TradeBuilderWithStock { + private final MethodChainingOrderBuilder builder; + private final Trade trade; + + public TradeBuilderWithStock( MethodChainingOrderBuilder builder, Trade trade ) { + this.builder = builder; + this.trade = trade; + } + + public MethodChainingOrderBuilder at(double price) { + trade.setPrice( price ); + return builder.addTrade( trade ); + } + } + + public static class StockBuilder { + private final MethodChainingOrderBuilder builder; + private final Trade trade; + private final Stock stock = new Stock(); + + private StockBuilder(MethodChainingOrderBuilder builder, Trade trade, String symbol) { + this.builder = builder; + this.trade = trade; + stock.setSymbol( symbol ); + } + + public TradeBuilderWithStock on(String market) { + stock.setMarket( market ); + trade.setStock( stock ); + return new TradeBuilderWithStock( builder, trade ); + } + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/Mixed.java b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/Mixed.java new file mode 100644 index 00000000..132dd998 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/Mixed.java @@ -0,0 +1,39 @@ +/* + * Copyright 2005 JBoss Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package lambdasinaction.dsl; + +import lambdasinaction.dsl.model.Order; + +import static lambdasinaction.dsl.MixedBuilder.buy; +import static lambdasinaction.dsl.MixedBuilder.sell; +import static lambdasinaction.dsl.MixedBuilder.forCustomer; + +public class Mixed { + public void mixed() { + Order order = + forCustomer( "BigBank", + buy( t -> t.quantity( 80 ) + .stock( "IBM" ) + .on( "NYSE" ) + .at( 125.00 )), + sell( t -> t.quantity( 50 ) + .stock( "GOOGLE" ) + .on( "NASDAQ" ) + .at( 125.00 )) ); + + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/MixedBuilder.java b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/MixedBuilder.java new file mode 100644 index 00000000..98d7ea97 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/MixedBuilder.java @@ -0,0 +1,85 @@ +/* + * Copyright 2005 JBoss Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package lambdasinaction.dsl; + +import lambdasinaction.dsl.model.Order; +import lambdasinaction.dsl.model.Stock; +import lambdasinaction.dsl.model.Trade; + +import java.util.function.Consumer; +import java.util.stream.Stream; + +public class MixedBuilder { + + public static Order forCustomer(String customer, TradeBuilder... builders) { + Order order = new Order(); + order.setCustomer( customer ); + Stream.of(builders).forEach( b -> order.addTrade( b.trade ) ); + return order; + } + + public static TradeBuilder buy(Consumer consumer) { + return buildTrade( consumer, Trade.Type.BUY ); + } + + public static TradeBuilder sell(Consumer consumer) { + return buildTrade( consumer, Trade.Type.SELL ); + } + + private static TradeBuilder buildTrade( Consumer consumer, Trade.Type buy ) { + TradeBuilder builder = new TradeBuilder(); + builder.trade.setType( buy ); + consumer.accept( builder ); + return builder; + } + + public static class TradeBuilder { + private Trade trade = new Trade(); + + public TradeBuilder quantity(int quantity) { + trade.setQuantity( quantity ); + return this; + } + + public TradeBuilder at(double price) { + trade.setPrice( price ); + return this; + } + + public StockBuilder stock(String symbol) { + return new StockBuilder(this, trade, symbol); + } + } + + public static class StockBuilder { + private final TradeBuilder builder; + private final Trade trade; + private final Stock stock = new Stock(); + + private StockBuilder(TradeBuilder builder, Trade trade, String symbol) { + this.builder = builder; + this.trade = trade; + stock.setSymbol( symbol ); + } + + public TradeBuilder on(String market) { + stock.setMarket( market ); + trade.setStock( stock ); + return builder; + } + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/NestedFunctionOrderBuilder.java b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/NestedFunctionOrderBuilder.java new file mode 100644 index 00000000..b9daaac9 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/NestedFunctionOrderBuilder.java @@ -0,0 +1,64 @@ +/* + * Copyright 2005 JBoss Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package lambdasinaction.dsl; + +import lambdasinaction.dsl.model.Order; +import lambdasinaction.dsl.model.Stock; +import lambdasinaction.dsl.model.Trade; + +import java.util.stream.Stream; + +public class NestedFunctionOrderBuilder { + + public static Order order(String customer, Trade... trades) { + Order order = new Order(); + order.setCustomer( customer ); + Stream.of(trades).forEach( order::addTrade ); + return order; + } + + public static Trade buy(int quantity, Stock stock, double price) { + return buildTrade( stock, price, Trade.Type.BUY ); + } + + public static Trade sell(int quantity, Stock stock, double price) { + return buildTrade( stock, price, Trade.Type.SELL ); + } + + private static Trade buildTrade( Stock stock, double price, Trade.Type buy ) { + Trade trade = new Trade(); + trade.setType( buy ); + trade.setStock( stock ); + trade.setPrice( price ); + return trade; + } + + public static double at(double price) { + return price; + } + + public static Stock stock(String symbol, String market) { + Stock stock = new Stock(); + stock.setSymbol( symbol ); + stock.setMarket( market ); + return stock; + } + + public static String on(String market) { + return market; + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/TaxCalculator.java b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/TaxCalculator.java new file mode 100644 index 00000000..9ef38cef --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/TaxCalculator.java @@ -0,0 +1,81 @@ +/* + * Copyright 2005 JBoss Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package lambdasinaction.dsl; + +import lambdasinaction.dsl.model.Order; +import lambdasinaction.dsl.model.Tax; + +import java.util.function.Function; + +public class TaxCalculator { + + public static double calculate( Order order, boolean useRegional, boolean useGeneral, boolean useSurcharge ) { + double value = order.getValue(); + if (useRegional) value = Tax.regional(value); + if (useGeneral) value = Tax.general(value); + if (useSurcharge) value = Tax.surcharge(value); + return value; + } + + private boolean useRegional; + private boolean useGeneral; + private boolean useSurcharge; + + public TaxCalculator withTaxRegional() { + useRegional = true; + return this; + } + + public TaxCalculator withTaxGeneral() { + useGeneral= true; + return this; + } + + public TaxCalculator withTaxSurcharge() { + useSurcharge = true; + return this; + } + + public double calculate(Order order) { + return calculate( order, useRegional, useGeneral, useSurcharge ); + } + + public Function taxFuncion = Function.identity(); + + public TaxCalculator with(Function f) { + taxFuncion.andThen( f ); + return this; + } + + public double calculateF(Order order) { + return taxFuncion.apply( order.getValue() ); + } + + public static void main(String[] args) { + Order order = new Order(); + + double value = TaxCalculator.calculate( order, true, false, true ); + + value = new TaxCalculator().withTaxRegional() + .withTaxSurcharge() + .calculate( order ); + + value = new TaxCalculator().with(Tax::regional) + .with(Tax::surcharge) + .calculate( order ); + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/model/Order.java b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/model/Order.java new file mode 100644 index 00000000..21c06747 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/model/Order.java @@ -0,0 +1,43 @@ +/* + * Copyright 2005 JBoss Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package lambdasinaction.dsl.model; + +import java.util.ArrayList; +import java.util.List; + +public class Order { + + private String customer; + + private List trades = new ArrayList<>(); + + public void addTrade( Trade trade ) { + trades.add( trade ); + } + + public String getCustomer() { + return customer; + } + + public void setCustomer( String customer ) { + this.customer = customer; + } + + public double getValue() { + return trades.stream().mapToDouble( Trade::getValue ).sum(); + } +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/model/Stock.java b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/model/Stock.java new file mode 100644 index 00000000..1e0302e2 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/model/Stock.java @@ -0,0 +1,40 @@ +/* + * Copyright 2005 JBoss Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package lambdasinaction.dsl.model; + +public class Stock { + + private String symbol; + + private String market; + + public String getSymbol() { + return symbol; + } + + public void setSymbol( String symbol ) { + this.symbol = symbol; + } + + public String getMarket() { + return market; + } + + public void setMarket( String market ) { + this.market = market; + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/model/Tax.java b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/model/Tax.java new file mode 100644 index 00000000..d2d6bf22 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/model/Tax.java @@ -0,0 +1,31 @@ +/* + * Copyright 2005 JBoss Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package lambdasinaction.dsl.model; + +public class Tax { + public static double regional(double value) { + return value * 1.1; + } + + public static double general(double value) { + return value * 1.3; + } + + public static double surcharge(double value) { + return value * 1.05; + } +} diff --git a/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/model/Trade.java b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/model/Trade.java new file mode 100644 index 00000000..c8f35813 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/lambdasinaction/dsl/model/Trade.java @@ -0,0 +1,66 @@ +/* + * Copyright 2005 JBoss Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package lambdasinaction.dsl.model; + +public class Trade { + + public enum Type { BUY, SELL } + + private Type type; + + private Stock stock; + + private int quantity; + + private double price; + + public Type getType() { + return type; + } + + public void setType( Type type ) { + this.type = type; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity( int quantity ) { + this.quantity = quantity; + } + + public double getPrice() { + return price; + } + + public void setPrice( double price ) { + this.price = price; + } + + public Stock getStock() { + return stock; + } + + public void setStock( Stock stock ) { + this.stock = stock; + } + + public double getValue() { + return quantity * price; + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Atomic1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Atomic1.java new file mode 100644 index 00000000..233f8f3a --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Atomic1.java @@ -0,0 +1,70 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.IntStream; + +/** + * @author Benjamin Winterberg + */ +public class Atomic1 { + + private static final int NUM_INCREMENTS = 1000; + + private static AtomicInteger atomicInt = new AtomicInteger(0); + + public static void main(String[] args) { + testIncrement(); + testAccumulate(); + testUpdate(); + } + + private static void testUpdate() { + atomicInt.set(0); + + ExecutorService executor = Executors.newFixedThreadPool(2); + + IntStream.range(0, NUM_INCREMENTS) + .forEach(i -> { + Runnable task = () -> + atomicInt.updateAndGet(n -> n + 2); + executor.submit(task); + }); + + ConcurrentUtils.stop(executor); + + System.out.format("Update: %d\n", atomicInt.get()); + } + + private static void testAccumulate() { + atomicInt.set(0); + + ExecutorService executor = Executors.newFixedThreadPool(2); + + IntStream.range(0, NUM_INCREMENTS) + .forEach(i -> { + Runnable task = () -> + atomicInt.accumulateAndGet(i, (n, m) -> n + m); + executor.submit(task); + }); + + ConcurrentUtils.stop(executor); + + System.out.format("Accumulate: %d\n", atomicInt.get()); + } + + private static void testIncrement() { + atomicInt.set(0); + + ExecutorService executor = Executors.newFixedThreadPool(2); + + IntStream.range(0, NUM_INCREMENTS) + .forEach(i -> executor.submit(atomicInt::incrementAndGet)); + + ConcurrentUtils.stop(executor); + + System.out.format("Increment: Expected=%d; Is=%d\n", NUM_INCREMENTS, atomicInt.get()); + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/CompletableFuture1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/CompletableFuture1.java new file mode 100644 index 00000000..f2aff549 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/CompletableFuture1.java @@ -0,0 +1,22 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +/** + * @author Benjamin Winterberg + */ +public class CompletableFuture1 { + + public static void main(String[] args) throws ExecutionException, InterruptedException { + CompletableFuture future = new CompletableFuture<>(); + + future.complete("42"); + + future + .thenAccept(System.out::println) + .thenAccept(v -> System.out.println("done")); + + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/ConcurrentHashMap1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/ConcurrentHashMap1.java new file mode 100644 index 00000000..a5597d65 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/ConcurrentHashMap1.java @@ -0,0 +1,77 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ForkJoinPool; + +/** + * @author Benjamin Winterberg + */ +public class ConcurrentHashMap1 { + + public static void main(String[] args) { + System.out.println("Parallelism: " + ForkJoinPool.getCommonPoolParallelism()); + + testForEach(); + testSearch(); + testReduce(); + } + + private static void testReduce() { + ConcurrentHashMap map = new ConcurrentHashMap<>(); + map.putIfAbsent("foo", "bar"); + map.putIfAbsent("han", "solo"); + map.putIfAbsent("r2", "d2"); + map.putIfAbsent("c3", "p0"); + + String reduced = map.reduce(1, (key, value) -> key + "=" + value, + (s1, s2) -> s1 + ", " + s2); + + System.out.println(reduced); + } + + private static void testSearch() { + ConcurrentHashMap map = new ConcurrentHashMap<>(); + map.putIfAbsent("foo", "bar"); + map.putIfAbsent("han", "solo"); + map.putIfAbsent("r2", "d2"); + map.putIfAbsent("c3", "p0"); + + System.out.println("\nsearch()\n"); + + String result1 = map.search(1, (key, value) -> { + System.out.println(Thread.currentThread().getName()); + if (key.equals("foo") && value.equals("bar")) { + return "foobar"; + } + return null; + }); + + System.out.println(result1); + + System.out.println("\nsearchValues()\n"); + + String result2 = map.searchValues(1, value -> { + System.out.println(Thread.currentThread().getName()); + if (value.length() > 3) { + return value; + } + return null; + }); + + System.out.println(result2); + } + + private static void testForEach() { + ConcurrentHashMap map = new ConcurrentHashMap<>(); + map.putIfAbsent("foo", "bar"); + map.putIfAbsent("han", "solo"); + map.putIfAbsent("r2", "d2"); + map.putIfAbsent("c3", "p0"); + + map.forEach(1, (key, value) -> System.out.printf("key: %s; value: %s; thread: %s\n", key, value, Thread.currentThread().getName())); +// map.forEach(5, (key, value) -> System.out.printf("key: %s; value: %s; thread: %s\n", key, value, Thread.currentThread().getName())); + + System.out.println(map.mappingCount()); + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/ConcurrentUtils.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/ConcurrentUtils.java new file mode 100644 index 00000000..3c868e94 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/ConcurrentUtils.java @@ -0,0 +1,35 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * @author Benjamin Winterberg + */ +public class ConcurrentUtils { + + public static void stop(ExecutorService executor) { + try { + executor.shutdown(); + executor.awaitTermination(60, TimeUnit.SECONDS); + } + catch (InterruptedException e) { + System.err.println("termination interrupted"); + } + finally { + if (!executor.isTerminated()) { + System.err.println("killing non-finished tasks"); + } + executor.shutdownNow(); + } + } + + public static void sleep(int seconds) { + try { + TimeUnit.SECONDS.sleep(seconds); + } catch (InterruptedException e) { + throw new IllegalStateException(e); + } + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Executors1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Executors1.java new file mode 100644 index 00000000..e5eeb7f0 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Executors1.java @@ -0,0 +1,49 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +/** + * @author Benjamin Winterberg + */ +public class Executors1 { + + public static void main(String[] args) { + test1(3); +// test1(7); + } + + private static void test1(long seconds) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + executor.submit(() -> { + try { + TimeUnit.SECONDS.sleep(seconds); + String name = Thread.currentThread().getName(); + System.out.println("task finished: " + name); + } + catch (InterruptedException e) { + System.err.println("task interrupted"); + } + }); + stop(executor); + } + + static void stop(ExecutorService executor) { + try { + System.out.println("attempt to shutdown executor"); + executor.shutdown(); + executor.awaitTermination(5, TimeUnit.SECONDS); + } + catch (InterruptedException e) { + System.err.println("termination interrupted"); + } + finally { + if (!executor.isTerminated()) { + System.err.println("killing non-finished tasks"); + } + executor.shutdownNow(); + System.out.println("shutdown finished"); + } + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Executors2.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Executors2.java new file mode 100644 index 00000000..c174dfcb --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Executors2.java @@ -0,0 +1,72 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.*; + +/** + * @author Benjamin Winterberg + */ +public class Executors2 { + + public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException { +// test1(); +// test2(); + test3(); + } + + private static void test3() throws InterruptedException, ExecutionException, TimeoutException { + ExecutorService executor = Executors.newFixedThreadPool(1); + + Future future = executor.submit(() -> { + try { + TimeUnit.SECONDS.sleep(2); + return 123; + } + catch (InterruptedException e) { + throw new IllegalStateException("task interrupted", e); + } + }); + + future.get(1, TimeUnit.SECONDS); + } + + private static void test2() throws InterruptedException, ExecutionException { + ExecutorService executor = Executors.newFixedThreadPool(1); + + Future future = executor.submit(() -> { + try { + TimeUnit.SECONDS.sleep(1); + return 123; + } + catch (InterruptedException e) { + throw new IllegalStateException("task interrupted", e); + } + }); + + executor.shutdownNow(); + future.get(); + } + + private static void test1() throws InterruptedException, ExecutionException { + ExecutorService executor = Executors.newFixedThreadPool(1); + + Future future = executor.submit(() -> { + try { + TimeUnit.SECONDS.sleep(1); + return 123; + } + catch (InterruptedException e) { + throw new IllegalStateException("task interrupted", e); + } + }); + + System.out.println("future done: " + future.isDone()); + + Integer result = future.get(); + + System.out.println("future done: " + future.isDone()); + System.out.print("result: " + result); + + executor.shutdownNow(); + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Executors3.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Executors3.java new file mode 100644 index 00000000..d5da94a0 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Executors3.java @@ -0,0 +1,102 @@ +package winterbe.java8.samples.concurrent; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.*; + +/** + * @author Benjamin Winterberg + */ +public class Executors3 { + + public static void main(String[] args) throws InterruptedException, ExecutionException { + test1(); +// test2(); +// test3(); + +// test4(); +// test5(); + } + + private static void test5() throws InterruptedException, ExecutionException { + ExecutorService executor = Executors.newWorkStealingPool(); + + List> callables = Arrays.asList( + callable("task1", 2), + callable("task2", 1), + callable("task3", 3)); + + String result = executor.invokeAny(callables); + System.out.println(result); + + executor.shutdown(); + } + + private static Callable callable(String result, long sleepSeconds) { + return () -> { + TimeUnit.SECONDS.sleep(sleepSeconds); + return result; + }; + } + + private static void test4() throws InterruptedException { + ExecutorService executor = Executors.newWorkStealingPool(); + + List> callables = Arrays.asList( + () -> "task1", + () -> "task2", + () -> "task3"); + + executor.invokeAll(callables) + .stream() + .map(future -> { + try { + return future.get(); + } + catch (Exception e) { + throw new IllegalStateException(e); + } + }) + .forEach(System.out::println); + + executor.shutdown(); + } + + private static void test3() { + ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); + + Runnable task = () -> { + try { + TimeUnit.SECONDS.sleep(2); + System.out.println("Scheduling: " + System.nanoTime()); + } + catch (InterruptedException e) { + System.err.println("task interrupted"); + } + }; + + executor.scheduleWithFixedDelay(task, 0, 1, TimeUnit.SECONDS); + } + + private static void test2() { + ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); + Runnable task = () -> System.out.println("Scheduling: " + System.nanoTime()); + int initialDelay = 0; + int period = 1; + executor.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.SECONDS); + } + + private static void test1() throws InterruptedException { + ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); + + Runnable task = () -> System.out.println("Scheduling: " + System.nanoTime()); + int delay = 3; + ScheduledFuture future = executor.schedule(task, delay, TimeUnit.SECONDS); + + TimeUnit.MILLISECONDS.sleep(1337); + + long remainingDelay = future.getDelay(TimeUnit.MILLISECONDS); + System.out.printf("Remaining Delay: %sms\n", remainingDelay); + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock1.java new file mode 100644 index 00000000..15f6688d --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock1.java @@ -0,0 +1,45 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.locks.ReentrantLock; +import java.util.stream.IntStream; + +/** + * @author Benjamin Winterberg + */ +public class Lock1 { + + private static final int NUM_INCREMENTS = 10000; + + private static ReentrantLock lock = new ReentrantLock(); + + private static int count = 0; + + private static void increment() { + lock.lock(); + try { + count++; + } finally { + lock.unlock(); + } + } + + public static void main(String[] args) { + testLock(); + } + + private static void testLock() { + count = 0; + + ExecutorService executor = Executors.newFixedThreadPool(2); + + IntStream.range(0, NUM_INCREMENTS) + .forEach(i -> executor.submit(Lock1::increment)); + + ConcurrentUtils.stop(executor); + + System.out.println(count); + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock2.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock2.java new file mode 100644 index 00000000..07c9a052 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock2.java @@ -0,0 +1,36 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.locks.ReentrantLock; + +/** + * @author Benjamin Winterberg + */ +public class Lock2 { + + public static void main(String[] args) { + ExecutorService executor = Executors.newFixedThreadPool(2); + + ReentrantLock lock = new ReentrantLock(); + + executor.submit(() -> { + lock.lock(); + try { + ConcurrentUtils.sleep(1); + } finally { + lock.unlock(); + } + }); + + executor.submit(() -> { + System.out.println("Locked: " + lock.isLocked()); + System.out.println("Held by me: " + lock.isHeldByCurrentThread()); + boolean locked = lock.tryLock(); + System.out.println("Lock acquired: " + locked); + }); + + ConcurrentUtils.stop(executor); + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock3.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock3.java new file mode 100644 index 00000000..f52f231c --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock3.java @@ -0,0 +1,47 @@ +package winterbe.java8.samples.concurrent; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * @author Benjamin Winterberg + */ +public class Lock3 { + + public static void main(String[] args) { + ExecutorService executor = Executors.newFixedThreadPool(2); + + Map map = new HashMap<>(); + + ReadWriteLock lock = new ReentrantReadWriteLock(); + + executor.submit(() -> { + lock.writeLock().lock(); + try { + ConcurrentUtils.sleep(1); + map.put("foo", "bar"); + } finally { + lock.writeLock().unlock(); + } + }); + + Runnable readTask = () -> { + lock.readLock().lock(); + try { + System.out.println(map.get("foo")); + ConcurrentUtils.sleep(1); + } finally { + lock.readLock().unlock(); + } + }; + executor.submit(readTask); + executor.submit(readTask); + + ConcurrentUtils.stop(executor); + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock4.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock4.java new file mode 100644 index 00000000..655a0ae2 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock4.java @@ -0,0 +1,46 @@ +package winterbe.java8.samples.concurrent; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.locks.StampedLock; + +/** + * @author Benjamin Winterberg + */ +public class Lock4 { + + public static void main(String[] args) { + ExecutorService executor = Executors.newFixedThreadPool(2); + + Map map = new HashMap<>(); + + StampedLock lock = new StampedLock(); + + executor.submit(() -> { + long stamp = lock.writeLock(); + try { + ConcurrentUtils.sleep(1); + map.put("foo", "bar"); + } finally { + lock.unlockWrite(stamp); + } + }); + + Runnable readTask = () -> { + long stamp = lock.readLock(); + try { + System.out.println(map.get("foo")); + ConcurrentUtils.sleep(1); + } finally { + lock.unlockRead(stamp); + } + }; + executor.submit(readTask); + executor.submit(readTask); + + ConcurrentUtils.stop(executor); + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock5.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock5.java new file mode 100644 index 00000000..9589336a --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock5.java @@ -0,0 +1,44 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.locks.StampedLock; + +/** + * @author Benjamin Winterberg + */ +public class Lock5 { + + public static void main(String[] args) { + ExecutorService executor = Executors.newFixedThreadPool(2); + + StampedLock lock = new StampedLock(); + + executor.submit(() -> { + long stamp = lock.tryOptimisticRead(); + try { + System.out.println("Optimistic Lock Valid: " + lock.validate(stamp)); + ConcurrentUtils.sleep(1); + System.out.println("Optimistic Lock Valid: " + lock.validate(stamp)); + ConcurrentUtils.sleep(2); + System.out.println("Optimistic Lock Valid: " + lock.validate(stamp)); + } finally { + lock.unlock(stamp); + } + }); + + executor.submit(() -> { + long stamp = lock.writeLock(); + try { + System.out.println("Write Lock acquired"); + ConcurrentUtils.sleep(2); + } finally { + lock.unlock(stamp); + System.out.println("Write done"); + } + }); + + ConcurrentUtils.stop(executor); + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock6.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock6.java new file mode 100644 index 00000000..fe9fc572 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Lock6.java @@ -0,0 +1,39 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.locks.StampedLock; + +/** + * @author Benjamin Winterberg + */ +public class Lock6 { + + private static int count = 0; + + public static void main(String[] args) { + ExecutorService executor = Executors.newFixedThreadPool(2); + + StampedLock lock = new StampedLock(); + + executor.submit(() -> { + long stamp = lock.readLock(); + try { + if (count == 0) { + stamp = lock.tryConvertToWriteLock(stamp); + if (stamp == 0L) { + System.out.println("Could not convert to write lock"); + stamp = lock.writeLock(); + } + count = 23; + } + System.out.println(count); + } finally { + lock.unlock(stamp); + } + }); + + ConcurrentUtils.stop(executor); + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/LongAccumulator1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/LongAccumulator1.java new file mode 100644 index 00000000..0edf8dba --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/LongAccumulator1.java @@ -0,0 +1,31 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.LongAccumulator; +import java.util.function.LongBinaryOperator; +import java.util.stream.IntStream; + +/** + * @author Benjamin Winterberg + */ +public class LongAccumulator1 { + + public static void main(String[] args) { + testAccumulate(); + } + + private static void testAccumulate() { + LongBinaryOperator op = (x, y) -> 2 * x + y; + LongAccumulator accumulator = new LongAccumulator(op, 1L); + + ExecutorService executor = Executors.newFixedThreadPool(2); + + IntStream.range(0, 10) + .forEach(i -> executor.submit(() -> accumulator.accumulate(i))); + + ConcurrentUtils.stop(executor); + + System.out.format("Add: %d\n", accumulator.getThenReset()); + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/LongAdder1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/LongAdder1.java new file mode 100644 index 00000000..03677dc6 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/LongAdder1.java @@ -0,0 +1,43 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.LongAdder; +import java.util.stream.IntStream; + +/** + * @author Benjamin Winterberg + */ +public class LongAdder1 { + + private static final int NUM_INCREMENTS = 10000; + + private static LongAdder adder = new LongAdder(); + + public static void main(String[] args) { + testIncrement(); + testAdd(); + } + + private static void testAdd() { + ExecutorService executor = Executors.newFixedThreadPool(2); + + IntStream.range(0, NUM_INCREMENTS) + .forEach(i -> executor.submit(() -> adder.add(2))); + + ConcurrentUtils.stop(executor); + + System.out.format("Add: %d\n", adder.sumThenReset()); + } + + private static void testIncrement() { + ExecutorService executor = Executors.newFixedThreadPool(2); + + IntStream.range(0, NUM_INCREMENTS) + .forEach(i -> executor.submit(adder::increment)); + + ConcurrentUtils.stop(executor); + + System.out.format("Increment: Expected=%d; Is=%d\n", NUM_INCREMENTS, adder.sumThenReset()); + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Semaphore1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Semaphore1.java new file mode 100644 index 00000000..9c795c16 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Semaphore1.java @@ -0,0 +1,51 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; +import java.util.stream.IntStream; + +/** + * @author Benjamin Winterberg + */ +public class Semaphore1 { + + private static final int NUM_INCREMENTS = 10000; + + private static Semaphore semaphore = new Semaphore(1); + + private static int count = 0; + + public static void main(String[] args) { + testIncrement(); + } + + private static void testIncrement() { + ExecutorService executor = Executors.newFixedThreadPool(2); + + IntStream.range(0, NUM_INCREMENTS) + .forEach(i -> executor.submit(Semaphore1::increment)); + + ConcurrentUtils.stop(executor); + + System.out.println("Increment: " + count); + } + + private static void increment() { + boolean permit = false; + try { + permit = semaphore.tryAcquire(5, TimeUnit.SECONDS); + count++; + } + catch (InterruptedException e) { + throw new RuntimeException("could not increment"); + } + finally { + if (permit) { + semaphore.release(); + } + } + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Semaphore2.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Semaphore2.java new file mode 100644 index 00000000..f78d6443 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Semaphore2.java @@ -0,0 +1,44 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; +import java.util.stream.IntStream; + +/** + * @author Benjamin Winterberg + */ +public class Semaphore2 { + + private static Semaphore semaphore = new Semaphore(5); + + public static void main(String[] args) { + ExecutorService executor = Executors.newFixedThreadPool(10); + + IntStream.range(0, 10) + .forEach(i -> executor.submit(Semaphore2::doWork)); + + ConcurrentUtils.stop(executor); + } + + private static void doWork() { + boolean permit = false; + try { + permit = semaphore.tryAcquire(1, TimeUnit.SECONDS); + if (permit) { + System.out.println("Semaphore acquired"); + ConcurrentUtils.sleep(5); + } else { + System.out.println("Could not acquire semaphore"); + } + } catch (InterruptedException e) { + throw new IllegalStateException(e); + } finally { + if (permit) { + semaphore.release(); + } + } + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Synchronized1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Synchronized1.java new file mode 100644 index 00000000..320f51ea --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Synchronized1.java @@ -0,0 +1,55 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.stream.IntStream; + +/** + * @author Benjamin Winterberg + */ +public class Synchronized1 { + + private static final int NUM_INCREMENTS = 10000; + + private static int count = 0; + + public static void main(String[] args) { + testSyncIncrement(); + testNonSyncIncrement(); + } + + private static void testSyncIncrement() { + count = 0; + + ExecutorService executor = Executors.newFixedThreadPool(2); + + IntStream.range(0, NUM_INCREMENTS) + .forEach(i -> executor.submit(Synchronized1::incrementSync)); + + ConcurrentUtils.stop(executor); + + System.out.println(" Sync: " + count); + } + + private static void testNonSyncIncrement() { + count = 0; + + ExecutorService executor = Executors.newFixedThreadPool(2); + + IntStream.range(0, NUM_INCREMENTS) + .forEach(i -> executor.submit(Synchronized1::increment)); + + ConcurrentUtils.stop(executor); + + System.out.println("NonSync: " + count); + } + + private static synchronized void incrementSync() { + count = count + 1; + } + + private static void increment() { + count = count + 1; + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Synchronized2.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Synchronized2.java new file mode 100644 index 00000000..de67f1ed --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Synchronized2.java @@ -0,0 +1,39 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.stream.IntStream; + +/** + * @author Benjamin Winterberg + */ +public class Synchronized2 { + + private static final int NUM_INCREMENTS = 10000; + + private static int count = 0; + + public static void main(String[] args) { + testSyncIncrement(); + } + + private static void testSyncIncrement() { + count = 0; + + ExecutorService executor = Executors.newFixedThreadPool(2); + + IntStream.range(0, NUM_INCREMENTS) + .forEach(i -> executor.submit(Synchronized2::incrementSync)); + + ConcurrentUtils.stop(executor); + + System.out.println(count); + } + + private static void incrementSync() { + synchronized (Synchronized2.class) { + count = count + 1; + } + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Threads1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Threads1.java new file mode 100644 index 00000000..7a6a92d3 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/concurrent/Threads1.java @@ -0,0 +1,61 @@ +package winterbe.java8.samples.concurrent; + +import java.util.concurrent.TimeUnit; + +/** + * @author Benjamin Winterberg + */ +public class Threads1 { + + public static void main(String[] args) { + test1(); +// test2(); +// test3(); + } + + private static void test3() { + Runnable runnable = () -> { + try { + System.out.println("Foo " + Thread.currentThread().getName()); + TimeUnit.SECONDS.sleep(1); + System.out.println("Bar " + Thread.currentThread().getName()); + } + catch (InterruptedException e) { + e.printStackTrace(); + } + }; + + Thread thread = new Thread(runnable); + thread.start(); + } + + private static void test2() { + Runnable runnable = () -> { + try { + System.out.println("Foo " + Thread.currentThread().getName()); + Thread.sleep(1000); + System.out.println("Bar " + Thread.currentThread().getName()); + } + catch (InterruptedException e) { + e.printStackTrace(); + } + }; + + Thread thread = new Thread(runnable); + thread.start(); + } + + private static void test1() { + Runnable runnable = () -> { + String threadName = Thread.currentThread().getName(); + System.out.println("Hello " + threadName); + }; + + runnable.run(); + + Thread thread = new Thread(runnable); + thread.start(); + + System.out.println("Done!"); + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Interface1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Interface1.java new file mode 100644 index 00000000..02f47cd3 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Interface1.java @@ -0,0 +1,35 @@ +package winterbe.java8.samples.lambda; + +/** + * @author Benjamin Winterberg + */ +public class Interface1 { + + interface Formula { + double calculate(int a); + + default double sqrt(int a) { + return Math.sqrt(positive(a)); + } + + static int positive(int a) { + return a > 0 ? a : 0; + } + } + + public static void main(String[] args) { + Formula formula1 = new Formula() { + @Override + public double calculate(int a) { + return sqrt(a * 100); + } + }; + + formula1.calculate(100); // 100.0 + formula1.sqrt(-23); // 0.0 + Formula.positive(-4); // 0.0 + +// Formula formula2 = (a) -> sqrt( a * 100); + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Lambda1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Lambda1.java new file mode 100644 index 00000000..e0a854b2 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Lambda1.java @@ -0,0 +1,45 @@ +package winterbe.java8.samples.lambda; + +import java.util.*; + +/** + * @author Benjamin Winterberg + */ +public class Lambda1 { + + public static void main(String[] args) { + List names = Arrays.asList("peter", "anna", "mike", "xenia"); + + Collections.sort(names, new Comparator() { + @Override + public int compare(String a, String b) { + return b.compareTo(a); + } + }); + + Collections.sort(names, (String a, String b) -> { + return b.compareTo(a); + }); + + Collections.sort(names, (String a, String b) -> b.compareTo(a)); + + Collections.sort(names, (a, b) -> b.compareTo(a)); + + System.out.println(names); + + names.sort(Collections.reverseOrder()); + + System.out.println(names); + + List names2 = Arrays.asList("peter", null, "anna", "mike", "xenia"); + names2.sort(Comparator.nullsLast(String::compareTo)); + System.out.println(names2); + + List names3 = null; + + Optional.ofNullable(names3).ifPresent(list -> list.sort(Comparator.naturalOrder())); + + System.out.println(names3); + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Lambda2.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Lambda2.java new file mode 100644 index 00000000..4cdad5cf --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Lambda2.java @@ -0,0 +1,47 @@ +package winterbe.java8.samples.lambda; + +/** + * @author Benjamin Winterberg + */ +public class Lambda2 { + + @FunctionalInterface + public static interface Converter { + T convert(F from); + } + + static class Something { + String startsWith(String s) { + return String.valueOf(s.charAt(0)); + } + } + + interface PersonFactory

{ + P create(String firstName, String lastName); + } + + public static void main(String[] args) { + Converter integerConverter1 = (from) -> Integer.valueOf(from); + Integer converted1 = integerConverter1.convert("123"); + System.out.println(converted1); // result: 123 + + + // method reference + + Converter integerConverter2 = Integer::valueOf; + Integer converted2 = integerConverter2.convert("123"); + System.out.println(converted2); // result: 123 + + + Something something = new Something(); + + Converter stringConverter = something::startsWith; + String converted3 = stringConverter.convert("Java"); + System.out.println(converted3); // result J + + // constructor reference + + PersonFactory personFactory = Person::new; + Person person = personFactory.create("Peter", "Parker"); + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Lambda3.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Lambda3.java new file mode 100644 index 00000000..85724581 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Lambda3.java @@ -0,0 +1,84 @@ +package winterbe.java8.samples.lambda; + +import java.util.Comparator; +import java.util.Objects; +import java.util.UUID; +import java.util.concurrent.Callable; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.Supplier; + +/** + * Common standard functions from the Java API. + * + * @author Benjamin Winterberg + */ +public class Lambda3 { + + @FunctionalInterface + interface Fun { + void foo(); + } + + public static void main(String[] args) throws Exception { + + // Predicates + + Predicate predicate = (s) -> s.length() > 0; + + predicate.test("foo"); // true + predicate.negate().test("foo"); // false + + Predicate nonNull = Objects::nonNull; + Predicate isNull = Objects::isNull; + + Predicate isEmpty = String::isEmpty; + Predicate isNotEmpty = isEmpty.negate(); + + + // Functions + + Function toInteger = Integer::valueOf; + Function backToString = toInteger.andThen(String::valueOf); + + backToString.apply("123"); // "123" + + + // Suppliers + + Supplier personSupplier = Person::new; + personSupplier.get(); // new Person + + + // Consumers + + Consumer greeter = (p) -> System.out.println("Hello, " + p.firstName); + greeter.accept(new Person("Luke", "Skywalker")); + + + + // Comparators + + Comparator comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName); + + Person p1 = new Person("John", "Doe"); + Person p2 = new Person("Alice", "Wonderland"); + + comparator.compare(p1, p2); // > 0 + comparator.reversed().compare(p1, p2); // < 0 + + + // Runnables + + Runnable runnable = () -> System.out.println(UUID.randomUUID()); + runnable.run(); + + + // Callables + + Callable callable = UUID::randomUUID; + callable.call(); + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Lambda4.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Lambda4.java new file mode 100644 index 00000000..33a43fa8 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Lambda4.java @@ -0,0 +1,41 @@ +package winterbe.java8.samples.lambda; + +/** + * @author Benjamin Winterberg + */ +public class Lambda4 { + + static int outerStaticNum; + + int outerNum; + + void testScopes() { + int num = 1; + + Lambda2.Converter stringConverter = + (from) -> String.valueOf(from + num); + + String convert = stringConverter.convert(2); + System.out.println(convert); // 3 + + Lambda2.Converter stringConverter2 = (from) -> { + outerNum = 13; + return String.valueOf(from); + }; + + String[] array = new String[1]; + Lambda2.Converter stringConverter3 = (from) -> { + array[0] = "Hi there"; + return String.valueOf(from); + }; + + stringConverter3.convert(23); + + System.out.println(array[0]); + } + + public static void main(String[] args) { + new Lambda4().testScopes(); + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Lambda5.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Lambda5.java new file mode 100644 index 00000000..b00c35a4 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Lambda5.java @@ -0,0 +1,32 @@ +package winterbe.java8.samples.lambda; + +import java.util.HashMap; +import java.util.function.BiConsumer; + +/** + * Created by grijesh + */ +public class Lambda5 { + + //Pre-Defined Functional Interfaces + public static void main(String... args) { + + //BiConsumer Example + BiConsumer printKeyAndValue + = (key,value) -> System.out.println(key+"-"+value); + + printKeyAndValue.accept("One",1); + printKeyAndValue.accept("Two",2); + + System.out.println("##################"); + + //Java Hash-Map foreach supports BiConsumer + HashMap dummyValues = new HashMap<>(); + dummyValues.put("One", 1); + dummyValues.put("Two", 2); + dummyValues.put("Three", 3); + + dummyValues.forEach((key,value) -> System.out.println(key+"-"+value)); + + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Person.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Person.java new file mode 100644 index 00000000..eb0201be --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/lambda/Person.java @@ -0,0 +1,16 @@ +package winterbe.java8.samples.lambda; + +/** +* @author Benjamin Winterberg +*/ +public class Person { + public String firstName; + public String lastName; + + public Person() {} + + public Person(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/Annotations1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/Annotations1.java new file mode 100644 index 00000000..16f1c075 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/Annotations1.java @@ -0,0 +1,43 @@ +package winterbe.java8.samples.misc; + +import java.lang.annotation.*; + +/** + * @author Benjamin Winterberg + */ +public class Annotations1 { + + @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE}) + @interface MyAnnotation { + + } + + @Retention(RetentionPolicy.RUNTIME) + @interface Hints { + Hint[] value(); + } + + @Repeatable(Hints.class) + @Retention(RetentionPolicy.RUNTIME) + @interface Hint { + String value(); + } + + @Hint("hint1") + @Hint("hint2") + class Person { + + } + + public static void main(String[] args) { + Hint hint = Person.class.getAnnotation(Hint.class); + System.out.println(hint); // null + + Hints hints1 = Person.class.getAnnotation(Hints.class); + System.out.println(hints1.value().length); // 2 + + Hint[] hints2 = Person.class.getAnnotationsByType(Hint.class); + System.out.println(hints2.length); // 2 + + } +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/CheckedFunctions.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/CheckedFunctions.java new file mode 100644 index 00000000..8281d9bd --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/CheckedFunctions.java @@ -0,0 +1,92 @@ +package winterbe.java8.samples.misc; + +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; + +/** + * Utilities for hassle-free usage of lambda expressions who throw checked exceptions. + * + * @author Benjamin Winterberg + */ +public final class CheckedFunctions { + + @FunctionalInterface + public interface CheckedConsumer { + void accept(T input) throws Exception; + } + + @FunctionalInterface + public interface CheckedPredicate { + boolean test(T input) throws Exception; + } + + @FunctionalInterface + public interface CheckedFunction { + T apply(F input) throws Exception; + } + + /** + * Return a function which rethrows possible checked exceptions as runtime exception. + * + * @param function + * @param + * @param + * @return + */ + public static Function function(CheckedFunction function) { + return input -> { + try { + return function.apply(input); + } + catch (Exception e) { + if (e instanceof RuntimeException) { + throw (RuntimeException) e; + } + throw new RuntimeException(e); + } + }; + } + + /** + * Return a predicate which rethrows possible checked exceptions as runtime exception. + * + * @param predicate + * @param + * @return + */ + public static Predicate predicate(CheckedPredicate predicate) { + return input -> { + try { + return predicate.test(input); + } + catch (Exception e) { + if (e instanceof RuntimeException) { + throw (RuntimeException) e; + } + throw new RuntimeException(e); + } + }; + } + + /** + * Return a consumer which rethrows possible checked exceptions as runtime exception. + * + * @param consumer + * @param + * @return + */ + public static Consumer consumer(CheckedConsumer consumer) { + return input -> { + try { + consumer.accept(input); + } + catch (Exception e) { + if (e instanceof RuntimeException) { + throw (RuntimeException) e; + } + throw new RuntimeException(e); + } + }; + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/Concurrency1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/Concurrency1.java new file mode 100644 index 00000000..3ee5588e --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/Concurrency1.java @@ -0,0 +1,37 @@ +package winterbe.java8.samples.misc; + +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @author Benjamin Winterberg + */ +public class Concurrency1 { + + public static void main(String[] args) { + ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap<>(); + + for (int i = 0; i < 100; i++) { + concurrentHashMap.put(i, UUID.randomUUID()); + } + + int threshold = 1; + + concurrentHashMap.forEachValue(threshold, System.out::println); + + concurrentHashMap.forEach((id, uuid) -> { + if (id % 10 == 0) { + System.out.println(String.format("%s: %s", id, uuid)); + } + }); + + UUID searchResult = concurrentHashMap.search(threshold, (id, uuid) -> { + if (String.valueOf(uuid).startsWith(String.valueOf(id))) { + return uuid; + } + return null; + }); + + System.out.println(searchResult); + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/Files1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/Files1.java new file mode 100644 index 00000000..24db1ac5 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/Files1.java @@ -0,0 +1,104 @@ +package winterbe.java8.samples.misc; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * @author Benjamin Winterberg + */ +public class Files1 { + + public static void main(String[] args) throws IOException { + testWalk(); + testFind(); + testList(); + testLines(); + testReader(); + testWriter(); + testReadWriteLines(); + testReaderLines(); + } + + private static void testReaderLines() throws IOException { + Path path = Paths.get("res/nashorn1.js"); + try (BufferedReader reader = Files.newBufferedReader(path)) { + long countPrints = reader + .lines() + .filter(line -> line.contains("print")) + .count(); + System.out.println(countPrints); + } + } + + private static void testWriter() throws IOException { + Path path = Paths.get("res/output.js"); + try (BufferedWriter writer = Files.newBufferedWriter(path)) { + writer.write("print('Hello World');"); + } + } + + private static void testReader() throws IOException { + Path path = Paths.get("res/nashorn1.js"); + try (BufferedReader reader = Files.newBufferedReader(path)) { + System.out.println(reader.readLine()); + } + } + + private static void testWalk() throws IOException { + Path start = Paths.get(""); + int maxDepth = 5; + try (Stream stream = Files.walk(start, maxDepth)) { + String joined = stream + .map(String::valueOf) + .filter(path -> path.endsWith(".js")) + .collect(Collectors.joining("; ")); + System.out.println("walk(): " + joined); + } + } + + private static void testFind() throws IOException { + Path start = Paths.get(""); + int maxDepth = 5; + try (Stream stream = Files.find(start, maxDepth, (path, attr) -> + String.valueOf(path).endsWith(".js"))) { + String joined = stream + .sorted() + .map(String::valueOf) + .collect(Collectors.joining("; ")); + System.out.println("find(): " + joined); + } + } + + private static void testList() throws IOException { + try (Stream stream = Files.list(Paths.get(""))) { + String joined = stream + .map(String::valueOf) + .filter(path -> !path.startsWith(".")) + .sorted() + .collect(Collectors.joining("; ")); + System.out.println("list(): " + joined); + } + } + + private static void testLines() throws IOException { + try (Stream stream = Files.lines(Paths.get("res/nashorn1.js"))) { + stream + .filter(line -> line.contains("print")) + .map(String::trim) + .forEach(System.out::println); + } + } + + private static void testReadWriteLines() throws IOException { + List lines = Files.readAllLines(Paths.get("res/nashorn1.js")); + lines.add("print('foobar');"); + Files.write(Paths.get("res", "nashorn1-modified.js"), lines); + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/Maps1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/Maps1.java new file mode 100644 index 00000000..d41a889a --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/Maps1.java @@ -0,0 +1,48 @@ +package winterbe.java8.samples.misc; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Benjamin Winterberg + */ +public class Maps1 { + + public static void main(String[] args) { + Map map = new HashMap<>(); + + for (int i = 0; i < 10; i++) { + map.putIfAbsent(i, "val" + i); + } + + map.forEach((id, val) -> System.out.println(val)); + + + map.computeIfPresent(3, (num, val) -> val + num); + System.out.println(map.get(3)); // val33 + + map.computeIfPresent(9, (num, val) -> null); + System.out.println(map.containsKey(9)); // false + + map.computeIfAbsent(23, num -> "val" + num); + System.out.println(map.containsKey(23)); // true + + map.computeIfAbsent(3, num -> "bam"); + System.out.println(map.get(3)); // val33 + + System.out.println(map.getOrDefault(42, "not found")); // not found + + map.remove(3, "val3"); + System.out.println(map.get(3)); // val33 + + map.remove(3, "val33"); + System.out.println(map.get(3)); // null + + map.merge(9, "val9", (value, newValue) -> value.concat(newValue)); + System.out.println(map.get(9)); // val9 + + map.merge(9, "concat", (value, newValue) -> value.concat(newValue)); + System.out.println(map.get(9)); // val9concat + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/Math1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/Math1.java new file mode 100644 index 00000000..396edf94 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/Math1.java @@ -0,0 +1,58 @@ +package winterbe.java8.samples.misc; + +/** + * @author Benjamin Winterberg + */ +public class Math1 { + + public static void main(String[] args) { + testMathExact(); + testUnsignedInt(); + } + + private static void testUnsignedInt() { + try { + Integer.parseUnsignedInt("-123", 10); + } + catch (NumberFormatException e) { + System.out.println(e.getMessage()); + } + + long maxUnsignedInt = (1l << 32) - 1; + System.out.println(maxUnsignedInt); + + String string = String.valueOf(maxUnsignedInt); + + int unsignedInt = Integer.parseUnsignedInt(string, 10); + System.out.println(unsignedInt); + + String string2 = Integer.toUnsignedString(unsignedInt, 10); + System.out.println(string2); + + try { + Integer.parseInt(string, 10); + } + catch (NumberFormatException e) { + System.err.println("could not parse signed int of " + maxUnsignedInt); + } + } + + private static void testMathExact() { + System.out.println(Integer.MAX_VALUE); + System.out.println(Integer.MAX_VALUE + 1); + + try { + Math.addExact(Integer.MAX_VALUE, 1); + } + catch (ArithmeticException e) { + System.err.println(e.getMessage()); + } + + try { + Math.toIntExact(Long.MAX_VALUE); + } + catch (ArithmeticException e) { + System.err.println(e.getMessage()); + } + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/String1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/String1.java new file mode 100644 index 00000000..36640d44 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/misc/String1.java @@ -0,0 +1,49 @@ +package winterbe.java8.samples.misc; + +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * @author Benjamin Winterberg + */ +public class String1 { + + public static void main(String[] args) { + testJoin(); + testChars(); + testPatternPredicate(); + testPatternSplit(); + } + + private static void testChars() { + String string = "foobar:foo:bar" + .chars() + .distinct() + .mapToObj(c -> String.valueOf((char) c)) + .sorted() + .collect(Collectors.joining()); + System.out.println(string); + } + + private static void testPatternSplit() { + String string = Pattern.compile(":") + .splitAsStream("foobar:foo:bar") + .filter(s -> s.contains("bar")) + .sorted() + .collect(Collectors.joining(":")); + System.out.println(string); + } + + private static void testPatternPredicate() { + long count = Stream.of("bob@gmail.com", "alice@hotmail.com") + .filter(Pattern.compile(".*@gmail\\.com").asPredicate()) + .count(); + System.out.println(count); + } + + private static void testJoin() { + String string = String.join(":", "foobar", "foo", "bar"); + System.out.println(string); + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn1.java new file mode 100644 index 00000000..9d8425c0 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn1.java @@ -0,0 +1,33 @@ +package winterbe.java8.samples.nashorn; + +import winterbe.java8.samples.lambda.Person; + +import javax.script.Invocable; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import java.io.FileReader; +import java.time.LocalDateTime; +import java.util.Date; + +/** + * Calling javascript functions from java with nashorn. + * + * @author Benjamin Winterberg + */ +public class Nashorn1 { + + public static void main(String[] args) throws Exception { + ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); + engine.eval(new FileReader("res/nashorn1.js")); + + Invocable invocable = (Invocable) engine; + Object result = invocable.invokeFunction("fun1", "Peter Parker"); + System.out.println(result); + System.out.println(result.getClass()); + + invocable.invokeFunction("fun2", new Date()); + invocable.invokeFunction("fun2", LocalDateTime.now()); + invocable.invokeFunction("fun2", new Person()); + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn10.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn10.java new file mode 100644 index 00000000..1edda9d9 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn10.java @@ -0,0 +1,27 @@ +package winterbe.java8.samples.nashorn; + +import jdk.nashorn.api.scripting.NashornScriptEngine; + +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import java.util.concurrent.TimeUnit; + +/** + * @author Benjamin Winterberg + */ +public class Nashorn10 { + + public static void main(String[] args) throws ScriptException, NoSuchMethodException { + NashornScriptEngine engine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn"); + engine.eval("load('res/nashorn10.js')"); + + long t0 = System.nanoTime(); + + for (int i = 0; i < 100000; i++) { + engine.invokeFunction("testPerf"); + } + + long took = System.nanoTime() - t0; + System.out.format("Elapsed time: %d ms", TimeUnit.NANOSECONDS.toMillis(took)); + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn11.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn11.java new file mode 100644 index 00000000..9089eb52 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn11.java @@ -0,0 +1,152 @@ +package winterbe.java8.samples.nashorn; + +import jdk.nashorn.api.scripting.NashornScriptEngine; + +import javax.script.*; + +/** + * @author Benjamin Winterberg + */ +public class Nashorn11 { + + public static void main(String[] args) throws Exception { +// test1(); +// test2(); +// test3(); +// test4(); +// test5(); +// test6(); +// test7(); + test8(); + } + + private static void test8() throws ScriptException { + NashornScriptEngine engine = createEngine(); + + engine.eval("var obj = { foo: 23 };"); + + ScriptContext defaultContext = engine.getContext(); + Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); + + SimpleScriptContext context1 = new SimpleScriptContext(); + context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); + + SimpleScriptContext context2 = new SimpleScriptContext(); + context2.getBindings(ScriptContext.ENGINE_SCOPE).put("obj", defaultBindings.get("obj")); + + engine.eval("obj.foo = 44;", context1); + engine.eval("print(obj.foo);", context1); + engine.eval("print(obj.foo);", context2); + } + + private static void test7() throws ScriptException { + NashornScriptEngine engine = createEngine(); + + engine.eval("var foo = 23;"); + + ScriptContext defaultContext = engine.getContext(); + Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); + + SimpleScriptContext context1 = new SimpleScriptContext(); + context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); + + SimpleScriptContext context2 = new SimpleScriptContext(); + context2.getBindings(ScriptContext.ENGINE_SCOPE).put("foo", defaultBindings.get("foo")); + + engine.eval("foo = 44;", context1); + engine.eval("print(foo);", context1); + engine.eval("print(foo);", context2); + } + + private static void test6() throws ScriptException { + NashornScriptEngine engine = createEngine(); + + ScriptContext defaultContext = engine.getContext(); + defaultContext.getBindings(ScriptContext.GLOBAL_SCOPE).put("foo", "hello"); + + ScriptContext customContext = new SimpleScriptContext(); + customContext.setBindings(defaultContext.getBindings(ScriptContext.ENGINE_SCOPE), ScriptContext.ENGINE_SCOPE); + + Bindings bindings = new SimpleBindings(); + bindings.put("foo", "world"); + customContext.setBindings(bindings, ScriptContext.GLOBAL_SCOPE); + +// engine.eval("foo = 23;"); // overrides foo in all contexts, why??? + + engine.eval("print(foo)"); // hello + engine.eval("print(foo)", customContext); // world + engine.eval("print(foo)", defaultContext); // hello + } + + private static void test5() throws ScriptException { + NashornScriptEngine engine = createEngine(); + + engine.eval("var obj = { foo: 'foo' };"); + engine.eval("function printFoo() { print(obj.foo) };"); + + ScriptContext defaultContext = engine.getContext(); + Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); + + SimpleScriptContext context1 = new SimpleScriptContext(); + context1.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); + + SimpleScriptContext context2 = new SimpleScriptContext(); + context2.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); + + engine.eval("obj.foo = 'bar';", context1); + engine.eval("printFoo();", context1); + engine.eval("printFoo();", context2); + } + + private static void test4() throws ScriptException { + NashornScriptEngine engine = createEngine(); + + engine.eval("function foo() { print('bar') };"); + + ScriptContext defaultContext = engine.getContext(); + Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); + + SimpleScriptContext context = new SimpleScriptContext(); + context.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); + + engine.eval("foo();", context); + System.out.println(context.getAttribute("foo")); + } + + private static void test3() throws ScriptException { + NashornScriptEngine engine = createEngine(); + + ScriptContext defaultContext = engine.getContext(); + Bindings defaultBindings = defaultContext.getBindings(ScriptContext.ENGINE_SCOPE); + + SimpleScriptContext context = new SimpleScriptContext(); + context.setBindings(defaultBindings, ScriptContext.ENGINE_SCOPE); + + engine.eval("function foo() { print('bar') };", context); + engine.eval("foo();", context); + + Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE); + System.out.println(bindings.get("foo")); + System.out.println(context.getAttribute("foo")); + } + + private static void test2() throws ScriptException { + NashornScriptEngine engine = createEngine(); + engine.eval("function foo() { print('bar') };"); + + SimpleScriptContext context = new SimpleScriptContext(); + engine.eval("print(Function);", context); + engine.eval("foo();", context); + } + + private static void test1() throws ScriptException { + NashornScriptEngine engine = createEngine(); + engine.eval("function foo() { print('bar') };"); + engine.eval("foo();"); + } + + private static NashornScriptEngine createEngine() { + return (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn"); + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn2.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn2.java new file mode 100644 index 00000000..6128a70a --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn2.java @@ -0,0 +1,39 @@ +package winterbe.java8.samples.nashorn; + +import jdk.nashorn.api.scripting.ScriptObjectMirror; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import java.io.FileReader; +import java.util.Arrays; + +/** + * Calling java methods from javascript with nashorn. + * + * @author Benjamin Winterberg + */ +public class Nashorn2 { + + public static String fun(String name) { + System.out.format("Hi there from Java, %s", name); + return "greetings from java"; + } + + public static void fun2(Object object) { + System.out.println(object.getClass()); + } + + public static void fun3(ScriptObjectMirror mirror) { + System.out.println(mirror.getClassName() + ": " + Arrays.toString(mirror.getOwnKeys(true))); + } + + public static void fun4(ScriptObjectMirror person) { + System.out.println("Full Name is: " + person.callMember("getFullName")); + } + + public static void main(String[] args) throws Exception { + ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); + engine.eval(new FileReader("res/nashorn2.js")); + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn3.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn3.java new file mode 100644 index 00000000..6d11a003 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn3.java @@ -0,0 +1,18 @@ +package winterbe.java8.samples.nashorn; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; + +/** + * Working with java types from javascript. + * + * @author Benjamin Winterberg + */ +public class Nashorn3 { + + public static void main(String[] args) throws Exception { + ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); + engine.eval("load('res/nashorn3.js')"); + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn4.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn4.java new file mode 100644 index 00000000..2431c90d --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn4.java @@ -0,0 +1,18 @@ +package winterbe.java8.samples.nashorn; + +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; + +/** + * Working with java types from javascript. + * + * @author Benjamin Winterberg + */ +public class Nashorn4 { + + public static void main(String[] args) throws Exception { + ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); + engine.eval("loadWithNewGlobal('res/nashorn4.js')"); + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn5.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn5.java new file mode 100644 index 00000000..fa5d1c00 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn5.java @@ -0,0 +1,29 @@ +package winterbe.java8.samples.nashorn; + +import javax.script.Invocable; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; + +/** + * Bind java objects to custom javascript objects. + * + * @author Benjamin Winterberg + */ +public class Nashorn5 { + + public static void main(String[] args) throws Exception { + ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); + engine.eval("load('res/nashorn5.js')"); + + Invocable invocable = (Invocable) engine; + + Product product = new Product(); + product.setName("Rubber"); + product.setPrice(1.99); + product.setStock(1037); + + Object result = invocable.invokeFunction("getValueOfGoods", product); + System.out.println(result); + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn6.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn6.java new file mode 100644 index 00000000..9b3184c0 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn6.java @@ -0,0 +1,36 @@ +package winterbe.java8.samples.nashorn; + +import jdk.nashorn.api.scripting.ScriptObjectMirror; + +import javax.script.Invocable; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; + +/** + * Using Backbone Models from Nashorn. + * + * @author Benjamin Winterberg + */ +public class Nashorn6 { + + public static void main(String[] args) throws Exception { + ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); + engine.eval("load('res/nashorn6.js')"); + + Invocable invocable = (Invocable) engine; + + Product product = new Product(); + product.setName("Rubber"); + product.setPrice(1.99); + product.setStock(1337); + + ScriptObjectMirror result = (ScriptObjectMirror) + invocable.invokeFunction("calculate", product); + System.out.println(result.get("name") + ": " + result.get("valueOfGoods")); + } + + public static void getProduct(ScriptObjectMirror result) { + System.out.println(result.get("name") + ": " + result.get("valueOfGoods")); + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn7.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn7.java new file mode 100644 index 00000000..463c9bdf --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn7.java @@ -0,0 +1,43 @@ +package winterbe.java8.samples.nashorn; + +import javax.script.Invocable; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + +/** + * @author Benjamin Winterberg + */ +public class Nashorn7 { + + public static class Person { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getLengthOfName() { + return name.length(); + } + } + + public static void main(String[] args) throws ScriptException, NoSuchMethodException { + ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); + engine.eval("function foo(predicate, obj) { return !!(eval(predicate)); };"); + + Invocable invocable = (Invocable) engine; + + Person person = new Person(); + person.setName("Hans"); + + String predicate = "obj.getLengthOfName() >= 4"; + Object result = invocable.invokeFunction("foo", predicate, person); + System.out.println(result); + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn8.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn8.java new file mode 100644 index 00000000..7b7cdcd7 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn8.java @@ -0,0 +1,23 @@ +package winterbe.java8.samples.nashorn; + +import winterbe.java8.samples.lambda.Person; +import jdk.nashorn.api.scripting.NashornScriptEngine; + +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; + +/** + * @author Benjamin Winterberg + */ +public class Nashorn8 { + public static void main(String[] args) throws ScriptException, NoSuchMethodException { + NashornScriptEngine engine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn"); + engine.eval("load('res/nashorn8.js')"); + + engine.invokeFunction("evaluate1"); // [object global] + engine.invokeFunction("evaluate2"); // [object Object] + engine.invokeFunction("evaluate3", "Foobar"); // Foobar + engine.invokeFunction("evaluate3", new Person("John", "Doe")); // [object global] <- ??????? + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn9.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn9.java new file mode 100644 index 00000000..3f4a291a --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Nashorn9.java @@ -0,0 +1,31 @@ +package winterbe.java8.samples.nashorn; + +import jdk.nashorn.api.scripting.NashornScriptEngine; + +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; +import java.util.concurrent.TimeUnit; + +/** + * @author Benjamin Winterberg + */ +public class Nashorn9 { + + public static void main(String[] args) throws ScriptException, NoSuchMethodException { + NashornScriptEngine engine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn"); + engine.eval("load('res/nashorn9.js')"); + + long t0 = System.nanoTime(); + + double result = 0; + for (int i = 0; i < 1000; i++) { + double num = (double) engine.invokeFunction("testPerf"); + result += num; + } + + System.out.println(result > 0); + + long took = System.nanoTime() - t0; + System.out.format("Elapsed time: %d ms", TimeUnit.NANOSECONDS.toMillis(took)); + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Product.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Product.java new file mode 100644 index 00000000..64662b8f --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/Product.java @@ -0,0 +1,43 @@ +package winterbe.java8.samples.nashorn; + +/** + * @author Benjamin Winterberg + */ +public class Product { + private String name; + private double price; + private int stock; + private double valueOfGoods; + + public double getValueOfGoods() { + return valueOfGoods; + } + + public void setValueOfGoods(double valueOfGoods) { + this.valueOfGoods = valueOfGoods; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + public int getStock() { + return stock; + } + + public void setStock(int stock) { + this.stock = stock; + } +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/SuperRunner.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/SuperRunner.java new file mode 100644 index 00000000..aa67d683 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/nashorn/SuperRunner.java @@ -0,0 +1,13 @@ +package winterbe.java8.samples.nashorn; + +/** + * @author Benjamin Winterberg + */ +public class SuperRunner implements Runnable { + + @Override + public void run() { + System.out.println("super run"); + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Optional1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Optional1.java new file mode 100644 index 00000000..4f5cdcc6 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Optional1.java @@ -0,0 +1,20 @@ +package winterbe.java8.samples.stream; + +import java.util.Optional; + +/** + * @author Benjamin Winterberg + */ +public class Optional1 { + + public static void main(String[] args) { + Optional optional = Optional.of("bam"); + + optional.isPresent(); // true + optional.get(); // "bam" + optional.orElse("fallback"); // "bam" + + optional.ifPresent((s) -> System.out.println(s.charAt(0))); // "b" + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Optional2.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Optional2.java new file mode 100644 index 00000000..415e3972 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Optional2.java @@ -0,0 +1,76 @@ +package winterbe.java8.samples.stream; + +import java.util.Optional; +import java.util.function.Supplier; + +/** + * Examples how to avoid null checks with Optional: + * + * http://winterbe.com/posts/2015/03/15/avoid-null-checks-in-java/ + * + * @author Benjamin Winterberg + */ +public class Optional2 { + + static class Outer { + Nested nested = new Nested(); + + public Nested getNested() { + return nested; + } + } + + static class Nested { + Inner inner = new Inner(); + + public Inner getInner() { + return inner; + } + } + + static class Inner { + String foo = "boo"; + + public String getFoo() { + return foo; + } + } + + public static void main(String[] args) { + test1(); + test2(); + test3(); + } + + public static Optional resolve(Supplier resolver) { + try { + T result = resolver.get(); + return Optional.ofNullable(result); + } + catch (NullPointerException e) { + return Optional.empty(); + } + } + + private static void test3() { + Outer outer = new Outer(); + resolve(() -> outer.getNested().getInner().getFoo()) + .ifPresent(System.out::println); + } + + private static void test2() { + Optional.of(new Outer()) + .map(Outer::getNested) + .map(Nested::getInner) + .map(Inner::getFoo) + .ifPresent(System.out::println); + } + + private static void test1() { + Optional.of(new Outer()) + .flatMap(o -> Optional.ofNullable(o.nested)) + .flatMap(n -> Optional.ofNullable(n.inner)) + .flatMap(i -> Optional.ofNullable(i.foo)) + .ifPresent(System.out::println); + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams1.java new file mode 100644 index 00000000..978af89a --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams1.java @@ -0,0 +1,102 @@ +package winterbe.java8.samples.stream; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +/** + * @author Benjamin Winterberg + */ +public class Streams1 { + + public static void main(String[] args) { + + List stringCollection = new ArrayList<>(); + stringCollection.add("ddd2"); + stringCollection.add("aaa2"); + stringCollection.add("bbb1"); + stringCollection.add("aaa1"); + stringCollection.add("bbb3"); + stringCollection.add("ccc"); + stringCollection.add("bbb2"); + stringCollection.add("ddd1"); + + + // filtering + + stringCollection + .stream() + .filter((s) -> s.startsWith("a")) + .forEach(System.out::println); + + // "aaa2", "aaa1" + + + // sorting + + stringCollection + .stream() + .sorted() + .filter((s) -> s.startsWith("a")) + .forEach(System.out::println); + + // "aaa1", "aaa2" + + + // mapping + + stringCollection + .stream() + .map(String::toUpperCase) + .sorted((a, b) -> b.compareTo(a)) + .forEach(System.out::println); + + // "DDD2", "DDD1", "CCC", "BBB3", "BBB2", "AAA2", "AAA1" + + + // matching + + boolean anyStartsWithA = stringCollection + .stream() + .anyMatch((s) -> s.startsWith("a")); + + System.out.println(anyStartsWithA); // true + + boolean allStartsWithA = stringCollection + .stream() + .allMatch((s) -> s.startsWith("a")); + + System.out.println(allStartsWithA); // false + + boolean noneStartsWithZ = stringCollection + .stream() + .noneMatch((s) -> s.startsWith("z")); + + System.out.println(noneStartsWithZ); // true + + + // counting + + long startsWithB = stringCollection + .stream() + .filter((s) -> s.startsWith("b")) + .count(); + + System.out.println(startsWithB); // 3 + + + // reducing + + Optional reduced = + stringCollection + .stream() + .sorted() + .reduce((s1, s2) -> s1 + "#" + s2); + + reduced.ifPresent(System.out::println); + // "aaa1#aaa2#bbb1#bbb2#bbb3#ccc#ddd1#ddd2" + + + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams10.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams10.java new file mode 100644 index 00000000..832ffec7 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams10.java @@ -0,0 +1,178 @@ +package winterbe.java8.samples.stream; + +import java.util.*; +import java.util.stream.Collector; +import java.util.stream.Collectors; + +/** + * @author Benjamin Winterberg + */ +public class Streams10 { + + static class Person { + String name; + int age; + + Person(String name, int age) { + this.name = name; + this.age = age; + } + + @Override + public String toString() { + return name; + } + } + + public static void main(String[] args) { + List persons = + Arrays.asList( + new Person("Max", 18), + new Person("Peter", 23), + new Person("Pamela", 23), + new Person("David", 12)); + +// test1(persons); +// test2(persons); +// test3(persons); +// test4(persons); +// test5(persons); +// test6(persons); +// test7(persons); +// test8(persons); + test9(persons); + } + + private static void test1(List persons) { + List filtered = + persons + .stream() + .filter(p -> p.name.startsWith("P")) + .collect(Collectors.toList()); + + System.out.println(filtered); // [Peter, Pamela] + } + + private static void test2(List persons) { + Map> personsByAge = persons + .stream() + .collect(Collectors.groupingBy(p -> p.age)); + + personsByAge + .forEach((age, p) -> System.out.format("age %s: %s\n", age, p)); + + // age 18: [Max] + // age 23:[Peter, Pamela] + // age 12:[David] + } + + private static void test3(List persons) { + Double averageAge = persons + .stream() + .collect(Collectors.averagingInt(p -> p.age)); + + System.out.println(averageAge); // 19.0 + } + + private static void test4(List persons) { + IntSummaryStatistics ageSummary = + persons + .stream() + .collect(Collectors.summarizingInt(p -> p.age)); + + System.out.println(ageSummary); + // IntSummaryStatistics{count=4, sum=76, min=12, average=19,000000, max=23} + } + + private static void test5(List persons) { + String names = persons + .stream() + .filter(p -> p.age >= 18) + .map(p -> p.name) + .collect(Collectors.joining(" and ", "In Germany ", " are of legal age.")); + + System.out.println(names); + // In Germany Max and Peter and Pamela are of legal age. + } + + private static void test6(List persons) { + Map map = persons + .stream() + .collect(Collectors.toMap( + p -> p.age, + p -> p.name, + (name1, name2) -> name1 + ";" + name2)); + + System.out.println(map); + // {18=Max, 23=Peter;Pamela, 12=David} + } + + private static void test7(List persons) { + Collector personNameCollector = + Collector.of( + () -> new StringJoiner(" | "), // supplier + (j, p) -> j.add(p.name.toUpperCase()), // accumulator + (j1, j2) -> j1.merge(j2), // combiner + StringJoiner::toString); // finisher + + String names = persons + .stream() + .collect(personNameCollector); + + System.out.println(names); // MAX | PETER | PAMELA | DAVID + } + + private static void test8(List persons) { + Collector personNameCollector = + Collector.of( + () -> { + System.out.println("supplier"); + return new StringJoiner(" | "); + }, + (j, p) -> { + System.out.format("accumulator: p=%s; j=%s\n", p, j); + j.add(p.name.toUpperCase()); + }, + (j1, j2) -> { + System.out.println("merge"); + return j1.merge(j2); + }, + j -> { + System.out.println("finisher"); + return j.toString(); + }); + + String names = persons + .stream() + .collect(personNameCollector); + + System.out.println(names); // MAX | PETER | PAMELA | DAVID + } + + private static void test9(List persons) { + Collector personNameCollector = + Collector.of( + () -> { + System.out.println("supplier"); + return new StringJoiner(" | "); + }, + (j, p) -> { + System.out.format("accumulator: p=%s; j=%s\n", p, j); + j.add(p.name.toUpperCase()); + }, + (j1, j2) -> { + System.out.println("merge"); + return j1.merge(j2); + }, + j -> { + System.out.println("finisher"); + return j.toString(); + }); + + String names = persons + .parallelStream() + .collect(personNameCollector); + + System.out.println(names); // MAX | PETER | PAMELA | DAVID + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams11.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams11.java new file mode 100644 index 00000000..53e519f1 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams11.java @@ -0,0 +1,119 @@ +package winterbe.java8.samples.stream; + +import java.util.Arrays; +import java.util.List; + +/** + * @author Benjamin Winterberg + */ +public class Streams11 { + + static class Person { + String name; + int age; + + Person(String name, int age) { + this.name = name; + this.age = age; + } + + @Override + public String toString() { + return name; + } + } + + public static void main(String[] args) { + List persons = + Arrays.asList( + new Person("Max", 18), + new Person("Peter", 23), + new Person("Pamela", 23), + new Person("David", 12)); + +// test1(persons); +// test2(persons); +// test3(persons); +// test4(persons); +// test5(persons); + test6(persons); + } + + private static void test1(List persons) { + persons + .stream() + .reduce((p1, p2) -> p1.age > p2.age ? p1 : p2) + .ifPresent(System.out::println); // Pamela + } + + private static void test2(List persons) { + Person result = + persons + .stream() + .reduce(new Person("", 0), (p1, p2) -> { + p1.age += p2.age; + p1.name += p2.name; + return p1; + }); + + System.out.format("name=%s; age=%s", result.name, result.age); + } + + private static void test3(List persons) { + Integer ageSum = persons + .stream() + .reduce(0, (sum, p) -> sum += p.age, (sum1, sum2) -> sum1 + sum2); + + System.out.println(ageSum); + } + + private static void test4(List persons) { + Integer ageSum = persons + .stream() + .reduce(0, + (sum, p) -> { + System.out.format("accumulator: sum=%s; person=%s\n", sum, p); + return sum += p.age; + }, + (sum1, sum2) -> { + System.out.format("combiner: sum1=%s; sum2=%s\n", sum1, sum2); + return sum1 + sum2; + }); + + System.out.println(ageSum); + } + + private static void test5(List persons) { + Integer ageSum = persons + .parallelStream() + .reduce(0, + (sum, p) -> { + System.out.format("accumulator: sum=%s; person=%s\n", sum, p); + return sum += p.age; + }, + (sum1, sum2) -> { + System.out.format("combiner: sum1=%s; sum2=%s\n", sum1, sum2); + return sum1 + sum2; + }); + + System.out.println(ageSum); + } + + private static void test6(List persons) { + Integer ageSum = persons + .parallelStream() + .reduce(0, + (sum, p) -> { + System.out.format("accumulator: sum=%s; person=%s; thread=%s\n", + sum, p, Thread.currentThread().getName()); + return sum += p.age; + }, + (sum1, sum2) -> { + System.out.format("combiner: sum1=%s; sum2=%s; thread=%s\n", + sum1, sum2, Thread.currentThread().getName()); + return sum1 + sum2; + }); + + System.out.println(ageSum); + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams12.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams12.java new file mode 100644 index 00000000..178a40b0 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams12.java @@ -0,0 +1,88 @@ +package winterbe.java8.samples.stream; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.TimeUnit; + +/** + * @author Benjamin Winterberg + */ +public class Streams12 { + + public static void main(String[] args) { + List strings = Arrays.asList("a1", "a2", "b1", "c2", "c1"); + +// test1(); +// test2(strings); + test3(strings); +// test4(); + } + + private static void test4() { + List values = new ArrayList<>(100); + for (int i = 0; i < 100; i++) { + UUID uuid = UUID.randomUUID(); + values.add(uuid.toString()); + } + + // sequential + + long t0 = System.nanoTime(); + + long count = values + .parallelStream() + .sorted((s1, s2) -> { + System.out.format("sort: %s <> %s [%s]\n", s1, s2, Thread.currentThread().getName()); + return s1.compareTo(s2); + }) + .count(); + System.out.println(count); + + long t1 = System.nanoTime(); + + long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0); + System.out.println(String.format("parallel sort took: %d ms", millis)); + } + + private static void test3(List strings) { + strings + .parallelStream() + .filter(s -> { + System.out.format("filter: %s [%s]\n", s, Thread.currentThread().getName()); + return true; + }) + .map(s -> { + System.out.format("map: %s [%s]\n", s, Thread.currentThread().getName()); + return s.toUpperCase(); + }) + .sorted((s1, s2) -> { + System.out.format("sort: %s <> %s [%s]\n", s1, s2, Thread.currentThread().getName()); + return s1.compareTo(s2); + }) + .forEach(s -> System.out.format("forEach: %s [%s]\n", s, Thread.currentThread().getName())); + } + + private static void test2(List strings) { + strings + .parallelStream() + .filter(s -> { + System.out.format("filter: %s [%s]\n", s, Thread.currentThread().getName()); + return true; + }) + .map(s -> { + System.out.format("map: %s [%s]\n", s, Thread.currentThread().getName()); + return s.toUpperCase(); + }) + .forEach(s -> System.out.format("forEach: %s [%s]\n", s, Thread.currentThread().getName())); + } + + private static void test1() { + // -Djava.util.concurrent.ForkJoinPool.common.parallelism=5 + + ForkJoinPool commonPool = ForkJoinPool.commonPool(); + System.out.println(commonPool.getParallelism()); + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams13.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams13.java new file mode 100644 index 00000000..7524e020 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams13.java @@ -0,0 +1,26 @@ +package winterbe.java8.samples.stream; + +import java.security.SecureRandom; +import java.util.Arrays; +import java.util.stream.IntStream; + +/** + * @author Benjamin Winterberg + */ +public class Streams13 { + + public static void main(String[] args) { + SecureRandom secureRandom = new SecureRandom(new byte[]{1, 3, 3, 7}); + int[] randoms = IntStream.generate(secureRandom::nextInt) + .filter(n -> n > 0) + .limit(10) + .toArray(); + System.out.println(Arrays.toString(randoms)); + + + int[] nums = IntStream.iterate(1, n -> n * 2) + .limit(11) + .toArray(); + System.out.println(Arrays.toString(nums)); + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams2.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams2.java new file mode 100644 index 00000000..3ed84794 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams2.java @@ -0,0 +1,37 @@ +package winterbe.java8.samples.stream; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Benjamin Winterberg + */ +public class Streams2 { + + public static void main(String[] args) { + + List stringCollection = new ArrayList<>(); + stringCollection.add("ddd2"); + stringCollection.add("aaa2"); + stringCollection.add("bbb1"); + stringCollection.add("aaa1"); + stringCollection.add("bbb3"); + stringCollection.add("ccc"); + stringCollection.add("bbb2"); + stringCollection.add("ddd1"); + + + // sorting + + stringCollection + .stream() + .sorted() + .forEach(System.out::println); + + System.out.println(stringCollection); + + + + } + +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams3.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams3.java new file mode 100644 index 00000000..24813b8f --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams3.java @@ -0,0 +1,59 @@ +package winterbe.java8.samples.stream; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +/** + * @author Benjamin Winterberg + */ +public class Streams3 { + + public static final int MAX = 1000000; + + public static void sortSequential() { + List values = new ArrayList<>(MAX); + for (int i = 0; i < MAX; i++) { + UUID uuid = UUID.randomUUID(); + values.add(uuid.toString()); + } + + // sequential + + long t0 = System.nanoTime(); + + long count = values.stream().sorted().count(); + System.out.println(count); + + long t1 = System.nanoTime(); + + long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0); + System.out.println(String.format("sequential sort took: %d ms", millis)); + } + + public static void sortParallel() { + List values = new ArrayList<>(MAX); + for (int i = 0; i < MAX; i++) { + UUID uuid = UUID.randomUUID(); + values.add(uuid.toString()); + } + + // sequential + + long t0 = System.nanoTime(); + + long count = values.parallelStream().sorted().count(); + System.out.println(count); + + long t1 = System.nanoTime(); + + long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0); + System.out.println(String.format("parallel sort took: %d ms", millis)); + } + + public static void main(String[] args) { + sortSequential(); + sortParallel(); + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams4.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams4.java new file mode 100644 index 00000000..29bff5cc --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams4.java @@ -0,0 +1,37 @@ +package winterbe.java8.samples.stream; + +import java.util.OptionalInt; +import java.util.stream.IntStream; + +/** + * @author Benjamin Winterberg + */ +public class Streams4 { + + public static void main(String[] args) { + for (int i = 0; i < 10; i++) { + if (i % 2 == 1) { + System.out.println(i); + } + } + + IntStream.range(0, 10) + .forEach(i -> { + if (i % 2 == 1) System.out.println(i); + }); + + IntStream.range(0, 10) + .filter(i -> i % 2 == 1) + .forEach(System.out::println); + + OptionalInt reduced1 = + IntStream.range(0, 10) + .reduce((a, b) -> a + b); + System.out.println(reduced1.getAsInt()); + + int reduced2 = + IntStream.range(0, 10) + .reduce(7, (a, b) -> a + b); + System.out.println(reduced2); + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams5.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams5.java new file mode 100644 index 00000000..39a536ce --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams5.java @@ -0,0 +1,138 @@ +package winterbe.java8.samples.stream; + +import java.util.Arrays; +import java.util.List; +import java.util.function.Supplier; +import java.util.stream.Stream; + +/** + * Testing the order of execution. + * + * @author Benjamin Winterberg + */ +public class Streams5 { + + public static void main(String[] args) { + List strings = + Arrays.asList("d2", "a2", "b1", "b3", "c"); + +// test1(strings); +// test2(strings); +// test3(strings); +// test4(strings); +// test5(strings); +// test6(strings); +// test7(strings); + test8(strings); + } + + private static void test8(List stringCollection) { + Supplier> streamSupplier = + () -> stringCollection + .stream() + .filter(s -> s.startsWith("a")); + + streamSupplier.get().anyMatch(s -> true); + streamSupplier.get().noneMatch(s -> true); + } + + // stream has already been operated upon or closed + private static void test7(List stringCollection) { + Stream stream = stringCollection + .stream() + .filter(s -> s.startsWith("a")); + + stream.anyMatch(s -> true); + stream.noneMatch(s -> true); + } + + // short-circuit + private static void test6(List stringCollection) { + stringCollection + .stream() + .map(s -> { + System.out.println("map: " + s); + return s.toUpperCase(); + }) + .anyMatch(s -> { + System.out.println("anyMatch: " + s); + return s.startsWith("A"); + }); + } + + private static void test5(List stringCollection) { + stringCollection + .stream() + .filter(s -> { + System.out.println("filter: " + s); + return s.toLowerCase().startsWith("a"); + }) + .sorted((s1, s2) -> { + System.out.printf("sort: %s; %s\n", s1, s2); + return s1.compareTo(s2); + }) + .map(s -> { + System.out.println("map: " + s); + return s.toUpperCase(); + }) + .forEach(s -> System.out.println("forEach: " + s)); + } + + // sorted = horizontal + private static void test4(List stringCollection) { + stringCollection + .stream() + .sorted((s1, s2) -> { + System.out.printf("sort: %s; %s\n", s1, s2); + return s1.compareTo(s2); + }) + .filter(s -> { + System.out.println("filter: " + s); + return s.toLowerCase().startsWith("a"); + }) + .map(s -> { + System.out.println("map: " + s); + return s.toUpperCase(); + }) + .forEach(s -> System.out.println("forEach: " + s)); + } + + private static void test3(List stringCollection) { + stringCollection + .stream() + .filter(s -> { + System.out.println("filter: " + s); + return s.startsWith("a"); + }) + .map(s -> { + System.out.println("map: " + s); + return s.toUpperCase(); + }) + .forEach(s -> System.out.println("forEach: " + s)); + } + + private static void test2(List stringCollection) { + stringCollection + .stream() + .map(s -> { + System.out.println("map: " + s); + return s.toUpperCase(); + }) + .filter(s -> { + System.out.println("filter: " + s); + return s.startsWith("A"); + }) + .forEach(s -> System.out.println("forEach: " + s)); + } + + private static void test1(List stringCollection) { + stringCollection + .stream() + .filter(s -> { + System.out.println("filter: " + s); + return true; + }) + .forEach(s -> System.out.println("forEach: " + s)); + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams6.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams6.java new file mode 100644 index 00000000..06c0525e --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams6.java @@ -0,0 +1,57 @@ +package winterbe.java8.samples.stream; + +import java.io.IOException; +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +/** + * @author Benjamin Winterberg + */ +public class Streams6 { + + public static void main(String[] args) throws IOException { + test1(); + test2(); + test3(); + test4(); + } + + private static void test4() { + Stream + .of(new BigDecimal("1.2"), new BigDecimal("3.7")) + .mapToDouble(BigDecimal::doubleValue) + .average() + .ifPresent(System.out::println); + } + + private static void test3() { + IntStream + .range(0, 10) + .average() + .ifPresent(System.out::println); + } + + private static void test2() { + IntStream + .builder() + .add(1) + .add(3) + .add(5) + .add(7) + .add(11) + .build() + .average() + .ifPresent(System.out::println); + + } + + private static void test1() { + int[] ints = {1, 3, 5, 7, 11}; + Arrays + .stream(ints) + .average() + .ifPresent(System.out::println); + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams7.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams7.java new file mode 100644 index 00000000..77ec3246 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams7.java @@ -0,0 +1,61 @@ +package winterbe.java8.samples.stream; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.IntStream; + +/** + * @author Benjamin Winterberg + */ +public class Streams7 { + + static class Foo { + String name; + List bars = new ArrayList<>(); + + Foo(String name) { + this.name = name; + } + } + + static class Bar { + String name; + + Bar(String name) { + this.name = name; + } + } + + public static void main(String[] args) { +// test1(); + test2(); + } + + static void test2() { + IntStream.range(1, 4) + .mapToObj(num -> new Foo("Foo" + num)) + .peek(f -> IntStream.range(1, 4) + .mapToObj(num -> new Bar("Bar" + num + " <- " + f.name)) + .forEach(f.bars::add)) + .flatMap(f -> f.bars.stream()) + .forEach(b -> System.out.println(b.name)); + } + + static void test1() { + List foos = new ArrayList<>(); + + IntStream + .range(1, 4) + .forEach(num -> foos.add(new Foo("Foo" + num))); + + foos.forEach(f -> + IntStream + .range(1, 4) + .forEach(num -> f.bars.add(new Bar("Bar" + num + " <- " + f.name)))); + + foos.stream() + .flatMap(f -> f.bars.stream()) + .forEach(b -> System.out.println(b.name)); + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams8.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams8.java new file mode 100644 index 00000000..4bdd69df --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams8.java @@ -0,0 +1,39 @@ +package winterbe.java8.samples.stream; + +import java.util.Arrays; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +/** + * @author Benjamin Winterberg + */ +public class Streams8 { + + public static void main(String[] args) { + Arrays.asList("a1", "a2", "a3") + .stream() + .findFirst() + .ifPresent(System.out::println); + + Stream.of("a1", "a2", "a3") + .map(s -> s.substring(1)) + .mapToInt(Integer::parseInt) + .max() + .ifPresent(System.out::println); + + IntStream.range(1, 4) + .mapToObj(i -> "a" + i) + .forEach(System.out::println); + + Arrays.stream(new int[] {1, 2, 3}) + .map(n -> 2 * n + 1) + .average() + .ifPresent(System.out::println); + + Stream.of(1.0, 2.0, 3.0) + .mapToInt(Double::intValue) + .mapToObj(i -> "a" + i) + .forEach(System.out::println); + + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams9.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams9.java new file mode 100644 index 00000000..08938e18 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/stream/Streams9.java @@ -0,0 +1,21 @@ +package winterbe.java8.samples.stream; + +import java.util.Arrays; + +/** + * @author Benjamin Winterberg + */ +public class Streams9 { + + public static void main(String[] args) { + Arrays.asList("a1", "a2", "b1", "c2", "c1") + .stream() + .filter(s -> s.startsWith("c")) + .map(String::toUpperCase) + .sorted() + .forEach(System.out::println); + + // C1 + // C2 + } +} diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/time/LocalDate1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/time/LocalDate1.java new file mode 100644 index 00000000..0389720b --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/time/LocalDate1.java @@ -0,0 +1,40 @@ +package winterbe.java8.samples.time; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.Month; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.time.temporal.ChronoUnit; +import java.util.Locale; + +/** + * @author Benjamin Winterberg + */ +public class LocalDate1 { + + public static void main(String[] args) { + LocalDate today = LocalDate.now(); + LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS); + LocalDate yesterday = tomorrow.minusDays(2); + + System.out.println(today); + System.out.println(tomorrow); + System.out.println(yesterday); + + LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4); + DayOfWeek dayOfWeek = independenceDay.getDayOfWeek(); + System.out.println(dayOfWeek); // FRIDAY + + DateTimeFormatter germanFormatter = + DateTimeFormatter + .ofLocalizedDate(FormatStyle.MEDIUM) + .withLocale(Locale.GERMAN); + + LocalDate xmas = LocalDate.parse("24.12.2014", germanFormatter); + System.out.println(xmas); // 2014-12-24 + + + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/time/LocalDateTime1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/time/LocalDateTime1.java new file mode 100644 index 00000000..56b44dae --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/time/LocalDateTime1.java @@ -0,0 +1,43 @@ +package winterbe.java8.samples.time; + +import java.time.*; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoField; +import java.util.Date; + +/** + * @author Benjamin Winterberg + */ +public class LocalDateTime1 { + + public static void main(String[] args) { + + LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59); + + DayOfWeek dayOfWeek = sylvester.getDayOfWeek(); + System.out.println(dayOfWeek); // WEDNESDAY + + Month month = sylvester.getMonth(); + System.out.println(month); // DECEMBER + + long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY); + System.out.println(minuteOfDay); // 1439 + + Instant instant = sylvester + .atZone(ZoneId.systemDefault()) + .toInstant(); + + Date legacyDate = Date.from(instant); + System.out.println(legacyDate); // Wed Dec 31 23:59:59 CET 2014 + + + DateTimeFormatter formatter = + DateTimeFormatter + .ofPattern("MMM dd, yyyy - HH:mm"); + + LocalDateTime parsed = LocalDateTime.parse("Nov 03, 2014 - 07:13", formatter); + String string = parsed.format(formatter); + System.out.println(string); // Nov 03, 2014 - 07:13 + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/java/winterbe/java8/samples/time/LocalTime1.java b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/time/LocalTime1.java new file mode 100644 index 00000000..e8eeb955 --- /dev/null +++ b/04fx/Java8inAction/src/main/java/winterbe/java8/samples/time/LocalTime1.java @@ -0,0 +1,72 @@ +package winterbe.java8.samples.time; + +import java.time.Clock; +import java.time.Instant; +import java.time.LocalTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.time.temporal.ChronoUnit; +import java.util.Date; +import java.util.Locale; + +/** + * @author Benjamin Winterberg + */ +public class LocalTime1 { + + public static void main(String[] args) { + + // get the current time + Clock clock = Clock.systemDefaultZone(); + long t0 = clock.millis(); + System.out.println(t0); + + Instant instant = clock.instant(); + Date legacyDate = Date.from(instant); + + + ZoneId zone1 = ZoneId.of("Europe/Berlin"); + ZoneId zone2 = ZoneId.of("Brazil/East"); + + System.out.println(zone1.getRules()); + System.out.println(zone2.getRules()); + + // time + LocalTime now1 = LocalTime.now(zone1); + LocalTime now2 = LocalTime.now(zone2); + + System.out.println(now1); + System.out.println(now2); + + System.out.println(now1.isBefore(now2)); // false + + long hoursBetween = ChronoUnit.HOURS.between(now1, now2); + long minutesBetween = ChronoUnit.MINUTES.between(now1, now2); + System.out.println(hoursBetween); + System.out.println(minutesBetween); + + + // create time + + LocalTime now = LocalTime.now(); + System.out.println(now); + + LocalTime late = LocalTime.of(23, 59, 59); + System.out.println(late); + + DateTimeFormatter germanFormatter = + DateTimeFormatter + .ofLocalizedTime(FormatStyle.SHORT) + .withLocale(Locale.GERMAN); + + LocalTime leetTime = LocalTime.parse("13:37", germanFormatter); + System.out.println(leetTime); + + + // to legacy date + + + } + +} \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/resources/lambdasinaction/chap3/data.txt b/04fx/Java8inAction/src/main/resources/lambdasinaction/chap3/data.txt new file mode 100644 index 00000000..20764bbf --- /dev/null +++ b/04fx/Java8inAction/src/main/resources/lambdasinaction/chap3/data.txt @@ -0,0 +1,5 @@ +Java +8 +Lambdas +In +Action \ No newline at end of file diff --git a/04fx/Java8inAction/src/main/resources/lambdasinaction/chap5/data.txt b/04fx/Java8inAction/src/main/resources/lambdasinaction/chap5/data.txt new file mode 100644 index 00000000..2230972a --- /dev/null +++ b/04fx/Java8inAction/src/main/resources/lambdasinaction/chap5/data.txt @@ -0,0 +1,2 @@ +The quick brown fox jumped over the lazy dog +The lazy dog jumped over the quick brown fox diff --git a/04fx/demo/.gitignore b/04fx/demo/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/04fx/demo/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/04fx/demo/.mvn/wrapper/MavenWrapperDownloader.java b/04fx/demo/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..e76d1f32 --- /dev/null +++ b/04fx/demo/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,117 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if(mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if(mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if(!outputFile.getParentFile().exists()) { + if(!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/04fx/demo/.mvn/wrapper/maven-wrapper.jar b/04fx/demo/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/04fx/demo/.mvn/wrapper/maven-wrapper.jar differ diff --git a/04fx/demo/.mvn/wrapper/maven-wrapper.properties b/04fx/demo/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/04fx/demo/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/04fx/demo/mvnw b/04fx/demo/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/04fx/demo/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/04fx/demo/mvnw.cmd b/04fx/demo/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/04fx/demo/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/04fx/demo/pom.xml b/04fx/demo/pom.xml new file mode 100644 index 00000000..943bcbe0 --- /dev/null +++ b/04fx/demo/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.2 + + + io.kimmking.javacourse + demo + 0.0.1-SNAPSHOT + demo + Demo project for Spring Boot + + 1.8 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + 1.18.22 + compile + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/04fx/demo/src/main/java/A.java b/04fx/demo/src/main/java/A.java new file mode 100644 index 00000000..117e905c --- /dev/null +++ b/04fx/demo/src/main/java/A.java @@ -0,0 +1,8 @@ +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2022/12/16 10:55 + */ +public class A { +} diff --git a/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfig.java b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfig.java new file mode 100644 index 00000000..c506234e --- /dev/null +++ b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfig.java @@ -0,0 +1,18 @@ +package io.kimmking.javacourse.demo; + +import lombok.AllArgsConstructor; +import lombok.Data; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2022/11/29 16:05 + */ +@Data +public class ConsConfig { + + private String demoName; + private String demoDesc; + +} diff --git a/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfigAutoConfiguration.java b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfigAutoConfiguration.java new file mode 100644 index 00000000..4461b3a8 --- /dev/null +++ b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfigAutoConfiguration.java @@ -0,0 +1,71 @@ +package io.kimmking.javacourse.demo; + +import org.springframework.beans.factory.config.ConstructorArgumentValues; +import org.springframework.beans.factory.support.AbstractBeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.context.EnvironmentAware; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; +import org.springframework.core.env.Environment; +import org.springframework.core.type.AnnotationMetadata; + + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2022/12/22 16:43 + */ + +@Configuration +//@EnableConfigurationProperties(ConsConfigs.class) +@Import(ConsConfigAutoConfiguration.ConsRegistrar.class) +public class ConsConfigAutoConfiguration { + public static class ConsRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware { + + Environment environment; +// ApplicationContext applicationContext; +// @Override +// public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { +// this.applicationContext = applicationContext; +// } + + @Override + public void setEnvironment(Environment environment) { + this.environment = environment; + } + + + + @Override + public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { + //ImportBeanDefinitionRegistrar.super.registerBeanDefinitions(importingClassMetadata, registry); + + RootBeanDefinition def = new RootBeanDefinition(); + def.setBeanClass(ConsConfig.class); + registry.registerBeanDefinition("cons", def); + + RootBeanDefinition definition = new RootBeanDefinition(); + definition.setBeanClass(ConsConfigs.class); + definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE); + definition.setDependencyCheck(AbstractBeanDefinition.DEPENDENCY_CHECK_NONE); + //ConstructorArgumentValues argumentValues = new ConstructorArgumentValues(); + + //ConsConfig config= applicationContext.getBean(ConsConfig.class); +// String name = ConsConfig.class.getCanonicalName(); +// boolean exist = registry.containsBeanDefinition(name); +// if(exist) { +// argumentValues.addGenericArgumentValue(registry.getBeanDefinition(name)); +// } else { +// ConsConfig cf = new ConsConfig(); +// cf.setDemoName("defaultName1"); +// cf.setDemoDesc("defaultDesc1"); +// argumentValues.addGenericArgumentValue(cf); +// } + //definition.setConstructorArgumentValues(argumentValues); + registry.registerBeanDefinition("conss", definition); + } + } +} diff --git a/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfigs.java b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfigs.java new file mode 100644 index 00000000..8ef243e6 --- /dev/null +++ b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/ConsConfigs.java @@ -0,0 +1,20 @@ +package io.kimmking.javacourse.demo; + +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.util.List; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2022/12/22 16:47 + */ +@Data +@AllArgsConstructor +public class ConsConfigs { + + private List config; + +} diff --git a/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoApplication.java b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoApplication.java new file mode 100644 index 00000000..401643fc --- /dev/null +++ b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoApplication.java @@ -0,0 +1,72 @@ +package io.kimmking.javacourse.demo; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.context.ApplicationContext; +import org.springframework.context.EnvironmentAware; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; + +import javax.annotation.Resource; + +@SpringBootApplication +public class DemoApplication implements EnvironmentAware, ApplicationRunner { + + Environment environment; + + @Resource(name="a1") + DemoConfig a1; + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + + @Override + public void setEnvironment(Environment environment) { + this.environment = environment; + } + + @Autowired + ApplicationContext context; + + @Bean + //@ConditionalOnMissingBean + public ConsConfig defaultConsConfig() { + ConsConfig cf = new ConsConfig(); + cf.setDemoName("defaultName"); + cf.setDemoDesc("defaultDesc"); + return cf; + } + + @Override + public void run(ApplicationArguments args) throws Exception { + System.out.println(environment.getProperty("a")); + System.out.println(a1.getDemoName()); + + DemoConfig a4 = (DemoConfig) context.getBean("a4"); + System.out.println(a4.getDemoName()); + + System.out.println(context.getBean(ConsConfigs.class)); + + } + + @EnableDemoConfigBindings(prefix = "demo.config", type = DemoConfig.class) + @PropertySource("application.properties") + public static class TestDemoConfig { + + } + + @Bean("a4") + @ConditionalOnClass(name="A") + DemoConfig createA4() { + DemoConfig config = new DemoConfig(); + config.setDemoName("a4"); + config.setDemoDesc("this is a4"); + return config; + } +} diff --git a/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoConfig.java b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoConfig.java new file mode 100644 index 00000000..fa484b74 --- /dev/null +++ b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoConfig.java @@ -0,0 +1,17 @@ +package io.kimmking.javacourse.demo; + +import lombok.Data; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2022/11/29 16:05 + */ +@Data +public class DemoConfig { + + private String demoName; + private String demoDesc; + +} diff --git a/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoConfigBindingsRegistrar.java b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoConfigBindingsRegistrar.java new file mode 100644 index 00000000..2d201027 --- /dev/null +++ b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/DemoConfigBindingsRegistrar.java @@ -0,0 +1,127 @@ +package io.kimmking.javacourse.demo; + +import org.springframework.beans.MutablePropertyValues; +import org.springframework.beans.factory.support.*; +import org.springframework.context.EnvironmentAware; +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; +import org.springframework.core.annotation.AnnotationAttributes; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.Environment; +import org.springframework.core.type.AnnotationMetadata; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; + +import java.util.*; + +import static io.kimmking.javacourse.demo.PropertySourcesUtils.getSubProperties; +import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; + +/** + * This class is cloned and modified from https://github.com/apache/dubbo/blob/58d1259cb9d89528594e43db5d8667179005dcfc/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/context/annotation/DubboConfigBindingsRegistrar.java. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2022/11/29 16:09 + */ +public class DemoConfigBindingsRegistrar implements ImportBeanDefinitionRegistrar, EnvironmentAware { + + private ConfigurableEnvironment environment; + + @Override + public void setEnvironment(Environment _environment) { + this.environment = (ConfigurableEnvironment) _environment; + } + + @Override + public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) { + registerBeanDefinitions(importingClassMetadata, registry); + } + + @Override + public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { + + AnnotationAttributes attributes = AnnotationAttributes.fromMap( + importingClassMetadata.getAnnotationAttributes(EnableDemoConfigBindings.class.getName())); + String prefix = environment.resolvePlaceholders(attributes.getString("prefix")); + Class configClass = attributes.getClass("type"); + boolean multiple = attributes.getBoolean("multiple"); + registerDubboConfigBeans(prefix, configClass, multiple, registry); + } + + private void registerDubboConfigBeans(String prefix, + Class configClass, + boolean multiple, + BeanDefinitionRegistry registry) { + Map properties = getSubProperties(environment.getPropertySources(), prefix); + if (CollectionUtils.isEmpty(properties)) { + System.out.println("There is no property for binding to demo config class [" + configClass.getName() + + "] within prefix [" + prefix + "]"); + return; + } + Set beanNames = multiple ? resolveMultipleBeanNames(properties) : + Collections.singleton(resolveSingleBeanName(properties, configClass, registry)); + Map> groupProperties = getGroupProperties(properties, beanNames); + for (String beanName : beanNames) { + + System.out.println(" ==> registerDemoConfigBean:" + beanName); + + registerDemoConfigBean(beanName, configClass, registry, groupProperties.get(beanName)); + //registerDubboConfigBindingBeanPostProcessor(prefix, beanName, multiple, registry); + } + //registerDubboConfigBeanCustomizers(registry); + } + + private Map> getGroupProperties(Map properties, Set beanNames) { + Map> map = new HashMap<>(); + for (String propertyName : properties.keySet()) { + int index = propertyName.indexOf("."); + if (index > 0) { + String beanName = propertyName.substring(0, index); + String beanPropertyName = propertyName.substring(index + 1); + if (beanNames.contains(beanName)) { + Map group = map.get(beanName); + if (group == null) { + group = new HashMap<>(); + map.put(beanName, group); + } + group.put(beanPropertyName, properties.get(propertyName)); + } + } + } + return map; + } + + private void registerDemoConfigBean(String beanName, Class configClass, + BeanDefinitionRegistry registry, Map properties) { + BeanDefinitionBuilder builder = rootBeanDefinition(configClass); + AbstractBeanDefinition beanDefinition = builder.getBeanDefinition(); + // Convert Map to MutablePropertyValues + MutablePropertyValues propertyValues = new MutablePropertyValues(properties); + beanDefinition.setPropertyValues(propertyValues); + registry.registerBeanDefinition(beanName, beanDefinition); + System.out.println("The demo config bean definition [name : " + beanName + ", class : " + configClass.getName() + + "] has been registered."); + } + + private Set resolveMultipleBeanNames(Map properties) { + Set beanNames = new LinkedHashSet(); + for (String propertyName : properties.keySet()) { + int index = propertyName.indexOf("."); + if (index > 0) { + String beanName = propertyName.substring(0, index); + beanNames.add(beanName); + } + } + return beanNames; + } + + private String resolveSingleBeanName(Map properties, Class configClass, + BeanDefinitionRegistry registry) { + String beanName = (String) properties.get("demoName"); + if (!StringUtils.hasText(beanName)) { + BeanDefinitionBuilder builder = rootBeanDefinition(configClass); + beanName = BeanDefinitionReaderUtils.generateBeanName(builder.getRawBeanDefinition(), registry); + } + return beanName; + } + +} diff --git a/04fx/demo/src/main/java/io/kimmking/javacourse/demo/EnableDemoConfigBindings.java b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/EnableDemoConfigBindings.java new file mode 100644 index 00000000..3a002944 --- /dev/null +++ b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/EnableDemoConfigBindings.java @@ -0,0 +1,36 @@ +package io.kimmking.javacourse.demo; + +import org.springframework.context.annotation.Import; + +import java.lang.annotation.*; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2022/11/29 16:10 + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Import(DemoConfigBindingsRegistrar.class) +public @interface EnableDemoConfigBindings { + /** + * The name prefix of the properties that are valid to bind. + * + * @return the name prefix of the properties to bind + */ + String prefix(); + + /** + * @return The binding type. + */ + Class type(); + + /** + * It indicates whether {@link #prefix()} binding to multiple Spring Beans. + * + * @return the default value is true + */ + boolean multiple() default true; +} diff --git a/04fx/demo/src/main/java/io/kimmking/javacourse/demo/PropertySourcesUtils.java b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/PropertySourcesUtils.java new file mode 100644 index 00000000..f436313e --- /dev/null +++ b/04fx/demo/src/main/java/io/kimmking/javacourse/demo/PropertySourcesUtils.java @@ -0,0 +1,91 @@ +package io.kimmking.javacourse.demo; + + +import org.springframework.core.env.*; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Properties; + +/** + * {@link PropertySources} Utilities + *

+ * The source code is cloned from https://github.com/alibaba/spring-context-support/blob/1.0.2/src/main/java/com/alibaba/spring/util/PropertySourcesUtils.java + * + * @since 2.6.6 + */ +public abstract class PropertySourcesUtils { + + /** + * Get Sub {@link Properties} + * + * @param propertySources {@link PropertySource} Iterable + * @param prefix the prefix of property name + * @return Map + * @see Properties + */ + public static Map getSubProperties(Iterable> propertySources, String prefix) { + + // Non-Extension AbstractEnvironment + AbstractEnvironment environment = new AbstractEnvironment() { + }; + + MutablePropertySources mutablePropertySources = environment.getPropertySources(); + + for (PropertySource source : propertySources) { + mutablePropertySources.addLast(source); + } + + return getSubProperties(environment, prefix); + + } + + /** + * Get Sub {@link Properties} + * + * @param environment {@link ConfigurableEnvironment} + * @param prefix the prefix of property name + * @return Map + * @see Properties + */ + public static Map getSubProperties(ConfigurableEnvironment environment, String prefix) { + + Map subProperties = new LinkedHashMap(); + + MutablePropertySources propertySources = environment.getPropertySources(); + + String normalizedPrefix = normalizePrefix(prefix); + + for (PropertySource source : propertySources) { + if (source instanceof EnumerablePropertySource) { + for (String name : ((EnumerablePropertySource) source).getPropertyNames()) { + if (!subProperties.containsKey(name) && name.startsWith(normalizedPrefix)) { + String subName = name.substring(normalizedPrefix.length()); + if (!subProperties.containsKey(subName)) { // take first one + Object value = source.getProperty(name); + if (value instanceof String) { + // Resolve placeholder + value = environment.resolvePlaceholders((String) value); + } + subProperties.put(subName, value); + } + } + } + } + } + + return Collections.unmodifiableMap(subProperties); + + } + + /** + * Normalize the prefix + * + * @param prefix the prefix + * @return the prefix + */ + public static String normalizePrefix(String prefix) { + return prefix.endsWith(".") ? prefix : prefix + "."; + } +} diff --git a/04fx/demo/src/main/resources/application.properties b/04fx/demo/src/main/resources/application.properties new file mode 100644 index 00000000..f7a98d9a --- /dev/null +++ b/04fx/demo/src/main/resources/application.properties @@ -0,0 +1,9 @@ + +demo.config.a1.demoName = d1 +demo.config.a1.demoDesc = demo1 + +demo.config.a2.demoName = d2 +demo.config.a2.demoDesc = demo2 + +demo.config.a3.demoName = d3 +demo.config.a3.demoDesc = demo3 \ No newline at end of file diff --git a/04fx/demo/src/test/java/io/kimmking/javacourse/demo/DemoApplicationTests.java b/04fx/demo/src/test/java/io/kimmking/javacourse/demo/DemoApplicationTests.java new file mode 100644 index 00000000..5101ce0e --- /dev/null +++ b/04fx/demo/src/test/java/io/kimmking/javacourse/demo/DemoApplicationTests.java @@ -0,0 +1,51 @@ +package io.kimmking.javacourse.demo; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.PropertySource; + +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class DemoApplicationTests { + @Test + void testDemoConfig() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + context.register(DemoApplication.TestDemoConfig.class); + context.refresh(); + + + + System.out.println(Arrays.toString(context.getBeanNamesForType(DemoConfig.class))); + assertEquals(3, context.getBeansOfType(DemoConfig.class).size()); + + DemoConfig demoConfig = context.getBean("a1", DemoConfig.class); + System.out.println("a1=" + demoConfig.toString()); + assertEquals("d1", demoConfig.getDemoName()); + assertEquals("demo1", demoConfig.getDemoDesc()); + + demoConfig = context.getBean("a2", DemoConfig.class); + System.out.println("a2=" + demoConfig.toString()); + assertEquals("d2", demoConfig.getDemoName()); + assertEquals("demo2", demoConfig.getDemoDesc()); + + demoConfig = context.getBean("a3", DemoConfig.class); + System.out.println("a3=" + demoConfig.toString()); + assertEquals("d3", demoConfig.getDemoName()); + assertEquals("demo3", demoConfig.getDemoDesc()); + +// context.refresh(); +// System.out.println(Arrays.toString(context.getBeanNamesForType(DemoConfig.class))); + + } + +// @EnableDemoConfigBindings(prefix = "demo.config", type = DemoConfig.class) +// @PropertySource("application.properties") +// private static class TestConfig { +// +// } + +} diff --git a/04fx/dtx01/pom.xml b/04fx/dtx01/pom.xml new file mode 100644 index 00000000..9b8dcebc --- /dev/null +++ b/04fx/dtx01/pom.xml @@ -0,0 +1,117 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.0.9.RELEASE + + + io.kimmking + dtx01 + 0.0.1-SNAPSHOT + dtx01 + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-jdbc + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.1.4 + + + mysql + mysql-connector-java + 5.1.47 + + + + org.apache.dubbo + dubbo + 2.7.8 + + + + org.projectlombok + lombok + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + + + + + + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/04fx/dtx01/src/main/java/io/kimmking/dtx01/Dtx01Application.java b/04fx/dtx01/src/main/java/io/kimmking/dtx01/Dtx01Application.java new file mode 100644 index 00000000..8ae6a888 --- /dev/null +++ b/04fx/dtx01/src/main/java/io/kimmking/dtx01/Dtx01Application.java @@ -0,0 +1,17 @@ +package io.kimmking.dtx01; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; + +@SpringBootApplication(scanBasePackages = "io.kimmking.dtx01") +@MapperScan("io.kimmking.dtx01.mapper") +@EnableCaching +public class Dtx01Application { + + public static void main(String[] args) { + SpringApplication.run(Dtx01Application.class, args); + } + +} diff --git a/04fx/dtx01/src/main/java/io/kimmking/dtx01/controller/UserController.java b/04fx/dtx01/src/main/java/io/kimmking/dtx01/controller/UserController.java new file mode 100644 index 00000000..de4e9ac0 --- /dev/null +++ b/04fx/dtx01/src/main/java/io/kimmking/dtx01/controller/UserController.java @@ -0,0 +1,31 @@ +package io.kimmking.dtx01.controller; + +import io.kimmking.dtx01.entity.User; +import io.kimmking.dtx01.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@EnableAutoConfiguration +public class UserController { + + @Autowired + UserService userService; + + @RequestMapping("/user/find") + User find(Long id) { + return userService.find(id); + //return new User(1,"KK", 28); + } + + @RequestMapping("/user/list") + List list() { + return userService.list(); +// return Arrays.asList(new User(1,"KK", 28), +// new User(2,"CC", 18)); + } +} \ No newline at end of file diff --git a/04fx/dtx01/src/main/java/io/kimmking/dtx01/entity/User.java b/04fx/dtx01/src/main/java/io/kimmking/dtx01/entity/User.java new file mode 100644 index 00000000..ae7f3a19 --- /dev/null +++ b/04fx/dtx01/src/main/java/io/kimmking/dtx01/entity/User.java @@ -0,0 +1,16 @@ +package io.kimmking.dtx01.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class User implements Serializable { + private Long id; + private String name; + private Integer age; +} \ No newline at end of file diff --git a/04fx/dtx01/src/main/java/io/kimmking/dtx01/mapper/UserMapper.java b/04fx/dtx01/src/main/java/io/kimmking/dtx01/mapper/UserMapper.java new file mode 100644 index 00000000..27705655 --- /dev/null +++ b/04fx/dtx01/src/main/java/io/kimmking/dtx01/mapper/UserMapper.java @@ -0,0 +1,19 @@ +package io.kimmking.dtx01.mapper; + +import io.kimmking.dtx01.entity.User; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface UserMapper { + + @Select("select * from user where id = #{id};") + User find(@Param("id") Long id); + + @Select("select * from user;") + List list(); + +} diff --git a/04fx/dtx01/src/main/java/io/kimmking/dtx01/service/UserService.java b/04fx/dtx01/src/main/java/io/kimmking/dtx01/service/UserService.java new file mode 100644 index 00000000..f1df3180 --- /dev/null +++ b/04fx/dtx01/src/main/java/io/kimmking/dtx01/service/UserService.java @@ -0,0 +1,13 @@ +package io.kimmking.dtx01.service; + +import io.kimmking.dtx01.entity.User; + +import java.util.List; + +public interface UserService { + + User find(Long id); + + List list(); + +} diff --git a/04fx/dtx01/src/main/java/io/kimmking/dtx01/service/UserServiceImpl.java b/04fx/dtx01/src/main/java/io/kimmking/dtx01/service/UserServiceImpl.java new file mode 100644 index 00000000..3d953bbf --- /dev/null +++ b/04fx/dtx01/src/main/java/io/kimmking/dtx01/service/UserServiceImpl.java @@ -0,0 +1,25 @@ +package io.kimmking.dtx01.service; + +import io.kimmking.dtx01.entity.User; +import io.kimmking.dtx01.mapper.UserMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class UserServiceImpl implements UserService { + + @Autowired + UserMapper userMapper; //DAO // Repository + + public User find(Long id) { + System.out.println(" ==> find " + id); + return userMapper.find(id); + } + + public List list(){ + return userMapper.list(); + } + +} diff --git a/04fx/dtx01/src/main/resources/application.yml b/04fx/dtx01/src/main/resources/application.yml new file mode 100644 index 00000000..c55e64c5 --- /dev/null +++ b/04fx/dtx01/src/main/resources/application.yml @@ -0,0 +1,31 @@ +server: + port: 8080 + +spring: + datasource: + username: root + password: + url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false + driver-class-name: com.mysql.jdbc.Driver +# cache: +# type: redis +# redis: +# host: localhost +# lettuce: +# pool: +# max-active: 16 +# max-wait: 10ms + +# type: ehcache +# ehcache: +# config: ehcache.xml + +#mybatis: +# mapper-locations: classpath:mapper/*Mapper.xml +# type-aliases-package: io.kimmking.dtx01.entity + +logging: + level: + io: + kimmking: + cache : info diff --git a/04fx/dtx01/src/main/resources/mapper/UserMapper.xml b/04fx/dtx01/src/main/resources/mapper/UserMapper.xml new file mode 100644 index 00000000..55bd7280 --- /dev/null +++ b/04fx/dtx01/src/main/resources/mapper/UserMapper.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/04fx/dtx01/src/test/java/io/kimmking/dtx01/Dtx01ApplicationTests.java b/04fx/dtx01/src/test/java/io/kimmking/dtx01/Dtx01ApplicationTests.java new file mode 100644 index 00000000..d04ffc9d --- /dev/null +++ b/04fx/dtx01/src/test/java/io/kimmking/dtx01/Dtx01ApplicationTests.java @@ -0,0 +1,13 @@ +package io.kimmking.dtx01; + +import org.junit.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class Dtx01ApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/04fx/java8/pom.xml b/04fx/java8/pom.xml new file mode 100644 index 00000000..695421a5 --- /dev/null +++ b/04fx/java8/pom.xml @@ -0,0 +1,180 @@ + + + 4.0.0 + + io.kimmking + java8 + 1.0 + + + + 4.3.29.RELEASE + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + + org.openjdk.jol + jol-core + 0.9 + + + com.google.guava + guava + 29.0-jre + + + org.projectlombok + lombok + 1.18.12 + + + org.springframework + spring-core + ${spring-version} + + + + + + + + org.springframework + spring-beans + ${spring-version} + + + org.springframework + spring-aop + ${spring-version} + + + org.springframework + spring-aspects + ${spring-version} + + + org.springframework + spring-context + ${spring-version} + + + org.springframework + spring-context-support + ${spring-version} + + + org.springframework + spring-orm + ${spring-version} + + + org.springframework + spring-tx + ${spring-version} + + + org.springframework + spring-test + ${spring-version} + + + org.springframework + spring-messaging + ${spring-version} + + + org.springframework + spring-jms + ${spring-version} + + + org.springframework + spring-expression + ${spring-version} + + + + com.alibaba + fastjson + 1.2.72 + + + commons-logging + commons-logging + 1.2 + + + log4j + log4j + 1.2.17 + + + log4j + log4j + 1.2.17 + + + org.slf4j + slf4j-api + 1.7.25 + + + org.slf4j + slf4j-log4j12 + 1.7.25 + test + + + org.slf4j + slf4j-simple + 1.7.25 + + + junit + junit + 4.12 + test + + + org.mockito + mockito-all + 1.10.19 + test + + + + org.springframework + spring-jms + 4.3.29.RELEASE + + + org.apache.activemq + activemq-client + 5.15.0 + + + + + + + + + + + + \ No newline at end of file diff --git a/04fx/java8/src/main/java/io/kimmking/java8/A.java b/04fx/java8/src/main/java/io/kimmking/java8/A.java new file mode 100644 index 00000000..e3027fce --- /dev/null +++ b/04fx/java8/src/main/java/io/kimmking/java8/A.java @@ -0,0 +1,24 @@ +package io.kimmking.java8; + +import lombok.*; +import lombok.extern.slf4j.Slf4j; + +@ToString +@NoArgsConstructor +@AllArgsConstructor +@Slf4j +@Builder +@Getter +@Setter +public class A { + + private int age; + + private String name; + +// public void test(){ +// log.info("this message is logged by lombok"); +// System.out.println(this.toString()); +// } + +} diff --git a/04fx/java8/src/main/java/io/kimmking/java8/CollectionDemo.java b/04fx/java8/src/main/java/io/kimmking/java8/CollectionDemo.java new file mode 100644 index 00000000..90670a12 --- /dev/null +++ b/04fx/java8/src/main/java/io/kimmking/java8/CollectionDemo.java @@ -0,0 +1,36 @@ +package io.kimmking.java8; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class CollectionDemo { + public static void main(String[] args) throws IOException { + + List list = Arrays.asList(4,2,3,5,1,2,2,7,6); // Arrays还可以包装stream + print(list); + Collections.sort(list); + print(list); + Collections.reverse(list); + print(list); + Collections.shuffle(list); + print(list); + + System.out.println(Collections.frequency(list, 2)); + System.out.println(Collections.max(list)); + + Collections.fill(list,8); + print(list); + + list = Collections.singletonList(6); + print(list); + + } + + private static void print(List list) { + System.out.println(String.join(",",list.stream().map(i -> i.toString()).collect(Collectors.toList()).toArray(new String[]{}))); + } + +} diff --git a/04fx/java8/src/main/java/io/kimmking/java8/ForeachDemo.java b/04fx/java8/src/main/java/io/kimmking/java8/ForeachDemo.java new file mode 100644 index 00000000..b4f8507d --- /dev/null +++ b/04fx/java8/src/main/java/io/kimmking/java8/ForeachDemo.java @@ -0,0 +1,28 @@ +package io.kimmking.java8; + +import java.util.Arrays; +import java.util.List; + +public class ForeachDemo { + + private int x=1; + + public static void main(String[] args) { + + ForeachDemo demo = new ForeachDemo(); + + demo.test(); + + System.out.println(demo.x); + } + + private void test() { + List list = Arrays.asList(1,2); + int y = 1; + list.forEach(e -> { + x=2; + //y=2; // can't be compiled + }); + } + +} diff --git a/04fx/java8/src/main/java/io/kimmking/java8/GenericDemo.java b/04fx/java8/src/main/java/io/kimmking/java8/GenericDemo.java new file mode 100644 index 00000000..7de88d61 --- /dev/null +++ b/04fx/java8/src/main/java/io/kimmking/java8/GenericDemo.java @@ -0,0 +1,31 @@ +package io.kimmking.java8; + +import java.io.Serializable; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +public class GenericDemo implements Serializable { + public static void main(String[] args) { + Demo demo = new Demo(); + Class clazz = demo.getClass(); + //getSuperclass()获得该类的父类 + System.out.println(clazz.getSuperclass()); + //getGenericSuperclass()获得带有泛型的父类 + //Type是 Java 编程语言中所有类型的公共高级接口。它们包括原始类型、参数化类型、数组类型、类型变量和基本类型。 + Type type = clazz.getGenericSuperclass(); + System.out.println(type); + //ParameterizedType参数化类型,即泛型 + ParameterizedType p = (ParameterizedType) type; + //getActualTypeArguments获取参数化类型的数组,泛型可能有多个 + Class c = (Class) p.getActualTypeArguments()[0]; + System.out.println(c); + } + + public static class Person { + + } + + public static class Demo extends Person { + + } +} diff --git a/04fx/java8/src/main/java/io/kimmking/java8/GuavaDemo.java b/04fx/java8/src/main/java/io/kimmking/java8/GuavaDemo.java new file mode 100644 index 00000000..e505fec7 --- /dev/null +++ b/04fx/java8/src/main/java/io/kimmking/java8/GuavaDemo.java @@ -0,0 +1,121 @@ +package io.kimmking.java8; + +import com.alibaba.fastjson.JSON; +import com.google.common.base.Joiner; +import com.google.common.base.Splitter; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Multimap; +import com.google.common.eventbus.EventBus; +import com.google.common.eventbus.Subscribe; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.SneakyThrows; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +public class GuavaDemo { + + static EventBus bus = new EventBus(); + static { + bus.register(new GuavaDemo()); + } + + + @SneakyThrows + public static void main(String[] args) throws IOException { + + List lists = testString(); + + List list = testList(); + + testMap(list); + + testBiMap(lists); + + testEventBus(); + + } + + private static void testEventBus() { + // EventBus + // SPI+service loader + // Callback/Listener + // + Student student2 = new Student(2, "KK02"); + System.out.println(Thread.currentThread().getName()+" I want " + student2 + " run now."); + bus.post(new AEvent(student2)); + } + + private static void testBiMap(List lists) { + BiMap words = HashBiMap.create(); + words.put("First", 1); + words.put("Second", 2); + words.put("Third", 3); + + System.out.println(words.get("Second").intValue()); + System.out.println(words.inverse().get(3)); + + Map map1 = Maps.toMap(lists.listIterator(), a -> a+"-value"); + print(map1); + } + + private static void testMap(List list) { + //Map map = list.stream().collect(Collectors.toMap(a->a,a->(a+1))); + Multimap bMultimap = ArrayListMultimap.create(); + list.forEach( + a -> bMultimap.put(a,a+1) + ); + print(bMultimap); + } + + private static List testList() { + // 更强的集合操作 + // 简化 创建 + + List list = Lists.newArrayList(4,2,3,5,1,2,2,7,6); + + List> list1 = Lists.partition(list,3); + + print(list1); + return list; + } + + private static List testString() { + // 字符串处理 + // + List lists = Lists.newArrayList("a","b","g","8","9"); + + String result = Joiner.on(",").join(lists); + System.out.println(result); + + String test = "34344,,,34,34,哈哈"; + lists = Splitter.on(",").splitToList(test); + System.out.println(lists); + return lists; + } + + private static void print(Object obj) { + System.out.println(JSON.toJSONString(obj)); + } + + + @Data + @AllArgsConstructor + public static class AEvent{ + private Student student; + } + + @Subscribe + public void handle(AEvent ae){ + System.out.println(Thread.currentThread().getName()+" "+ae.student + " is running."); + } + + + +} diff --git a/04fx/java8/src/main/java/io/kimmking/java8/LambdaDemo.java b/04fx/java8/src/main/java/io/kimmking/java8/LambdaDemo.java new file mode 100644 index 00000000..94996f3b --- /dev/null +++ b/04fx/java8/src/main/java/io/kimmking/java8/LambdaDemo.java @@ -0,0 +1,90 @@ +package io.kimmking.java8; + +import java.io.Serializable; +import java.util.Arrays; +import java.util.Collection; + +public class LambdaDemo { + + public static void main(String args[]){ + LambdaDemo demo = new LambdaDemo(); + + MathOperation op = new MathOperation() { + @Override + public Integer operation(int a, int b) { + return 1; + } + }; + + MathOperation op1 = (a, b) -> 1; + + + MathOperation op2 = new MathOperation() { + @Override + public Integer operation(int a, int b) { + return a+b; + } + }; + + // 类型声明 + MathOperation addition = (int a, int b) -> a + b; + + // 不用类型声明 + MathOperation subtraction = (int a, int b) -> a - b ; + + // 大括号中的返回语句 + MathOperation multiplication = (int a, int b) -> { + //int c = 1000; + return a * b;// + c; + }; + + // 没有大括号及返回语句 + MathOperation division = (int a, int b) -> a / b; + + System.out.println("10 + 5 = " + demo.operate(10, 5, addition)); + System.out.println("10 - 5 = " + demo.operate(10, 5, subtraction)); + System.out.println("10 x 5 = " + demo.operate(10, 5, multiplication)); + System.out.println("10 / 5 = " + demo.operate(10, 5, division)); + + //System.out.println("10 ^ 5 = " + demo.operate(10, 5, (a, b) -> new Double(Math.pow(a,b)).intValue())); + + System.out.println("10 ^ 5 = " + demo.operate(10, 5, (a, b) -> Math.pow(a,b))); + + Runnable task = () -> System.out.println(1111); + + // 不用括号 + GreetingService greetService1 = message -> + System.out.println("Hello " + message); + + // 用括号 + GreetingService greetService2 = (message) -> { + System.out.println(message); + }; + + GreetingService greetService3 = System.out::println; + + Arrays.asList(1,2,3).forEach( x -> System.out.println(x+3)); + Arrays.asList(1,2,3).forEach( LambdaDemo::println ); + + greetService1.sayMessage("kimmking"); + greetService2.sayMessage("Java"); + greetService3.sayMessage("CuiCuilaoshi"); + } + + private static void println(int x) { + System.out.println(x+3); + } + + interface MathOperation { + T operation(int a, int b); // 返回类型+函数名+参数类型的列表 + } + + interface GreetingService { + void sayMessage(String message); + } + + private T operate(int a, int b, MathOperation mathOperation){ + return mathOperation.operation(a, b); + } + +} diff --git a/04fx/java8/src/main/java/io/kimmking/java8/LombokDemo.java b/04fx/java8/src/main/java/io/kimmking/java8/LombokDemo.java new file mode 100644 index 00000000..a1333f4f --- /dev/null +++ b/04fx/java8/src/main/java/io/kimmking/java8/LombokDemo.java @@ -0,0 +1,38 @@ +package io.kimmking.java8; + +import lombok.extern.java.Log; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +@Log +public class LombokDemo { + + public static void main(String[] args) throws IOException { + + // Spring IoC + // ServiceLoader.load SPI + // Listener/Callback + // EventBus + + A a = new A(1, "KK"); + System.out.println(a.toString()); + A b = A.builder().age(1).name("KKK").build(); + + new LombokDemo().demo(); + + Student student1 = new Student(); + student1.setId(1); + student1.setName("KK01"); + System.out.println(student1.toString()); + + Student student2 = new Student(2, "KK02"); + //student2.init(); + System.out.println(student2.toString()); + } + + private void demo() { + log.info("demo in log."); + } + +} diff --git a/04fx/java8/src/main/java/io/kimmking/java8/ParallelDemo.java b/04fx/java8/src/main/java/io/kimmking/java8/ParallelDemo.java new file mode 100644 index 00000000..b977df26 --- /dev/null +++ b/04fx/java8/src/main/java/io/kimmking/java8/ParallelDemo.java @@ -0,0 +1,46 @@ +package io.kimmking.java8; + +import java.util.ArrayList; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +public class ParallelDemo { + + public static void main(String[] args) { + testSerialize(); + testParallel(); + } + + private static void testSerialize() { + ArrayList list = new ArrayList<>(5000000); + for (int i = 0; i < 5000000; i++) { + list.add(UUID.randomUUID().toString()); + } + + System.out.println("开始排序"); + long startTime = System.nanoTime();//纳秒 比毫秒的精度高 + list.stream().sorted().count(); + + long endTime = System.nanoTime(); //纳秒, 结束时间 + + long millis = TimeUnit.NANOSECONDS.toMillis(endTime - startTime); + System.out.println("串行Stream耗时:" + millis + "毫秒"); + } + + private static void testParallel() { + ArrayList list = new ArrayList<>(5000000); + for (int i = 0; i < 5000000; i++) { + list.add(UUID.randomUUID().toString()); + } + + System.out.println("开始排序"); + long startTime = System.nanoTime();//纳秒 比毫秒的精度高 + list.parallelStream().sorted().count(); + + long endTime = System.nanoTime(); //纳秒, 结束时间 + + long millis = TimeUnit.NANOSECONDS.toMillis(endTime - startTime); + System.out.println("并行Stream耗时:" + millis + "毫秒"); + } + +} diff --git a/04fx/java8/src/main/java/io/kimmking/java8/ResourceLoader.java b/04fx/java8/src/main/java/io/kimmking/java8/ResourceLoader.java new file mode 100644 index 00000000..6922b2de --- /dev/null +++ b/04fx/java8/src/main/java/io/kimmking/java8/ResourceLoader.java @@ -0,0 +1,56 @@ +package io.kimmking.java8; + +import lombok.SneakyThrows; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class ResourceLoader { + + static String file = "conf/a.properties"; + + public static void main(String[] args) { + + // testClassLoaderRootPath(); // classloader方式不能加根路径,否则文件路径会直接被替换掉最终成了 /conf/a.properties + testClassLoader(); // 从当前的classpath,不管是 文件夹,还是jar里去找 资源 + testClassRootPath(); // 判断第一个字符是根路径,去掉根,转成 testClassLoader 调用 + // testClass(); // class 方式必须加根路径,不加根会根据类路径io.kimmking.java8.ResourceLoader 去拼成 io/kimmking/java8/conf/a.properties + + + } + + private static void testClassLoader() { + System.out.println("====> testClassLoader"); + loadStream(ResourceLoader.class.getClassLoader().getResourceAsStream(file)); + } + + private static void testClassLoaderRootPath() { + System.out.println("====> testClassLoader"); + loadStream(ResourceLoader.class.getClassLoader().getResourceAsStream("/"+file)); + } + + private static void testClass() { + System.out.println("====> testClass"); + loadStream(ResourceLoader.class.getResourceAsStream(file)); + } + + private static void testClassRootPath() { + System.out.println("====> testClassRootPath"); + loadStream(ResourceLoader.class.getResourceAsStream("/"+file)); + } + + @SneakyThrows + private static void loadStream(InputStream in) { + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + StringBuffer buffer = new StringBuffer(); + String line = null; + while ((line = reader.readLine()) != null) { + buffer.append(line); + } + System.out.println(buffer.toString()); + reader.close(); + in.close(); + } + +} diff --git a/04fx/java8/src/main/java/io/kimmking/java8/StreamDemo.java b/04fx/java8/src/main/java/io/kimmking/java8/StreamDemo.java new file mode 100644 index 00000000..b2771ff3 --- /dev/null +++ b/04fx/java8/src/main/java/io/kimmking/java8/StreamDemo.java @@ -0,0 +1,53 @@ +package io.kimmking.java8; + +import com.alibaba.fastjson.JSON; + +import java.io.IOException; +import java.util.*; +import java.util.stream.Collectors; + +public class StreamDemo { + + public static void main(String[] args) throws IOException { + + List list = Arrays.asList(4,2,3,5,1,2,2,7,6); + print(list); + + // Optional + Optional first = list.stream().findFirst(); + + System.out.println(first.map(i -> i * 100).orElse(100)); + + //1,2,3 + // 0, 1, 2, 3 + int sum = list.stream().filter( i -> i<4).distinct().reduce(0,(a,b)->a+b); + System.out.println("sum="+sum); + + //1,2,3 + // 1, 1, 2, 3 + int multi = list.stream().filter( i -> i<4).distinct().reduce(1,(a,b)->a*b); + System.out.println("multi="+multi); + + //Map map1 = list.stream().collect(Collectors.toMap(a->a,a->(a+1))); + Map map = list.stream().parallel().collect(Collectors.toMap(a->a,a->(a+1),(a,b)->a, LinkedHashMap::new)); + print(map); + + + map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v)); + List list1 = map.entrySet().parallelStream().map(e -> e.getKey()+e.getValue()).collect(Collectors.toList()); + print(list1); + + // parallelStream() + + // 总结: + // 1. Fluent API:继续Stream + // 2. 终止操作:得到结果 + + + } + + + private static void print(Object obj) { + System.out.println(JSON.toJSONString(obj)); + } +} diff --git a/04fx/java8/src/main/java/io/kimmking/java8/StreamDemo2.java b/04fx/java8/src/main/java/io/kimmking/java8/StreamDemo2.java new file mode 100644 index 00000000..eeba0670 --- /dev/null +++ b/04fx/java8/src/main/java/io/kimmking/java8/StreamDemo2.java @@ -0,0 +1,188 @@ +package io.kimmking.java8; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.IntSummaryStatistics; +import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; + +public class StreamDemo2 { + public static void main(String args[]){ + System.out.println("使用 Java 8: "); + + // 计算空字符串 + List strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); + System.out.println("列表: " +strings); + long count = getCountEmptyStringUsingJava7(strings); + + System.out.println("空字符数量为: " + count); + count = getCountLength3UsingJava7(strings); + + System.out.println("字符串长度为 3 的数量为: " + count); + + // 删除空字符串 + List filtered = deleteEmptyStringsUsingJava7(strings); + System.out.println("筛选后的列表: " + filtered); + + // 删除空字符串,并使用逗号把它们合并起来 + String mergedString = getMergedStringUsingJava7(strings,", "); + System.out.println("合并字符串: " + mergedString); + List numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5); + + // 获取列表元素平方数 + List squaresList = getSquares(numbers); + System.out.println("平方数列表: " + squaresList); + List integers = Arrays.asList(1,2,13,4,15,6,17,8,19); + + System.out.println("列表: " +integers); + System.out.println("列表中最大的数 : " + getMax(integers)); + System.out.println("列表中最小的数 : " + getMin(integers)); + System.out.println("所有数之和 : " + getSum(integers)); + System.out.println("平均数 : " + getAverage(integers)); + System.out.println("随机数: "); + + // 输出10个随机数 + Random random = new Random(); + + for(int i=0; i < 10; i++){ + System.out.println(random.nextInt()); + } + + System.out.println("使用 Java 8: "); + System.out.println("列表: " +strings); + + count = strings.stream().filter(string->string.isEmpty()).count(); + System.out.println("空字符串数量为: " + count); + + count = strings.stream().filter(string -> string.length() == 3).count(); + System.out.println("字符串长度为 3 的数量为: " + count); + + filtered = strings.stream().filter(string ->!string.isEmpty()).collect(Collectors.toList()); + System.out.println("筛选后的列表: " + filtered); + + mergedString = strings.stream().filter(string ->!string.isEmpty()).collect(Collectors.joining(", ")); + System.out.println("合并字符串: " + mergedString); + + squaresList = numbers.stream().map( i ->i*i).distinct().collect(Collectors.toList()); + System.out.println("Squares List: " + squaresList); + System.out.println("列表: " +integers); + + IntSummaryStatistics stats = integers.stream().mapToInt((x) ->x).summaryStatistics(); + + System.out.println("列表中最大的数 : " + stats.getMax()); + System.out.println("列表中最小的数 : " + stats.getMin()); + System.out.println("所有数之和 : " + stats.getSum()); + System.out.println("平均数 : " + stats.getAverage()); + System.out.println("随机数: "); + + random.ints().limit(10).sorted().forEach(System.out::println); + + // 并行处理 + count = strings.parallelStream().filter(string -> string.isEmpty()).count(); + System.out.println("空字符串的数量为: " + count); + } + + private static int getCountEmptyStringUsingJava7(List strings){ + int count = 0; + + for(String string: strings){ + + if(string.isEmpty()){ + count++; + } + } + return count; + } + + private static int getCountLength3UsingJava7(List strings){ + int count = 0; + + for(String string: strings){ + + if(string.length() == 3){ + count++; + } + } + return count; + } + + private static List deleteEmptyStringsUsingJava7(List strings){ + List filteredList = new ArrayList(); + + for(String string: strings){ + + if(!string.isEmpty()){ + filteredList.add(string); + } + } + return filteredList; + } + + private static String getMergedStringUsingJava7(List strings, String separator){ + StringBuilder stringBuilder = new StringBuilder(); + + for(String string: strings){ + + if(!string.isEmpty()){ + stringBuilder.append(string); + stringBuilder.append(separator); + } + } + String mergedString = stringBuilder.toString(); + return mergedString.substring(0, mergedString.length()-2); + } + + private static List getSquares(List numbers){ + List squaresList = new ArrayList(); + + for(Integer number: numbers){ + Integer square = new Integer(number.intValue() * number.intValue()); + + if(!squaresList.contains(square)){ + squaresList.add(square); + } + } + return squaresList; + } + + private static int getMax(List numbers){ + int max = numbers.get(0); + + for(int i=1;i < numbers.size();i++){ + + Integer number = numbers.get(i); + + if(number.intValue() > max){ + max = number.intValue(); + } + } + return max; + } + + private static int getMin(List numbers){ + int min = numbers.get(0); + + for(int i=1;i < numbers.size();i++){ + Integer number = numbers.get(i); + + if(number.intValue() < min){ + min = number.intValue(); + } + } + return min; + } + + private static int getSum(List numbers){ + int sum = (int)(numbers.get(0)); + + for(int i=1;i < numbers.size();i++){ + sum += (int)numbers.get(i); + } + return sum; + } + + private static int getAverage(List numbers){ + return getSum(numbers) / numbers.size(); + } +} diff --git a/04fx/java8/src/main/java/io/kimmking/java8/Student.java b/04fx/java8/src/main/java/io/kimmking/java8/Student.java new file mode 100644 index 00000000..f08748c4 --- /dev/null +++ b/04fx/java8/src/main/java/io/kimmking/java8/Student.java @@ -0,0 +1,47 @@ +package io.kimmking.java8; + + +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +import java.io.Serializable; + + +@Data +@NoArgsConstructor +@ToString +@Slf4j +public class Student implements Serializable, ApplicationContextAware { + + private int id; + private String name; + + private ApplicationContext applicationContext; + + public Student(int id, String name) { + this.id = id; + this.name = name; + } + + public void init(){ + System.out.println("hello..........."); + log.debug("ddddddddddddddd"); + } + + public Student create(){ + + this.applicationContext.getBeanDefinitionNames(); + Student s = new Student(101,"KK101"); + return s; + } + + @Override + public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } +} diff --git a/04fx/java8/src/main/java/io/kimmking/java8/TestMem.java b/04fx/java8/src/main/java/io/kimmking/java8/TestMem.java new file mode 100644 index 00000000..1ffcf8f7 --- /dev/null +++ b/04fx/java8/src/main/java/io/kimmking/java8/TestMem.java @@ -0,0 +1,27 @@ +package io.kimmking.java8; + +import org.openjdk.jol.info.ClassLayout; +import org.openjdk.jol.info.GraphLayout; + +public class TestMem { + public static void main(String[] args) { + int[] arr1 = new int[256]; + int[][] arr2 = new int[128][2]; + int[][][] arr3 = new int[64][2][2]; + + print("1-size : " + GraphLayout.parseInstance(arr1).totalSize()); + print(ClassLayout.parseInstance(arr1).toPrintable()); + print("2-size : " + GraphLayout.parseInstance(arr2).totalSize()); + print(ClassLayout.parseInstance(arr2).toPrintable()); + print(GraphLayout.parseInstance(arr2).toPrintable()); + print("3-size : " + GraphLayout.parseInstance(arr3).totalSize()); + print(ClassLayout.parseInstance(arr3).toPrintable()); + print(GraphLayout.parseInstance(arr3).toPrintable()); + System.out.println(); + } + + static void print(String message) { + System.out.println(message); + System.out.println("-------------------------"); + } +} \ No newline at end of file diff --git a/04fx/java8/src/main/resources/conf/a.properties b/04fx/java8/src/main/resources/conf/a.properties new file mode 100644 index 00000000..86fbde04 --- /dev/null +++ b/04fx/java8/src/main/resources/conf/a.properties @@ -0,0 +1,2 @@ +a=1 +b=2 \ No newline at end of file diff --git a/04fx/java8/src/main/resources/log4j.xml b/04fx/java8/src/main/resources/log4j.xml new file mode 100644 index 00000000..efc1f4f6 --- /dev/null +++ b/04fx/java8/src/main/resources/log4j.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/04fx/java8/src/test/java/io/kimmking/java8/StudentTest.java b/04fx/java8/src/test/java/io/kimmking/java8/StudentTest.java new file mode 100644 index 00000000..9f83fcaa --- /dev/null +++ b/04fx/java8/src/test/java/io/kimmking/java8/StudentTest.java @@ -0,0 +1,18 @@ +package io.kimmking.java8; + +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +public class StudentTest { + + private Student class1; + + @Test + public void KlassTest(){ + class1 = Mockito.mock(Student.class, Mockito.RETURNS_DEEP_STUBS); + Mockito.when(class1.getApplicationContext().getId()).thenReturn("10"); + Assert.assertEquals("10", (class1.getApplicationContext().getId())); + } + // 单元测试 +} diff --git a/04fx/spring01/pom.xml b/04fx/spring01/pom.xml new file mode 100644 index 00000000..3075aaf2 --- /dev/null +++ b/04fx/spring01/pom.xml @@ -0,0 +1,170 @@ + + + 4.0.0 + + io.kimmking + spring01 + 1.0 + + + + 4.3.30.RELEASE + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 11 + 11 + + + + + + + + com.google.guava + guava + 29.0-jre + + + org.projectlombok + lombok + 1.18.12 + + + org.springframework + spring-core + ${spring-version} + + + + + + + + org.springframework + spring-beans + ${spring-version} + + + org.springframework + spring-aop + ${spring-version} + + + org.springframework + spring-aspects + ${spring-version} + + + org.springframework + spring-context + ${spring-version} + + + org.springframework + spring-context-support + ${spring-version} + + + org.springframework + spring-orm + ${spring-version} + + + org.springframework + spring-tx + ${spring-version} + + + org.springframework + spring-test + ${spring-version} + + + org.springframework + spring-messaging + ${spring-version} + + + org.springframework + spring-jms + ${spring-version} + + + org.springframework + spring-expression + ${spring-version} + + + + com.alibaba + fastjson + 1.2.72 + + + commons-logging + commons-logging + 1.2 + + + log4j + log4j + 1.2.17 + + + log4j + log4j + 1.2.17 + + + org.slf4j + slf4j-api + 1.7.25 + + + org.slf4j + slf4j-log4j12 + 1.7.25 + test + + + org.slf4j + slf4j-simple + 1.7.25 + + + junit + junit + 4.12 + test + + + org.mockito + mockito-all + 1.10.19 + test + + + + org.apache.activemq + activemq-client + 5.15.0 + + + + + + + + + + + + \ No newline at end of file diff --git a/04fx/spring01/src/main/java/io/kimmking/DemoMethodIncepter.java b/04fx/spring01/src/main/java/io/kimmking/DemoMethodIncepter.java new file mode 100644 index 00000000..d9a00e1f --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/DemoMethodIncepter.java @@ -0,0 +1,17 @@ +package io.kimmking; + +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; + +public class DemoMethodIncepter implements MethodInterceptor { + + public Object invoke(MethodInvocation invocation) throws Throwable { + + long s = System.currentTimeMillis(); + System.out.println(" *****====> " + s + " " + invocation.getMethod().getName()); + Object result = invocation.proceed(); + System.out.println(" *****====> " + (System.currentTimeMillis() - s) + " ms"); + return result; + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/anno/AnnoDemo.java b/04fx/spring01/src/main/java/io/kimmking/anno/AnnoDemo.java new file mode 100644 index 00000000..630f9e19 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/anno/AnnoDemo.java @@ -0,0 +1,34 @@ +package io.kimmking.anno; + +import java.lang.annotation.Annotation; +import java.util.Arrays; + +public class AnnoDemo { + + public static void main(String[] args) { + IA2 ia2 = new IA2() { + @Override + public void a2() { + System.out.println("a2."); + } + + @Override + public void a1() { + System.out.println("a1."); + } + }; + + Annotation[] annotations = ia2.getClass().getInterfaces()[0].getAnnotations(); + Arrays.stream(annotations).forEach(x -> System.out.println(x.annotationType().getCanonicalName())); + + Annotation[] annos2 = ia2.getClass().getInterfaces()[0].getInterfaces()[0].getAnnotations(); + Arrays.stream(annos2).forEach(x -> System.out.println(x.annotationType().getCanonicalName())); + Arrays.stream(annos2).forEach( + x -> { + IAnno2 anno2 = (IAnno2) x; + System.out.println(anno2.value()); + }); + + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/anno/IA1.java b/04fx/spring01/src/main/java/io/kimmking/anno/IA1.java new file mode 100644 index 00000000..8d7f50dc --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/anno/IA1.java @@ -0,0 +1,9 @@ +package io.kimmking.anno; + + +@IAnno2("anno2") +public interface IA1 { + + void a1(); + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/anno/IA2.java b/04fx/spring01/src/main/java/io/kimmking/anno/IA2.java new file mode 100644 index 00000000..88e8db00 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/anno/IA2.java @@ -0,0 +1,9 @@ +package io.kimmking.anno; + + +@IAnnotation +public interface IA2 extends IA1 { + + void a2(); + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/anno/IAnno2.java b/04fx/spring01/src/main/java/io/kimmking/anno/IAnno2.java new file mode 100644 index 00000000..da04c7d5 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/anno/IAnno2.java @@ -0,0 +1,14 @@ +package io.kimmking.anno; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface IAnno2 { + + String value(); + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/anno/IAnnotation.java b/04fx/spring01/src/main/java/io/kimmking/anno/IAnnotation.java new file mode 100644 index 00000000..becda7a8 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/anno/IAnnotation.java @@ -0,0 +1,12 @@ +package io.kimmking.anno; + + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface IAnnotation { +} diff --git a/04fx/spring01/src/main/java/io/kimmking/aop/ISchool.java b/04fx/spring01/src/main/java/io/kimmking/aop/ISchool.java new file mode 100644 index 00000000..3e9d8f1f --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/aop/ISchool.java @@ -0,0 +1,7 @@ +package io.kimmking.aop; + +public interface ISchool { + + void ding(); + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring01/A.java b/04fx/spring01/src/main/java/io/kimmking/spring01/A.java new file mode 100644 index 00000000..f0873cca --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring01/A.java @@ -0,0 +1,10 @@ +package io.kimmking.spring01; + +import lombok.Data; + +@Data +public class A { + + private int age; + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring01/CollectionDemo.java b/04fx/spring01/src/main/java/io/kimmking/spring01/CollectionDemo.java new file mode 100644 index 00000000..bdb18885 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring01/CollectionDemo.java @@ -0,0 +1,36 @@ +package io.kimmking.spring01; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class CollectionDemo { + public static void main(String[] args) throws IOException { + + List list = Arrays.asList(4,2,3,5,1,2,2,7,6); // Arrays还可以包装stream + print(list); + Collections.sort(list); + print(list); + Collections.reverse(list); + print(list); + Collections.shuffle(list); + print(list); + + System.out.println(Collections.frequency(list, 2)); + System.out.println(Collections.max(list)); + + Collections.fill(list,8); + print(list); + + list = Collections.singletonList(6); + print(list); + + } + + private static void print(List list) { + System.out.println(String.join(",",list.stream().map(i -> i.toString()).collect(Collectors.toList()).toArray(new String[]{}))); + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring01/GuavaDemo.java b/04fx/spring01/src/main/java/io/kimmking/spring01/GuavaDemo.java new file mode 100644 index 00000000..697b680c --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring01/GuavaDemo.java @@ -0,0 +1,108 @@ +package io.kimmking.spring01; + +import com.alibaba.fastjson.JSON; +import com.google.common.base.Joiner; +import com.google.common.base.Splitter; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Multimap; +import com.google.common.eventbus.EventBus; +import com.google.common.eventbus.Subscribe; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.SneakyThrows; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; + +public class GuavaDemo { + + static EventBus bus = new EventBus(); + static { + bus.register(new GuavaDemo()); + } + + + @SneakyThrows + public static void main(String[] args) throws IOException { + + // 字符串处理 + // + List lists = Lists.newArrayList("a","b","g","8","9"); + String result = Joiner.on(",").join(lists); + System.out.println(result); + + String test = "34344,34,34,哈哈"; + lists = Splitter.on(",").splitToList(test); + System.out.println(lists); + + // 更强的集合操作 + // 简化 创建 + + List list = Lists.newArrayList(4,2,3,5,1,2,2,7,6); + + List> list1 = Lists.partition(list,3); + print(list1); + + + + //Map map = list.stream().collect(Collectors.toMap(a->a,a->(a+1))); + Multimap bMultimap = ArrayListMultimap.create(); + list.forEach( + a -> bMultimap.put(a,a+1) + ); + print(bMultimap); + + BiMap words = HashBiMap.create(); + words.put("First", 1); + words.put("Second", 2); + words.put("Third", 3); + + System.out.println(words.get("Second").intValue()); + System.out.println(words.inverse().get(3)); + + Map map1 = Maps.toMap(lists.listIterator(),a -> a+"-value"); + print(map1); + + + + // EventBus + // SPI+service loader + // Callback/Listener + // + Student student2 = Student.create(); + System.out.println("I want " + student2 + " run now."); + bus.post(new AEvent(student2)); + + + + } + + private static void print(Object obj) { + System.out.println(JSON.toJSONString(obj)); + } + + + @Data + @AllArgsConstructor + public static class AEvent{ + private Student student; + } + + @Subscribe + public void handle(AEvent ae){ + System.out.println(ae.student + " is running."); + } + + + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring01/LombokDemo.java b/04fx/spring01/src/main/java/io/kimmking/spring01/LombokDemo.java new file mode 100644 index 00000000..c28b48e2 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring01/LombokDemo.java @@ -0,0 +1,27 @@ +package io.kimmking.spring01; + +import lombok.extern.java.Log; + +import java.io.IOException; + +@Log +public class LombokDemo { + + public static void main(String[] args) throws IOException { + + new LombokDemo().demo(); + + Student student1 = new Student(); + student1.setId(1); + student1.setName("KK01"); + System.out.println(student1.toString()); + + Student student2 = Student.create(); + System.out.println(student2.toString()); + } + + private void demo() { + log.info("demo in log."); + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring01/StreamDemo.java b/04fx/spring01/src/main/java/io/kimmking/spring01/StreamDemo.java new file mode 100644 index 00000000..835e1f21 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring01/StreamDemo.java @@ -0,0 +1,52 @@ +package io.kimmking.spring01; + +import com.alibaba.fastjson.JSON; + +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; + +public class StreamDemo { + + public static void main(String[] args) throws IOException { + + List list = Arrays.asList(4,2,3,5,1,2,2,7,6); + print(list); + + // Optional + Optional first = list.stream().findFirst(); + + System.out.println(first.map(i -> i * 100).orElse(100)); + + + + int sum = list.stream().filter( i -> i<4).distinct().reduce(0,(a,b)->a+b); + System.out.println("sum="+sum); + + //Map map = list.stream().collect(Collectors.toMap(a->a,a->(a+1))); + Map map = list.parallelStream().collect(Collectors.toMap(a->a,a->(a+1),(a,b)->a, HashMap::new)); + print(map); + + + map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v)); + List list1 = map.entrySet().parallelStream().map(e -> e.getKey()+e.getValue()).collect(Collectors.toList()); + print(list1); + + // parallelStream() + + // 总结: + // 1. Fluent API:继续Stream + // 2. 终止操作:得到结果 + + + } + + + private static void print(Object obj) { + System.out.println(JSON.toJSONString(obj)); + } +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring01/Student.java b/04fx/spring01/src/main/java/io/kimmking/spring01/Student.java new file mode 100644 index 00000000..a2357bf6 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring01/Student.java @@ -0,0 +1,45 @@ +package io.kimmking.spring01; + + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanNameAware; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +import java.io.Serializable; + + +@Data +@AllArgsConstructor +@NoArgsConstructor +@ToString +public class Student implements Serializable, BeanNameAware, ApplicationContextAware { + + + private int id; + private String name; + + private String beanName; + private ApplicationContext applicationContext; + + public void init(){ + System.out.println("hello..........."); + } + + public static Student create(){ + return new Student(102,"KK102",null, null); + } + + public void print() { + System.out.println(this.beanName); + System.out.println(" context.getBeanDefinitionNames() ===>> " + + String.join(",", applicationContext.getBeanDefinitionNames())); + + } + + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring01/http/HttpServer01.java b/04fx/spring01/src/main/java/io/kimmking/spring01/http/HttpServer01.java new file mode 100644 index 00000000..16a98428 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring01/http/HttpServer01.java @@ -0,0 +1,35 @@ +package io.kimmking.spring01.http; + +import java.io.IOException; +import java.io.PrintWriter; +import java.net.ServerSocket; +import java.net.Socket; + +public class HttpServer01 { + public static void main(String[] args) throws IOException{ + ServerSocket serverSocket = new ServerSocket(8801); + while (true) { + try { + Socket socket = serverSocket.accept(); + service(socket); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + private static void service(Socket socket) { + try { + Thread.sleep(20); + PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true); + printWriter.println("HTTP/1.1 200 OK"); + printWriter.println("Content-Type:text/html;charset=utf-8"); + printWriter.println(); + printWriter.write("hello,nio"); + printWriter.close(); + socket.close(); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/04fx/spring01/src/main/java/io/kimmking/spring01/http/HttpServer02.java b/04fx/spring01/src/main/java/io/kimmking/spring01/http/HttpServer02.java new file mode 100644 index 00000000..dceeb09e --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring01/http/HttpServer02.java @@ -0,0 +1,38 @@ +package io.kimmking.spring01.http; + +import java.io.IOException; +import java.io.PrintWriter; +import java.net.ServerSocket; +import java.net.Socket; + +public class HttpServer02 { + public static void main(String[] args) throws IOException{ + ServerSocket serverSocket = new ServerSocket(8802); + while (true) { + try { + final Socket socket = serverSocket.accept(); + + new Thread(() -> { + service(socket); + }).start(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + private static void service(Socket socket) { + try { + Thread.sleep(20); + PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true); + printWriter.println("HTTP/1.1 200 OK"); + printWriter.println("Content-Type:text/html;charset=utf-8"); + printWriter.println(); + printWriter.write("hello,nio"); + printWriter.close(); + socket.close(); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/04fx/spring01/src/main/java/io/kimmking/spring01/http/HttpServer03.java b/04fx/spring01/src/main/java/io/kimmking/spring01/http/HttpServer03.java new file mode 100644 index 00000000..6ac3f63c --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring01/http/HttpServer03.java @@ -0,0 +1,38 @@ +package io.kimmking.spring01.http; + +import java.io.IOException; +import java.io.PrintWriter; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +public class HttpServer03 { + public static void main(String[] args) throws IOException{ + ExecutorService executorService = Executors.newFixedThreadPool(40); + final ServerSocket serverSocket = new ServerSocket(8803); + while (true) { + try { + final Socket socket = serverSocket.accept(); + executorService.execute(() -> service(socket)); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + private static void service(Socket socket) { + try { + Thread.sleep(20); // 模拟业务 20 ms + PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true); + printWriter.println("HTTP/1.1 200 OK"); + printWriter.println("Content-Type:text/html;charset=utf-8"); + printWriter.println(); + printWriter.write("hello,nio"); + printWriter.close(); + socket.close(); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/04fx/spring01/src/main/java/io/kimmking/spring02/Aop1.java b/04fx/spring01/src/main/java/io/kimmking/spring02/Aop1.java new file mode 100644 index 00000000..fa919d39 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring02/Aop1.java @@ -0,0 +1,26 @@ +package io.kimmking.spring02; + +import org.aspectj.lang.ProceedingJoinPoint; + +public class Aop1 { + + //前置通知 + public void startTransaction(){ + System.out.println(" ====>begin ding... "); //2 + } + + //后置通知 + public void commitTransaction(){ + System.out.println(" ====>finish ding... "); //4 + } + + //环绕通知 + public void around(ProceedingJoinPoint joinPoint) throws Throwable{ + System.out.println(" ====>around begin ding"); //1 + //调用process()方法才会真正的执行实际被代理的方法 + joinPoint.proceed(); + + System.out.println(" ====>around finish ding"); //3 + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring02/Aop2.java b/04fx/spring01/src/main/java/io/kimmking/spring02/Aop2.java new file mode 100644 index 00000000..4edbe006 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring02/Aop2.java @@ -0,0 +1,37 @@ +package io.kimmking.spring02; + + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.AfterReturning; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; + +@Aspect +public class Aop2 { + + @Pointcut(value="execution(* io.kimmking.*.Klass.*dong(..))") + public void point(){ + + } + + @Before(value="point()") + public void before(){ + System.out.println("========>begin klass dong... //2"); + } + + @AfterReturning(value = "point()") + public void after(){ + System.out.println("========>after klass dong... //4"); + } + + @Around("point()") + public void around(ProceedingJoinPoint joinPoint) throws Throwable{ + System.out.println("========>around begin klass dong //1"); + joinPoint.proceed(); + System.out.println("========>around after klass dong //3"); + + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring02/HelloBeanDefinitionRegistryPostProcessor.java b/04fx/spring01/src/main/java/io/kimmking/spring02/HelloBeanDefinitionRegistryPostProcessor.java new file mode 100644 index 00000000..d6433bd6 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring02/HelloBeanDefinitionRegistryPostProcessor.java @@ -0,0 +1,29 @@ +package io.kimmking.spring02; + +import io.kimmking.spring01.Student; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.stereotype.Component; + +@Component +public class HelloBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor { + @Override + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { + System.out.println(" ==> postProcessBeanDefinitionRegistry: "+registry.getBeanDefinitionCount()); + System.out.println(" ==> postProcessBeanDefinitionRegistry: "+String.join(",",registry.getBeanDefinitionNames())); + RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(Student.class); + rootBeanDefinition.getPropertyValues().add("id", 101); + rootBeanDefinition.getPropertyValues().add("name", "KK101"); + registry.registerBeanDefinition("s101", rootBeanDefinition); + } + + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { + System.out.println(" ==> postProcessBeanFactory: "+beanFactory.getBeanDefinitionCount()); + System.out.println(" ==> postProcessBeanFactory: "+String.join(",",beanFactory.getBeanDefinitionNames())); + beanFactory.registerSingleton("s102", Student.create()); + } +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring02/HelloBeanPostProcessor.java b/04fx/spring01/src/main/java/io/kimmking/spring02/HelloBeanPostProcessor.java new file mode 100644 index 00000000..451c829b --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring02/HelloBeanPostProcessor.java @@ -0,0 +1,27 @@ +package io.kimmking.spring02; + +import io.kimmking.spring01.Student; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.stereotype.Component; + +@Component +public class HelloBeanPostProcessor implements BeanPostProcessor { + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + System.out.println(" ====> postProcessBeforeInitialization " + beanName +":"+ bean); + // 可以加点额外处理 + // 例如 + if (bean instanceof Student) { + Student student = (Student) bean; + student.setName(student.getName() + "-" + System.currentTimeMillis()); + } + return bean; + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + System.out.println(" ====> postProcessAfterInitialization " + beanName +":"+ bean); + return bean; + } +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring02/Klass.java b/04fx/spring01/src/main/java/io/kimmking/spring02/Klass.java new file mode 100644 index 00000000..65268a7a --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring02/Klass.java @@ -0,0 +1,17 @@ +package io.kimmking.spring02; + +import io.kimmking.spring01.Student; +import lombok.Data; + +import java.util.List; + +@Data +public class Klass { + + List students; + + public void dong(){ + System.out.println(this.getStudents()); + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring02/School.java b/04fx/spring01/src/main/java/io/kimmking/spring02/School.java new file mode 100644 index 00000000..c80ba638 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring02/School.java @@ -0,0 +1,30 @@ +package io.kimmking.spring02; + +import io.kimmking.aop.ISchool; +import io.kimmking.spring01.Student; +import lombok.Data; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; + +@Data +public class School implements ISchool { + + // Resource + @Autowired(required = true) //primary + Klass class1; + + @Resource(name = "student100") + Student student100; + + @Override + public void ding(){ + + System.out.println("Class1 have " + this.class1.getStudents().size() + " students and one is " + this.student100); + + } + + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo01.java b/04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo01.java new file mode 100644 index 00000000..ae3b7206 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo01.java @@ -0,0 +1,105 @@ +package io.kimmking.spring02; + +import io.kimmking.aop.ISchool; +import io.kimmking.spring01.Student; +import org.springframework.cglib.proxy.Enhancer; +import org.springframework.cglib.proxy.MethodInterceptor; +import org.springframework.cglib.proxy.MethodProxy; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.lang.reflect.Method; + +public class SpringDemo01 { + + public static void main(String[] args) { + + long s = System.currentTimeMillis(); + + ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); +// Student student123 = context.getBean(Student.class); + + Student student123 = (Student) context.getBean("student123"); + System.out.println(student123.toString()); + + student123.print(); + + Student student100 = (Student) context.getBean("student100"); + System.out.println(student100.toString()); + + student100.print(); + + + Klass class1 = context.getBean(Klass.class); + System.out.println(class1); + System.out.println("Klass对象AOP代理后的实际类型:"+class1.getClass()); + System.out.println("Klass对象AOP代理后的实际类型是否是Klass子类:"+(class1 instanceof Klass)); + + s = System.currentTimeMillis(); + class1.dong(); + System.out.println(" *****====> class1.dong() " + (System.currentTimeMillis() - s) + " ms"); + + ISchool school = context.getBean(ISchool.class); + System.out.println(school); + System.out.println("ISchool接口的对象AOP代理后的实际类型:"+school.getClass()); + + + + + s = System.currentTimeMillis(); + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(Demo.class); + enhancer.setCallback(new MI()); + enhancer.setUseCache(true); + Demo demo = (Demo) enhancer.create(); + for (int i = 0; i < 1; i++) { + demo.a1(1); + demo.a2("hello"); + } + System.out.println(" *****====> enhancer proxy " + (System.currentTimeMillis() - s) + " ms"); + + + + + ISchool school1 = context.getBean(ISchool.class); + System.out.println(school1); + System.out.println("ISchool接口的对象AOP代理后的实际类型:"+school1.getClass()); + + school1.ding(); + + class1.dong(); + + System.out.println(" context.getBeanDefinitionNames() ===>> "+ String.join(",", context.getBeanDefinitionNames())); + + Student s101 = (Student) context.getBean("s101"); + if (s101 != null) { + System.out.println(s101); + } + Student s102 = (Student) context.getBean("s102"); + if (s102 != null) { + System.out.println(s102); + } + } + + static class MI implements MethodInterceptor { + @Override + public Object intercept(Object obj, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { + long s = System.currentTimeMillis(); + System.out.println(" *****====> " + s + " " +"Before:"+method.getName()); + Object result= methodProxy.invokeSuper(obj, objects); + System.out.println(" *****====> " + (System.currentTimeMillis() - s) + " ms After:"+method.getName()); + return result; + } + } + + static class Demo { + public void a1(int i) { + System.out.println(" *****====> Demo a1, " + i); + } + + public void a2(String str) { + System.out.println(" *****====> Demo a2," + str); + } + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo11.java b/04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo11.java new file mode 100644 index 00000000..bcbc7e30 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring02/SpringDemo11.java @@ -0,0 +1,49 @@ +package io.kimmking.spring02; + +import org.springframework.cglib.proxy.Enhancer; +import org.springframework.cglib.proxy.MethodInterceptor; +import org.springframework.cglib.proxy.MethodProxy; + +import java.lang.reflect.Method; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/22 18:01 + */ +public class SpringDemo11 { + + public static void main(String[] args) { + long s = System.currentTimeMillis(); + Enhancer enhancer = new Enhancer(); + enhancer.setInterfaces(new Class[]{IAction.class}); + enhancer.setCallback(new MI()); + enhancer.setUseCache(true); + IAction demo = (IAction) enhancer.create(); + for (int i = 0; i < 5; i++) { + long ss = System.currentTimeMillis(); + System.out.println(demo.action()); + System.out.println( i + " *****====> invoke proxy " + (System.currentTimeMillis() - ss) + " ms"); + } + System.out.println(" *****====> enhancer proxy " + (System.currentTimeMillis() - s) + " ms"); + + } + + public interface IAction { + Object action(); + } + + + static class MI implements MethodInterceptor { + @Override + public Object intercept(Object obj, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { + long s = System.currentTimeMillis(); + System.out.println(" *****==MI==> " + s + " " +"Before:"+method.getName()); + Object result = "S-" + s;//methodProxy.invokeSuper(obj, objects); + System.out.println(" *****==MI==> " + (System.currentTimeMillis() - s) + " ms After:"+method.getName()); + return result; + } + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring03/Spring03Main.java b/04fx/spring01/src/main/java/io/kimmking/spring03/Spring03Main.java new file mode 100644 index 00000000..5ad222e7 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring03/Spring03Main.java @@ -0,0 +1,14 @@ +package io.kimmking.spring03; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class Spring03Main { + + public static void main(String[] args) { + ApplicationContext context = new AnnotationConfigApplicationContext(Spring03Main.class.getPackage().getName()); + TestService1 testService1 = context.getBean(TestService1.class); + testService1.test1(); + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring03/TestService1.java b/04fx/spring01/src/main/java/io/kimmking/spring03/TestService1.java new file mode 100644 index 00000000..1a215396 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring03/TestService1.java @@ -0,0 +1,24 @@ +package io.kimmking.spring03; + +import lombok.Data; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.stereotype.Service; + +@Service +@Data +@EnableAsync +public class TestService1 { // TODO rename this class to TestService6 and it works well. + + @Autowired + private TestService2 service2; + + @Async + public void test1() { + System.out.println("test1"); + System.out.println(this.getClass().getCanonicalName()); + System.out.println(Thread.currentThread().getName()); + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring03/TestService2.java b/04fx/spring01/src/main/java/io/kimmking/spring03/TestService2.java new file mode 100644 index 00000000..37349631 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring03/TestService2.java @@ -0,0 +1,18 @@ +package io.kimmking.spring03; + +import lombok.Data; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +@Data +public class TestService2 { + + @Autowired + private TestService1 service1; + + public void test2() { + System.out.println("test2"); + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring04/ABCPlugin.java b/04fx/spring01/src/main/java/io/kimmking/spring04/ABCPlugin.java new file mode 100644 index 00000000..0971612d --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring04/ABCPlugin.java @@ -0,0 +1,10 @@ +package io.kimmking.spring04; + +import org.springframework.core.Ordered; + +public interface ABCPlugin extends Ordered { + + void startup() throws Exception; + void shutdown() throws Exception; + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring04/ABCStartup.java b/04fx/spring01/src/main/java/io/kimmking/spring04/ABCStartup.java new file mode 100644 index 00000000..7c451e01 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring04/ABCStartup.java @@ -0,0 +1,26 @@ +package io.kimmking.spring04; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.annotation.PreDestroy; +import java.util.List; + +@Component +public class ABCStartup { + + @Autowired + private List services; + + public void call () { + services.forEach(System.out::println); + } + + @PreDestroy + public void destroy() { + + System.out.println(Thread.currentThread().getName()+"-destroy:" + this.getClass().getSimpleName()); + + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring04/ABCStartupListener.java b/04fx/spring01/src/main/java/io/kimmking/spring04/ABCStartupListener.java new file mode 100644 index 00000000..9cd14157 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring04/ABCStartupListener.java @@ -0,0 +1,46 @@ +package io.kimmking.spring04; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextClosedEvent; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.context.event.ContextStartedEvent; +import org.springframework.context.event.ContextStoppedEvent; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class ABCStartupListener implements ApplicationListener { + + @Autowired + private List services; + + @Override + public void onApplicationEvent(ApplicationEvent event) { + + System.out.println(event); + + // 如果用spring boot可以怎么改进 + if (event instanceof ContextStartedEvent || event instanceof ContextRefreshedEvent) { + services.forEach(x -> { + try { + x.startup(); + } catch (Exception e) { + e.printStackTrace(); + } + }); + } + + if (event instanceof ContextClosedEvent | event instanceof ContextStoppedEvent) { + services.forEach(x -> { + try { + x.shutdown(); + } catch (Exception e) { + e.printStackTrace(); + } + }); + } + } +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring04/MockABCPlugin1.java b/04fx/spring01/src/main/java/io/kimmking/spring04/MockABCPlugin1.java new file mode 100644 index 00000000..a159eced --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring04/MockABCPlugin1.java @@ -0,0 +1,36 @@ +package io.kimmking.spring04; + +import org.springframework.beans.factory.DisposableBean; +import org.springframework.core.Ordered; +import org.springframework.stereotype.Component; + +@Component() +public class MockABCPlugin1 implements ABCPlugin, DisposableBean { + + @Override + public String toString() { + System.out.println(Thread.currentThread().getName()+"-"+this.getClass().getSimpleName()+" toString."); + return this.getClass().getSimpleName(); + } + + @Override + public int getOrder() { + return 1; + } + + @Override + public void startup() throws Exception { + // mock for netty server start + System.out.println(Thread.currentThread().getName()+"-"+this.toString()+" started."); + } + + @Override + public void shutdown() throws Exception { // 线程池之类可以改进为优雅停机 + System.out.println(Thread.currentThread().getName()+"-"+this.toString()+" stopped."); + } + + @Override + public void destroy() throws Exception { + System.out.println(Thread.currentThread().getName()+"-DisposableBean:" + this.toString()); + } +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring04/MockABCPlugin2.java b/04fx/spring01/src/main/java/io/kimmking/spring04/MockABCPlugin2.java new file mode 100644 index 00000000..5c9e6af6 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring04/MockABCPlugin2.java @@ -0,0 +1,28 @@ +package io.kimmking.spring04; + +import org.springframework.core.Ordered; +import org.springframework.stereotype.Component; + +@Component() +public class MockABCPlugin2 implements ABCPlugin { + + @Override + public String toString() { + return this.getClass().getSimpleName(); + } + + @Override + public int getOrder() { + return 2; + } + + @Override + public void startup() throws Exception { + System.out.println(this.toString()+" started."); + } + + @Override + public void shutdown() throws Exception { + System.out.println(this.toString()+" stopped."); + } +} diff --git a/04fx/spring01/src/main/java/io/kimmking/spring04/Spring04Main.java b/04fx/spring01/src/main/java/io/kimmking/spring04/Spring04Main.java new file mode 100644 index 00000000..8c7a2f0d --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/spring04/Spring04Main.java @@ -0,0 +1,21 @@ +package io.kimmking.spring04; + +import org.springframework.context.ApplicationEvent; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class Spring04Main { + + public static void main(String[] args) { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spring04Main.class.getPackage().getName()); + ABCStartup abc = context.getBean(ABCStartup.class); + abc.call(); + + context.publishEvent(new ApplicationEvent("myEvent") {}); + + context.close(); + // context.destroy(); + // context.stop(); // 这3个有什么区别? + + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/springjms/JmsListener.java b/04fx/spring01/src/main/java/io/kimmking/springjms/JmsListener.java new file mode 100644 index 00000000..7518e6e2 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/springjms/JmsListener.java @@ -0,0 +1,23 @@ +package io.kimmking.springjms; + +import org.springframework.stereotype.Component; + +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.ObjectMessage; + +@Component(value = "jmsListener") +public class JmsListener implements MessageListener { + + //收到信息时的动作 + @Override + public void onMessage(Message m) { + ObjectMessage message = (ObjectMessage) m; + try { + System.out.println("收到的信息:" + message.getObject()); + } catch (JMSException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/04fx/spring01/src/main/java/io/kimmking/springjms/JmsReceiver.java b/04fx/spring01/src/main/java/io/kimmking/springjms/JmsReceiver.java new file mode 100644 index 00000000..1373b66f --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/springjms/JmsReceiver.java @@ -0,0 +1,20 @@ +package io.kimmking.springjms; + +import io.kimmking.spring01.Student; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.io.IOException; + +public class JmsReceiver { + + public static void main( String[] args ) throws IOException { + + ApplicationContext context = new ClassPathXmlApplicationContext("classpath:springjms-receiver.xml"); + + System.in.read(); + + System.out.println("send successfully, please visit http://localhost:8161/admin to see it"); + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/springjms/JmsSender.java b/04fx/spring01/src/main/java/io/kimmking/springjms/JmsSender.java new file mode 100644 index 00000000..fb0fbeec --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/springjms/JmsSender.java @@ -0,0 +1,24 @@ +package io.kimmking.springjms; + +import io.kimmking.spring01.Student; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class JmsSender { + + public static void main( String[] args ) + { + Student student2 = Student.create(); + + ApplicationContext context = new ClassPathXmlApplicationContext("classpath:springjms-sender.xml"); + + SendService sendService = (SendService)context.getBean("sendService"); + + student2.setName("KK103"); + + sendService.send(student2); + + System.out.println("send successfully, please visit http://localhost:8161/admin to see it"); + } + +} diff --git a/04fx/spring01/src/main/java/io/kimmking/springjms/SendService.java b/04fx/spring01/src/main/java/io/kimmking/springjms/SendService.java new file mode 100644 index 00000000..15bafce1 --- /dev/null +++ b/04fx/spring01/src/main/java/io/kimmking/springjms/SendService.java @@ -0,0 +1,27 @@ +package io.kimmking.springjms; + +import com.alibaba.fastjson.JSON; +import io.kimmking.spring01.Student; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jms.core.JmsTemplate; +import org.springframework.jms.core.MessageCreator; +import org.springframework.stereotype.Component; + +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.Session; + +@Component +public class SendService { + @Autowired + JmsTemplate jmsTemplate; + + public void send(final Student user) { + jmsTemplate.send("test.queue", new MessageCreator() { + + public Message createMessage(Session session) throws JMSException { + return session.createObjectMessage(JSON.toJSONString(user)); + } + }); + } +} \ No newline at end of file diff --git a/04fx/spring01/src/main/resources/applicationContext.xml b/04fx/spring01/src/main/resources/applicationContext.xml new file mode 100644 index 00000000..83bc6eac --- /dev/null +++ b/04fx/spring01/src/main/resources/applicationContext.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/04fx/spring01/src/main/resources/log4j.xml b/04fx/spring01/src/main/resources/log4j.xml new file mode 100644 index 00000000..efc1f4f6 --- /dev/null +++ b/04fx/spring01/src/main/resources/log4j.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/04fx/spring01/src/main/resources/springjms-receiver.xml b/04fx/spring01/src/main/resources/springjms-receiver.xml new file mode 100644 index 00000000..e0d9fbc6 --- /dev/null +++ b/04fx/spring01/src/main/resources/springjms-receiver.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/04fx/spring01/src/main/resources/springjms-sender.xml b/04fx/spring01/src/main/resources/springjms-sender.xml new file mode 100644 index 00000000..119ec07a --- /dev/null +++ b/04fx/spring01/src/main/resources/springjms-sender.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/04fx/spring01/src/test/java/Spring02Test.java b/04fx/spring01/src/test/java/Spring02Test.java new file mode 100644 index 00000000..2cacae2e --- /dev/null +++ b/04fx/spring01/src/test/java/Spring02Test.java @@ -0,0 +1,18 @@ +import io.kimmking.spring02.Klass; +import org.junit.Assert; +import org.junit.Test; +import static org.mockito.Mockito.*; + +public class Spring02Test { + + private Klass class1; + + @Test + public void KlassTest(){ + class1 = mock(Klass.class, RETURNS_DEEP_STUBS); + when(class1.getStudents().size()).thenReturn(2); + Assert.assertEquals(2, class1.getStudents().size()); + } + + // 单元测试 +} diff --git a/04fx/spring01/src/test/java/Sprint01Test.java b/04fx/spring01/src/test/java/Sprint01Test.java new file mode 100644 index 00000000..cbb69b90 --- /dev/null +++ b/04fx/spring01/src/test/java/Sprint01Test.java @@ -0,0 +1,22 @@ +import io.kimmking.spring02.Klass; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = {"classpath:applicationContext.xml"}) +public class Sprint01Test { + + @Autowired + private Klass class1; + + @Test + public void KlassTest(){ + Assert.assertEquals(2, class1.getStudents().size()); + } + + // 集成测试 +} diff --git a/04fx/springboot01/pom.xml b/04fx/springboot01/pom.xml new file mode 100644 index 00000000..93d83574 --- /dev/null +++ b/04fx/springboot01/pom.xml @@ -0,0 +1,70 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.0.9.RELEASE + + + io.kimmking + springboot01 + 0.0.1-SNAPSHOT + springboot01 + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-activemq + + + org.springframework.boot + spring-boot-starter-web + + + org.apache.activemq + activemq-pool + 5.15.0 + + + + + org.springframework.boot + spring-boot-starter-data-mongodb + + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/04fx/springboot01/src/main/java/io/kimmking/springboot01/Springboot01Application.java b/04fx/springboot01/src/main/java/io/kimmking/springboot01/Springboot01Application.java new file mode 100644 index 00000000..0cfd0cb6 --- /dev/null +++ b/04fx/springboot01/src/main/java/io/kimmking/springboot01/Springboot01Application.java @@ -0,0 +1,18 @@ +package io.kimmking.springboot01; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; +import org.springframework.jms.annotation.EnableJms; + +@SpringBootApplication +//@EnableJms //启动消息队列 +//@EnableMongoRepositories +public class Springboot01Application { + + public static void main(String[] args) { + SpringApplication.run(Springboot01Application.class, args); + } + +} diff --git a/04fx/springboot01/src/main/java/io/kimmking/springboot01/jms/BeanConfig.java b/04fx/springboot01/src/main/java/io/kimmking/springboot01/jms/BeanConfig.java new file mode 100644 index 00000000..fd19a424 --- /dev/null +++ b/04fx/springboot01/src/main/java/io/kimmking/springboot01/jms/BeanConfig.java @@ -0,0 +1,73 @@ +package io.kimmking.springboot01.jms; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.command.ActiveMQQueue; +import org.apache.activemq.command.ActiveMQTopic; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jms.config.JmsListenerContainerFactory; +import org.springframework.jms.config.SimpleJmsListenerContainerFactory; +import org.springframework.jms.core.JmsMessagingTemplate; + +import javax.jms.ConnectionFactory; +import javax.jms.Queue; +import javax.jms.Topic; + +@Configuration +public class BeanConfig { + + @Value("${spring.activemq.broker-url}") + private String brokerUrl; + + @Value("${spring.activemq.user}") + private String username; + + @Value("${spring.activemq.topic-name}") + private String password; + + @Value("${spring.activemq.queue-name}") + private String queueName; + + @Value("${spring.activemq.topic-name}") + private String topicName; + + @Bean(name = "queue") + public Queue queue() { + return new ActiveMQQueue(queueName); + } + + @Bean(name = "topic") + public Topic topic() { + return new ActiveMQTopic(topicName); + } + + @Bean + public ConnectionFactory connectionFactory() { + return new ActiveMQConnectionFactory(username, password, brokerUrl); + } + + @Bean + public JmsMessagingTemplate jmsMessageTemplate() { + return new JmsMessagingTemplate(connectionFactory()); + } + + // 在Queue模式中,对消息的监听需要对containerFactory进行配置 + @Bean("queueListener") + public JmsListenerContainerFactory queueJmsListenerContainerFactory(ConnectionFactory connectionFactory) { + SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory(); + factory.setConnectionFactory(connectionFactory); + factory.setPubSubDomain(false); + return factory; + } + + //在Topic模式中,对消息的监听需要对containerFactory进行配置 + @Bean("topicListener") + public JmsListenerContainerFactory topicJmsListenerContainerFactory(ConnectionFactory connectionFactory) { + SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory(); + factory.setConnectionFactory(connectionFactory); + factory.setPubSubDomain(true); + return factory; + } +} + \ No newline at end of file diff --git a/04fx/springboot01/src/main/java/io/kimmking/springboot01/jms/ProducerController.java b/04fx/springboot01/src/main/java/io/kimmking/springboot01/jms/ProducerController.java new file mode 100644 index 00000000..92894be3 --- /dev/null +++ b/04fx/springboot01/src/main/java/io/kimmking/springboot01/jms/ProducerController.java @@ -0,0 +1,44 @@ +package io.kimmking.springboot01.jms; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jms.core.JmsMessagingTemplate; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; +import javax.jms.Destination; +import javax.jms.Queue; +import javax.jms.Topic; + +@RestController +public class ProducerController +{ + @Autowired + private JmsMessagingTemplate jmsMessagingTemplate; + + @Autowired + private Queue queue; + + @Autowired + private Topic topic; + + + // curl http://localhost:8080/queue/test -X POST -d "testququuquqq" + @PostMapping("/queue/test") + public String sendQueue(@RequestBody String str) { + this.sendMessage(this.queue, str); + return "success"; + } + + + //curl http://localhost:8080/topic/test -X POST -d "testtopiccccc" + @PostMapping("/topic/test") + public String sendTopic(@RequestBody String str) { + this.sendMessage(this.topic, str); + return "success"; + } + + // 发送消息,destination是发送到的队列,message是待发送的消息 + private void sendMessage(Destination destination, final String message){ + jmsMessagingTemplate.convertAndSend(destination, message); + } +} diff --git a/04fx/springboot01/src/main/java/io/kimmking/springboot01/jms/QueueConsumerListener.java b/04fx/springboot01/src/main/java/io/kimmking/springboot01/jms/QueueConsumerListener.java new file mode 100644 index 00000000..e1697fc0 --- /dev/null +++ b/04fx/springboot01/src/main/java/io/kimmking/springboot01/jms/QueueConsumerListener.java @@ -0,0 +1,13 @@ +package io.kimmking.springboot01.jms; + +import org.springframework.jms.annotation.JmsListener; +import org.springframework.stereotype.Component; + +@Component +public class QueueConsumerListener { + //queue模式的消费者 + @JmsListener(destination = "${spring.activemq.queue-name}", containerFactory = "queueListener") + public void readActiveQueue(String message) { + System.out.println("queue接受到:" + message); + } +} \ No newline at end of file diff --git a/04fx/springboot01/src/main/java/io/kimmking/springboot01/jms/TopicConsumerListener.java b/04fx/springboot01/src/main/java/io/kimmking/springboot01/jms/TopicConsumerListener.java new file mode 100644 index 00000000..feaf2e82 --- /dev/null +++ b/04fx/springboot01/src/main/java/io/kimmking/springboot01/jms/TopicConsumerListener.java @@ -0,0 +1,13 @@ +package io.kimmking.springboot01.jms; + +import org.springframework.jms.annotation.JmsListener; +import org.springframework.stereotype.Component; + +@Component +public class TopicConsumerListener { + //topic模式的消费者 + @JmsListener(destination = "${spring.activemq.topic-name}", containerFactory = "topicListener") + public void readActiveQueue(String message) { + System.out.println("topic接受到:" + message); + } +} \ No newline at end of file diff --git a/04fx/springboot01/src/main/java/io/kimmking/springboot01/mongo/MongoController.java b/04fx/springboot01/src/main/java/io/kimmking/springboot01/mongo/MongoController.java new file mode 100644 index 00000000..3cc93c0a --- /dev/null +++ b/04fx/springboot01/src/main/java/io/kimmking/springboot01/mongo/MongoController.java @@ -0,0 +1,46 @@ +package io.kimmking.springboot01.mongo; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import java.util.List; + +@Controller +@EnableAutoConfiguration +public class MongoController { + + @Autowired + MongoTemplate mongoTemplate; + + @RequestMapping("/mongo/list") + @ResponseBody + List mongo() { + //Query query = new Query(); + //query.addCriteria(Criteria.where("name").is("kk")); + //String name = mongotemplate.findOne(query, User.class).getName(); + return mongoTemplate.findAll(User.class); + } + + @RequestMapping("/mongo/test") + @ResponseBody + String test() { + //Query query = new Query(); + //query.addCriteria(Criteria.where("name").is("kk")); + //String name = mongotemplate.findOne(query, User.class).getName(); + User user = new User(); + user.setId(new Long(System.currentTimeMillis()).toString()); + user.setName("KK" + System.currentTimeMillis() % 1000); + user.setAge("33"); + mongoTemplate.insert(user); + return "test ok"; + } + + +} \ No newline at end of file diff --git a/04fx/springboot01/src/main/java/io/kimmking/springboot01/mongo/User.java b/04fx/springboot01/src/main/java/io/kimmking/springboot01/mongo/User.java new file mode 100644 index 00000000..23c71985 --- /dev/null +++ b/04fx/springboot01/src/main/java/io/kimmking/springboot01/mongo/User.java @@ -0,0 +1,85 @@ +package io.kimmking.springboot01.mongo; + +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; +import org.springframework.data.mongodb.core.mapping.Field; + +@Document(collection = "user") +public class User { + + @Id + private String id; + @Field("username") + private String username; + private String password; + private String registerTime; + private String phone; + private String name; + private String sex; + private String age; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getRegisterTime() { + return registerTime; + } + + public void setRegisterTime(String registerTime) { + this.registerTime = registerTime; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public String getAge() { + return age; + } + + public void setAge(String age) { + this.age = age; + } + +} \ No newline at end of file diff --git a/04fx/springboot01/src/main/resources/application.yml b/04fx/springboot01/src/main/resources/application.yml new file mode 100644 index 00000000..048ab343 --- /dev/null +++ b/04fx/springboot01/src/main/resources/application.yml @@ -0,0 +1,59 @@ +server: + port: 8080 + +spring: + activemq: + broker-url: tcp://127.0.0.1:61616 + user: admin + password: admin + close-timeout: 15s # 在考虑结束之前等待的时间 + in-memory: true # 默认代理URL是否应该在内存中。如果指定了显式代理,则忽略此值。 + non-blocking-redelivery: false # 是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。 + send-timeout: 0 # 等待消息发送响应的时间。设置为0等待永远。 + queue-name: active.queue + topic-name: active.topic.name.model + packages: + trust-all: true #不配置此项,会报错 + pool: + enabled: true + max-connections: 10 #连接池最大连接数 + idle-timeout: 30000 #空闲的连接过期时间,默认为30秒 + + + + data: + mongodb: + uri: mongodb://localhost:27017/mydb + + profiles: + active: true + +# jms: +# pub-sub-domain: true #默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置 + +# 是否信任所有包 +#spring.activemq.packages.trust-all= +# 要信任的特定包的逗号分隔列表(当不信任所有包时) +#spring.activemq.packages.trusted= +# 当连接请求和池满时是否阻塞。设置false会抛“JMSException异常”。 +#spring.activemq.pool.block-if-full=true +# 如果池仍然满,则在抛出异常前阻塞时间。 +#spring.activemq.pool.block-if-full-timeout=-1ms +# 是否在启动时创建连接。可以在启动时用于加热池。 +#spring.activemq.pool.create-connection-on-startup=true +# 是否用Pooledconnectionfactory代替普通的ConnectionFactory。 +#spring.activemq.pool.enabled=false +# 连接过期超时。 +#spring.activemq.pool.expiry-timeout=0ms +# 连接空闲超时 +#spring.activemq.pool.idle-timeout=30s +# 连接池最大连接数 +#spring.activemq.pool.max-connections=1 +# 每个连接的有效会话的最大数目。 +#spring.activemq.pool.maximum-active-session-per-connection=500 +# 当有"JMSException"时尝试重新连接 +#spring.activemq.pool.reconnect-on-exception=true +# 在空闲连接清除线程之间运行的时间。当为负数时,没有空闲连接驱逐线程运行。 +#spring.activemq.pool.time-between-expiration-check=-1ms +# 是否只使用一个MessageProducer +#spring.activemq.pool.use-anonymous-producers=true \ No newline at end of file diff --git a/04fx/springboot01/src/test/java/io/kimmking/springboot01/Springboot01ApplicationTests.java b/04fx/springboot01/src/test/java/io/kimmking/springboot01/Springboot01ApplicationTests.java new file mode 100644 index 00000000..a1fdf515 --- /dev/null +++ b/04fx/springboot01/src/test/java/io/kimmking/springboot01/Springboot01ApplicationTests.java @@ -0,0 +1,13 @@ +package io.kimmking.springboot01; + +import org.junit.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class Springboot01ApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/06db/shardingsphere/config-replica-query.yaml b/06db/shardingsphere/config-replica-query.yaml new file mode 100644 index 00000000..d060ab5e --- /dev/null +++ b/06db/shardingsphere/config-replica-query.yaml @@ -0,0 +1,30 @@ + +schemaName: replica_query_db + +dataSourceCommon: + username: root + password: + connectionTimeoutMilliseconds: 30000 + idleTimeoutMilliseconds: 60000 + maxLifetimeMilliseconds: 1800000 + maxPoolSize: 10 + minPoolSize: 1 + maintenanceIntervalMilliseconds: 30000 + +dataSources: + primary_ds: + url: jdbc:mysql://127.0.0.1:3306/demo_master?serverTimezone=UTC&useSSL=false + replica_ds_0: + url: jdbc:mysql://127.0.0.1:3306/demo_slave_0?serverTimezone=UTC&useSSL=false + replica_ds_1: + url: jdbc:mysql://127.0.0.1:3306/demo_slave_1?serverTimezone=UTC&useSSL=false + +rules: +- !REPLICA_QUERY + dataSources: + pr_ds: + name: pr_ds + primaryDataSourceName: primary_ds + replicaDataSourceNames: + - replica_ds_0 + - replica_ds_1 diff --git a/06db/shardingsphere/config-sharding.yaml b/06db/shardingsphere/config-sharding.yaml new file mode 100644 index 00000000..ffbc1546 --- /dev/null +++ b/06db/shardingsphere/config-sharding.yaml @@ -0,0 +1,68 @@ + +schemaName: sharding_db + +dataSourceCommon: + username: root + password: + connectionTimeoutMilliseconds: 30000 + idleTimeoutMilliseconds: 60000 + maxLifetimeMilliseconds: 1800000 + maxPoolSize: 5 + minPoolSize: 1 + maintenanceIntervalMilliseconds: 30000 + +dataSources: + ds_0: + url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false + ds_1: + url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false + +rules: +- !SHARDING + tables: + t_order: + actualDataNodes: ds_${0..1}.t_order_${0..1} + tableStrategy: + standard: + shardingColumn: order_id + shardingAlgorithmName: t_order_inline + keyGenerateStrategy: + column: order_id + keyGeneratorName: snowflake + t_order_item: + actualDataNodes: ds_${0..1}.t_order_item_${0..1} + tableStrategy: + standard: + shardingColumn: order_id + shardingAlgorithmName: t_order_item_inline + keyGenerateStrategy: + column: order_item_id + keyGeneratorName: snowflake + bindingTables: + - t_order,t_order_item + defaultDatabaseStrategy: + standard: + shardingColumn: user_id + shardingAlgorithmName: database_inline + defaultTableStrategy: + none: + + shardingAlgorithms: + database_inline: + type: INLINE + props: + algorithm-expression: ds_${user_id % 2} + t_order_inline: + type: INLINE + props: + algorithm-expression: t_order_${order_id % 2} + t_order_item_inline: + type: INLINE + props: + algorithm-expression: t_order_item_${order_id % 2} + + keyGenerators: + snowflake: + type: SNOWFLAKE + props: + worker-id: 123 diff --git a/06db/shardingsphere/init.sql b/06db/shardingsphere/init.sql new file mode 100644 index 00000000..9b930268 --- /dev/null +++ b/06db/shardingsphere/init.sql @@ -0,0 +1,37 @@ + +## 读写分离 + +create schema demo_master; +create schema demo_slave_0; +create schema demo_slave_1; + +create table demo_master.users(id bigint, name varchar(8), comment varchar(16)); +create table demo_slave_0.users(id bigint, name varchar(8), comment varchar(16)); +create table demo_slave_1.users(id bigint, name varchar(8), comment varchar(16)); + +insert into demo_master.users values(1,'KK01','master'),(2,'KK02','master'),(3,'KK03','master'); +insert into demo_slave_0.users values(1,'KK01','slave0'),(2,'KK02','slave0'),(3,'KK03','slave0'); +insert into demo_slave_1.users values(1,'KK01','slave1'),(2,'KK02','slave1'),(3,'KK03','slave1'); + + +## 分库分表 + +create schema demo_ds_0; +create schema demo_ds_1; + +CREATE TABLE IF NOT EXISTS demo_ds_0.t_order_0 (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id)); +CREATE TABLE IF NOT EXISTS demo_ds_0.t_order_1 (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id)); +CREATE TABLE IF NOT EXISTS demo_ds_1.t_order_0 (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id)); +CREATE TABLE IF NOT EXISTS demo_ds_1.t_order_1 (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id)); + +CREATE TABLE IF NOT EXISTS demo_ds_0.t_order_item_0 (order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id BIGINT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_item_id)); +CREATE TABLE IF NOT EXISTS demo_ds_0.t_order_item_1 (order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id BIGINT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_item_id)); +CREATE TABLE IF NOT EXISTS demo_ds_1.t_order_item_0 (order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id BIGINT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_item_id)); +CREATE TABLE IF NOT EXISTS demo_ds_1.t_order_item_1 (order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id BIGINT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_item_id)); + + + + + +# CREATE TABLE t_order (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id)); +# CREATE TABLE t_order_item (order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id BIGINT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_item_id)); diff --git a/06db/shardingsphere/server.yaml b/06db/shardingsphere/server.yaml new file mode 100644 index 00000000..87df2ef0 --- /dev/null +++ b/06db/shardingsphere/server.yaml @@ -0,0 +1,35 @@ + +# governance: +# name: governance_ds +# registryCenter: +# type: ZooKeeper +# serverLists: localhost:2181 +# props: +# retryIntervalMilliseconds: 500 +# timeToLiveSeconds: 60 +# maxRetries: 3 +# operationTimeoutMilliseconds: 500 +# overwrite: true + +authentication: + users: + root: + password: root +# sharding: +# password: sharding +# authorizedSchemas: sharding_db + +props: + max-connections-size-per-query: 1 + acceptor-size: 16 # The default value is available processors count * 2. + executor-size: 16 # Infinite by default. + proxy-frontend-flush-threshold: 128 # The default value is 128. + # LOCAL: Proxy will run with LOCAL transaction. + # XA: Proxy will run with XA transaction. + # BASE: Proxy will run with B.A.S.E transaction. + proxy-transaction-type: LOCAL + proxy-opentracing-enabled: false + proxy-hint-enabled: false + query-with-cipher-column: false + sql-show: true + check-table-metadata-enabled: false diff --git a/07rpc/README.md b/07rpc/README.md new file mode 100644 index 00000000..746e228e --- /dev/null +++ b/07rpc/README.md @@ -0,0 +1,77 @@ +# 第7周作业 + + +## 作业内容 + +> Week07 作业题目: + +###Week07 作业题目 + +1. (选做)用今天课上学习的知识,分析自己系统的 SQL 和表结构 +2. (必做)按自己设计的表结构,插入 100 万订单模拟数据,测试不同方式的插入效率 +3. (选做)按自己设计的表结构,插入 1000 万订单模拟数据,测试不同方式的插入效率 +4. (选做)使用不同的索引或组合,测试不同方式查询效率 +5. (选做)调整测试数据,使得数据尽量均匀,模拟 1 年时间内的交易,计算一年的销售报表:销售总额,订单数,客单价,每月销售量,前十的商品等等(可以自己设计更多指标) +6. (选做)尝试自己做一个 ID 生成器(可以模拟 Seq 或 Snowflake) +7. (选做)尝试实现或改造一个非精确分页的程序 +8. (选做)配置一遍异步复制,半同步复制、组复制 +9. (必做)读写分离 - 动态切换数据源版本 1.0 +10. (必做)读写分离 - 数据库框架版本 2.0 +11. (选做)读写分离 - 数据库中间件版本 3.0 +12. (选做)配置 MHA,模拟 master 宕机 +13. (选做)配置 MGR,模拟 master 宕机 +14. (选做)配置 Orchestrator,模拟 master 宕机,演练 UI 调整拓扑结构 + +### 作业提交规范: + +1. 作业不要打包 ; +2. 同学们写在 md 文件里,而不要发 Word, Excel , PDF 等 ; +3. 代码类作业需提交完整 Java 代码,不能是片段; +4. 作业按课时分目录,仅上传作业相关,笔记分开记录; +5. 画图类作业提交可直接打开的图片或 md,手画的图手机拍照上传后太大,难以查看,推荐画图(推荐 PPT、Keynote); +6. 提交记录最好要标明明确的含义(比如第几题作业)。 + + + +## 操作步骤 + + +### 第七周-作业1. (选做) + +> 本题目需要同学们自己完成. + +1. 找一个业务系统。 +2. 分析SQL表结构 +3. 范式 +4. 考虑: 开发效率、查询效率、拓展性、索引、有哪些优化空间。 +5. 其他 + + +### 第七周-作业2. (必做) + +0. 作业题目: 按自己设计的表结构,插入 100 万订单模拟数据,测试不同方式的插入效率 +1. 打开 Spring 官网: https://spring.io/ +2. 找到 Projects --> Spring Initializr: https://start.spring.io/ +3. 填写项目信息: + * Project: Maven Project + * Language: Java + * Spring Boot版本: 2.5.4 + * Group: 自己的包名, 以做标识; + * Artifact: mysql-demo + * JDK版本: 8 + * Dependencies: 添加依赖, 比如 MyBatis, Spring Web, MySQL, JDBC + * 生成 maven 项目; 下载并解压。 参考: [mysql-demo项目Share信息](https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.5.4&packaging=jar&jvmVersion=1.8&groupId=com.cncounter&artifactId=mysql-demo&name=mysql-demo&description=MySQL%20Demo&packageName=com.cncounter.mysql-demo&dependencies=mybatis,web,mysql,data-jdbc) + +4. Idea或者Eclipse从已有的Source导入Maven项目。 +5. 搜索依赖, 推荐 mvnrepository: https://mvnrepository.com/ +6. 搜索 fastjson , 然后在 pom.xml 之中增加对应的依赖。 +7. 准备MySQL环境, 创建订单表 + - 7.1 +8. 生成项目; + - 8.1 创建Controller +9. 执行与测试. + +参考: [mysql-demo/README.md](./mysql-demo/README.md) + + +https://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/ diff --git a/07rpc/mysql-demo/.gitignore b/07rpc/mysql-demo/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/07rpc/mysql-demo/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/07rpc/mysql-demo/README.md b/07rpc/mysql-demo/README.md new file mode 100644 index 00000000..fad579e2 --- /dev/null +++ b/07rpc/mysql-demo/README.md @@ -0,0 +1,208 @@ +## 说明 + +第七周-作业2. (必做): 按自己设计的表结构,插入 100 万订单模拟数据,测试不同方式的插入效率 + +## 操作步骤 + + +### 第七周-作业2. (必做) + + +#### 1. 创建基本项目 + +1. 打开 Spring 官网: https://spring.io/ +2. 找到 Projects --> Spring Initializr: https://start.spring.io/ +3. 填写项目信息: + * Project: Maven Project + * Language: Java + * Spring Boot版本: 2.5.4 + * Group: 自己的包名, 以做标识; + * Artifact: mysql-demo + * JDK版本: 8 + * Dependencies: 添加依赖, 比如 MyBatis, Spring Web, MySQL, JDBC + * 生成 maven 项目; 下载并解压。 参考: [mysql-demo项目Share信息](https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.5.4&packaging=jar&jvmVersion=1.8&groupId=com.cncounter&artifactId=mysql-demo&name=mysql-demo&description=MySQL%20Demo&packageName=com.cncounter.mysql-demo&dependencies=mybatis,web,mysql,data-jdbc) + +4. Idea或者Eclipse从已有的Source导入Maven项目。 + + +#### 2. 准备mysql环境 + +mysql服务器, 可以使用这些方式: + +- 物理机安装 +- 虚拟机安装 +- 购买云服务 +- 使用已有MySQL +- 使用Docker +- 使用docker-compose: 参考配置文件: [docker-compose.yml](./docker-compose.yml) + +这里使用 docker-compose 方式。 + +根据配置信息, 我们需要创建好以下2个目录: + +- `./docker_data/docker-entrypoint-initdb.d` ; MySQL镜像启动时会自动执行下面的SQL. +- `./docker_data/var/lib/mysql` ; 注意这个目录下不能有任何文件, 否则启动报错。 + +安装好 docker 之后, 使用 `docker-compose up -d` 命令来启动。 + +这个命令会自动查找当前目录下的 [docker-compose.yml](./docker-compose.yml) 配置文件。 + +如果启动失败, 可以在当前目录下, 使用 `docker-compose logs -f` 命令查看日志信息。 + +可以使用命令来查看当前物理机监听的端口号; + + +```shell +# mac +lsof -iTCP -sTCP:LISTEN -n -P + +# linux +netstat -ntlp + +``` + +启动成功后, 应该能看到我们配置的 `13306` 端口信息. + +然后使用MySQL客户端连接即可, 我们在配置文件里面指定了ROOT用户的密码为 `123456`; + + +#### 3. 项目基础配置 + +如果直接启动下载下来的 MysqlDemoApplication, 报错信息大致如下: + +```text +*************************** +APPLICATION FAILED TO START +*************************** + +Description: + +Failed to configure a DataSource: + 'url' attribute is not specified + and no embedded datasource could be configured. + +Reason: Failed to determine a suitable driver class +``` + +可以看到这里提示 缺少了DataSource的url等配置信息 + +我们增加一个配置文件: [src/main/resources/application.yml](./src/main/resources/application.yml) + +文件内容参考: + +```yaml +# Spring相关配置 +spring: + datasource: + type: com.zaxxer.hikari.HikariDataSource + hikari: + minimum-idle: 1 + maximum-pool-size: 5 + auto-commit: true + idle-timeout: 30000 + pool-name: MySQLHikariCP1 + max-lifetime: 1800000 + connection-timeout: 30000 + connection-test-query: SELECT 1 + driverClassName: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:13306/mysql_demo?useUnicode=true&characterEncoding=utf-8&useSSL=false + username: root + password: 123456 + +``` + +然后再增加 logback的日志级别: + +```yaml +# Logback日志级别配置 +logging.level.root: info +logging.level.com.cncounter: debug +logging.level.org.springframework.test.web.servlet.result: debug + +``` + +以及 Tomcat 配置: + +```yaml +# Tomcat 配置 +server: + port: 8080 + tomcat: + uri-encoding: UTF-8 + max-threads: 1000 + min-spare-threads: 10 + servlet: + context-path: / + encoding: + enabled: true + force: true + charset: UTF-8 +``` + + +然后重新启动 MysqlDemoApplication. + +启动成功之后, 可以查看本机的端口号, 也可以访问: [http://localhost:8080/](http://localhost:8080/) + + +提示: 可以用 jvisualvm 来观察启动好的Java进程。 + + +#### 4. 数据库脚本 + +可以使用前面课程的作业中提交的数据库表。 + +也可以参考: [docker_data/docker-entrypoint-initdb.d/01-mysql_demo-init.ddl.sql](./docker_data/docker-entrypoint-initdb.d/01-mysql_demo-init.ddl.sql) + + +#### 5. MyBatis生成器与配置 + +官方文档: [MyBatis Generator Doc](https://mybatis.org/generator/configreference/xmlconfig.html) + +配置文件: [src/main/resources/MybatisGeneratorConfig.xml](./src/main/resources/MybatisGeneratorConfig.xml) + +在 [pom.xml](./pom.xml) 文件中增加 `mybatis-generator-maven-plugin` 插件. + +执行生成(也可以使用 [generateMapper.sh](./generateMapper.sh) 脚本文件): + +```shell +mvn mybatis-generator:generate -X -e +``` + +可以看到生成了以下文件: + +- [`src/main/resources/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.xml`](./src/main/resources/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.xml) +- [`src/main/java/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.java`](./src/main/java/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.java) +- [`src/main/java/com/cncounter/mysqldemo/model/TBizOrder.java`](./src/main/java/com/cncounter/mysqldemo/model/TBizOrder.java) + +#### MyBatis 扫描配置 + +要使用MyBatis, 首先要配置数据源. 当然, 在前面的步骤中我们已经配置好了. + +文件内容参考: [src/main/resources/application.yml](./src/main/resources/application.yml) + +然后, 我们在配置文件中增加 MyBatis 相关的配置 + + + +#### WebMVC + + + + + + +#### 请求链路分析 + + + + + + +#### MySQL服务器参数调整 + + + + + +#### 应用程序优化 \ No newline at end of file diff --git a/07rpc/mysql-demo/docker-compose.yml b/07rpc/mysql-demo/docker-compose.yml new file mode 100644 index 00000000..90c90acf --- /dev/null +++ b/07rpc/mysql-demo/docker-compose.yml @@ -0,0 +1,14 @@ +version: '3.1' + +services: + + mysql: + image: mysql:5.7 + command: --default-authentication-plugin=mysql_native_password + ports: + - 13306:3306 + environment: + MYSQL_ROOT_PASSWORD: 123456 + volumes: + - ./docker_data/docker-entrypoint-initdb.d/:/docker-entrypoint-initdb.d + - ./docker_data/var/lib/mysql/:/var/lib/mysql diff --git a/07rpc/mysql-demo/docker_data/docker-entrypoint-initdb.d/01-mysql_demo-init.ddl.sql b/07rpc/mysql-demo/docker_data/docker-entrypoint-initdb.d/01-mysql_demo-init.ddl.sql new file mode 100644 index 00000000..fff3f7ed --- /dev/null +++ b/07rpc/mysql-demo/docker_data/docker-entrypoint-initdb.d/01-mysql_demo-init.ddl.sql @@ -0,0 +1,16 @@ +CREATE DATABASE IF NOT EXISTS `mysql_demo`; + +USE `mysql_demo`; + +-- 业务订单表 +CREATE TABLE `t_biz_order` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增主键;订单id', + `user_id` bigint(20) NOT NULL COMMENT '用户userId', + `state` int(8) NOT NULL DEFAULT '0' COMMENT '订单状态;参考OrderStateEnum', + `create_time` bigint(20) NOT NULL COMMENT '下单时间', + `update_time` bigint(20) NOT NULL COMMENT '更新时间', + PRIMARY KEY (`id`), + KEY `idx_user_id` (`user_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单信息表'; + +-- 订单-商品信息表; diff --git a/07rpc/mysql-demo/generateMapper.sh b/07rpc/mysql-demo/generateMapper.sh new file mode 100755 index 00000000..7f5ae537 --- /dev/null +++ b/07rpc/mysql-demo/generateMapper.sh @@ -0,0 +1,6 @@ + +echo mvn clean -U -DskipTests +mvn clean -U -DskipTests + +echo mvn mybatis-generator:generate -X -e +mvn mybatis-generator:generate -X -e diff --git a/07rpc/mysql-demo/pom.xml b/07rpc/mysql-demo/pom.xml new file mode 100644 index 00000000..bf7ac49e --- /dev/null +++ b/07rpc/mysql-demo/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.5.4 + + + com.cncounter + mysql-demo + 0.0.1-SNAPSHOT + mysql-demo + MySQL Demo + + 1.8 + + + + org.springframework.boot + spring-boot-starter-data-jdbc + + + org.springframework.boot + spring-boot-starter-web + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.0 + + + + + com.alibaba + fastjson + 1.2.78 + + + + mysql + mysql-connector-java + 8.0.26 + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.mybatis.generator + mybatis-generator-maven-plugin + 1.3.6 + + ${basedir}/src/main/resources/MybatisGeneratorConfig.xml + true + true + + + + + mysql + mysql-connector-java + 8.0.26 + + + + + + + diff --git a/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/MysqlDemoApplication.java b/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/MysqlDemoApplication.java new file mode 100644 index 00000000..e839acba --- /dev/null +++ b/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/MysqlDemoApplication.java @@ -0,0 +1,13 @@ +package com.cncounter.mysqldemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class MysqlDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(MysqlDemoApplication.class, args); + } + +} diff --git a/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.java b/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.java new file mode 100644 index 00000000..623c5c16 --- /dev/null +++ b/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.java @@ -0,0 +1,53 @@ +package com.cncounter.mysqldemo.dao.mapper; + +import com.cncounter.mysqldemo.model.TBizOrder; + +public interface TBizOrderMapper { + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table t_biz_order + * + * @mbg.generated + */ + int deleteByPrimaryKey(Long id); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table t_biz_order + * + * @mbg.generated + */ + int insert(TBizOrder record); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table t_biz_order + * + * @mbg.generated + */ + int insertSelective(TBizOrder record); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table t_biz_order + * + * @mbg.generated + */ + TBizOrder selectByPrimaryKey(Long id); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table t_biz_order + * + * @mbg.generated + */ + int updateByPrimaryKeySelective(TBizOrder record); + + /** + * This method was generated by MyBatis Generator. + * This method corresponds to the database table t_biz_order + * + * @mbg.generated + */ + int updateByPrimaryKey(TBizOrder record); +} \ No newline at end of file diff --git a/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/model/TBizOrder.java b/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/model/TBizOrder.java new file mode 100644 index 00000000..0592cd94 --- /dev/null +++ b/07rpc/mysql-demo/src/main/java/com/cncounter/mysqldemo/model/TBizOrder.java @@ -0,0 +1,187 @@ +package com.cncounter.mysqldemo.model; + +/** + * Database Table Remarks: + * 订单信息表 + * + * This class was generated by MyBatis Generator. + * This class corresponds to the database table t_biz_order + * + * @mbg.generated do_not_delete_during_merge + */ +public class TBizOrder { + /** + * Database Column Remarks: + * 自增主键;订单id + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column t_biz_order.id + * + * @mbg.generated + */ + private Long id; + + /** + * Database Column Remarks: + * 用户userId + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column t_biz_order.user_id + * + * @mbg.generated + */ + private Long userId; + + /** + * Database Column Remarks: + * 订单状态;参考OrderStateEnum + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column t_biz_order.state + * + * @mbg.generated + */ + private Integer state; + + /** + * Database Column Remarks: + * 下单时间 + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column t_biz_order.create_time + * + * @mbg.generated + */ + private Long createTime; + + /** + * Database Column Remarks: + * 更新时间 + * + * This field was generated by MyBatis Generator. + * This field corresponds to the database column t_biz_order.update_time + * + * @mbg.generated + */ + private Long updateTime; + + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column t_biz_order.id + * + * @return the value of t_biz_order.id + * + * @mbg.generated + */ + public Long getId() { + return id; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column t_biz_order.id + * + * @param id the value for t_biz_order.id + * + * @mbg.generated + */ + public void setId(Long id) { + this.id = id; + } + + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column t_biz_order.user_id + * + * @return the value of t_biz_order.user_id + * + * @mbg.generated + */ + public Long getUserId() { + return userId; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column t_biz_order.user_id + * + * @param userId the value for t_biz_order.user_id + * + * @mbg.generated + */ + public void setUserId(Long userId) { + this.userId = userId; + } + + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column t_biz_order.state + * + * @return the value of t_biz_order.state + * + * @mbg.generated + */ + public Integer getState() { + return state; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column t_biz_order.state + * + * @param state the value for t_biz_order.state + * + * @mbg.generated + */ + public void setState(Integer state) { + this.state = state; + } + + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column t_biz_order.create_time + * + * @return the value of t_biz_order.create_time + * + * @mbg.generated + */ + public Long getCreateTime() { + return createTime; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column t_biz_order.create_time + * + * @param createTime the value for t_biz_order.create_time + * + * @mbg.generated + */ + public void setCreateTime(Long createTime) { + this.createTime = createTime; + } + + /** + * This method was generated by MyBatis Generator. + * This method returns the value of the database column t_biz_order.update_time + * + * @return the value of t_biz_order.update_time + * + * @mbg.generated + */ + public Long getUpdateTime() { + return updateTime; + } + + /** + * This method was generated by MyBatis Generator. + * This method sets the value of the database column t_biz_order.update_time + * + * @param updateTime the value for t_biz_order.update_time + * + * @mbg.generated + */ + public void setUpdateTime(Long updateTime) { + this.updateTime = updateTime; + } +} \ No newline at end of file diff --git a/07rpc/mysql-demo/src/main/resources/MybatisGeneratorConfig.xml b/07rpc/mysql-demo/src/main/resources/MybatisGeneratorConfig.xml new file mode 100644 index 00000000..54506445 --- /dev/null +++ b/07rpc/mysql-demo/src/main/resources/MybatisGeneratorConfig.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+
\ No newline at end of file diff --git a/07rpc/mysql-demo/src/main/resources/application.yml b/07rpc/mysql-demo/src/main/resources/application.yml new file mode 100644 index 00000000..967bfa95 --- /dev/null +++ b/07rpc/mysql-demo/src/main/resources/application.yml @@ -0,0 +1,50 @@ +# Tomcat 配置 +server: + port: 8080 + tomcat: + uri-encoding: UTF-8 + max-threads: 1000 + min-spare-threads: 10 + servlet: + context-path: / + encoding: + enabled: true + force: true + charset: UTF-8 + +# MyBatis 配置 +mybatis: + config-locations: classpath:mybatis-configuration.xml + mapper-locations: classpath:com/cncounter/mysqldemo/dao/mapper/*.xml + type-handlers-package: com.cncounter.mysqldemo.dao.mybatis.handlers.auto + configuration: + cache-enabled: false + lazy-loading-enabled: false + use-generated-keys: true + auto-mapping-behavior: full + default-executor-type: REUSE + default-statement-timeout: 20000 + +# Spring相关配置 +spring: + datasource: + type: com.zaxxer.hikari.HikariDataSource + hikari: + minimum-idle: 1 + maximum-pool-size: 5 + auto-commit: true + idle-timeout: 30000 + pool-name: MySQLHikariCP1 + max-lifetime: 1800000 + connection-timeout: 30000 + connection-test-query: SELECT 1 + driverClassName: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:13306/mysql_demo?useUnicode=true&characterEncoding=utf-8&useSSL=false + username: root + password: 123456 + +# Logback日志级别配置 +logging.level.root: info +logging.level.com.cncounter: debug +logging.level.org.springframework.test.web.servlet.result: debug + diff --git a/07rpc/mysql-demo/src/main/resources/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.xml b/07rpc/mysql-demo/src/main/resources/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.xml new file mode 100644 index 00000000..030c2b54 --- /dev/null +++ b/07rpc/mysql-demo/src/main/resources/com/cncounter/mysqldemo/dao/mapper/TBizOrderMapper.xml @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + id, user_id, state, create_time, update_time + + + + + delete from t_biz_order + where id = #{id,jdbcType=BIGINT} + + + + + SELECT LAST_INSERT_ID() + + insert into t_biz_order (user_id, state, create_time, + update_time) + values (#{userId,jdbcType=BIGINT}, #{state,jdbcType=TINYINT}, #{createTime,jdbcType=BIGINT}, + #{updateTime,jdbcType=BIGINT}) + + + + + SELECT LAST_INSERT_ID() + + insert into t_biz_order + + + user_id, + + + state, + + + create_time, + + + update_time, + + + + + #{userId,jdbcType=BIGINT}, + + + #{state,jdbcType=TINYINT}, + + + #{createTime,jdbcType=BIGINT}, + + + #{updateTime,jdbcType=BIGINT}, + + + + + + update t_biz_order + + + user_id = #{userId,jdbcType=BIGINT}, + + + state = #{state,jdbcType=TINYINT}, + + + create_time = #{createTime,jdbcType=BIGINT}, + + + update_time = #{updateTime,jdbcType=BIGINT}, + + + where id = #{id,jdbcType=BIGINT} + + + + update t_biz_order + set user_id = #{userId,jdbcType=BIGINT}, + state = #{state,jdbcType=TINYINT}, + create_time = #{createTime,jdbcType=BIGINT}, + update_time = #{updateTime,jdbcType=BIGINT} + where id = #{id,jdbcType=BIGINT} + + \ No newline at end of file diff --git a/07rpc/mysql-demo/src/test/java/com/cncounter/mysqldemo/MysqlDemoApplicationTests.java b/07rpc/mysql-demo/src/test/java/com/cncounter/mysqldemo/MysqlDemoApplicationTests.java new file mode 100644 index 00000000..0403040d --- /dev/null +++ b/07rpc/mysql-demo/src/test/java/com/cncounter/mysqldemo/MysqlDemoApplicationTests.java @@ -0,0 +1,13 @@ +package com.cncounter.mysqldemo; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class MysqlDemoApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/07rpc/rpc01/client-rest.http b/07rpc/rpc01/client-rest.http new file mode 100644 index 00000000..cfe742ec --- /dev/null +++ b/07rpc/rpc01/client-rest.http @@ -0,0 +1 @@ +http://127.0.0.1:8091/api/hello \ No newline at end of file diff --git a/07rpc/rpc01/pom.xml b/07rpc/rpc01/pom.xml new file mode 100644 index 00000000..8c924dcf --- /dev/null +++ b/07rpc/rpc01/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.7.3 + + + io.kimmking + rpcfx + 0.0.1-SNAPSHOT + rpcfx + pom + RPC demo project for Spring Boot + + + rpcfx-core + rpcfx-demo-api + rpcfx-demo-consumer + rpcfx-demo-provider + + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + diff --git a/07rpc/rpc01/rpcfx-core/pom.xml b/07rpc/rpc01/rpcfx-core/pom.xml new file mode 100644 index 00000000..5a1eeac3 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/pom.xml @@ -0,0 +1,76 @@ + + + 4.0.0 + + io.kimmking + rpcfx + 0.0.1-SNAPSHOT + + + io.kimmking + rpcfx-core + 0.0.1-SNAPSHOT + rpcfx-core + + + 1.8 + + + + + com.alibaba + fastjson + 1.2.83 + + + + org.projectlombok + lombok + 1.18.16 + + + + com.squareup.okhttp3 + okhttp + 3.12.2 + + + + + org.apache.curator + curator-client + 5.1.0 + + + + org.apache.curator + curator-recipes + 5.1.0 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/annotation/RpcfxReference.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/annotation/RpcfxReference.java new file mode 100644 index 00000000..570b8ab8 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/annotation/RpcfxReference.java @@ -0,0 +1,17 @@ +package io.kimmking.rpcfx.annotation; + +import java.lang.annotation.*; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/1 20:00 + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +@Inherited +public @interface RpcfxReference { + +} \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/annotation/RpcfxService.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/annotation/RpcfxService.java new file mode 100644 index 00000000..9db3c7e3 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/annotation/RpcfxService.java @@ -0,0 +1,18 @@ +package io.kimmking.rpcfx.annotation; + +import java.lang.annotation.*; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/1 20:00 + */ + +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Inherited +public @interface RpcfxService { + +} \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Filter.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Filter.java new file mode 100644 index 00000000..29060ace --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Filter.java @@ -0,0 +1,11 @@ +package io.kimmking.rpcfx.api; + +public interface Filter { + + RpcfxResponse prefilter(RpcfxRequest request); + + RpcfxResponse postfilter(RpcfxRequest request, RpcfxResponse response); + + // Filter next(); + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/LoadBalancer.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/LoadBalancer.java new file mode 100644 index 00000000..5ac4fab2 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/LoadBalancer.java @@ -0,0 +1,11 @@ +package io.kimmking.rpcfx.api; + +import io.kimmking.rpcfx.meta.InstanceMeta; + +import java.util.List; + +public interface LoadBalancer { + + InstanceMeta select(List instances); + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Router.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Router.java new file mode 100644 index 00000000..a4ed3225 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/Router.java @@ -0,0 +1,10 @@ +package io.kimmking.rpcfx.api; + +import io.kimmking.rpcfx.meta.InstanceMeta; + +import java.util.List; + +public interface Router { + + List route(List instances); +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcContext.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcContext.java new file mode 100644 index 00000000..79a65bde --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcContext.java @@ -0,0 +1,41 @@ +package io.kimmking.rpcfx.api; + +import io.kimmking.rpcfx.meta.ProviderMeta; +import lombok.Getter; +import lombok.Setter; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; + +import java.util.HashMap; +import java.util.Map; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/13 20:34 + */ +public class RpcContext { + + @Getter + private final MultiValueMap providerHolder = new LinkedMultiValueMap<>(); + + @Getter + private final Map consumerHolder = new HashMap<>(); + + @Getter + private final Map parameters = new HashMap<>(); + + @Getter + @Setter + private Router router; + + @Getter + @Setter + private LoadBalancer loadBalancer; + + @Getter + @Setter + private Filter[] filters; + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java new file mode 100644 index 00000000..1e9edfb4 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxRequest.java @@ -0,0 +1,10 @@ +package io.kimmking.rpcfx.api; + +import lombok.Data; + +@Data +public class RpcfxRequest { + private String serviceClass; + private String methodSign; + private Object[] params; +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxResponse.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxResponse.java new file mode 100644 index 00000000..b37747ad --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxResponse.java @@ -0,0 +1,10 @@ +package io.kimmking.rpcfx.api; + +import lombok.Data; + +@Data +public class RpcfxResponse { + private Object result; + private boolean status; + private Exception exception; +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/ServiceProviderDesc.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/ServiceProviderDesc.java new file mode 100644 index 00000000..f66ec625 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/ServiceProviderDesc.java @@ -0,0 +1,16 @@ +package io.kimmking.rpcfx.api; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class ServiceProviderDesc { + + private String host; + private Integer port; + private String serviceClass; + + // group + // version +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/ConsumerBootstrap.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/ConsumerBootstrap.java new file mode 100644 index 00000000..a69fe083 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/ConsumerBootstrap.java @@ -0,0 +1,110 @@ +package io.kimmking.rpcfx.consumer; + +import io.kimmking.rpcfx.annotation.RpcfxReference; +import io.kimmking.rpcfx.api.RpcContext; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.RegistryCenter; +import io.kimmking.rpcfx.registry.RegistryConfiguration; +import io.kimmking.rpcfx.stub.StubSkeletonHelper; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeansException; +import org.springframework.beans.PropertyValues; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; +import org.springframework.context.annotation.Import; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.Closeable; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/13 23:26 + */ +@Slf4j +@Component +@Import({RegistryConfiguration.class}) +public class ConsumerBootstrap implements Closeable, InstantiationAwareBeanPostProcessor { + + private RpcContext context = new RpcContext(); + + private String scanPackage = "io.kimmking"; + + @Value("${app.id:app1}") + public String app; + @Value("${app.namespace:public}") + public String ns; + @Value("${app.env:dev}") + public String env; + @Value("${app.mock:false}") + public boolean mock; + @Value("${app.cache:false}") + public boolean cache; + @Value("${app.retry:1}") + public int retry; + + @Autowired + RegistryCenter rc; + + @PostConstruct + public void init() { + this.context.getParameters().put("app.id", app); + this.context.getParameters().put("app.namespace", ns); + this.context.getParameters().put("app.env", env); + this.context.getParameters().put("app.mock", String.valueOf(mock)); + this.context.getParameters().put("app.cache", String.valueOf(cache)); + this.context.getParameters().put("app.retry", String.valueOf(retry)); + } + + @Override + public void close() throws IOException { + + } + + @Override + public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException { + if (bean.getClass().getPackage().getName().startsWith(scanPackage)) { + Field[] declaredFields = resolveAllField(bean.getClass()); // 解决父类里的注解扫描不到的问题 + + List consumers = Arrays.stream(declaredFields) + .filter(field -> field.isAnnotationPresent(RpcfxReference.class)) + .collect(Collectors.toList()); + + consumers.stream().forEach(field -> { + Object consumer = createConsumer(field.getType()); + try { + field.setAccessible(true); + field.set(bean, consumer); + } catch (IllegalAccessException e) { + log.error(e.getMessage(), e); + } + }); + } + return null; + } + + private Field[] resolveAllField(Class aClass) { + List res = new ArrayList<>(20); + while ( !Object.class.equals(aClass) ) { + Field[] fields = aClass.getDeclaredFields(); + res.addAll(Arrays.asList(fields)); + aClass = aClass.getSuperclass(); + } + return res.toArray(new Field[0]); + } + + private T createConsumer(Class clazz) { + ServiceMeta sm = ServiceMeta.builder().name(clazz.getCanonicalName()) + .app(app).namespace(ns).env(env).build(); + return (T) StubSkeletonHelper.createConsumer(sm, context, rc); + } +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxConsumerInvoker.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxConsumerInvoker.java new file mode 100644 index 00000000..66d9ea89 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxConsumerInvoker.java @@ -0,0 +1,70 @@ +package io.kimmking.rpcfx.consumer; + + +import com.alibaba.fastjson.parser.ParserConfig; +import io.kimmking.rpcfx.api.*; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.RegistryCenter; + +import java.lang.reflect.Proxy; +import java.util.ArrayList; +import java.util.List; + +public final class RpcfxConsumerInvoker { + + static { + ParserConfig.getGlobalInstance().addAccept("io.kimmking"); + } + + RpcContext ctx; + + RegistryCenter rc; + + public RpcfxConsumerInvoker(RpcContext ctx, RegistryCenter rc) { + this.ctx = ctx; + this.rc = rc; //"localhost:2181" + } + + public void start() { + this.rc.start(); + } + + public void stop() { + this.rc.stop(); + } + + public T createFromRegistry(final ServiceMeta sm, RpcContext ctx) { + + String service = sm.getName();//"io.kimking.rpcfx.demo.api.UserService"; + System.out.println("====> "+service); + List invokers = new ArrayList<>(); + Class serviceClass = null; + try { + + serviceClass = Class.forName(service); + + List insts = rc.fetchInstances(sm); + if(insts != null && insts.size()>0) invokers.addAll(insts); + rc.subscribe(sm, e -> { + invokers.clear(); + invokers.addAll((List)e.getData()); + }); + + } catch (Exception ex) { + ex.printStackTrace(); + throw new RuntimeException(ex); + } + + return (T) create(serviceClass, invokers, ctx); + + } + + private T create(Class serviceClass, List invokers, RpcContext ctx) { + RpcfxInvocationHandler invocationHandler + = new RpcfxInvocationHandler(serviceClass, invokers, ctx); + return (T) Proxy.newProxyInstance(RpcfxConsumerInvoker.class.getClassLoader(), + new Class[]{serviceClass}, invocationHandler); + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxInvocationHandler.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxInvocationHandler.java new file mode 100644 index 00000000..0325e0c7 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/consumer/RpcfxInvocationHandler.java @@ -0,0 +1,140 @@ +package io.kimmking.rpcfx.consumer; + +import com.alibaba.fastjson.JSON; +import io.kimmking.rpcfx.api.*; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.stub.StubSkeletonHelper; +import io.kimmking.rpcfx.utils.MethodUtils; +import okhttp3.*; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.net.SocketTimeoutException; +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class RpcfxInvocationHandler implements InvocationHandler { + + public final Object target = new Object(); + + public static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8"); + + private final Class serviceClass; + private final List invokers; + + private final RpcContext context; + + public RpcfxInvocationHandler(Class serviceClass, List invokers, RpcContext ctx) { + this.serviceClass = serviceClass; + this.invokers = invokers; + this.context = ctx; + } + + // 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx + // int byte char float double long bool + // [], data class + + @Override + public Object invoke(Object proxy, Method method, Object[] params) throws Throwable { + + long start = System.currentTimeMillis(); + + if (!StubSkeletonHelper.checkRpcMethod(method)){ + return method.invoke(target, params); + } + + + int retry = 2; + while (retry-- > 0) { + System.out.println("retry:" + retry); + try { + + // check mock, 挡板功能 TODO 3 + + List insts = context.getRouter().route(invokers); +// System.out.println("router.route => "); +// urls.forEach(System.out::println); + InstanceMeta instance = context.getLoadBalancer().select(insts); // router, loadbalance +// System.out.println("loadBalance.select => "); +// System.out.println("final => " + url); + + if (instance == null) { + throw new RuntimeException("No available providers from registry center."); + } + + + RpcfxRequest request = new RpcfxRequest(); + request.setServiceClass(this.serviceClass.getName()); + request.setMethodSign(MethodUtils.methodSign(method)); + request.setParams(params); + + Filter[] filters = context.getFilters(); + + if (null != filters) { + for (Filter filter : filters) { + RpcfxResponse response = filter.prefilter(request); + if (response != null) { + return JSON.parse(response.getResult().toString()); + } + } + } + + // 没有控制超时,可能会很久 TODO 2 + RpcfxResponse response = post(request, instance); + + if (null != filters) { + for (Filter filter : filters) { + RpcfxResponse postResponse = filter.postfilter(request, response); + if (postResponse!=null) { + response = postResponse; + } + } + } + + System.out.println("Invoke spend " + (System.currentTimeMillis()-start) + " ms"); + + // 加filter地方之三 + // Student.setTeacher("cuijing"); + + // 这里判断response.status,处理异常 + // 考虑封装一个全局的RpcfxException + + return JSON.parse(response.getResult().toString()); + + } catch (RuntimeException ex) { + ex.printStackTrace(); + if(! (ex.getCause() instanceof SocketTimeoutException)) { + break; + } + } + } + return null; + + } + + OkHttpClient client = new OkHttpClient.Builder() + .connectionPool(new ConnectionPool(128, 60, TimeUnit.SECONDS)) +// .dispatcher(dispatcher) + .readTimeout(1, TimeUnit.SECONDS) + .writeTimeout(1, TimeUnit.SECONDS) + .connectTimeout(1, TimeUnit.SECONDS) + .build(); + + private RpcfxResponse post(RpcfxRequest req, InstanceMeta instance) throws Exception { + String reqJson = JSON.toJSONString(req); + System.out.println("req json: "+reqJson); + + final Request request = new Request.Builder() + .url(instance.toString()) + .post(RequestBody.create(JSONTYPE, reqJson)) + .build(); + String respJson; + try { + respJson = client.newCall(request).execute().body().string(); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + System.out.println("resp json: "+respJson); + return JSON.parseObject(respJson, RpcfxResponse.class); + } +} \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/InstanceMeta.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/InstanceMeta.java new file mode 100644 index 00000000..e1388143 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/InstanceMeta.java @@ -0,0 +1,48 @@ +package io.kimmking.rpcfx.meta; + +import com.google.common.base.Strings; +import lombok.*; + +import java.net.URI; +import java.util.Map; +import java.util.Objects; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 19:46 + */ + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode(of = {"scheme", "host", "port", "context"}) +public class InstanceMeta { + + private String scheme; + private String host; + private Integer port; + private String context; + private boolean status; + private Map metadata; + + public static InstanceMeta from(String instance) { + URI uri = URI.create(instance); + String path = uri.getPath(); + path = Strings.isNullOrEmpty(path) ? "" : path.substring(1); + return InstanceMeta.builder() + .scheme(uri.getScheme()) + .host(uri.getHost()) + .port(uri.getPort()) + .context(path) + .build(); + } + + @Override + public String toString() { + return scheme + "://" + host + ":" + port + "/" + context; + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ProviderMeta.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ProviderMeta.java new file mode 100644 index 00000000..f0b9e0ae --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ProviderMeta.java @@ -0,0 +1,19 @@ +package io.kimmking.rpcfx.meta; + +import lombok.Data; + +import java.lang.reflect.Method; + +/** + * @author lirui + */ +@Data +public class ProviderMeta { + + private Object serviceImpl; + + private Method method; + + private String methodSign; + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServerMeta.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServerMeta.java new file mode 100644 index 00000000..239042b8 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServerMeta.java @@ -0,0 +1,24 @@ +package io.kimmking.rpcfx.meta; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/4/13 21:43 + */ + +@Data +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode(of = {"url"}) +public class ServerMeta { + private String url; + private boolean leader; + private boolean status; + private long version; +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServiceMeta.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServiceMeta.java new file mode 100644 index 00000000..c443a272 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/meta/ServiceMeta.java @@ -0,0 +1,25 @@ +package io.kimmking.rpcfx.meta; + +import lombok.Builder; +import lombok.Data; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 19:46 + */ +@Data +@Builder +public class ServiceMeta { + + private String app; + private String namespace; + private String env; + private String name; + + @Override + public String toString() { + return String.format("%s_%s_%s_%s", app, namespace, env, name); + } +} \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/ProviderBootstrap.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/ProviderBootstrap.java new file mode 100644 index 00000000..ce714bb0 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/ProviderBootstrap.java @@ -0,0 +1,154 @@ +package io.kimmking.rpcfx.provider; + +import io.kimmking.rpcfx.annotation.RpcfxService; +import io.kimmking.rpcfx.api.RpcContext; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.RegistryCenter; +import io.kimmking.rpcfx.registry.RegistryConfiguration; +import io.kimmking.rpcfx.stub.StubSkeletonHelper; +import lombok.Getter; +import lombok.SneakyThrows; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.ApplicationRunner; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; +import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.core.annotation.Order; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; +import java.net.InetAddress; +import java.util.Arrays; +import java.util.Objects; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/13 20:27 + */ + +@Component +@Import({RegistryConfiguration.class}) +public class ProviderBootstrap { + + @Autowired + private ApplicationContext applicationContext; + + @Autowired + Environment environment; + + @Value("${app.id:app1}") + public String app; + @Value("${app.namespace:public}") + public String ns; + @Value("${app.env:dev}") + public String env; + + private final RpcContext context = new RpcContext(); + + @Getter + private final RpcfxProviderInvoker invoker = new RpcfxProviderInvoker(context);; + + private static String SCHEME = "http"; + private static String ip; + private static int port; + + @Autowired + RegistryCenter registry;// = new KKRegistryCenter(); + + @SneakyThrows + @PostConstruct + public void start(){ + System.out.println("build all services from annotation..."); + buildProvider(); + + System.out.println("get IP and PORT..."); + ip = InetAddress.getLocalHost().getHostAddress(); + port = Integer.parseInt(Objects.requireNonNull(environment.getProperty("server.port"))); + } + + private void buildProvider() { + String[] beansName = applicationContext.getBeanDefinitionNames(); + for (int i = 0; i < beansName.length; i++) { + String beanName = beansName[i]; + Object bean = applicationContext.getBean(beanName); + RpcfxService provider = AnnotationUtils.findAnnotation(bean.getClass(), RpcfxService.class); + if (provider == null) { + continue; + } + Class[] classes = bean.getClass().getInterfaces(); + if (classes == null || classes.length == 0) { + continue; + } + Arrays.stream(classes).forEach(c -> this.createProvider(c, bean)); + } + } + + private void createProvider(Class clazz, Object bean) { + StubSkeletonHelper.createProvider(clazz, bean, context); // 初始化了holder + } + + @Order(Integer.MIN_VALUE) + @Bean + public ApplicationRunner run() throws Exception { + return x -> registerServices(); + } + + private void registerServices() { + + registry.start(); + + System.out.println("registry all services from RegistryCenter..."); + context.getProviderHolder().forEach( (x, y) -> + { + System.out.println(" register " + x); + ServiceMeta sm = ServiceMeta.builder().name(x) + .app(app).namespace(ns).env(env).build(); + + InstanceMeta im = InstanceMeta.builder() + .scheme(SCHEME).host(ip).port(port).context("").build(); + try { + registry.registerService(sm, im); + registry.heartbeat(sm, im); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + ); + } + + @PreDestroy + public void stop() { + unregisterServices(); + } + + private void unregisterServices() { + System.out.println("unregistry all services from RegistryCenter..."); + context.getProviderHolder().forEach( (x, y) -> + { + System.out.println(" unregister " + x); + ServiceMeta sm = ServiceMeta.builder().name(x) + .app(app).namespace(ns).env(env).build(); + InstanceMeta im = InstanceMeta.builder() + .scheme(SCHEME).host(ip).port(port).context("").build(); + try { + registry.unregisterService(sm, im); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + ); + + registry.stop(); + + } + + + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/RpcfxProviderInvoker.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/RpcfxProviderInvoker.java new file mode 100644 index 00000000..6842dfe6 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/provider/RpcfxProviderInvoker.java @@ -0,0 +1,61 @@ +package io.kimmking.rpcfx.provider; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.SerializerFeature; +import io.kimmking.rpcfx.api.RpcContext; +import io.kimmking.rpcfx.api.RpcfxRequest; +import io.kimmking.rpcfx.api.RpcfxResponse; +import io.kimmking.rpcfx.meta.ProviderMeta; +import org.springframework.util.CollectionUtils; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.List; +import java.util.Optional; + +public class RpcfxProviderInvoker { + + RpcContext context; + + public RpcfxProviderInvoker(RpcContext context) { + this.context = context; + } + + public RpcfxResponse invoke(RpcfxRequest request) { + RpcfxResponse response = new RpcfxResponse(); + String serviceClass = request.getServiceClass(); + + ProviderMeta meta = findProvider(serviceClass, request.getMethodSign()); + + try { + Method method = meta.getMethod(); + // 没有控制超时,所以可能会很久 TODO 1 + Object result = method.invoke(meta.getServiceImpl(), request.getParams()); // dubbo, fastjson, + // 两次json序列化能否合并成一个 + response.setResult(JSON.toJSONString(result, SerializerFeature.WriteClassName)); + response.setStatus(true); + return response; + } catch ( IllegalAccessException | InvocationTargetException e) { + + // 3.Xstream + + // 2.封装一个统一的RpcfxException + // 客户端也需要判断异常 + e.printStackTrace(); + response.setException(e); + response.setStatus(false); + return response; + } + } + + protected ProviderMeta findProvider(String interfaceName, String methodSign) { + List providerMetas = context.getProviderHolder().get(interfaceName); + if (!CollectionUtils.isEmpty(providerMetas)) { + Optional providerMeta = providerMetas.stream() + .filter(provider -> methodSign.equals(provider.getMethodSign())).findFirst(); + return providerMeta.orElse(null); + } + return null; + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/ChangedListener.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/ChangedListener.java new file mode 100644 index 00000000..971cbcda --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/ChangedListener.java @@ -0,0 +1,13 @@ +package io.kimmking.rpcfx.registry; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 20:19 + */ +public interface ChangedListener { + + void fireEvent(Event e); + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/Event.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/Event.java new file mode 100644 index 00000000..19a92cd4 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/Event.java @@ -0,0 +1,29 @@ +package io.kimmking.rpcfx.registry; + +import io.kimmking.rpcfx.meta.InstanceMeta; +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.util.List; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 20:20 + */ +public interface Event { + + T getData(); + + static Event> withData(List list) { + return new ChangedEvent(list); + } + + @Data + @AllArgsConstructor + class ChangedEvent implements Event> { + List data; + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryCenter.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryCenter.java new file mode 100644 index 00000000..871c1a7e --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryCenter.java @@ -0,0 +1,35 @@ +package io.kimmking.rpcfx.registry; + +import io.kimmking.rpcfx.api.ServiceProviderDesc; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.zookeeper.CreateMode; + +import java.util.List; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 15:23 + */ +public interface RegistryCenter { + + void start(); + + void stop(); + + void registerService(ServiceMeta service, InstanceMeta instance) throws Exception; + + void unregisterService(ServiceMeta service, InstanceMeta instance) throws Exception; + + List fetchInstances(ServiceMeta service) throws Exception; + + void subscribe(ServiceMeta service, ChangedListener> listener); + + void heartbeat(ServiceMeta service, InstanceMeta instance); + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryConfiguration.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryConfiguration.java new file mode 100644 index 00000000..8391c4aa --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/RegistryConfiguration.java @@ -0,0 +1,24 @@ +package io.kimmking.rpcfx.registry; + +import io.kimmking.rpcfx.registry.kkregistry.KKRegistryCenter; +import io.kimmking.rpcfx.registry.zookeeper.ZookeeperRegistryCenter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/9 01:05 + */ + +@Configuration +public class RegistryConfiguration { + + @Bean + RegistryCenter createRC() { + return new KKRegistryCenter(); + //return new ZookeeperRegistryCenter(); //KKRegistryCenter(); + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKHeathChecker.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKHeathChecker.java new file mode 100644 index 00000000..bbf0e17e --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKHeathChecker.java @@ -0,0 +1,42 @@ +package io.kimmking.rpcfx.registry.kkregistry; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/1 06:18 + */ +public class KKHeathChecker { + + final int interval = 5_000; + + final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); + static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"); + + public void check(Callback callback) { + executor.scheduleWithFixedDelay(() -> { + System.out.println("start to check kk health ...[" + DTF.format(LocalDateTime.now()) + "]"); + try { + callback.call(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }, interval, interval, TimeUnit.MILLISECONDS); + } + + public void stop() { + this.executor.shutdown(); + } + + public interface Callback { + void call() throws Exception; + } + + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKRegistryCenter.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKRegistryCenter.java new file mode 100644 index 00000000..001a3854 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/kkregistry/KKRegistryCenter.java @@ -0,0 +1,209 @@ +package io.kimmking.rpcfx.registry.kkregistry; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.TypeReference; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ServerMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.ChangedListener; +import io.kimmking.rpcfx.registry.Event; +import io.kimmking.rpcfx.registry.RegistryCenter; +import lombok.SneakyThrows; +import okhttp3.ConnectionPool; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.TimeUnit; + +import static io.kimmking.rpcfx.consumer.RpcfxInvocationHandler.JSONTYPE; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/8 15:25 + */ +public class KKRegistryCenter implements RegistryCenter { + + public String RC_Server = "http://localhost:8485"; + private ServerMeta leader; + private List servers; + private Map TV = new HashMap<>(); + + OkHttpClient client; + @SneakyThrows + @Override + public void start() { + client = new OkHttpClient.Builder() + .connectionPool(new ConnectionPool(128, 60, TimeUnit.SECONDS)) +// .dispatcher(dispatcher) + .readTimeout(65, TimeUnit.SECONDS) + .writeTimeout(65, TimeUnit.SECONDS) + .connectTimeout(3, TimeUnit.SECONDS) + .build(); + + String url = RC_Server + "/cluster"; + boolean init = false; + while(!init) { + System.out.println("===============>> cluster info from :" + url); + List new_servers = null; + ServerMeta new_leader = null; + try { + String respJson = get(url); + new_servers = JSON.parseObject(respJson, new TypeReference>() { + }); + new_leader = new_servers.stream().filter(ServerMeta::isStatus) + .filter(ServerMeta::isLeader).findFirst().orElse(null); + } catch (Exception exception) { + exception.printStackTrace(); + } + + if(new_leader == null) { + System.out.println("===============>> no leader, 500ms later and retry."); + Thread.sleep(500); + Random random = new Random(); + if(new_servers !=null && new_servers.size() > 1) { + url = new_servers.get(random.nextInt(new_servers.size())).getUrl() + "/cluster"; + } else if((new_servers ==null || new_servers.isEmpty()) && !servers.isEmpty()) { + url = servers.get(random.nextInt(servers.size())).getUrl() + "/cluster"; + } + } else { + this.servers = new_servers; + this.leader = new_leader; + init = true; + System.out.println("===============>> init ok, new_leader = " + new_leader); + System.out.println("===============>> init ok, new_servers = " + new_servers); + } + } + } + + @Override + public void stop() { + this.checker.stop(); + } + + @Override + public void registerService(ServiceMeta service, InstanceMeta instance) throws Exception { + instance.setStatus(true); + String reqJson = JSON.toJSONString(instance); + String url = leader.getUrl() + "/reg?service=" + service; + post(url, reqJson); +// String reqJson = "{\n" + +// " \"scheme\": \"http\",\n" + +// " \"ip\": \"" + instance.getIp() + "\",\n" + +// " \"port\": \"" + instance.getPort() + "\",\n" + +// " \"context\": \"\",\n" + +// " \"status\": \"online\",\n" + +// " \"metadata\": {\n" + +// " \"env\": \"dev\",\n" + +// " \"tag\": \"RED\"\n" + +// " }\n" + +// "}"; +// final Request request = new Request.Builder() +// .url("http://localhost:8484/reg?service=" + service) +// .post(RequestBody.create(JSONTYPE, reqJson)) +// .build(); +// String respJson = client.newCall(request).execute().body().string(); +// System.out.println(respJson); + } + + private String post(String url, String reqJson) throws IOException { + System.out.println(" ====> request: " + url); + final Request request = new Request.Builder() + .url(url) + .post(RequestBody.create(JSONTYPE, reqJson)) + .build(); + String respJson = client.newCall(request).execute().body().string(); + System.out.println(" ====> response: " + respJson); + return respJson; + } + + private String get(String url) throws IOException { + System.out.println(" ====> request: " + url); + final Request request = new Request.Builder() + .url(url) + .get() + .build(); + String respJson = client.newCall(request).execute().body().string(); + System.out.println(" ====> response: " + respJson); + return respJson; + } + + @Override + public void unregisterService(ServiceMeta service, InstanceMeta instance) throws Exception { + String reqJson = "{\n" + + " \"scheme\": \"http\",\n" + + " \"host\": \"" + instance.getHost() + "\",\n" + + " \"port\": \"" + instance.getPort() + "\",\n" + + " \"context\": \"\"\n" + + "}"; + String url = leader.getUrl() + "/unreg?service=" + service; + post(url, reqJson); + } + + public List fetchInstances(ServiceMeta service) throws Exception { + String url = RC_Server + "/findAll?service=" + service; + String respJson = get(url); + List instances = JSON.parseObject(respJson, new TypeReference>() { + }); + return instances; + } + + KKHeathChecker checker = new KKHeathChecker(); + + // for Consumer + public void subscribe(ServiceMeta service, final ChangedListener> listener) { + checker.check( () -> { + if(hb(service)) { + List instances = fetchInstances(service); + Event> e = Event.withData(instances); + listener.fireEvent(e); + } + }); + + // 定时器轮询 + // 保存上一次的TV + // 如果有差异就fire + } + + private boolean hb(ServiceMeta service) throws Exception { + String svc = service.toString(); + String url = RC_Server + "/version?service=" + svc; + String respJson = get(url); + Long v = Long.valueOf(respJson); + Long o = TV.getOrDefault(svc, -1L); + if ( v > o) { + TV.put(svc, v); + return o > -1L; + } + return false; + } + + + // for Provider + public void heartbeat(ServiceMeta service, InstanceMeta instance) { + checker.check( () -> { + heart(service, instance); + }); + } + + Long heart(ServiceMeta service, InstanceMeta instance) throws Exception { + String reqJson = "{\n" + + " \"scheme\": \"http\",\n" + + " \"host\": \"" + instance.getHost() + "\",\n" + + " \"port\": \"" + instance.getPort() + "\",\n" + + " \"context\": \"\",\n" + + " \"status\": true\n" + + "}"; + String url = leader.getUrl() + "/renew?service=" + service; + String respJson = post(url, reqJson); + return Long.valueOf(respJson); + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/zookeeper/ZookeeperRegistryCenter.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/zookeeper/ZookeeperRegistryCenter.java new file mode 100644 index 00000000..37f7b6ea --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/registry/zookeeper/ZookeeperRegistryCenter.java @@ -0,0 +1,101 @@ +package io.kimmking.rpcfx.registry.zookeeper; + +import io.kimmking.rpcfx.api.ServiceProviderDesc; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.ChangedListener; +import io.kimmking.rpcfx.registry.Event; +import io.kimmking.rpcfx.registry.RegistryCenter; +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.recipes.cache.TreeCache; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.zookeeper.CreateMode; + +import java.util.ArrayList; +import java.util.List; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/13 20:16 + */ +public class ZookeeperRegistryCenter implements RegistryCenter { + +// private final List listeners = new ArrayList<>(); +// public void addListener(ChangedListener listener) { +// this.listeners.add(listener); +// } + + CuratorFramework client = null; + public void start() { + RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); + client = CuratorFrameworkFactory.builder().connectString("localhost:2181").namespace("rpcfx").retryPolicy(retryPolicy).build(); + client.start(); + } + + public void stop(){ + client.close(); + } + + public void registerService(ServiceMeta service, InstanceMeta instance) throws Exception { + ServiceProviderDesc userServiceSesc = ServiceProviderDesc.builder() + .host(instance.getHost()) + .port(instance.getPort()).serviceClass(service.getName()).build(); + // String userServiceSescJson = JSON.toJSONString(userServiceSesc); + + try { + if ( null == client.checkExists().forPath("/" + service)) { + client.create().withMode(CreateMode.PERSISTENT).forPath("/" + service, "service".getBytes()); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + client.create().withMode(CreateMode.EPHEMERAL). + forPath( "/" + service + "/" + userServiceSesc.getHost() + "_" + userServiceSesc.getPort(), "provider".getBytes()); + } + + public void unregisterService(ServiceMeta service, InstanceMeta instance) throws Exception { + + if (null == client.checkExists().forPath("/" + service)) { + return; + } + System.out.println("delete " + "/" + service + "/" + instance.getHost() + "_" + instance.getPort()); + client.delete().quietly(). + forPath( "/" + service + "/" + instance.getHost() + "_" + instance.getPort()); + } + + public List fetchInstances(ServiceMeta service) throws Exception { + List services = client.getChildren().forPath("/" + service); + List instances = new ArrayList<>(); + for (String svc : services) { + System.out.println(svc); + String url = svc.replace("_", ":"); + instances.add(InstanceMeta.from("http://" + url)); + } + return instances; + } + + public void subscribe(ServiceMeta service, ChangedListener listener) { + final TreeCache treeCache = TreeCache.newBuilder(client, "/" + service).setCacheData(true).setMaxDepth(2).build(); + treeCache.getListenable().addListener((curatorFramework, treeCacheEvent) -> { + System.out.println("treeCacheEvent: "+treeCacheEvent); + List instances = fetchInstances(service); + listener.fireEvent(Event.withData(instances)); + }); + try { + treeCache.start(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public void heartbeat(ServiceMeta service, InstanceMeta instance) { + // do nothing + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/MockHandler.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/MockHandler.java new file mode 100644 index 00000000..9fe5269c --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/MockHandler.java @@ -0,0 +1,30 @@ +package io.kimmking.rpcfx.stub; + +import io.kimmking.rpcfx.utils.MockUtils; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/11 02:57 + */ +public class MockHandler implements InvocationHandler { + + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + Class type = method.getReturnType(); + System.out.println("invoke by mock handler..."); + return MockUtils.mock(type, null); + } + + public static T createMock(Class serviceClass) { + //final ServiceMeta sm, Router router, LoadBalancer loadBalance, Filter filter) { + return (T) Proxy.newProxyInstance(MockHandler.class.getClassLoader(), + new Class[]{serviceClass}, new MockHandler()); + + } +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/StubSkeletonHelper.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/StubSkeletonHelper.java new file mode 100644 index 00000000..e77215a8 --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/stub/StubSkeletonHelper.java @@ -0,0 +1,186 @@ +package io.kimmking.rpcfx.stub; + +import io.kimmking.rpcfx.api.*; +import io.kimmking.rpcfx.consumer.RpcfxConsumerInvoker; +import io.kimmking.rpcfx.meta.InstanceMeta; +import io.kimmking.rpcfx.meta.ProviderMeta; +import io.kimmking.rpcfx.meta.ServiceMeta; +import io.kimmking.rpcfx.registry.RegistryCenter; +import io.kimmking.rpcfx.utils.MethodUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.util.MultiValueMap; + +import java.lang.reflect.Method; +import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * @author lirui + */ +public class StubSkeletonHelper { + + public static void createProvider(Class clazz, Object serviceImpl, RpcContext rpcContext) { + String clazzName = clazz.getName(); + Class callClass = serviceImpl.getClass(); + + Method[] methodList = callClass.getMethods(); + for (Method method : methodList) { + if (!checkRpcMethod(method)) { + continue; + } + ProviderMeta providerMeta = buildProviderMeta(method, serviceImpl); + + MultiValueMap providerHolder = rpcContext.getProviderHolder(); + providerHolder.add(clazzName, providerMeta); + } + } + + private static ProviderMeta buildProviderMeta(Method method, Object serviceImpl) { + String methodSign = MethodUtils.methodSign(method); + ProviderMeta providerMeta = new ProviderMeta(); + providerMeta.setMethod(method); + providerMeta.setServiceImpl(serviceImpl); + providerMeta.setMethodSign(methodSign); + return providerMeta; + } + + public static boolean checkRpcMethod(final Method method) { + //本地方法不代理 + if ("toString".equals(method.getName()) || + "hashCode".equals(method.getName()) || + "notifyAll".equals(method.getName()) || + "equals".equals(method.getName()) || + "wait".equals(method.getName()) || + "getClass".equals(method.getName()) || + "notify".equals(method.getName())) { + return false; + } + return true; + } + + public static T createConsumer(ServiceMeta sm, RpcContext ctx, RegistryCenter rc) { + String clazzName = sm.getName(); + Class serviceClass = null; + try { + serviceClass = Class.forName(clazzName); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + T proxyHandler = (T) ctx.getConsumerHolder().get(clazzName); + if (proxyHandler == null) { // TODO configuration + + ctx.setRouter(new TagRouter()); + ctx.setLoadBalancer(new RoundRibbonLoadBalancer()); + ctx.setFilters(createFilters(ctx)); + + T mockHandler = createMockHandler(ctx, serviceClass); + if(mockHandler != null) { + return mockHandler; + } + + RpcfxConsumerInvoker consumerInvoker = new RpcfxConsumerInvoker(ctx, rc); + consumerInvoker.start(); + proxyHandler = consumerInvoker.createFromRegistry(sm, ctx); + ctx.getConsumerHolder().put(clazzName, proxyHandler); + } + return proxyHandler; + } + + private static Filter[] createFilters(RpcContext ctx) { + String cache = ctx.getParameters().getOrDefault("app.cache", "false"); + Filter[] filters = null; + if("true".equalsIgnoreCase(cache)) { + filters = new Filter[]{new CuicuiFilter(), new CacheFilter()}; + } else { + filters = new Filter[]{new CuicuiFilter()}; + } + return filters; + } + + private static T createMockHandler(RpcContext ctx, Class serviceClass) { + String mock = ctx.getParameters().getOrDefault("app.mock", "false"); + if("true".equalsIgnoreCase(mock)) { + return (T) MockHandler.createMock(serviceClass); + } + return null; + } + + + private static class TagRouter implements Router { + @Override + public List route(List instances) { + return instances; + } + } + + private static class RoundRibbonLoadBalancer implements LoadBalancer { + private final AtomicInteger count = new AtomicInteger(0); + @Override + public InstanceMeta select(List instances) { + if(instances.isEmpty()) return null; + return instances.get((count.getAndIncrement() & Integer.MAX_VALUE) % instances.size()); + } + } + + private static class RandomLoadBalancer implements LoadBalancer { + private final Random random = new Random(); + @Override + public InstanceMeta select(List instances) { + if(instances.isEmpty()) return null; + return instances.get(random.nextInt(instances.size())); + } + } + + @Slf4j + private static class CuicuiFilter implements Filter { + @Override + public RpcfxResponse prefilter(RpcfxRequest request) { + //log.info("filter {} -> {}", this.getClass().getName(), request.toString()); + //System.out.printf("filter %s -> %s%n", this.getClass().getName(), request.toString()); + return null; + } + + @Override + public RpcfxResponse postfilter(RpcfxRequest request, RpcfxResponse response) { + return response; + } + + } + + private static class CacheFilter implements Filter { + + static Map CACHE = new HashMap<>(); + + @Override + public RpcfxResponse prefilter(RpcfxRequest request) { + RpcfxResponse response = CACHE.get(genKey(request)); + if(response != null) { + System.out.println("CacheFilter.prefilter hit! => request: \n" + request + "\n =>response: \n" + response); + } + return response; + //log.info("filter {} -> {}", this.getClass().getName(), request.toString()); + //System.out.printf("filter %s -> %s%n", this.getClass().getName(), request.toString()); + } + + @Override + public RpcfxResponse postfilter(RpcfxRequest request, RpcfxResponse response) { + String key = genKey(request); + if(!CACHE.containsKey(key)) { + CACHE.put(key, response); + } + return response; + } + + } + + public static String genKey(RpcfxRequest request) { + StringBuilder sb = new StringBuilder(); + sb.append(request.getServiceClass()); + sb.append("@"); + sb.append(request.getMethodSign()); + //sb.append(""); + Arrays.stream(request.getParams()).forEach(x -> sb.append("_"+x.toString())); + return sb.toString(); + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MethodUtils.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MethodUtils.java new file mode 100644 index 00000000..f17a102f --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MethodUtils.java @@ -0,0 +1,30 @@ +package io.kimmking.rpcfx.utils; + +import org.springframework.util.DigestUtils; + +import java.lang.reflect.Method; +import java.util.Arrays; + +public class MethodUtils { + + public static String methodSign(Method method) { + if (method != null) { + StringBuilder builder = new StringBuilder(); + String name = method.getName(); + builder.append(name); + builder.append("@"); + int count = method.getParameterCount(); + builder.append(count); + builder.append("_"); + if (count > 0) { + Class[] classes = method.getParameterTypes(); + Arrays.stream(classes).forEach(c -> builder.append(c.getName() + ",")); + } + return builder.toString(); +// String string = builder.toString(); +// return DigestUtils.md5DigestAsHex(string.getBytes()); + } + return ""; + } + +} \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MockUtils.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MockUtils.java new file mode 100644 index 00000000..8d22536a --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/MockUtils.java @@ -0,0 +1,143 @@ +package io.kimmking.rpcfx.utils; + +import lombok.Data; +import org.springframework.util.ClassUtils; + +import java.lang.reflect.Array; +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.*; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/11 03:15 + */ +public class MockUtils { + + public static Object mock(Class clazz, Type[] generics) { + boolean primitiveOrWrapper = ClassUtils.isPrimitiveOrWrapper(clazz); + if(primitiveOrWrapper) return mockPrimitive(clazz); + if(String.class.equals(clazz)) return mockString(); + if (Number.class.isAssignableFrom(clazz)) { + return 10; + } + if(clazz.isArray()) { + return mockArray(clazz.getComponentType()); + } + if(List.class.isAssignableFrom(clazz)) { + return mockList(clazz, generics[0]); + } + if(Map.class.isAssignableFrom(clazz)) { + return mockMap(clazz, generics[1]); + } + return mockPojo(clazz); + } + + private static Object mockMap(Class clazz, Type generic) { + HashMap map = new HashMap<>(); + map.put("a", mock((Class)generic, null)); + map.put("b", mock((Class)generic, null)); + return map; + } + + private static Object mockList(Class clazz, Type generic) { + List list = new ArrayList<>(); + list.add(mock((Class)generic, null)); + list.add(mock((Class)generic, null)); + return list; + } + + private static Object mockArray(Class clazz) { + Object array = Array.newInstance(clazz, 2); + Array.set(array, 0, mock(clazz, null)); + Array.set(array, 1, mock(clazz,null)); + return array; + } + + private static Object mockPojo(Class clazz) { + try { + Object object = clazz.getDeclaredConstructor().newInstance(); + Field[] fields = clazz.getDeclaredFields(); + for (Field f : fields) { + f.setAccessible(true); + Type genericType = f.getGenericType(); +// System.out.println(f.getGenericType()); +// System.out.println(f.getType()); + if (genericType instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) genericType; + Type[] typeArguments = parameterizedType.getActualTypeArguments(); + System.out.println("genericType="+Arrays.toString(typeArguments)); + f.set(object, mock(f.getType(), typeArguments)); + } else { + f.set(object, mock(f.getType(),null)); + } + } + return object; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + private static Object mockString() { + return "this_is_a_mock_string"; + } + + private static Object mockPrimitive(Class clazz) { + + if (Boolean.class.equals(clazz)) { + return true; + } + + return 1; + } + + public static void main(String[] args) { + + System.out.println(mock(ListPojo.class,null)); + + +// System.out.println(mock(Byte.class)); +// System.out.println(mock(Character.class)); +// System.out.println(mock(Boolean.class)); +// System.out.println(mock(Integer.class)); +// System.out.println(mock(Float.class)); +// System.out.println(mock(Short.class)); +// System.out.println(mock(Long.class)); +// System.out.println(mock(Double.class)); +// System.out.println(mock(BigInteger.class)); +// System.out.println(mock(BigDecimal.class)); +// System.out.println(mock(String.class)); + +// System.out.println(mock(Pojo.class)); + +// Arrays.stream(((Pojo[]) mock(new Pojo[]{}.getClass()))).forEach(System.out::println); + + } + + + @Data + public static class Pojo { + private int id; + private String name; + private float amount; + private InnerPojo inner; + } + + @Data + public static class InnerPojo { + private int value; + private String key; + } + + @Data + public static class ListPojo { + private List list; + private Integer inner; + private Map map; + } + +} diff --git a/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/RoundRobinByWeightLoadBalance.java b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/RoundRobinByWeightLoadBalance.java new file mode 100644 index 00000000..6ada40ed --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/RoundRobinByWeightLoadBalance.java @@ -0,0 +1,219 @@ +package io.kimmking.rpcfx.utils; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/2/13 23:44 + */ + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by caojun on 2018/2/20. + * + * 基本概念: + * weight: 配置文件中指定的该后端的权重,这个值是固定不变的。 + * effective_weight: 后端的有效权重,初始值为weight。 + * 在释放后端时,如果发现和后端的通信过程中发生了错误,就减小effective_weight。 + * 此后有新的请求过来时,在选取后端的过程中,再逐步增加effective_weight,最终又恢复到weight。 + * 之所以增加这个字段,是为了当后端发生错误时,降低其权重。 + * current_weight: + * 后端目前的权重,一开始为0,之后会动态调整。那么是怎么个动态调整呢? + * 每次选取后端时,会遍历集群中所有后端,对于每个后端,让它的current_weight增加它的effective_weight, + * 同时累加所有后端的effective_weight,保存为total。 + * 如果该后端的current_weight是最大的,就选定这个后端,然后把它的current_weight减去total。 + * 如果该后端没有被选定,那么current_weight不用减小。 + * + * 算法逻辑: + * 1. 对于每个请求,遍历集群中的所有可用后端,对于每个后端peer执行: + *     peer->current_weight += peer->effecitve_weight。 + *     同时累加所有peer的effective_weight,保存为total。 + * 2. 从集群中选出current_weight最大的peer,作为本次选定的后端。 + * 3. 对于本次选定的后端,执行:peer->current_weight -= total。 + * + */ +public class RoundRobinByWeightLoadBalance { + + //约定的invoker和权重的键值对 + final private List nodes; + + public RoundRobinByWeightLoadBalance(Map invokersWeight){ + if (invokersWeight != null && !invokersWeight.isEmpty()) { + nodes = new ArrayList<>(invokersWeight.size()); + invokersWeight.forEach((invoker, weight)->nodes.add(new Node(invoker, weight))); + }else + nodes = null; + } + + /** + * 算法逻辑: + * 1. 对于每个请求,遍历集群中的所有可用后端,对于每个后端peer执行: + *     peer->current_weight += peer->effecitve_weight。 + *     同时累加所有peer的effective_weight,保存为total。 + * 2. 从集群中选出current_weight最大的peer,作为本次选定的后端。 + * 3. 对于本次选定的后端,执行:peer->current_weight -= total。 + * + * @Return ivoker + */ + public Invoker select(){ + if (! checkNodes()) + return null; + else if (nodes.size() == 1) { + if (nodes.get(0).invoker.isAvalable()) + return nodes.get(0).invoker; + else + return null; + } + Integer total = 0; + Node nodeOfMaxWeight = null; + for (Node node : nodes) { + total += node.effectiveWeight; + node.currentWeight += node.effectiveWeight; + + if (nodeOfMaxWeight == null) { + nodeOfMaxWeight = node; + }else{ + nodeOfMaxWeight = nodeOfMaxWeight.compareTo(node) > 0 ? nodeOfMaxWeight : node; + } + } + + nodeOfMaxWeight.currentWeight -= total; + return nodeOfMaxWeight.invoker; + } + + public void onInvokeSuccess(Invoker invoker){ + if (checkNodes()){ + nodes.stream() + .filter((Node node)->invoker.id().equals(node.invoker.id())) + .findFirst() + .get() + .onInvokeSuccess(); + } + } + + public void onInvokeFail(Invoker invoker){ + if (checkNodes()){ + nodes.stream() + .filter((Node node)->invoker.id().equals(node.invoker.id())) + .findFirst() + .get() + .onInvokeFail(); + } + } + + private boolean checkNodes(){ + return (nodes != null && nodes.size() > 0); + } + + public void printCurrenctWeightBeforeSelect(){ + if (checkNodes()) { + final StringBuffer out = new StringBuffer("{"); + nodes.forEach(node->out.append(node.invoker.id()) + .append("=") + .append(node.currentWeight+node.effectiveWeight) + .append(",")); + out.append("}"); + System.out.print(out); + } + } + + public void printCurrenctWeight(){ + if (checkNodes()) { + final StringBuffer out = new StringBuffer("{"); + nodes.forEach(node->out.append(node.invoker.id()) + .append("=") + .append(node.currentWeight) + .append(",")); + out.append("}"); + System.out.print(out); + } + } + + public interface Invoker{ + Boolean isAvalable(); + String id(); + } + + private static class Node implements Comparable{ + final Invoker invoker; + final Integer weight; + Integer effectiveWeight; + Integer currentWeight; + + Node(Invoker invoker, Integer weight){ + this.invoker = invoker; + this.weight = weight; + this.effectiveWeight = weight; + this.currentWeight = 0; + } + + @Override + public int compareTo(Node o) { + return currentWeight > o.currentWeight ? 1 : (currentWeight.equals(o.currentWeight) ? 0 : -1); + } + + public void onInvokeSuccess(){ + if (effectiveWeight < this.weight) + effectiveWeight++; + } + + public void onInvokeFail(){ + effectiveWeight--; + } + } + + public static void main(String[] args){ + Map invokersWeight = new HashMap<>(3); + Integer aWeight = 4; + Integer bWeight = 2; + Integer cWeight = 1; + + invokersWeight.put(new Invoker() { + @Override + public Boolean isAvalable() { + return true; + } + @Override + public String id() { + return "a"; + } + }, aWeight); + + invokersWeight.put(new Invoker() { + @Override + public Boolean isAvalable() { + return true; + } + @Override + public String id() { + return "b"; + } + }, bWeight); + + invokersWeight.put(new Invoker() { + @Override + public Boolean isAvalable() { + return true; + } + @Override + public String id() { + return "c"; + } + }, cWeight); + + Integer times = 7; + RoundRobinByWeightLoadBalance roundRobin = new RoundRobinByWeightLoadBalance(invokersWeight); + for(int i=1; i<=times; i++){ + System.out.print(new StringBuffer(i+"").append(" ")); + roundRobin.printCurrenctWeightBeforeSelect(); + Invoker invoker = roundRobin.select(); + System.out.print(new StringBuffer(" ").append(invoker.id()).append(" ")); + roundRobin.printCurrenctWeight(); + System.out.println(); + } + } +} diff --git a/07rpc/rpc01/rpcfx-core/src/test/java/FourSumCount.java b/07rpc/rpc01/rpcfx-core/src/test/java/FourSumCount.java new file mode 100644 index 00000000..7375135e --- /dev/null +++ b/07rpc/rpc01/rpcfx-core/src/test/java/FourSumCount.java @@ -0,0 +1,47 @@ +import java.util.HashMap; +import java.util.Map; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/3/14 17:52 + */ +public class FourSumCount { + public static void main(String[] args) { + int[] numsA = {1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2}; + int[] numsB = {-2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1,-2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1, -2, -1}; + int[] numsC = {-1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2, -1, 2}; + int[] numsD = {0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2}; + + long start = System.nanoTime(); + int count = fourSumCount(numsA, numsB, numsC, numsD); + System.out.println(" take " + (System.nanoTime()-start)/1000000.0 + " ms"); + System.out.println(count); + } + + public static int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { + Map map = new HashMap<>(); + int temp; + int res = 0; + for (int i : nums1) { + for (int j : nums2) { + temp = i + j; + if (map.containsKey(temp)) { + map.put(temp, map.get(temp) + 1); + } else { + map.put(temp, 1); + } + } + } + for (int i : nums3) { + for (int j : nums4) { + temp = i + j; + if (map.containsKey(0 - temp)) { + res += map.get(0 - temp); + } + } + } + return res; + } +} \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-demo-api/pom.xml b/07rpc/rpc01/rpcfx-demo-api/pom.xml new file mode 100644 index 00000000..2a378409 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-api/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + io.kimmking + rpcfx + 0.0.1-SNAPSHOT + + + io.kimmking + rpcfx-demo-api + 0.0.1-SNAPSHOT + rpcfx-demo-api + + + 1.8 + + + diff --git a/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/Order.java b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/Order.java new file mode 100644 index 00000000..a2fd6c91 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/Order.java @@ -0,0 +1,40 @@ +package io.kimmking.rpcfx.demo.api; + +public class Order { + + private int id; + + private String name; + + private float amount; + + public Order(int id, String name, float amount) { + this.id = id; + this.name = name; + this.amount = amount; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public float getAmount() { + return amount; + } + + public void setAmount(float amount) { + this.amount = amount; + } +} diff --git a/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/OrderService.java b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/OrderService.java new file mode 100644 index 00000000..1884fedb --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/OrderService.java @@ -0,0 +1,7 @@ +package io.kimmking.rpcfx.demo.api; + +public interface OrderService { + + Order findOrderById(int id); + +} diff --git a/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/User.java b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/User.java new file mode 100644 index 00000000..dec23e7c --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/User.java @@ -0,0 +1,30 @@ +package io.kimmking.rpcfx.demo.api; + +public class User { + + public User(){} + + public User(int id, String name) { + this.id = id; + this.name = name; + } + + private int id; + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/UserService.java b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/UserService.java new file mode 100644 index 00000000..c7678f10 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-api/src/main/java/io/kimmking/rpcfx/demo/api/UserService.java @@ -0,0 +1,11 @@ +package io.kimmking.rpcfx.demo.api; + +public interface UserService { + + User findById(int id); + + User find(int timeout); + + //User findById(int id, String name); + +} diff --git a/07rpc/rpc01/rpcfx-demo-consumer/pom.xml b/07rpc/rpc01/rpcfx-demo-consumer/pom.xml new file mode 100644 index 00000000..8f994f07 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-consumer/pom.xml @@ -0,0 +1,63 @@ + + + + rpcfx + io.kimmking + 0.0.1-SNAPSHOT + + 4.0.0 + + rpcfx-demo-consumer + + + 8 + 8 + + + + + io.kimmking + rpcfx-core + ${project.version} + + + io.kimmking + rpcfx-demo-api + ${project.version} + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/HelloController.java b/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/HelloController.java new file mode 100644 index 00000000..14f75607 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/HelloController.java @@ -0,0 +1,26 @@ +package io.kimmking.rpcfx.demo.consumer; + +import io.kimmking.rpcfx.annotation.RpcfxReference; +import io.kimmking.rpcfx.demo.api.User; +import io.kimmking.rpcfx.demo.api.UserService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/1/14 00:35 + */ + +@RestController +public class HelloController { + @RpcfxReference + UserService userService2;// = Rpcfx.createFromRegistry(UserService.class, "localhost:2181", new TagRouter(), new RandomLoadBalancer(), new CuicuiFilter()); + + @GetMapping("/api/hello") + public User invoke() { + return userService2.findById(100); + } + +} diff --git a/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java b/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java new file mode 100644 index 00000000..88e23382 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-consumer/src/main/java/io/kimmking/rpcfx/demo/consumer/RpcfxClientApplication.java @@ -0,0 +1,57 @@ +package io.kimmking.rpcfx.demo.consumer; + +import com.alibaba.fastjson.JSON; +import io.kimmking.rpcfx.annotation.RpcfxReference; +import io.kimmking.rpcfx.demo.api.OrderService; +import io.kimmking.rpcfx.demo.api.UserService; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@ComponentScan({"io.kimmking.rpcfx.consumer","io.kimmking.rpcfx.demo.consumer"}) +public class RpcfxClientApplication { + + public static void main(String[] args) { + + // UserService service = new xxx(); + // service.findById + +// UserService userService = Rpcfx.create(UserService.class, "http://localhost:8080/"); +// User user = userService.findById(1); +// System.out.println("find user id=1 from server: " + user.getName()); +// +// OrderService orderService = Rpcfx.create(OrderService.class, "http://localhost:8080/"); +// Order order = orderService.findOrderById(1992129); +// System.out.println(String.format("find order name=%s, amount=%f",order.getName(),order.getAmount())); + +// UserService userService2 = Rpcfx.createFromRegistry(UserService.class, "localhost:2181", new TagRouter(), new RandomLoadBalancer(), new CuicuiFilter()); +// User user = userService2.findById(1); +// System.out.println(user.getName()); + + SpringApplication.run(RpcfxClientApplication.class, args); + } + + @RpcfxReference + UserService userService; + + @RpcfxReference + OrderService orderService; + + @Bean + public ApplicationRunner runUserService() { + System.out.println(JSON.toJSONString(userService.hashCode())); + return x -> System.out.println(JSON.toJSONString(userService.find(500))); + } + +// @Bean +// public ApplicationRunner runOrderService() { +// return x -> System.out.println(JSON.toJSONString(orderService.findOrderById(11))); +// } + +} + + + diff --git a/07rpc/rpc01/rpcfx-demo-consumer/src/main/resources/application.yml b/07rpc/rpc01/rpcfx-demo-consumer/src/main/resources/application.yml new file mode 100644 index 00000000..8b216a03 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-consumer/src/main/resources/application.yml @@ -0,0 +1,22 @@ +server: + port: 8080 + shutdown: graceful + +spring: + lifecycle: + timeout-per-shutdown-phase: 20s + main: + allow-circular-references: true + task: + execution: + pool: + core-size: 32 + max-size: 128 + +app: + id: app2 + namespace: ns1 + env: sit + mock: false + cache: false + retry: 2 diff --git a/07rpc/rpc01/rpcfx-demo-provider/pom.xml b/07rpc/rpc01/rpcfx-demo-provider/pom.xml new file mode 100644 index 00000000..c0f9b5c9 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-provider/pom.xml @@ -0,0 +1,63 @@ + + + + rpcfx + io.kimmking + 0.0.1-SNAPSHOT + + 4.0.0 + + rpcfx-demo-provider + + + 8 + 8 + + + + + io.kimmking + rpcfx-core + ${project.version} + + + io.kimmking + rpcfx-demo-api + ${project.version} + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/OrderServiceImpl.java b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/OrderServiceImpl.java new file mode 100644 index 00000000..96a4768a --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/OrderServiceImpl.java @@ -0,0 +1,22 @@ +package io.kimmking.rpcfx.demo.provider; + +import io.kimmking.rpcfx.annotation.RpcfxService; +import io.kimmking.rpcfx.demo.api.Order; +import io.kimmking.rpcfx.demo.api.OrderService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@RpcfxService +@Component +public class OrderServiceImpl implements OrderService { + + @Autowired + Environment environment; + + @Override + public Order findOrderById(int id) { + return new Order(id, "KK-" + + environment.getProperty("server.port") + "_Order" + System.currentTimeMillis(), 9.9f); + } +} diff --git a/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java new file mode 100644 index 00000000..ca236d16 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/RpcfxServerApplication.java @@ -0,0 +1,51 @@ +package io.kimmking.rpcfx.demo.provider; + +import com.alibaba.fastjson.JSON; +import io.kimmking.rpcfx.api.RpcfxRequest; +import io.kimmking.rpcfx.api.RpcfxResponse; +import io.kimmking.rpcfx.provider.ProviderBootstrap; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + + +@SpringBootApplication +@RestController +@ComponentScan({"io.kimmking.rpcfx.provider", "io.kimmking.rpcfx.demo.provider"}) +public class RpcfxServerApplication implements CommandLineRunner { + + @Autowired + ProviderBootstrap bootstrap; + + public static void main(String[] args) { + SpringApplication.run(RpcfxServerApplication.class, args); + } + + + @PostMapping("/") + public RpcfxResponse invoke(@RequestBody RpcfxRequest request) { + return bootstrap.getInvoker().invoke(request); + } + + @GetMapping("/api/hello") + public RpcfxResponse invoke() { + RpcfxRequest request = new RpcfxRequest(); + request.setServiceClass("io.kimmking.rpcfx.demo.api.UserService"); + request.setParams(new Object[]{1}); + request.setMethodSign("findById@1_int,"); + return bootstrap.getInvoker().invoke(request); + } + + @Override + public void run(String... args) { + RpcfxResponse response = invoke(); + System.out.println(JSON.toJSONString(response)); + } + +} diff --git a/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/UserServiceImpl.java b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/UserServiceImpl.java new file mode 100644 index 00000000..425fe315 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/UserServiceImpl.java @@ -0,0 +1,32 @@ +package io.kimmking.rpcfx.demo.provider; + +import io.kimmking.rpcfx.annotation.RpcfxService; +import io.kimmking.rpcfx.demo.api.User; +import io.kimmking.rpcfx.demo.api.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +@Component("io.kimmking.rpcfx.demo.api.UserService") +@RpcfxService +public class UserServiceImpl implements UserService { + + @Autowired + Environment environment; + + @Override + public User findById(int id) { + return new User(id, "KK-" + + environment.getProperty("server.port") + "_" + System.currentTimeMillis()); + } + + public User find(int timeout) { + try { + String p = environment.getProperty("server.port"); + if("8081".equals(p)) Thread.sleep(timeout); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + return findById(100); + } +} diff --git a/07rpc/rpc01/rpcfx-demo-provider/src/main/resources/application.yml b/07rpc/rpc01/rpcfx-demo-provider/src/main/resources/application.yml new file mode 100644 index 00000000..9abe8100 --- /dev/null +++ b/07rpc/rpc01/rpcfx-demo-provider/src/main/resources/application.yml @@ -0,0 +1,14 @@ +server: + port: 8083 + shutdown: graceful + +spring: + lifecycle: + timeout-per-shutdown-phase: 20s + main: + allow-circular-references: true + +app: + id: app2 + namespace: ns1 + env: sit \ No newline at end of file diff --git a/07rpc/rpc02/dubbo-demo-api/pom.xml b/07rpc/rpc02/dubbo-demo-api/pom.xml new file mode 100644 index 00000000..7735be34 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-api/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + io.kimmking + dubbo-demo + 0.0.1-SNAPSHOT + + + io.kimmking + dubbo-demo-api + 0.0.1-SNAPSHOT + dubbo-demo-api + + + 1.8 + + + diff --git a/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/Order.java b/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/Order.java new file mode 100644 index 00000000..9f9b1b27 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/Order.java @@ -0,0 +1,40 @@ +package io.kimmking.dubbo.demo.api; + +public class Order implements java.io.Serializable { + + private int id; + + private String name; + + private float amount; + + public Order(int id, String name, float amount) { + this.id = id; + this.name = name; + this.amount = amount; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public float getAmount() { + return amount; + } + + public void setAmount(float amount) { + this.amount = amount; + } +} diff --git a/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/OrderService.java b/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/OrderService.java new file mode 100644 index 00000000..1ff086d7 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/OrderService.java @@ -0,0 +1,7 @@ +package io.kimmking.dubbo.demo.api; + +public interface OrderService { + + Order findOrderById(int id); + +} diff --git a/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/User.java b/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/User.java new file mode 100644 index 00000000..5223bb69 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/User.java @@ -0,0 +1,30 @@ +package io.kimmking.dubbo.demo.api; + +public class User implements java.io.Serializable { + + public User(){} + + public User(int id, String name) { + this.id = id; + this.name = name; + } + + private int id; + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/UserService.java b/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/UserService.java new file mode 100644 index 00000000..a4d26ca4 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-api/src/main/java/io/kimmking/dubbo/demo/api/UserService.java @@ -0,0 +1,7 @@ +package io.kimmking.dubbo.demo.api; + +public interface UserService { + + User findById(int id); + +} diff --git a/07rpc/rpc02/dubbo-demo-consumer/pom.xml b/07rpc/rpc02/dubbo-demo-consumer/pom.xml new file mode 100644 index 00000000..c67eae75 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-consumer/pom.xml @@ -0,0 +1,64 @@ + + + + dubbo-demo + io.kimmking + 0.0.1-SNAPSHOT + + 4.0.0 + + dubbo-demo-consumer + + + 8 + 8 + + + + + io.kimmking + dubbo-demo-api + ${project.version} + + + + org.springframework.boot + spring-boot-starter + + + + + + + + + org.apache.dubbo + dubbo-spring-boot-starter + 2.7.7 + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/07rpc/rpc02/dubbo-demo-consumer/src/main/java/io/kimmking/dubbo/demo/consumer/DubboClientApplication.java b/07rpc/rpc02/dubbo-demo-consumer/src/main/java/io/kimmking/dubbo/demo/consumer/DubboClientApplication.java new file mode 100644 index 00000000..4818df30 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-consumer/src/main/java/io/kimmking/dubbo/demo/consumer/DubboClientApplication.java @@ -0,0 +1,56 @@ +package io.kimmking.dubbo.demo.consumer; + +import io.kimmking.dubbo.demo.api.Order; +import io.kimmking.dubbo.demo.api.OrderService; +import io.kimmking.dubbo.demo.api.User; +import io.kimmking.dubbo.demo.api.UserService; +import org.apache.dubbo.config.annotation.DubboReference; +import org.apache.dubbo.rpc.service.EchoService; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class DubboClientApplication { + + @DubboReference(version = "1.0.0", timeout = 1000) //, url = "dubbo://127.0.0.1:12345") + private UserService userService; + + @DubboReference(version = "1.0.0", timeout = 1000) //, url = "dubbo://127.0.0.1:12345") + private OrderService orderService; + + public static void main(String[] args) { + + SpringApplication.run(DubboClientApplication.class).close(); + + // UserService service = new xxx(); + // service.findById + +// UserService userService = Rpcfx.create(UserService.class, "http://localhost:8080/"); +// User user = userService.findById(1); +// System.out.println("find user id=1 from server: " + user.getName()); +// +// OrderService orderService = Rpcfx.create(OrderService.class, "http://localhost:8080/"); +// Order order = orderService.findOrderById(1992129); +// System.out.println(String.format("find order name=%s, amount=%f",order.getName(),order.getAmount())); + + } + + @Bean + public ApplicationRunner runner() { + return args -> { + + EchoService echoService = (EchoService) userService; + System.out.println(echoService.$echo("hello,user.")); + + for (int i = 0; i < 10000; i++) { + User user = userService.findById(1); + System.out.println("find user id=1 from server: " + user.getName()); + Order order = orderService.findOrderById(1992129); + System.out.println(String.format("find order name=%s, amount=%f",order.getName(),order.getAmount())); + } + }; + } + +} diff --git a/07rpc/rpc02/dubbo-demo-consumer/src/main/resources/application.yml b/07rpc/rpc02/dubbo-demo-consumer/src/main/resources/application.yml new file mode 100644 index 00000000..d900c6dd --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-consumer/src/main/resources/application.yml @@ -0,0 +1,20 @@ + +spring: + application: + name: dubbo-demo-consumer + main: + allow-bean-definition-overriding: true + web-application-type: none +dubbo: + scan: + base-packages: io.kimmking.dubbo.demo.consumer + registry: + address: zookeeper://localhost:2181 + metadata-report: + address: zookeeper://localhost:2181 +logging: + level: + root: debug + org: debug + sun: info + com: info \ No newline at end of file diff --git a/07rpc/rpc02/dubbo-demo-provider/pom.xml b/07rpc/rpc02/dubbo-demo-provider/pom.xml new file mode 100644 index 00000000..e36f904b --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-provider/pom.xml @@ -0,0 +1,64 @@ + + + + dubbo-demo + io.kimmking + 0.0.1-SNAPSHOT + + 4.0.0 + + dubbo-demo-provider + + + 8 + 8 + + + + + io.kimmking + dubbo-demo-api + ${project.version} + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + org.apache.dubbo + dubbo-spring-boot-starter + 2.7.7 + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/DubboServerApplication.java b/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/DubboServerApplication.java new file mode 100644 index 00000000..0b23758d --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/DubboServerApplication.java @@ -0,0 +1,13 @@ +package io.kimmking.dubbo.demo.provider; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DubboServerApplication { + + public static void main(String[] args) { + SpringApplication.run(DubboServerApplication.class, args); + } + +} diff --git a/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/OrderServiceImpl.java b/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/OrderServiceImpl.java new file mode 100644 index 00000000..9f27b588 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/OrderServiceImpl.java @@ -0,0 +1,15 @@ +package io.kimmking.dubbo.demo.provider; + +import io.kimmking.dubbo.demo.api.Order; +import io.kimmking.dubbo.demo.api.OrderService; +import org.apache.dubbo.config.annotation.DubboService; + + +@DubboService(version = "1.0.0", tag = "red", weight = 100) +public class OrderServiceImpl implements OrderService { + + @Override + public Order findOrderById(int id) { + return new Order(id, "Order" + System.currentTimeMillis(), 9.9f); + } +} diff --git a/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/UserServiceImpl.java b/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/UserServiceImpl.java new file mode 100644 index 00000000..425300ae --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-provider/src/main/java/io/kimmking/dubbo/demo/provider/UserServiceImpl.java @@ -0,0 +1,20 @@ +package io.kimmking.dubbo.demo.provider; + +import io.kimmking.dubbo.demo.api.User; +import io.kimmking.dubbo.demo.api.UserService; +import org.apache.dubbo.config.annotation.DubboService; + +@DubboService(version = "1.0.0") +public class UserServiceImpl implements UserService { + + @Override + public User findById(int id) { +// try { +// System.out.println(" ==>" + id); +// Thread.sleep(1010); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } + return new User(id, "KK" + System.currentTimeMillis()); + } +} diff --git a/07rpc/rpc02/dubbo-demo-provider/src/main/resources/application.yml b/07rpc/rpc02/dubbo-demo-provider/src/main/resources/application.yml new file mode 100644 index 00000000..cf479c50 --- /dev/null +++ b/07rpc/rpc02/dubbo-demo-provider/src/main/resources/application.yml @@ -0,0 +1,31 @@ +server: + port: 8088 + +spring: + application: + name: dubbo-demo-provider + + +dubbo: + scan: + base-packages: io.kimmking.dubbo.demo.provider + protocol: + name: dubbo + port: 12345 + payload: 134217728 + registry: + address: zookeeper://localhost:2181 + metadata-report: + address: zookeeper://localhost:2181 + application: + qosEnable: true + qosPort: 22222 + qosAcceptForeignIp: true + qos-enable-compatible: true + qos-host-compatible: localhost + qos-port-compatible: 22222 + qos-accept-foreign-ip-compatible: true + qos-host: localhost +logging: + level: + root: debug \ No newline at end of file diff --git a/07rpc/rpc02/pom.xml b/07rpc/rpc02/pom.xml new file mode 100644 index 00000000..8b97b26b --- /dev/null +++ b/07rpc/rpc02/pom.xml @@ -0,0 +1,98 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.0.RELEASE + + + io.kimmking + dubbo-demo + 0.0.1-SNAPSHOT + dubbo-demo + pom + RPC demo project for Spring Boot + + + dubbo-demo-api + dubbo-demo-consumer + dubbo-demo-provider + + + + 1.8 + 2.3.0.RELEASE + 2.7.7 + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + org.apache.dubbo + dubbo-dependencies-bom + ${dubbo.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.apache.dubbo + dubbo-spring-boot-starter + ${dubbo.version} + + + + + org.apache.dubbo + dubbo-dependencies-zookeeper + ${dubbo.version} + pom + + + org.slf4j + slf4j-log4j12 + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + diff --git a/08cache/README.md b/08cache/README.md new file mode 100644 index 00000000..20e1807e --- /dev/null +++ b/08cache/README.md @@ -0,0 +1,35 @@ +# 第8周作业 + + +## 作业内容 + +> Week08 作业题目: + +###Week08 作业题目 + +1. (选做)分析前面作业设计的表,是否可以做垂直拆分。 +2. (必做)设计对前面的订单表数据进行水平分库分表,拆分 2 个库,每个库 16 张表。并在新结构在演示常见的增删改查操作。代码、sql 和配置文件,上传到 Github。 +3. (选做)模拟 1000 万的订单单表数据,迁移到上面作业 2 的分库分表中。 +4. (选做)重新搭建一套 4 个库各 64 个表的分库分表,将作业 2 中的数据迁移到新分库。 +5. (选做)列举常见的分布式事务,简单分析其使用场景和优缺点。 +6. (必做)基于 hmily TCC 或 ShardingSphere 的 Atomikos XA 实现一个简单的分布式事务应用 demo(二选一),提交到 Github。 +7. (选做)基于 ShardingSphere narayana XA 实现一个简单的分布式事务 demo。 +8. (选做)基于 seata 框架实现 TCC 或 AT 模式的分布式事务 demo。 +9. (选做☆)设计实现一个简单的 XA 分布式事务框架 demo,只需要能管理和调用 2 个 MySQL 的本地事务即可,不需要考虑全局事务的持久化和恢复、高可用等。 +10. (选做☆)设计实现一个 TCC 分布式事务框架的简单 Demo,需要实现事务管理器,不需要实现全局事务的持久化和恢复、高可用等。 +11. (选做☆)设计实现一个 AT 分布式事务框架的简单 Demo,仅需要支持根据主键 id 进行的单个删改操作的 SQL 或插入操作的事务。 + +### 作业提交规范: + +1. 作业不要打包 ; +2. 同学们写在 md 文件里,而不要发 Word, Excel , PDF 等 ; +3. 代码类作业需提交完整 Java 代码,不能是片段; +4. 作业按课时分目录,仅上传作业相关,笔记分开记录; +5. 画图类作业提交可直接打开的图片或 md,手画的图手机拍照上传后太大,难以查看,推荐画图(推荐 PPT、Keynote); +6. 提交记录最好要标明明确的含义(比如第几题作业)。 + + +## 操作步骤 + + +### 第八周-作业1. (选做) diff --git a/08cache/cache/pom.xml b/08cache/cache/pom.xml new file mode 100644 index 00000000..88c9a37e --- /dev/null +++ b/08cache/cache/pom.xml @@ -0,0 +1,96 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.0.9.RELEASE + + + io.kimmking.08cache + cache + 0.0.1-SNAPSHOT + cache + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-jdbc + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.1.4 + + + mysql + mysql-connector-java + 5.1.47 + + + org.projectlombok + lombok + + + org.springframework.boot + spring-boot-starter-cache + + + org.springframework.boot + spring-boot-starter-data-redis + + + io.lettuce + lettuce-core + + + org.apache.commons + commons-pool2 + + + net.sf.ehcache + ehcache + 2.8.3 + + + org.mybatis + mybatis-ehcache + 1.0.0 + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/08cache/cache/src/main/java/io/kimmking/cache/CacheApplication.java b/08cache/cache/src/main/java/io/kimmking/cache/CacheApplication.java new file mode 100644 index 00000000..9ed3fa75 --- /dev/null +++ b/08cache/cache/src/main/java/io/kimmking/cache/CacheApplication.java @@ -0,0 +1,77 @@ +package io.kimmking.cache; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; + +import javax.sql.DataSource; +import java.sql.*; + +@SpringBootApplication(scanBasePackages = "io.kimmking.cache") +@MapperScan("io.kimmking.cache.mapper") +@EnableCaching +public class CacheApplication { + + public static void main(String[] args) { + SpringApplication.run(CacheApplication.class, args); + } + + @Bean + @Autowired + public ApplicationRunner runner(DataSource dataSource) { + return (x) -> { + // testInsert(dataSource); + // 测试 insert into on duplicate key update时如果没有执行更新条件,则回填id为空 + }; + } + + private void testInsert(DataSource dataSource) throws SQLException { + Connection connection = dataSource.getConnection(); + + System.out.println(" =====> test Insert ..."); + PreparedStatement statement = connection.prepareStatement("insert into user(name,age) values(?,20)", Statement.RETURN_GENERATED_KEYS); + //boolean b = statement.execute("insert into user(name,age) values('K8',20)"); + //System.out.println("insert:" +b); + // You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate(), Statement.executeLargeUpdate() or Connection.prepareStatement(). + statement.setString(1, "K8"); + boolean b = statement.execute(); +// System.out.println("insert:" +b); + ResultSet rs = statement.getGeneratedKeys(); + System.out.println("insert getGeneratedKeys => " + (rs.next() ? rs.getInt(1): "NULL")); + rs.close(); + statement.close(); + + + System.out.println(" =====> test Duplicate ..."); + statement = connection.prepareStatement("insert into user(name,age) values(?,20) on duplicate key update age=age+1", Statement.RETURN_GENERATED_KEYS); + statement.setString(1, "K8"); + b = statement.execute(); +// System.out.println("insert:" +b); + rs = statement.getGeneratedKeys(); + System.out.println("insert getGeneratedKeys => " + (rs.next() ? rs.getInt(1): "NULL")); + rs.close(); + //statement.execute("delete from user where name='K8'"); + statement.close(); + + + System.out.println(" =====> test Duplicate No Update ..."); + statement = connection.prepareStatement("insert into user(name,age) values(?,20) on duplicate key update age=age", Statement.RETURN_GENERATED_KEYS); + statement.setString(1, "K8"); + b = statement.execute(); +// System.out.println("insert:" +b); + rs = statement.getGeneratedKeys(); + System.out.println("insert getGeneratedKeys => " + (rs.next() ? rs.getInt(1): "NULL")); + rs.close(); + //statement.execute("delete from user where name='K8'"); + statement.close(); + + + connection.close(); + } + + +} diff --git a/08cache/cache/src/main/java/io/kimmking/cache/CacheConfig.java b/08cache/cache/src/main/java/io/kimmking/cache/CacheConfig.java new file mode 100644 index 00000000..24ad27fe --- /dev/null +++ b/08cache/cache/src/main/java/io/kimmking/cache/CacheConfig.java @@ -0,0 +1,83 @@ +//package io.kimmking.cache; +// +//import org.springframework.cache.CacheManager; +//import org.springframework.cache.annotation.CachingConfigurerSupport; +//import org.springframework.cache.interceptor.*; +//import org.springframework.context.annotation.Bean; +//import org.springframework.context.annotation.Configuration; +//import org.springframework.data.redis.cache.RedisCacheConfiguration; +//import org.springframework.data.redis.cache.RedisCacheManager; +//import org.springframework.data.redis.connection.RedisConnectionFactory; +//import org.springframework.data.redis.core.RedisTemplate; +//import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +//import org.springframework.data.redis.serializer.RedisSerializationContext; +//import org.springframework.data.redis.serializer.StringRedisSerializer; +// +//import javax.annotation.Resource; +// +//import static org.springframework.data.redis.cache.RedisCacheConfiguration.defaultCacheConfig; +// +//@Configuration +//public class CacheConfig extends CachingConfigurerSupport { +// +// @Resource +// private RedisConnectionFactory factory; +// +// /** +// * 自定义生成redis-key +// * +// * @return +// */ +// @Override +// @Bean +// public KeyGenerator keyGenerator() { +// return (o, method, objects) -> { +// StringBuilder sb = new StringBuilder(); +// sb.append(o.getClass().getName()).append("."); +// sb.append(method.getName()).append("."); +// for (Object obj : objects) { +// sb.append(obj.toString()).append("."); +// } +// //System.out.println("keyGenerator=" + sb.toString()); +// return sb.toString(); +// }; +// } +// +// @Bean +// public RedisTemplate redisTemplate() { +// RedisTemplate redisTemplate = new RedisTemplate<>(); +// redisTemplate.setConnectionFactory(factory); +// +// GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); +// +// redisTemplate.setKeySerializer(genericJackson2JsonRedisSerializer); +// redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer); +// +// redisTemplate.setHashKeySerializer(new StringRedisSerializer()); +// redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer); +// return redisTemplate; +// } +// +// @Bean +// @Override +// public CacheResolver cacheResolver() { +// return new SimpleCacheResolver(cacheManager()); +// } +// +// @Bean +// @Override +// public CacheErrorHandler errorHandler() { +// // 用于捕获从Cache中进行CRUD时的异常的回调处理器。 +// return new SimpleCacheErrorHandler(); +// } +// +// @Bean +// @Override +// public CacheManager cacheManager() { +// RedisCacheConfiguration cacheConfiguration = +// defaultCacheConfig() +// .disableCachingNullValues() +// .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); +// return RedisCacheManager.builder(factory).cacheDefaults(cacheConfiguration).build(); +// } +//} \ No newline at end of file diff --git a/08cache/cache/src/main/java/io/kimmking/cache/controller/UserController.java b/08cache/cache/src/main/java/io/kimmking/cache/controller/UserController.java new file mode 100644 index 00000000..b19d9553 --- /dev/null +++ b/08cache/cache/src/main/java/io/kimmking/cache/controller/UserController.java @@ -0,0 +1,31 @@ +package io.kimmking.cache.controller; + +import io.kimmking.cache.entity.User; +import io.kimmking.cache.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@EnableAutoConfiguration +public class UserController { + + @Autowired + UserService userService; + + @RequestMapping("/user/find") + User find(Integer id) { + return userService.find(id); + //return new User(1,"KK", 28); + } + + @RequestMapping("/user/list") + List list() { + return userService.list(); +// return Arrays.asList(new User(1,"KK", 28), +// new User(2,"CC", 18)); + } +} \ No newline at end of file diff --git a/08cache/cache/src/main/java/io/kimmking/cache/entity/User.java b/08cache/cache/src/main/java/io/kimmking/cache/entity/User.java new file mode 100644 index 00000000..4f67ce12 --- /dev/null +++ b/08cache/cache/src/main/java/io/kimmking/cache/entity/User.java @@ -0,0 +1,16 @@ +package io.kimmking.cache.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class User implements Serializable { + private Integer id; + private String name; + private Integer age; +} \ No newline at end of file diff --git a/08cache/cache/src/main/java/io/kimmking/cache/mapper/UserMapper.java b/08cache/cache/src/main/java/io/kimmking/cache/mapper/UserMapper.java new file mode 100644 index 00000000..0c5d7608 --- /dev/null +++ b/08cache/cache/src/main/java/io/kimmking/cache/mapper/UserMapper.java @@ -0,0 +1,15 @@ +package io.kimmking.cache.mapper; + +import io.kimmking.cache.entity.User; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +@Mapper +public interface UserMapper { + + User find(int id); + + List list(); + +} diff --git a/08cache/cache/src/main/java/io/kimmking/cache/service/UserService.java b/08cache/cache/src/main/java/io/kimmking/cache/service/UserService.java new file mode 100644 index 00000000..22c3987b --- /dev/null +++ b/08cache/cache/src/main/java/io/kimmking/cache/service/UserService.java @@ -0,0 +1,15 @@ +package io.kimmking.cache.service; + +import io.kimmking.cache.entity.User; +import org.springframework.cache.annotation.CacheConfig; + +import java.util.List; + +@CacheConfig(cacheNames = "users") +public interface UserService { + + User find(int id); + + List list(); + +} diff --git a/08cache/cache/src/main/java/io/kimmking/cache/service/UserServiceImpl.java b/08cache/cache/src/main/java/io/kimmking/cache/service/UserServiceImpl.java new file mode 100644 index 00000000..4a1f5acf --- /dev/null +++ b/08cache/cache/src/main/java/io/kimmking/cache/service/UserServiceImpl.java @@ -0,0 +1,30 @@ +package io.kimmking.cache.service; + +import io.kimmking.cache.entity.User; +import io.kimmking.cache.mapper.UserMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class UserServiceImpl implements UserService { + + @Autowired + UserMapper userMapper; //DAO // Repository + + // 开启spring cache + @Cacheable(key="#id",value="userCache") + public User find(int id) { + System.out.println(" ==> find " + id); + return userMapper.find(id); + } + + // 开启spring cache + @Cacheable //(key="methodName",value="userCache") + public List list(){ + return userMapper.list(); + } + +} diff --git a/08cache/cache/src/main/resources/application.yml b/08cache/cache/src/main/resources/application.yml new file mode 100644 index 00000000..835c04a4 --- /dev/null +++ b/08cache/cache/src/main/resources/application.yml @@ -0,0 +1,45 @@ +server: + port: 8080 + +spring: + datasource: + username: root + password: + url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC + driver-class-name: com.mysql.jdbc.Driver + cache: + type: simple + +# type: redis +# redis: +# timeout: 3000ms +# database: 0 +# cluster: +# nodes: +# - 127.0.0.1:7000 +# - 127.0.0.1:7001 +# - 127.0.0.1:7002 +# - 127.0.0.1:7003 +# - 127.0.0.1:7004 +# - 127.0.0.1:7005 +# max-redirects: 3 # 获取失败 最大重定向次数 +# lettuce: +# pool: +# max-active: 1000 #连接池最大连接数(使用负值表示没有限制) +# max-idle: 10 # 连接池中的最大空闲连接 +# min-idle: 5 # 连接池中的最小空闲连接 +# max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) + +# type: ehcache +# ehcache: +# config: ehcache.xml + +mybatis: + mapper-locations: classpath:mapper/*Mapper.xml + type-aliases-package: io.kimmking.cache.entity + +logging: + level: + io: + kimmking: + cache : info diff --git a/08cache/cache/src/main/resources/ehcache.xml b/08cache/cache/src/main/resources/ehcache.xml new file mode 100644 index 00000000..f1a3efc5 --- /dev/null +++ b/08cache/cache/src/main/resources/ehcache.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/08cache/cache/src/main/resources/mapper/UserMapper.xml b/08cache/cache/src/main/resources/mapper/UserMapper.xml new file mode 100644 index 00000000..0d2e8ae5 --- /dev/null +++ b/08cache/cache/src/main/resources/mapper/UserMapper.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/08cache/cache/test.sql b/08cache/cache/test.sql new file mode 100644 index 00000000..76e9b60b --- /dev/null +++ b/08cache/cache/test.sql @@ -0,0 +1,54 @@ +-- MySQL dump 10.13 Distrib 5.7.32, for osx10.15 (x86_64) +-- +-- Host: localhost Database: test +-- ------------------------------------------------------ +-- Server version 5.7.32 + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table `user` +-- + +DROP TABLE IF EXISTS `user`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `user` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(32) NOT NULL, + `age` int(11) NOT NULL, + PRIMARY KEY (`id`), + constraint user_name_uindex + unique (name) +) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `user` +-- + +LOCK TABLES `user` WRITE; +/*!40000 ALTER TABLE `user` DISABLE KEYS */; +INSERT INTO `user` VALUES (1,'K1',21),(2,'K2',22),(3,'K3',23),(4,'K4',24),(5,'K5',25),(6,'K6',26); +/*!40000 ALTER TABLE `user` ENABLE KEYS */; +UNLOCK TABLES; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2020-12-28 15:46:14 diff --git a/08cache/ha/conf/cluster.txt b/08cache/ha/conf/cluster.txt new file mode 100644 index 00000000..6e6d2b6e --- /dev/null +++ b/08cache/ha/conf/cluster.txt @@ -0,0 +1,61 @@ +redis-server --port $PORT --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes + +redis-server --port 7004 --cluster-enabled yes --cluster-config-file nodes-7004.conf --cluster-node-timeout 3000 --appendonly yes --appendfilename appendonly-7004.aof --dbfilename dump-7004.rdb --logfile 7004.log --daemonize yes + + +redis-cli --cluster create $HOSTS --cluster-replicas $REPLICAS + + +redis-cli -p $PORT shutdown nosave + +redis-cli -p $PORT cluster nodes | head -30 + +redis-cli -p $PORT $2 $3 $4 $5 $6 $7 $8 $9 + + + +redis-cli -p 7000 cluster info + + +测试存取值,客户端连接集群redis-cli需要带上 -c ,redis-cli -c -p 端口号 + + + + +Redis 集群使用命令 CLUSTER SETSLOT 将一个槽中的所有 keys 从一个节点迁移至另一个节点。稍后介绍在其他命令配合下迁移是如何操作的。我们假定要操作的哈希槽的当前所有者为源节点,将要迁移至的节点为目的节点。 + +使用命令 CLUSTER SETSLOT IMPORTING 将目的节点槽置为 importing 状态。 +使用命令 CLUSTER SETSLOT MIGRATING 将源节点槽置为 migrating 状态。 +使用命令 CLUSTER GETKEYSINSLOT 从源节点获取所有 keys,并使用命令 MIGRATE 将它们导入到目的节点。 +在源节点活目的节点执行命令 CLUSTER SETSLOT NODE 。 +注意: + +步骤 1 和步骤 2 的顺序很重要。我们希望在源节点配置了重定向之后,目的节点已经可以接受 ASK 重定向。 +步骤 4 中,技术上讲,在重哈希不涉及的节点上执行 SETSLOT 是非必须的,因为新配置最终会分发到所有节点上,但是,这样操作也有好处,会快速停止节点中对已迁移的哈希槽的错误指向,降低命令重定向的发生。 + +https://www.knowledgedict.com/tutorial/redis-command-cluster-setslot.html + + +k2: +CLUSTER SETSLOT 449 importing c025732be5696d0446aad624dba3c31e13750341 +CLUSTER SETSLOT 449 migrating 2cf0fb991563bdedea2704a3cd43e5c211ea16b9 +CLUSTER COUNTKEYSINSLOT 449 +CLUSTER GETKEYSINSLOT 449 10 +MIGRATE 127.0.0.1 7004 k2 0 100 COPY +CLUSTER SETSLOT 449 node 2cf0fb991563bdedea2704a3cd43e5c211ea16b9 // 这句没有执行完之前,获取key会死循环 + +k6: +CLUSTER SETSLOT 325 node 2cf0fb991563bdedea2704a3cd43e5c211ea16b9 + + + +几个问题: +1、添加新节点后slot为空,需要从其他节点迁移slot,然后迁移key,一个个的处理,处理完后通知集群这个slot在新节点上方可使用,中间访问会重定向死循环。 +2、分片+主从? +3、 + + +解决办法:使用批量操作脚本来做。 +提供 reshard 一键重新分配数据的功能。 + + diff --git a/08cache/ha/conf/redis6379.conf b/08cache/ha/conf/redis6379.conf new file mode 100644 index 00000000..e33949fa --- /dev/null +++ b/08cache/ha/conf/redis6379.conf @@ -0,0 +1,1869 @@ +# Redis configuration file example. +# +# Note that in order to read the configuration file, Redis must be +# started with the file path as first argument: +# +# ./redis-server /path/to/redis.conf + +# Note on units: when memory size is needed, it is possible to specify +# it in the usual form of 1k 5GB 4M and so forth: +# +# 1k => 1000 bytes +# 1kb => 1024 bytes +# 1m => 1000000 bytes +# 1mb => 1024*1024 bytes +# 1g => 1000000000 bytes +# 1gb => 1024*1024*1024 bytes +# +# units are case insensitive so 1GB 1Gb 1gB are all the same. + +################################## INCLUDES ################################### + +# Include one or more other config files here. This is useful if you +# have a standard template that goes to all Redis servers but also need +# to customize a few per-server settings. Include files can include +# other files, so use this wisely. +# +# Note that option "include" won't be rewritten by command "CONFIG REWRITE" +# from admin or Redis Sentinel. Since Redis always uses the last processed +# line as value of a configuration directive, you'd better put includes +# at the beginning of this file to avoid overwriting config change at runtime. +# +# If instead you are interested in using includes to override configuration +# options, it is better to use include as the last line. +# +# include /path/to/local.conf +# include /path/to/other.conf + +################################## MODULES ##################################### + +# Load modules at startup. If the server is not able to load modules +# it will abort. It is possible to use multiple loadmodule directives. +# +# loadmodule /path/to/my_module.so +# loadmodule /path/to/other_module.so + +################################## NETWORK ##################################### + +# By default, if no "bind" configuration directive is specified, Redis listens +# for connections from all available network interfaces on the host machine. +# It is possible to listen to just one or multiple selected interfaces using +# the "bind" configuration directive, followed by one or more IP addresses. +# +# Examples: +# +# bind 192.168.1.100 10.0.0.1 +# bind 127.0.0.1 ::1 +# +# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the +# internet, binding to all the interfaces is dangerous and will expose the +# instance to everybody on the internet. So by default we uncomment the +# following bind directive, that will force Redis to listen only on the +# IPv4 loopback interface address (this means Redis will only be able to +# accept client connections from the same host that it is running on). +# +# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES +# JUST COMMENT OUT THE FOLLOWING LINE. +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +bind 127.0.0.1 ::1 + +# Protected mode is a layer of security protection, in order to avoid that +# Redis instances left open on the internet are accessed and exploited. +# +# When protected mode is on and if: +# +# 1) The server is not binding explicitly to a set of addresses using the +# "bind" directive. +# 2) No password is configured. +# +# The server only accepts connections from clients connecting from the +# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain +# sockets. +# +# By default protected mode is enabled. You should disable it only if +# you are sure you want clients from other hosts to connect to Redis +# even if no authentication is configured, nor a specific set of interfaces +# are explicitly listed using the "bind" directive. +protected-mode yes + +# Accept connections on the specified port, default is 6379 (IANA #815344). +# If port 0 is specified Redis will not listen on a TCP socket. +port 6379 + +# TCP listen() backlog. +# +# In high requests-per-second environments you need a high backlog in order +# to avoid slow clients connection issues. Note that the Linux kernel +# will silently truncate it to the value of /proc/sys/net/core/somaxconn so +# make sure to raise both the value of somaxconn and tcp_max_syn_backlog +# in order to get the desired effect. +tcp-backlog 511 + +# Unix socket. +# +# Specify the path for the Unix socket that will be used to listen for +# incoming connections. There is no default, so Redis will not listen +# on a unix socket when not specified. +# +# unixsocket /tmp/redis.sock +# unixsocketperm 700 + +# Close the connection after a client is idle for N seconds (0 to disable) +timeout 0 + +# TCP keepalive. +# +# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence +# of communication. This is useful for two reasons: +# +# 1) Detect dead peers. +# 2) Force network equipment in the middle to consider the connection to be +# alive. +# +# On Linux, the specified value (in seconds) is the period used to send ACKs. +# Note that to close the connection the double of the time is needed. +# On other kernels the period depends on the kernel configuration. +# +# A reasonable value for this option is 300 seconds, which is the new +# Redis default starting with Redis 3.2.1. +tcp-keepalive 300 + +################################# TLS/SSL ##################################### + +# By default, TLS/SSL is disabled. To enable it, the "tls-port" configuration +# directive can be used to define TLS-listening ports. To enable TLS on the +# default port, use: +# +# port 0 +# tls-port 6379 + +# Configure a X.509 certificate and private key to use for authenticating the +# server to connected clients, masters or cluster peers. These files should be +# PEM formatted. +# +# tls-cert-file redis.crt +# tls-key-file redis.key + +# Configure a DH parameters file to enable Diffie-Hellman (DH) key exchange: +# +# tls-dh-params-file redis.dh + +# Configure a CA certificate(s) bundle or directory to authenticate TLS/SSL +# clients and peers. Redis requires an explicit configuration of at least one +# of these, and will not implicitly use the system wide configuration. +# +# tls-ca-cert-file ca.crt +# tls-ca-cert-dir /etc/ssl/certs + +# By default, clients (including replica servers) on a TLS port are required +# to authenticate using valid client side certificates. +# +# If "no" is specified, client certificates are not required and not accepted. +# If "optional" is specified, client certificates are accepted and must be +# valid if provided, but are not required. +# +# tls-auth-clients no +# tls-auth-clients optional + +# By default, a Redis replica does not attempt to establish a TLS connection +# with its master. +# +# Use the following directive to enable TLS on replication links. +# +# tls-replication yes + +# By default, the Redis Cluster bus uses a plain TCP connection. To enable +# TLS for the bus protocol, use the following directive: +# +# tls-cluster yes + +# Explicitly specify TLS versions to support. Allowed values are case insensitive +# and include "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" (OpenSSL >= 1.1.1) or +# any combination. To enable only TLSv1.2 and TLSv1.3, use: +# +# tls-protocols "TLSv1.2 TLSv1.3" + +# Configure allowed ciphers. See the ciphers(1ssl) manpage for more information +# about the syntax of this string. +# +# Note: this configuration applies only to <= TLSv1.2. +# +# tls-ciphers DEFAULT:!MEDIUM + +# Configure allowed TLSv1.3 ciphersuites. See the ciphers(1ssl) manpage for more +# information about the syntax of this string, and specifically for TLSv1.3 +# ciphersuites. +# +# tls-ciphersuites TLS_CHACHA20_POLY1305_SHA256 + +# When choosing a cipher, use the server's preference instead of the client +# preference. By default, the server follows the client's preference. +# +# tls-prefer-server-ciphers yes + +# By default, TLS session caching is enabled to allow faster and less expensive +# reconnections by clients that support it. Use the following directive to disable +# caching. +# +# tls-session-caching no + +# Change the default number of TLS sessions cached. A zero value sets the cache +# to unlimited size. The default size is 20480. +# +# tls-session-cache-size 5000 + +# Change the default timeout of cached TLS sessions. The default timeout is 300 +# seconds. +# +# tls-session-cache-timeout 60 + +################################# GENERAL ##################################### + +# By default Redis does not run as a daemon. Use 'yes' if you need it. +# Note that Redis will write a pid file in /usr/local/var/run/redis.pid when daemonized. +daemonize no + +# If you run Redis from upstart or systemd, Redis can interact with your +# supervision tree. Options: +# supervised no - no supervision interaction +# supervised upstart - signal upstart by putting Redis into SIGSTOP mode +# requires "expect stop" in your upstart job config +# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET +# supervised auto - detect upstart or systemd method based on +# UPSTART_JOB or NOTIFY_SOCKET environment variables +# Note: these supervision methods only signal "process is ready." +# They do not enable continuous pings back to your supervisor. +supervised no + +# If a pid file is specified, Redis writes it where specified at startup +# and removes it at exit. +# +# When the server runs non daemonized, no pid file is created if none is +# specified in the configuration. When the server is daemonized, the pid file +# is used even if not specified, defaulting to "/usr/local/var/run/redis.pid". +# +# Creating a pid file is best effort: if Redis is not able to create it +# nothing bad happens, the server will start and run normally. +pidfile "/var/run/redis_6379.pid" + +# Specify the server verbosity level. +# This can be one of: +# debug (a lot of information, useful for development/testing) +# verbose (many rarely useful info, but not a mess like the debug level) +# notice (moderately verbose, what you want in production probably) +# warning (only very important / critical messages are logged) +loglevel notice + +# Specify the log file name. Also the empty string can be used to force +# Redis to log on the standard output. Note that if you use standard +# output for logging but daemonize, logs will be sent to /dev/null +logfile "" + +# To enable logging to the system logger, just set 'syslog-enabled' to yes, +# and optionally update the other syslog parameters to suit your needs. +# syslog-enabled no + +# Specify the syslog identity. +# syslog-ident redis + +# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7. +# syslog-facility local0 + +# Set the number of databases. The default database is DB 0, you can select +# a different one on a per-connection basis using SELECT where +# dbid is a number between 0 and 'databases'-1 +databases 16 + +# By default Redis shows an ASCII art logo only when started to log to the +# standard output and if the standard output is a TTY. Basically this means +# that normally a logo is displayed only in interactive sessions. +# +# However it is possible to force the pre-4.0 behavior and always show a +# ASCII art logo in startup logs by setting the following option to yes. +always-show-logo yes + +################################ SNAPSHOTTING ################################ +# +# Save the DB on disk: +# +# save +# +# Will save the DB if both the given number of seconds and the given +# number of write operations against the DB occurred. +# +# In the example below the behavior will be to save: +# after 900 sec (15 min) if at least 1 key changed +# after 300 sec (5 min) if at least 10 keys changed +# after 60 sec if at least 10000 keys changed +# +# Note: you can disable saving completely by commenting out all "save" lines. +# +# It is also possible to remove all the previously configured save +# points by adding a save directive with a single empty string argument +# like in the following example: +# +# save "" + +save 900 1 +save 300 10 +save 60 10000 + +# By default Redis will stop accepting writes if RDB snapshots are enabled +# (at least one save point) and the latest background save failed. +# This will make the user aware (in a hard way) that data is not persisting +# on disk properly, otherwise chances are that no one will notice and some +# disaster will happen. +# +# If the background saving process will start working again Redis will +# automatically allow writes again. +# +# However if you have setup your proper monitoring of the Redis server +# and persistence, you may want to disable this feature so that Redis will +# continue to work as usual even if there are problems with disk, +# permissions, and so forth. +stop-writes-on-bgsave-error yes + +# Compress string objects using LZF when dump .rdb databases? +# By default compression is enabled as it's almost always a win. +# If you want to save some CPU in the saving child set it to 'no' but +# the dataset will likely be bigger if you have compressible values or keys. +rdbcompression yes + +# Since version 5 of RDB a CRC64 checksum is placed at the end of the file. +# This makes the format more resistant to corruption but there is a performance +# hit to pay (around 10%) when saving and loading RDB files, so you can disable it +# for maximum performances. +# +# RDB files created with checksum disabled have a checksum of zero that will +# tell the loading code to skip the check. +rdbchecksum yes + +# The filename where to dump the DB +dbfilename "dump.rdb" + +# Remove RDB files used by replication in instances without persistence +# enabled. By default this option is disabled, however there are environments +# where for regulations or other security concerns, RDB files persisted on +# disk by masters in order to feed replicas, or stored on disk by replicas +# in order to load them for the initial synchronization, should be deleted +# ASAP. Note that this option ONLY WORKS in instances that have both AOF +# and RDB persistence disabled, otherwise is completely ignored. +# +# An alternative (and sometimes better) way to obtain the same effect is +# to use diskless replication on both master and replicas instances. However +# in the case of replicas, diskless is not always an option. +rdb-del-sync-files no + +# The working directory. +# +# The DB will be written inside this directory, with the filename specified +# above using the 'dbfilename' configuration directive. +# +# The Append Only File will also be created inside this directory. +# +# Note that you must specify a directory here, not a file name. +dir "/Users/kimmking/logs/redis0" + +################################# REPLICATION ################################# + +# Master-Replica replication. Use replicaof to make a Redis instance a copy of +# another Redis server. A few things to understand ASAP about Redis replication. +# +# +------------------+ +---------------+ +# | Master | ---> | Replica | +# | (receive writes) | | (exact copy) | +# +------------------+ +---------------+ +# +# 1) Redis replication is asynchronous, but you can configure a master to +# stop accepting writes if it appears to be not connected with at least +# a given number of replicas. +# 2) Redis replicas are able to perform a partial resynchronization with the +# master if the replication link is lost for a relatively small amount of +# time. You may want to configure the replication backlog size (see the next +# sections of this file) with a sensible value depending on your needs. +# 3) Replication is automatic and does not need user intervention. After a +# network partition replicas automatically try to reconnect to masters +# and resynchronize with them. +# +# replicaof + +# If the master is password protected (using the "requirepass" configuration +# directive below) it is possible to tell the replica to authenticate before +# starting the replication synchronization process, otherwise the master will +# refuse the replica request. +# +# masterauth +# +# However this is not enough if you are using Redis ACLs (for Redis version +# 6 or greater), and the default user is not capable of running the PSYNC +# command and/or other commands needed for replication. In this case it's +# better to configure a special user to use with replication, and specify the +# masteruser configuration as such: +# +# masteruser +# +# When masteruser is specified, the replica will authenticate against its +# master using the new AUTH form: AUTH . + +# When a replica loses its connection with the master, or when the replication +# is still in progress, the replica can act in two different ways: +# +# 1) if replica-serve-stale-data is set to 'yes' (the default) the replica will +# still reply to client requests, possibly with out of date data, or the +# data set may just be empty if this is the first synchronization. +# +# 2) If replica-serve-stale-data is set to 'no' the replica will reply with +# an error "SYNC with master in progress" to all commands except: +# INFO, REPLICAOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG, SUBSCRIBE, +# UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB, COMMAND, POST, +# HOST and LATENCY. +# +replica-serve-stale-data yes + +# You can configure a replica instance to accept writes or not. Writing against +# a replica instance may be useful to store some ephemeral data (because data +# written on a replica will be easily deleted after resync with the master) but +# may also cause problems if clients are writing to it because of a +# misconfiguration. +# +# Since Redis 2.6 by default replicas are read-only. +# +# Note: read only replicas are not designed to be exposed to untrusted clients +# on the internet. It's just a protection layer against misuse of the instance. +# Still a read only replica exports by default all the administrative commands +# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve +# security of read only replicas using 'rename-command' to shadow all the +# administrative / dangerous commands. +replica-read-only yes + +# Replication SYNC strategy: disk or socket. +# +# New replicas and reconnecting replicas that are not able to continue the +# replication process just receiving differences, need to do what is called a +# "full synchronization". An RDB file is transmitted from the master to the +# replicas. +# +# The transmission can happen in two different ways: +# +# 1) Disk-backed: The Redis master creates a new process that writes the RDB +# file on disk. Later the file is transferred by the parent +# process to the replicas incrementally. +# 2) Diskless: The Redis master creates a new process that directly writes the +# RDB file to replica sockets, without touching the disk at all. +# +# With disk-backed replication, while the RDB file is generated, more replicas +# can be queued and served with the RDB file as soon as the current child +# producing the RDB file finishes its work. With diskless replication instead +# once the transfer starts, new replicas arriving will be queued and a new +# transfer will start when the current one terminates. +# +# When diskless replication is used, the master waits a configurable amount of +# time (in seconds) before starting the transfer in the hope that multiple +# replicas will arrive and the transfer can be parallelized. +# +# With slow disks and fast (large bandwidth) networks, diskless replication +# works better. +repl-diskless-sync no + +# When diskless replication is enabled, it is possible to configure the delay +# the server waits in order to spawn the child that transfers the RDB via socket +# to the replicas. +# +# This is important since once the transfer starts, it is not possible to serve +# new replicas arriving, that will be queued for the next RDB transfer, so the +# server waits a delay in order to let more replicas arrive. +# +# The delay is specified in seconds, and by default is 5 seconds. To disable +# it entirely just set it to 0 seconds and the transfer will start ASAP. +repl-diskless-sync-delay 5 + +# ----------------------------------------------------------------------------- +# WARNING: RDB diskless load is experimental. Since in this setup the replica +# does not immediately store an RDB on disk, it may cause data loss during +# failovers. RDB diskless load + Redis modules not handling I/O reads may also +# cause Redis to abort in case of I/O errors during the initial synchronization +# stage with the master. Use only if your do what you are doing. +# ----------------------------------------------------------------------------- +# +# Replica can load the RDB it reads from the replication link directly from the +# socket, or store the RDB to a file and read that file after it was completely +# received from the master. +# +# In many cases the disk is slower than the network, and storing and loading +# the RDB file may increase replication time (and even increase the master's +# Copy on Write memory and salve buffers). +# However, parsing the RDB file directly from the socket may mean that we have +# to flush the contents of the current database before the full rdb was +# received. For this reason we have the following options: +# +# "disabled" - Don't use diskless load (store the rdb file to the disk first) +# "on-empty-db" - Use diskless load only when it is completely safe. +# "swapdb" - Keep a copy of the current db contents in RAM while parsing +# the data directly from the socket. note that this requires +# sufficient memory, if you don't have it, you risk an OOM kill. +repl-diskless-load disabled + +# Replicas send PINGs to server in a predefined interval. It's possible to +# change this interval with the repl_ping_replica_period option. The default +# value is 10 seconds. +# +# repl-ping-replica-period 10 + +# The following option sets the replication timeout for: +# +# 1) Bulk transfer I/O during SYNC, from the point of view of replica. +# 2) Master timeout from the point of view of replicas (data, pings). +# 3) Replica timeout from the point of view of masters (REPLCONF ACK pings). +# +# It is important to make sure that this value is greater than the value +# specified for repl-ping-replica-period otherwise a timeout will be detected +# every time there is low traffic between the master and the replica. The default +# value is 60 seconds. +# +# repl-timeout 60 + +# Disable TCP_NODELAY on the replica socket after SYNC? +# +# If you select "yes" Redis will use a smaller number of TCP packets and +# less bandwidth to send data to replicas. But this can add a delay for +# the data to appear on the replica side, up to 40 milliseconds with +# Linux kernels using a default configuration. +# +# If you select "no" the delay for data to appear on the replica side will +# be reduced but more bandwidth will be used for replication. +# +# By default we optimize for low latency, but in very high traffic conditions +# or when the master and replicas are many hops away, turning this to "yes" may +# be a good idea. +repl-disable-tcp-nodelay no + +# Set the replication backlog size. The backlog is a buffer that accumulates +# replica data when replicas are disconnected for some time, so that when a +# replica wants to reconnect again, often a full resync is not needed, but a +# partial resync is enough, just passing the portion of data the replica +# missed while disconnected. +# +# The bigger the replication backlog, the longer the replica can endure the +# disconnect and later be able to perform a partial resynchronization. +# +# The backlog is only allocated if there is at least one replica connected. +# +# repl-backlog-size 1mb + +# After a master has no connected replicas for some time, the backlog will be +# freed. The following option configures the amount of seconds that need to +# elapse, starting from the time the last replica disconnected, for the backlog +# buffer to be freed. +# +# Note that replicas never free the backlog for timeout, since they may be +# promoted to masters later, and should be able to correctly "partially +# resynchronize" with other replicas: hence they should always accumulate backlog. +# +# A value of 0 means to never release the backlog. +# +# repl-backlog-ttl 3600 + +# The replica priority is an integer number published by Redis in the INFO +# output. It is used by Redis Sentinel in order to select a replica to promote +# into a master if the master is no longer working correctly. +# +# A replica with a low priority number is considered better for promotion, so +# for instance if there are three replicas with priority 10, 100, 25 Sentinel +# will pick the one with priority 10, that is the lowest. +# +# However a special priority of 0 marks the replica as not able to perform the +# role of master, so a replica with priority of 0 will never be selected by +# Redis Sentinel for promotion. +# +# By default the priority is 100. +replica-priority 100 + +# It is possible for a master to stop accepting writes if there are less than +# N replicas connected, having a lag less or equal than M seconds. +# +# The N replicas need to be in "online" state. +# +# The lag in seconds, that must be <= the specified value, is calculated from +# the last ping received from the replica, that is usually sent every second. +# +# This option does not GUARANTEE that N replicas will accept the write, but +# will limit the window of exposure for lost writes in case not enough replicas +# are available, to the specified number of seconds. +# +# For example to require at least 3 replicas with a lag <= 10 seconds use: +# +# min-replicas-to-write 3 +# min-replicas-max-lag 10 +# +# Setting one or the other to 0 disables the feature. +# +# By default min-replicas-to-write is set to 0 (feature disabled) and +# min-replicas-max-lag is set to 10. + +# A Redis master is able to list the address and port of the attached +# replicas in different ways. For example the "INFO replication" section +# offers this information, which is used, among other tools, by +# Redis Sentinel in order to discover replica instances. +# Another place where this info is available is in the output of the +# "ROLE" command of a master. +# +# The listed IP address and port normally reported by a replica is +# obtained in the following way: +# +# IP: The address is auto detected by checking the peer address +# of the socket used by the replica to connect with the master. +# +# Port: The port is communicated by the replica during the replication +# handshake, and is normally the port that the replica is using to +# listen for connections. +# +# However when port forwarding or Network Address Translation (NAT) is +# used, the replica may actually be reachable via different IP and port +# pairs. The following two options can be used by a replica in order to +# report to its master a specific set of IP and port, so that both INFO +# and ROLE will report those values. +# +# There is no need to use both the options if you need to override just +# the port or the IP address. +# +# replica-announce-ip 5.5.5.5 +# replica-announce-port 1234 + +############################### KEYS TRACKING ################################# + +# Redis implements server assisted support for client side caching of values. +# This is implemented using an invalidation table that remembers, using +# 16 millions of slots, what clients may have certain subsets of keys. In turn +# this is used in order to send invalidation messages to clients. Please +# check this page to understand more about the feature: +# +# https://redis.io/topics/client-side-caching +# +# When tracking is enabled for a client, all the read only queries are assumed +# to be cached: this will force Redis to store information in the invalidation +# table. When keys are modified, such information is flushed away, and +# invalidation messages are sent to the clients. However if the workload is +# heavily dominated by reads, Redis could use more and more memory in order +# to track the keys fetched by many clients. +# +# For this reason it is possible to configure a maximum fill value for the +# invalidation table. By default it is set to 1M of keys, and once this limit +# is reached, Redis will start to evict keys in the invalidation table +# even if they were not modified, just to reclaim memory: this will in turn +# force the clients to invalidate the cached values. Basically the table +# maximum size is a trade off between the memory you want to spend server +# side to track information about who cached what, and the ability of clients +# to retain cached objects in memory. +# +# If you set the value to 0, it means there are no limits, and Redis will +# retain as many keys as needed in the invalidation table. +# In the "stats" INFO section, you can find information about the number of +# keys in the invalidation table at every given moment. +# +# Note: when key tracking is used in broadcasting mode, no memory is used +# in the server side so this setting is useless. +# +# tracking-table-max-keys 1000000 + +################################## SECURITY ################################### + +# Warning: since Redis is pretty fast, an outside user can try up to +# 1 million passwords per second against a modern box. This means that you +# should use very strong passwords, otherwise they will be very easy to break. +# Note that because the password is really a shared secret between the client +# and the server, and should not be memorized by any human, the password +# can be easily a long string from /dev/urandom or whatever, so by using a +# long and unguessable password no brute force attack will be possible. + +# Redis ACL users are defined in the following format: +# +# user ... acl rules ... +# +# For example: +# +# user worker +@list +@connection ~jobs:* on >ffa9203c493aa99 +# +# The special username "default" is used for new connections. If this user +# has the "nopass" rule, then new connections will be immediately authenticated +# as the "default" user without the need of any password provided via the +# AUTH command. Otherwise if the "default" user is not flagged with "nopass" +# the connections will start in not authenticated state, and will require +# AUTH (or the HELLO command AUTH option) in order to be authenticated and +# start to work. +# +# The ACL rules that describe what a user can do are the following: +# +# on Enable the user: it is possible to authenticate as this user. +# off Disable the user: it's no longer possible to authenticate +# with this user, however the already authenticated connections +# will still work. +# + Allow the execution of that command +# - Disallow the execution of that command +# +@ Allow the execution of all the commands in such category +# with valid categories are like @admin, @set, @sortedset, ... +# and so forth, see the full list in the server.c file where +# the Redis command table is described and defined. +# The special category @all means all the commands, but currently +# present in the server, and that will be loaded in the future +# via modules. +# +|subcommand Allow a specific subcommand of an otherwise +# disabled command. Note that this form is not +# allowed as negative like -DEBUG|SEGFAULT, but +# only additive starting with "+". +# allcommands Alias for +@all. Note that it implies the ability to execute +# all the future commands loaded via the modules system. +# nocommands Alias for -@all. +# ~ Add a pattern of keys that can be mentioned as part of +# commands. For instance ~* allows all the keys. The pattern +# is a glob-style pattern like the one of KEYS. +# It is possible to specify multiple patterns. +# allkeys Alias for ~* +# resetkeys Flush the list of allowed keys patterns. +# > Add this password to the list of valid password for the user. +# For example >mypass will add "mypass" to the list. +# This directive clears the "nopass" flag (see later). +# < Remove this password from the list of valid passwords. +# nopass All the set passwords of the user are removed, and the user +# is flagged as requiring no password: it means that every +# password will work against this user. If this directive is +# used for the default user, every new connection will be +# immediately authenticated with the default user without +# any explicit AUTH command required. Note that the "resetpass" +# directive will clear this condition. +# resetpass Flush the list of allowed passwords. Moreover removes the +# "nopass" status. After "resetpass" the user has no associated +# passwords and there is no way to authenticate without adding +# some password (or setting it as "nopass" later). +# reset Performs the following actions: resetpass, resetkeys, off, +# -@all. The user returns to the same state it has immediately +# after its creation. +# +# ACL rules can be specified in any order: for instance you can start with +# passwords, then flags, or key patterns. However note that the additive +# and subtractive rules will CHANGE MEANING depending on the ordering. +# For instance see the following example: +# +# user alice on +@all -DEBUG ~* >somepassword +# +# This will allow "alice" to use all the commands with the exception of the +# DEBUG command, since +@all added all the commands to the set of the commands +# alice can use, and later DEBUG was removed. However if we invert the order +# of two ACL rules the result will be different: +# +# user alice on -DEBUG +@all ~* >somepassword +# +# Now DEBUG was removed when alice had yet no commands in the set of allowed +# commands, later all the commands are added, so the user will be able to +# execute everything. +# +# Basically ACL rules are processed left-to-right. +# +# For more information about ACL configuration please refer to +# the Redis web site at https://redis.io/topics/acl + +# ACL LOG +# +# The ACL Log tracks failed commands and authentication events associated +# with ACLs. The ACL Log is useful to troubleshoot failed commands blocked +# by ACLs. The ACL Log is stored in memory. You can reclaim memory with +# ACL LOG RESET. Define the maximum entry length of the ACL Log below. +acllog-max-len 128 + +# Using an external ACL file +# +# Instead of configuring users here in this file, it is possible to use +# a stand-alone file just listing users. The two methods cannot be mixed: +# if you configure users here and at the same time you activate the external +# ACL file, the server will refuse to start. +# +# The format of the external ACL user file is exactly the same as the +# format that is used inside redis.conf to describe users. +# +# aclfile /etc/redis/users.acl + +# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility +# layer on top of the new ACL system. The option effect will be just setting +# the password for the default user. Clients will still authenticate using +# AUTH as usually, or more explicitly with AUTH default +# if they follow the new protocol: both will work. +# +# requirepass foobared + +# Command renaming (DEPRECATED). +# +# ------------------------------------------------------------------------ +# WARNING: avoid using this option if possible. Instead use ACLs to remove +# commands from the default user, and put them only in some admin user you +# create for administrative purposes. +# ------------------------------------------------------------------------ +# +# It is possible to change the name of dangerous commands in a shared +# environment. For instance the CONFIG command may be renamed into something +# hard to guess so that it will still be available for internal-use tools +# but not available for general clients. +# +# Example: +# +# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52 +# +# It is also possible to completely kill a command by renaming it into +# an empty string: +# +# rename-command CONFIG "" +# +# Please note that changing the name of commands that are logged into the +# AOF file or transmitted to replicas may cause problems. + +################################### CLIENTS #################################### + +# Set the max number of connected clients at the same time. By default +# this limit is set to 10000 clients, however if the Redis server is not +# able to configure the process file limit to allow for the specified limit +# the max number of allowed clients is set to the current file limit +# minus 32 (as Redis reserves a few file descriptors for internal uses). +# +# Once the limit is reached Redis will close all the new connections sending +# an error 'max number of clients reached'. +# +# IMPORTANT: When Redis Cluster is used, the max number of connections is also +# shared with the cluster bus: every node in the cluster will use two +# connections, one incoming and another outgoing. It is important to size the +# limit accordingly in case of very large clusters. +# +# maxclients 10000 + +############################## MEMORY MANAGEMENT ################################ + +# Set a memory usage limit to the specified amount of bytes. +# When the memory limit is reached Redis will try to remove keys +# according to the eviction policy selected (see maxmemory-policy). +# +# If Redis can't remove keys according to the policy, or if the policy is +# set to 'noeviction', Redis will start to reply with errors to commands +# that would use more memory, like SET, LPUSH, and so on, and will continue +# to reply to read-only commands like GET. +# +# This option is usually useful when using Redis as an LRU or LFU cache, or to +# set a hard memory limit for an instance (using the 'noeviction' policy). +# +# WARNING: If you have replicas attached to an instance with maxmemory on, +# the size of the output buffers needed to feed the replicas are subtracted +# from the used memory count, so that network problems / resyncs will +# not trigger a loop where keys are evicted, and in turn the output +# buffer of replicas is full with DELs of keys evicted triggering the deletion +# of more keys, and so forth until the database is completely emptied. +# +# In short... if you have replicas attached it is suggested that you set a lower +# limit for maxmemory so that there is some free RAM on the system for replica +# output buffers (but this is not needed if the policy is 'noeviction'). +# +# maxmemory + +# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory +# is reached. You can select one from the following behaviors: +# +# volatile-lru -> Evict using approximated LRU, only keys with an expire set. +# allkeys-lru -> Evict any key using approximated LRU. +# volatile-lfu -> Evict using approximated LFU, only keys with an expire set. +# allkeys-lfu -> Evict any key using approximated LFU. +# volatile-random -> Remove a random key having an expire set. +# allkeys-random -> Remove a random key, any key. +# volatile-ttl -> Remove the key with the nearest expire time (minor TTL) +# noeviction -> Don't evict anything, just return an error on write operations. +# +# LRU means Least Recently Used +# LFU means Least Frequently Used +# +# Both LRU, LFU and volatile-ttl are implemented using approximated +# randomized algorithms. +# +# Note: with any of the above policies, Redis will return an error on write +# operations, when there are no suitable keys for eviction. +# +# At the date of writing these commands are: set setnx setex append +# incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd +# sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby +# zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby +# getset mset msetnx exec sort +# +# The default is: +# +# maxmemory-policy noeviction + +# LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated +# algorithms (in order to save memory), so you can tune it for speed or +# accuracy. By default Redis will check five keys and pick the one that was +# used least recently, you can change the sample size using the following +# configuration directive. +# +# The default of 5 produces good enough results. 10 Approximates very closely +# true LRU but costs more CPU. 3 is faster but not very accurate. +# +# maxmemory-samples 5 + +# Starting from Redis 5, by default a replica will ignore its maxmemory setting +# (unless it is promoted to master after a failover or manually). It means +# that the eviction of keys will be just handled by the master, sending the +# DEL commands to the replica as keys evict in the master side. +# +# This behavior ensures that masters and replicas stay consistent, and is usually +# what you want, however if your replica is writable, or you want the replica +# to have a different memory setting, and you are sure all the writes performed +# to the replica are idempotent, then you may change this default (but be sure +# to understand what you are doing). +# +# Note that since the replica by default does not evict, it may end using more +# memory than the one set via maxmemory (there are certain buffers that may +# be larger on the replica, or data structures may sometimes take more memory +# and so forth). So make sure you monitor your replicas and make sure they +# have enough memory to never hit a real out-of-memory condition before the +# master hits the configured maxmemory setting. +# +# replica-ignore-maxmemory yes + +# Redis reclaims expired keys in two ways: upon access when those keys are +# found to be expired, and also in background, in what is called the +# "active expire key". The key space is slowly and interactively scanned +# looking for expired keys to reclaim, so that it is possible to free memory +# of keys that are expired and will never be accessed again in a short time. +# +# The default effort of the expire cycle will try to avoid having more than +# ten percent of expired keys still in memory, and will try to avoid consuming +# more than 25% of total memory and to add latency to the system. However +# it is possible to increase the expire "effort" that is normally set to +# "1", to a greater value, up to the value "10". At its maximum value the +# system will use more CPU, longer cycles (and technically may introduce +# more latency), and will tolerate less already expired keys still present +# in the system. It's a tradeoff between memory, CPU and latency. +# +# active-expire-effort 1 + +############################# LAZY FREEING #################################### + +# Redis has two primitives to delete keys. One is called DEL and is a blocking +# deletion of the object. It means that the server stops processing new commands +# in order to reclaim all the memory associated with an object in a synchronous +# way. If the key deleted is associated with a small object, the time needed +# in order to execute the DEL command is very small and comparable to most other +# O(1) or O(log_N) commands in Redis. However if the key is associated with an +# aggregated value containing millions of elements, the server can block for +# a long time (even seconds) in order to complete the operation. +# +# For the above reasons Redis also offers non blocking deletion primitives +# such as UNLINK (non blocking DEL) and the ASYNC option of FLUSHALL and +# FLUSHDB commands, in order to reclaim memory in background. Those commands +# are executed in constant time. Another thread will incrementally free the +# object in the background as fast as possible. +# +# DEL, UNLINK and ASYNC option of FLUSHALL and FLUSHDB are user-controlled. +# It's up to the design of the application to understand when it is a good +# idea to use one or the other. However the Redis server sometimes has to +# delete keys or flush the whole database as a side effect of other operations. +# Specifically Redis deletes objects independently of a user call in the +# following scenarios: +# +# 1) On eviction, because of the maxmemory and maxmemory policy configurations, +# in order to make room for new data, without going over the specified +# memory limit. +# 2) Because of expire: when a key with an associated time to live (see the +# EXPIRE command) must be deleted from memory. +# 3) Because of a side effect of a command that stores data on a key that may +# already exist. For example the RENAME command may delete the old key +# content when it is replaced with another one. Similarly SUNIONSTORE +# or SORT with STORE option may delete existing keys. The SET command +# itself removes any old content of the specified key in order to replace +# it with the specified string. +# 4) During replication, when a replica performs a full resynchronization with +# its master, the content of the whole database is removed in order to +# load the RDB file just transferred. +# +# In all the above cases the default is to delete objects in a blocking way, +# like if DEL was called. However you can configure each case specifically +# in order to instead release memory in a non-blocking way like if UNLINK +# was called, using the following configuration directives. + +lazyfree-lazy-eviction no +lazyfree-lazy-expire no +lazyfree-lazy-server-del no +replica-lazy-flush no + +# It is also possible, for the case when to replace the user code DEL calls +# with UNLINK calls is not easy, to modify the default behavior of the DEL +# command to act exactly like UNLINK, using the following configuration +# directive: + +lazyfree-lazy-user-del no + +################################ THREADED I/O ################################# + +# Redis is mostly single threaded, however there are certain threaded +# operations such as UNLINK, slow I/O accesses and other things that are +# performed on side threads. +# +# Now it is also possible to handle Redis clients socket reads and writes +# in different I/O threads. Since especially writing is so slow, normally +# Redis users use pipelining in order to speed up the Redis performances per +# core, and spawn multiple instances in order to scale more. Using I/O +# threads it is possible to easily speedup two times Redis without resorting +# to pipelining nor sharding of the instance. +# +# By default threading is disabled, we suggest enabling it only in machines +# that have at least 4 or more cores, leaving at least one spare core. +# Using more than 8 threads is unlikely to help much. We also recommend using +# threaded I/O only if you actually have performance problems, with Redis +# instances being able to use a quite big percentage of CPU time, otherwise +# there is no point in using this feature. +# +# So for instance if you have a four cores boxes, try to use 2 or 3 I/O +# threads, if you have a 8 cores, try to use 6 threads. In order to +# enable I/O threads use the following configuration directive: +# +# io-threads 4 +# +# Setting io-threads to 1 will just use the main thread as usual. +# When I/O threads are enabled, we only use threads for writes, that is +# to thread the write(2) syscall and transfer the client buffers to the +# socket. However it is also possible to enable threading of reads and +# protocol parsing using the following configuration directive, by setting +# it to yes: +# +# io-threads-do-reads no +# +# Usually threading reads doesn't help much. +# +# NOTE 1: This configuration directive cannot be changed at runtime via +# CONFIG SET. Aso this feature currently does not work when SSL is +# enabled. +# +# NOTE 2: If you want to test the Redis speedup using redis-benchmark, make +# sure you also run the benchmark itself in threaded mode, using the +# --threads option to match the number of Redis threads, otherwise you'll not +# be able to notice the improvements. + +############################ KERNEL OOM CONTROL ############################## + +# On Linux, it is possible to hint the kernel OOM killer on what processes +# should be killed first when out of memory. +# +# Enabling this feature makes Redis actively control the oom_score_adj value +# for all its processes, depending on their role. The default scores will +# attempt to have background child processes killed before all others, and +# replicas killed before masters. + +oom-score-adj no + +# When oom-score-adj is used, this directive controls the specific values used +# for master, replica and background child processes. Values range -1000 to +# 1000 (higher means more likely to be killed). +# +# Unprivileged processes (not root, and without CAP_SYS_RESOURCE capabilities) +# can freely increase their value, but not decrease it below its initial +# settings. +# +# Values are used relative to the initial value of oom_score_adj when the server +# starts. Because typically the initial value is 0, they will often match the +# absolute values. + +oom-score-adj-values 0 200 800 + +############################## APPEND ONLY MODE ############################### + +# By default Redis asynchronously dumps the dataset on disk. This mode is +# good enough in many applications, but an issue with the Redis process or +# a power outage may result into a few minutes of writes lost (depending on +# the configured save points). +# +# The Append Only File is an alternative persistence mode that provides +# much better durability. For instance using the default data fsync policy +# (see later in the config file) Redis can lose just one second of writes in a +# dramatic event like a server power outage, or a single write if something +# wrong with the Redis process itself happens, but the operating system is +# still running correctly. +# +# AOF and RDB persistence can be enabled at the same time without problems. +# If the AOF is enabled on startup Redis will load the AOF, that is the file +# with the better durability guarantees. +# +# Please check http://redis.io/topics/persistence for more information. + +appendonly no + +# The name of the append only file (default: "appendonly.aof") + +appendfilename "appendonly.aof" + +# The fsync() call tells the Operating System to actually write data on disk +# instead of waiting for more data in the output buffer. Some OS will really flush +# data on disk, some other OS will just try to do it ASAP. +# +# Redis supports three different modes: +# +# no: don't fsync, just let the OS flush the data when it wants. Faster. +# always: fsync after every write to the append only log. Slow, Safest. +# everysec: fsync only one time every second. Compromise. +# +# The default is "everysec", as that's usually the right compromise between +# speed and data safety. It's up to you to understand if you can relax this to +# "no" that will let the operating system flush the output buffer when +# it wants, for better performances (but if you can live with the idea of +# some data loss consider the default persistence mode that's snapshotting), +# or on the contrary, use "always" that's very slow but a bit safer than +# everysec. +# +# More details please check the following article: +# http://antirez.com/post/redis-persistence-demystified.html +# +# If unsure, use "everysec". + +# appendfsync always +appendfsync everysec +# appendfsync no + +# When the AOF fsync policy is set to always or everysec, and a background +# saving process (a background save or AOF log background rewriting) is +# performing a lot of I/O against the disk, in some Linux configurations +# Redis may block too long on the fsync() call. Note that there is no fix for +# this currently, as even performing fsync in a different thread will block +# our synchronous write(2) call. +# +# In order to mitigate this problem it's possible to use the following option +# that will prevent fsync() from being called in the main process while a +# BGSAVE or BGREWRITEAOF is in progress. +# +# This means that while another child is saving, the durability of Redis is +# the same as "appendfsync none". In practical terms, this means that it is +# possible to lose up to 30 seconds of log in the worst scenario (with the +# default Linux settings). +# +# If you have latency problems turn this to "yes". Otherwise leave it as +# "no" that is the safest pick from the point of view of durability. + +no-appendfsync-on-rewrite no + +# Automatic rewrite of the append only file. +# Redis is able to automatically rewrite the log file implicitly calling +# BGREWRITEAOF when the AOF log size grows by the specified percentage. +# +# This is how it works: Redis remembers the size of the AOF file after the +# latest rewrite (if no rewrite has happened since the restart, the size of +# the AOF at startup is used). +# +# This base size is compared to the current size. If the current size is +# bigger than the specified percentage, the rewrite is triggered. Also +# you need to specify a minimal size for the AOF file to be rewritten, this +# is useful to avoid rewriting the AOF file even if the percentage increase +# is reached but it is still pretty small. +# +# Specify a percentage of zero in order to disable the automatic AOF +# rewrite feature. + +auto-aof-rewrite-percentage 100 +auto-aof-rewrite-min-size 64mb + +# An AOF file may be found to be truncated at the end during the Redis +# startup process, when the AOF data gets loaded back into memory. +# This may happen when the system where Redis is running +# crashes, especially when an ext4 filesystem is mounted without the +# data=ordered option (however this can't happen when Redis itself +# crashes or aborts but the operating system still works correctly). +# +# Redis can either exit with an error when this happens, or load as much +# data as possible (the default now) and start if the AOF file is found +# to be truncated at the end. The following option controls this behavior. +# +# If aof-load-truncated is set to yes, a truncated AOF file is loaded and +# the Redis server starts emitting a log to inform the user of the event. +# Otherwise if the option is set to no, the server aborts with an error +# and refuses to start. When the option is set to no, the user requires +# to fix the AOF file using the "redis-check-aof" utility before to restart +# the server. +# +# Note that if the AOF file will be found to be corrupted in the middle +# the server will still exit with an error. This option only applies when +# Redis will try to read more data from the AOF file but not enough bytes +# will be found. +aof-load-truncated yes + +# When rewriting the AOF file, Redis is able to use an RDB preamble in the +# AOF file for faster rewrites and recoveries. When this option is turned +# on the rewritten AOF file is composed of two different stanzas: +# +# [RDB file][AOF tail] +# +# When loading, Redis recognizes that the AOF file starts with the "REDIS" +# string and loads the prefixed RDB file, then continues loading the AOF +# tail. +aof-use-rdb-preamble yes + +################################ LUA SCRIPTING ############################### + +# Max execution time of a Lua script in milliseconds. +# +# If the maximum execution time is reached Redis will log that a script is +# still in execution after the maximum allowed time and will start to +# reply to queries with an error. +# +# When a long running script exceeds the maximum execution time only the +# SCRIPT KILL and SHUTDOWN NOSAVE commands are available. The first can be +# used to stop a script that did not yet call any write commands. The second +# is the only way to shut down the server in the case a write command was +# already issued by the script but the user doesn't want to wait for the natural +# termination of the script. +# +# Set it to 0 or a negative value for unlimited execution without warnings. +lua-time-limit 5000 + +################################ REDIS CLUSTER ############################### + +# Normal Redis instances can't be part of a Redis Cluster; only nodes that are +# started as cluster nodes can. In order to start a Redis instance as a +# cluster node enable the cluster support uncommenting the following: +# +# cluster-enabled yes + +# Every cluster node has a cluster configuration file. This file is not +# intended to be edited by hand. It is created and updated by Redis nodes. +# Every Redis Cluster node requires a different cluster configuration file. +# Make sure that instances running in the same system do not have +# overlapping cluster configuration file names. +# +# cluster-config-file nodes-6379.conf + +# Cluster node timeout is the amount of milliseconds a node must be unreachable +# for it to be considered in failure state. +# Most other internal time limits are a multiple of the node timeout. +# +# cluster-node-timeout 15000 + +# A replica of a failing master will avoid to start a failover if its data +# looks too old. +# +# There is no simple way for a replica to actually have an exact measure of +# its "data age", so the following two checks are performed: +# +# 1) If there are multiple replicas able to failover, they exchange messages +# in order to try to give an advantage to the replica with the best +# replication offset (more data from the master processed). +# Replicas will try to get their rank by offset, and apply to the start +# of the failover a delay proportional to their rank. +# +# 2) Every single replica computes the time of the last interaction with +# its master. This can be the last ping or command received (if the master +# is still in the "connected" state), or the time that elapsed since the +# disconnection with the master (if the replication link is currently down). +# If the last interaction is too old, the replica will not try to failover +# at all. +# +# The point "2" can be tuned by user. Specifically a replica will not perform +# the failover if, since the last interaction with the master, the time +# elapsed is greater than: +# +# (node-timeout * cluster-replica-validity-factor) + repl-ping-replica-period +# +# So for example if node-timeout is 30 seconds, and the cluster-replica-validity-factor +# is 10, and assuming a default repl-ping-replica-period of 10 seconds, the +# replica will not try to failover if it was not able to talk with the master +# for longer than 310 seconds. +# +# A large cluster-replica-validity-factor may allow replicas with too old data to failover +# a master, while a too small value may prevent the cluster from being able to +# elect a replica at all. +# +# For maximum availability, it is possible to set the cluster-replica-validity-factor +# to a value of 0, which means, that replicas will always try to failover the +# master regardless of the last time they interacted with the master. +# (However they'll always try to apply a delay proportional to their +# offset rank). +# +# Zero is the only value able to guarantee that when all the partitions heal +# the cluster will always be able to continue. +# +# cluster-replica-validity-factor 10 + +# Cluster replicas are able to migrate to orphaned masters, that are masters +# that are left without working replicas. This improves the cluster ability +# to resist to failures as otherwise an orphaned master can't be failed over +# in case of failure if it has no working replicas. +# +# Replicas migrate to orphaned masters only if there are still at least a +# given number of other working replicas for their old master. This number +# is the "migration barrier". A migration barrier of 1 means that a replica +# will migrate only if there is at least 1 other working replica for its master +# and so forth. It usually reflects the number of replicas you want for every +# master in your cluster. +# +# Default is 1 (replicas migrate only if their masters remain with at least +# one replica). To disable migration just set it to a very large value. +# A value of 0 can be set but is useful only for debugging and dangerous +# in production. +# +# cluster-migration-barrier 1 + +# By default Redis Cluster nodes stop accepting queries if they detect there +# is at least a hash slot uncovered (no available node is serving it). +# This way if the cluster is partially down (for example a range of hash slots +# are no longer covered) all the cluster becomes, eventually, unavailable. +# It automatically returns available as soon as all the slots are covered again. +# +# However sometimes you want the subset of the cluster which is working, +# to continue to accept queries for the part of the key space that is still +# covered. In order to do so, just set the cluster-require-full-coverage +# option to no. +# +# cluster-require-full-coverage yes + +# This option, when set to yes, prevents replicas from trying to failover its +# master during master failures. However the master can still perform a +# manual failover, if forced to do so. +# +# This is useful in different scenarios, especially in the case of multiple +# data center operations, where we want one side to never be promoted if not +# in the case of a total DC failure. +# +# cluster-replica-no-failover no + +# This option, when set to yes, allows nodes to serve read traffic while the +# the cluster is in a down state, as long as it believes it owns the slots. +# +# This is useful for two cases. The first case is for when an application +# doesn't require consistency of data during node failures or network partitions. +# One example of this is a cache, where as long as the node has the data it +# should be able to serve it. +# +# The second use case is for configurations that don't meet the recommended +# three shards but want to enable cluster mode and scale later. A +# master outage in a 1 or 2 shard configuration causes a read/write outage to the +# entire cluster without this option set, with it set there is only a write outage. +# Without a quorum of masters, slot ownership will not change automatically. +# +# cluster-allow-reads-when-down no + +# In order to setup your cluster make sure to read the documentation +# available at http://redis.io web site. + +########################## CLUSTER DOCKER/NAT support ######################## + +# In certain deployments, Redis Cluster nodes address discovery fails, because +# addresses are NAT-ted or because ports are forwarded (the typical case is +# Docker and other containers). +# +# In order to make Redis Cluster working in such environments, a static +# configuration where each node knows its public address is needed. The +# following two options are used for this scope, and are: +# +# * cluster-announce-ip +# * cluster-announce-port +# * cluster-announce-bus-port +# +# Each instructs the node about its address, client port, and cluster message +# bus port. The information is then published in the header of the bus packets +# so that other nodes will be able to correctly map the address of the node +# publishing the information. +# +# If the above options are not used, the normal Redis Cluster auto-detection +# will be used instead. +# +# Note that when remapped, the bus port may not be at the fixed offset of +# clients port + 10000, so you can specify any port and bus-port depending +# on how they get remapped. If the bus-port is not set, a fixed offset of +# 10000 will be used as usual. +# +# Example: +# +# cluster-announce-ip 10.1.1.5 +# cluster-announce-port 6379 +# cluster-announce-bus-port 6380 + +################################## SLOW LOG ################################### + +# The Redis Slow Log is a system to log queries that exceeded a specified +# execution time. The execution time does not include the I/O operations +# like talking with the client, sending the reply and so forth, +# but just the time needed to actually execute the command (this is the only +# stage of command execution where the thread is blocked and can not serve +# other requests in the meantime). +# +# You can configure the slow log with two parameters: one tells Redis +# what is the execution time, in microseconds, to exceed in order for the +# command to get logged, and the other parameter is the length of the +# slow log. When a new command is logged the oldest one is removed from the +# queue of logged commands. + +# The following time is expressed in microseconds, so 1000000 is equivalent +# to one second. Note that a negative number disables the slow log, while +# a value of zero forces the logging of every command. +slowlog-log-slower-than 10000 + +# There is no limit to this length. Just be aware that it will consume memory. +# You can reclaim memory used by the slow log with SLOWLOG RESET. +slowlog-max-len 128 + +################################ LATENCY MONITOR ############################## + +# The Redis latency monitoring subsystem samples different operations +# at runtime in order to collect data related to possible sources of +# latency of a Redis instance. +# +# Via the LATENCY command this information is available to the user that can +# print graphs and obtain reports. +# +# The system only logs operations that were performed in a time equal or +# greater than the amount of milliseconds specified via the +# latency-monitor-threshold configuration directive. When its value is set +# to zero, the latency monitor is turned off. +# +# By default latency monitoring is disabled since it is mostly not needed +# if you don't have latency issues, and collecting data has a performance +# impact, that while very small, can be measured under big load. Latency +# monitoring can easily be enabled at runtime using the command +# "CONFIG SET latency-monitor-threshold " if needed. +latency-monitor-threshold 0 + +############################# EVENT NOTIFICATION ############################## + +# Redis can notify Pub/Sub clients about events happening in the key space. +# This feature is documented at http://redis.io/topics/notifications +# +# For instance if keyspace events notification is enabled, and a client +# performs a DEL operation on key "foo" stored in the Database 0, two +# messages will be published via Pub/Sub: +# +# PUBLISH __keyspace@0__:foo del +# PUBLISH __keyevent@0__:del foo +# +# It is possible to select the events that Redis will notify among a set +# of classes. Every class is identified by a single character: +# +# K Keyspace events, published with __keyspace@__ prefix. +# E Keyevent events, published with __keyevent@__ prefix. +# g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ... +# $ String commands +# l List commands +# s Set commands +# h Hash commands +# z Sorted set commands +# x Expired events (events generated every time a key expires) +# e Evicted events (events generated when a key is evicted for maxmemory) +# t Stream commands +# m Key-miss events (Note: It is not included in the 'A' class) +# A Alias for g$lshzxet, so that the "AKE" string means all the events +# (Except key-miss events which are excluded from 'A' due to their +# unique nature). +# +# The "notify-keyspace-events" takes as argument a string that is composed +# of zero or multiple characters. The empty string means that notifications +# are disabled. +# +# Example: to enable list and generic events, from the point of view of the +# event name, use: +# +# notify-keyspace-events Elg +# +# Example 2: to get the stream of the expired keys subscribing to channel +# name __keyevent@0__:expired use: +# +# notify-keyspace-events Ex +# +# By default all notifications are disabled because most users don't need +# this feature and the feature has some overhead. Note that if you don't +# specify at least one of K or E, no events will be delivered. +notify-keyspace-events "" + +############################### GOPHER SERVER ################################# + +# Redis contains an implementation of the Gopher protocol, as specified in +# the RFC 1436 (https://www.ietf.org/rfc/rfc1436.txt). +# +# The Gopher protocol was very popular in the late '90s. It is an alternative +# to the web, and the implementation both server and client side is so simple +# that the Redis server has just 100 lines of code in order to implement this +# support. +# +# What do you do with Gopher nowadays? Well Gopher never *really* died, and +# lately there is a movement in order for the Gopher more hierarchical content +# composed of just plain text documents to be resurrected. Some want a simpler +# internet, others believe that the mainstream internet became too much +# controlled, and it's cool to create an alternative space for people that +# want a bit of fresh air. +# +# Anyway for the 10nth birthday of the Redis, we gave it the Gopher protocol +# as a gift. +# +# --- HOW IT WORKS? --- +# +# The Redis Gopher support uses the inline protocol of Redis, and specifically +# two kind of inline requests that were anyway illegal: an empty request +# or any request that starts with "/" (there are no Redis commands starting +# with such a slash). Normal RESP2/RESP3 requests are completely out of the +# path of the Gopher protocol implementation and are served as usual as well. +# +# If you open a connection to Redis when Gopher is enabled and send it +# a string like "/foo", if there is a key named "/foo" it is served via the +# Gopher protocol. +# +# In order to create a real Gopher "hole" (the name of a Gopher site in Gopher +# talking), you likely need a script like the following: +# +# https://github.com/antirez/gopher2redis +# +# --- SECURITY WARNING --- +# +# If you plan to put Redis on the internet in a publicly accessible address +# to server Gopher pages MAKE SURE TO SET A PASSWORD to the instance. +# Once a password is set: +# +# 1. The Gopher server (when enabled, not by default) will still serve +# content via Gopher. +# 2. However other commands cannot be called before the client will +# authenticate. +# +# So use the 'requirepass' option to protect your instance. +# +# Note that Gopher is not currently supported when 'io-threads-do-reads' +# is enabled. +# +# To enable Gopher support, uncomment the following line and set the option +# from no (the default) to yes. +# +# gopher-enabled no + +############################### ADVANCED CONFIG ############################### + +# Hashes are encoded using a memory efficient data structure when they have a +# small number of entries, and the biggest entry does not exceed a given +# threshold. These thresholds can be configured using the following directives. +hash-max-ziplist-entries 512 +hash-max-ziplist-value 64 + +# Lists are also encoded in a special way to save a lot of space. +# The number of entries allowed per internal list node can be specified +# as a fixed maximum size or a maximum number of elements. +# For a fixed maximum size, use -5 through -1, meaning: +# -5: max size: 64 Kb <-- not recommended for normal workloads +# -4: max size: 32 Kb <-- not recommended +# -3: max size: 16 Kb <-- probably not recommended +# -2: max size: 8 Kb <-- good +# -1: max size: 4 Kb <-- good +# Positive numbers mean store up to _exactly_ that number of elements +# per list node. +# The highest performing option is usually -2 (8 Kb size) or -1 (4 Kb size), +# but if your use case is unique, adjust the settings as necessary. +list-max-ziplist-size -2 + +# Lists may also be compressed. +# Compress depth is the number of quicklist ziplist nodes from *each* side of +# the list to *exclude* from compression. The head and tail of the list +# are always uncompressed for fast push/pop operations. Settings are: +# 0: disable all list compression +# 1: depth 1 means "don't start compressing until after 1 node into the list, +# going from either the head or tail" +# So: [head]->node->node->...->node->[tail] +# [head], [tail] will always be uncompressed; inner nodes will compress. +# 2: [head]->[next]->node->node->...->node->[prev]->[tail] +# 2 here means: don't compress head or head->next or tail->prev or tail, +# but compress all nodes between them. +# 3: [head]->[next]->[next]->node->node->...->node->[prev]->[prev]->[tail] +# etc. +list-compress-depth 0 + +# Sets have a special encoding in just one case: when a set is composed +# of just strings that happen to be integers in radix 10 in the range +# of 64 bit signed integers. +# The following configuration setting sets the limit in the size of the +# set in order to use this special memory saving encoding. +set-max-intset-entries 512 + +# Similarly to hashes and lists, sorted sets are also specially encoded in +# order to save a lot of space. This encoding is only used when the length and +# elements of a sorted set are below the following limits: +zset-max-ziplist-entries 128 +zset-max-ziplist-value 64 + +# HyperLogLog sparse representation bytes limit. The limit includes the +# 16 bytes header. When an HyperLogLog using the sparse representation crosses +# this limit, it is converted into the dense representation. +# +# A value greater than 16000 is totally useless, since at that point the +# dense representation is more memory efficient. +# +# The suggested value is ~ 3000 in order to have the benefits of +# the space efficient encoding without slowing down too much PFADD, +# which is O(N) with the sparse encoding. The value can be raised to +# ~ 10000 when CPU is not a concern, but space is, and the data set is +# composed of many HyperLogLogs with cardinality in the 0 - 15000 range. +hll-sparse-max-bytes 3000 + +# Streams macro node max size / items. The stream data structure is a radix +# tree of big nodes that encode multiple items inside. Using this configuration +# it is possible to configure how big a single node can be in bytes, and the +# maximum number of items it may contain before switching to a new node when +# appending new stream entries. If any of the following settings are set to +# zero, the limit is ignored, so for instance it is possible to set just a +# max entires limit by setting max-bytes to 0 and max-entries to the desired +# value. +stream-node-max-bytes 4kb +stream-node-max-entries 100 + +# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in +# order to help rehashing the main Redis hash table (the one mapping top-level +# keys to values). The hash table implementation Redis uses (see dict.c) +# performs a lazy rehashing: the more operation you run into a hash table +# that is rehashing, the more rehashing "steps" are performed, so if the +# server is idle the rehashing is never complete and some more memory is used +# by the hash table. +# +# The default is to use this millisecond 10 times every second in order to +# actively rehash the main dictionaries, freeing memory when possible. +# +# If unsure: +# use "activerehashing no" if you have hard latency requirements and it is +# not a good thing in your environment that Redis can reply from time to time +# to queries with 2 milliseconds delay. +# +# use "activerehashing yes" if you don't have such hard requirements but +# want to free memory asap when possible. +activerehashing yes + +# The client output buffer limits can be used to force disconnection of clients +# that are not reading data from the server fast enough for some reason (a +# common reason is that a Pub/Sub client can't consume messages as fast as the +# publisher can produce them). +# +# The limit can be set differently for the three different classes of clients: +# +# normal -> normal clients including MONITOR clients +# replica -> replica clients +# pubsub -> clients subscribed to at least one pubsub channel or pattern +# +# The syntax of every client-output-buffer-limit directive is the following: +# +# client-output-buffer-limit +# +# A client is immediately disconnected once the hard limit is reached, or if +# the soft limit is reached and remains reached for the specified number of +# seconds (continuously). +# So for instance if the hard limit is 32 megabytes and the soft limit is +# 16 megabytes / 10 seconds, the client will get disconnected immediately +# if the size of the output buffers reach 32 megabytes, but will also get +# disconnected if the client reaches 16 megabytes and continuously overcomes +# the limit for 10 seconds. +# +# By default normal clients are not limited because they don't receive data +# without asking (in a push way), but just after a request, so only +# asynchronous clients may create a scenario where data is requested faster +# than it can read. +# +# Instead there is a default limit for pubsub and replica clients, since +# subscribers and replicas receive data in a push fashion. +# +# Both the hard or the soft limit can be disabled by setting them to zero. +client-output-buffer-limit normal 0 0 0 +client-output-buffer-limit replica 256mb 64mb 60 +client-output-buffer-limit pubsub 32mb 8mb 60 + +# Client query buffers accumulate new commands. They are limited to a fixed +# amount by default in order to avoid that a protocol desynchronization (for +# instance due to a bug in the client) will lead to unbound memory usage in +# the query buffer. However you can configure it here if you have very special +# needs, such us huge multi/exec requests or alike. +# +# client-query-buffer-limit 1gb + +# In the Redis protocol, bulk requests, that are, elements representing single +# strings, are normally limited to 512 mb. However you can change this limit +# here, but must be 1mb or greater +# +# proto-max-bulk-len 512mb + +# Redis calls an internal function to perform many background tasks, like +# closing connections of clients in timeout, purging expired keys that are +# never requested, and so forth. +# +# Not all tasks are performed with the same frequency, but Redis checks for +# tasks to perform according to the specified "hz" value. +# +# By default "hz" is set to 10. Raising the value will use more CPU when +# Redis is idle, but at the same time will make Redis more responsive when +# there are many keys expiring at the same time, and timeouts may be +# handled with more precision. +# +# The range is between 1 and 500, however a value over 100 is usually not +# a good idea. Most users should use the default of 10 and raise this up to +# 100 only in environments where very low latency is required. +hz 10 + +# Normally it is useful to have an HZ value which is proportional to the +# number of clients connected. This is useful in order, for instance, to +# avoid too many clients are processed for each background task invocation +# in order to avoid latency spikes. +# +# Since the default HZ value by default is conservatively set to 10, Redis +# offers, and enables by default, the ability to use an adaptive HZ value +# which will temporarily raise when there are many connected clients. +# +# When dynamic HZ is enabled, the actual configured HZ will be used +# as a baseline, but multiples of the configured HZ value will be actually +# used as needed once more clients are connected. In this way an idle +# instance will use very little CPU time while a busy instance will be +# more responsive. +dynamic-hz yes + +# When a child rewrites the AOF file, if the following option is enabled +# the file will be fsync-ed every 32 MB of data generated. This is useful +# in order to commit the file to the disk more incrementally and avoid +# big latency spikes. +aof-rewrite-incremental-fsync yes + +# When redis saves RDB file, if the following option is enabled +# the file will be fsync-ed every 32 MB of data generated. This is useful +# in order to commit the file to the disk more incrementally and avoid +# big latency spikes. +rdb-save-incremental-fsync yes + +# Redis LFU eviction (see maxmemory setting) can be tuned. However it is a good +# idea to start with the default settings and only change them after investigating +# how to improve the performances and how the keys LFU change over time, which +# is possible to inspect via the OBJECT FREQ command. +# +# There are two tunable parameters in the Redis LFU implementation: the +# counter logarithm factor and the counter decay time. It is important to +# understand what the two parameters mean before changing them. +# +# The LFU counter is just 8 bits per key, it's maximum value is 255, so Redis +# uses a probabilistic increment with logarithmic behavior. Given the value +# of the old counter, when a key is accessed, the counter is incremented in +# this way: +# +# 1. A random number R between 0 and 1 is extracted. +# 2. A probability P is calculated as 1/(old_value*lfu_log_factor+1). +# 3. The counter is incremented only if R < P. +# +# The default lfu-log-factor is 10. This is a table of how the frequency +# counter changes with a different number of accesses with different +# logarithmic factors: +# +# +--------+------------+------------+------------+------------+------------+ +# | factor | 100 hits | 1000 hits | 100K hits | 1M hits | 10M hits | +# +--------+------------+------------+------------+------------+------------+ +# | 0 | 104 | 255 | 255 | 255 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# | 1 | 18 | 49 | 255 | 255 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# | 10 | 10 | 18 | 142 | 255 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# | 100 | 8 | 11 | 49 | 143 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# +# NOTE: The above table was obtained by running the following commands: +# +# redis-benchmark -n 1000000 incr foo +# redis-cli object freq foo +# +# NOTE 2: The counter initial value is 5 in order to give new objects a chance +# to accumulate hits. +# +# The counter decay time is the time, in minutes, that must elapse in order +# for the key counter to be divided by two (or decremented if it has a value +# less <= 10). +# +# The default value for the lfu-decay-time is 1. A special value of 0 means to +# decay the counter every time it happens to be scanned. +# +# lfu-log-factor 10 +# lfu-decay-time 1 + +########################### ACTIVE DEFRAGMENTATION ####################### +# +# What is active defragmentation? +# ------------------------------- +# +# Active (online) defragmentation allows a Redis server to compact the +# spaces left between small allocations and deallocations of data in memory, +# thus allowing to reclaim back memory. +# +# Fragmentation is a natural process that happens with every allocator (but +# less so with Jemalloc, fortunately) and certain workloads. Normally a server +# restart is needed in order to lower the fragmentation, or at least to flush +# away all the data and create it again. However thanks to this feature +# implemented by Oran Agra for Redis 4.0 this process can happen at runtime +# in a "hot" way, while the server is running. +# +# Basically when the fragmentation is over a certain level (see the +# configuration options below) Redis will start to create new copies of the +# values in contiguous memory regions by exploiting certain specific Jemalloc +# features (in order to understand if an allocation is causing fragmentation +# and to allocate it in a better place), and at the same time, will release the +# old copies of the data. This process, repeated incrementally for all the keys +# will cause the fragmentation to drop back to normal values. +# +# Important things to understand: +# +# 1. This feature is disabled by default, and only works if you compiled Redis +# to use the copy of Jemalloc we ship with the source code of Redis. +# This is the default with Linux builds. +# +# 2. You never need to enable this feature if you don't have fragmentation +# issues. +# +# 3. Once you experience fragmentation, you can enable this feature when +# needed with the command "CONFIG SET activedefrag yes". +# +# The configuration parameters are able to fine tune the behavior of the +# defragmentation process. If you are not sure about what they mean it is +# a good idea to leave the defaults untouched. + +# Enabled active defragmentation +# activedefrag no + +# Minimum amount of fragmentation waste to start active defrag +# active-defrag-ignore-bytes 100mb + +# Minimum percentage of fragmentation to start active defrag +# active-defrag-threshold-lower 10 + +# Maximum percentage of fragmentation at which we use maximum effort +# active-defrag-threshold-upper 100 + +# Minimal effort for defrag in CPU percentage, to be used when the lower +# threshold is reached +# active-defrag-cycle-min 1 + +# Maximal effort for defrag in CPU percentage, to be used when the upper +# threshold is reached +# active-defrag-cycle-max 25 + +# Maximum number of set/hash/zset/list fields that will be processed from +# the main dictionary scan +# active-defrag-max-scan-fields 1000 + +# Jemalloc background thread for purging will be enabled by default +jemalloc-bg-thread yes + +# It is possible to pin different threads and processes of Redis to specific +# CPUs in your system, in order to maximize the performances of the server. +# This is useful both in order to pin different Redis threads in different +# CPUs, but also in order to make sure that multiple Redis instances running +# in the same host will be pinned to different CPUs. +# +# Normally you can do this using the "taskset" command, however it is also +# possible to this via Redis configuration directly, both in Linux and FreeBSD. +# +# You can pin the server/IO threads, bio threads, aof rewrite child process, and +# the bgsave child process. The syntax to specify the cpu list is the same as +# the taskset command: +# +# Set redis server/io threads to cpu affinity 0,2,4,6: +# server_cpulist 0-7:2 +# +# Set bio threads to cpu affinity 1,3: +# bio_cpulist 1,3 +# +# Set aof rewrite child process to cpu affinity 8,9,10,11: +# aof_rewrite_cpulist 8-11 +# +# Set bgsave child process to cpu affinity 1,10,11 +# bgsave_cpulist 1,10-11 +# Generated by CONFIG REWRITE +user default on nopass sanitize-payload ~* &* +@all + +replicaof 127.0.0.1 6380 diff --git a/08cache/ha/conf/redis6380.conf b/08cache/ha/conf/redis6380.conf new file mode 100644 index 00000000..20311df0 --- /dev/null +++ b/08cache/ha/conf/redis6380.conf @@ -0,0 +1,1868 @@ +# Redis configuration file example. +# +# Note that in order to read the configuration file, Redis must be +# started with the file path as first argument: +# +# ./redis-server /path/to/redis.conf + +# Note on units: when memory size is needed, it is possible to specify +# it in the usual form of 1k 5GB 4M and so forth: +# +# 1k => 1000 bytes +# 1kb => 1024 bytes +# 1m => 1000000 bytes +# 1mb => 1024*1024 bytes +# 1g => 1000000000 bytes +# 1gb => 1024*1024*1024 bytes +# +# units are case insensitive so 1GB 1Gb 1gB are all the same. + +################################## INCLUDES ################################### + +# Include one or more other config files here. This is useful if you +# have a standard template that goes to all Redis servers but also need +# to customize a few per-server settings. Include files can include +# other files, so use this wisely. +# +# Note that option "include" won't be rewritten by command "CONFIG REWRITE" +# from admin or Redis Sentinel. Since Redis always uses the last processed +# line as value of a configuration directive, you'd better put includes +# at the beginning of this file to avoid overwriting config change at runtime. +# +# If instead you are interested in using includes to override configuration +# options, it is better to use include as the last line. +# +# include /path/to/local.conf +# include /path/to/other.conf + +################################## MODULES ##################################### + +# Load modules at startup. If the server is not able to load modules +# it will abort. It is possible to use multiple loadmodule directives. +# +# loadmodule /path/to/my_module.so +# loadmodule /path/to/other_module.so + +################################## NETWORK ##################################### + +# By default, if no "bind" configuration directive is specified, Redis listens +# for connections from all available network interfaces on the host machine. +# It is possible to listen to just one or multiple selected interfaces using +# the "bind" configuration directive, followed by one or more IP addresses. +# +# Examples: +# +# bind 192.168.1.100 10.0.0.1 +# bind 127.0.0.1 ::1 +# +# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the +# internet, binding to all the interfaces is dangerous and will expose the +# instance to everybody on the internet. So by default we uncomment the +# following bind directive, that will force Redis to listen only on the +# IPv4 loopback interface address (this means Redis will only be able to +# accept client connections from the same host that it is running on). +# +# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES +# JUST COMMENT OUT THE FOLLOWING LINE. +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +bind 127.0.0.1 ::1 + +# Protected mode is a layer of security protection, in order to avoid that +# Redis instances left open on the internet are accessed and exploited. +# +# When protected mode is on and if: +# +# 1) The server is not binding explicitly to a set of addresses using the +# "bind" directive. +# 2) No password is configured. +# +# The server only accepts connections from clients connecting from the +# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain +# sockets. +# +# By default protected mode is enabled. You should disable it only if +# you are sure you want clients from other hosts to connect to Redis +# even if no authentication is configured, nor a specific set of interfaces +# are explicitly listed using the "bind" directive. +protected-mode yes + +# Accept connections on the specified port, default is 6379 (IANA #815344). +# If port 0 is specified Redis will not listen on a TCP socket. +port 6380 + +# TCP listen() backlog. +# +# In high requests-per-second environments you need a high backlog in order +# to avoid slow clients connection issues. Note that the Linux kernel +# will silently truncate it to the value of /proc/sys/net/core/somaxconn so +# make sure to raise both the value of somaxconn and tcp_max_syn_backlog +# in order to get the desired effect. +tcp-backlog 511 + +# Unix socket. +# +# Specify the path for the Unix socket that will be used to listen for +# incoming connections. There is no default, so Redis will not listen +# on a unix socket when not specified. +# +# unixsocket /tmp/redis.sock +# unixsocketperm 700 + +# Close the connection after a client is idle for N seconds (0 to disable) +timeout 0 + +# TCP keepalive. +# +# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence +# of communication. This is useful for two reasons: +# +# 1) Detect dead peers. +# 2) Force network equipment in the middle to consider the connection to be +# alive. +# +# On Linux, the specified value (in seconds) is the period used to send ACKs. +# Note that to close the connection the double of the time is needed. +# On other kernels the period depends on the kernel configuration. +# +# A reasonable value for this option is 300 seconds, which is the new +# Redis default starting with Redis 3.2.1. +tcp-keepalive 300 + +################################# TLS/SSL ##################################### + +# By default, TLS/SSL is disabled. To enable it, the "tls-port" configuration +# directive can be used to define TLS-listening ports. To enable TLS on the +# default port, use: +# +# port 0 +# tls-port 6379 + +# Configure a X.509 certificate and private key to use for authenticating the +# server to connected clients, masters or cluster peers. These files should be +# PEM formatted. +# +# tls-cert-file redis.crt +# tls-key-file redis.key + +# Configure a DH parameters file to enable Diffie-Hellman (DH) key exchange: +# +# tls-dh-params-file redis.dh + +# Configure a CA certificate(s) bundle or directory to authenticate TLS/SSL +# clients and peers. Redis requires an explicit configuration of at least one +# of these, and will not implicitly use the system wide configuration. +# +# tls-ca-cert-file ca.crt +# tls-ca-cert-dir /etc/ssl/certs + +# By default, clients (including replica servers) on a TLS port are required +# to authenticate using valid client side certificates. +# +# If "no" is specified, client certificates are not required and not accepted. +# If "optional" is specified, client certificates are accepted and must be +# valid if provided, but are not required. +# +# tls-auth-clients no +# tls-auth-clients optional + +# By default, a Redis replica does not attempt to establish a TLS connection +# with its master. +# +# Use the following directive to enable TLS on replication links. +# +# tls-replication yes + +# By default, the Redis Cluster bus uses a plain TCP connection. To enable +# TLS for the bus protocol, use the following directive: +# +# tls-cluster yes + +# Explicitly specify TLS versions to support. Allowed values are case insensitive +# and include "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" (OpenSSL >= 1.1.1) or +# any combination. To enable only TLSv1.2 and TLSv1.3, use: +# +# tls-protocols "TLSv1.2 TLSv1.3" + +# Configure allowed ciphers. See the ciphers(1ssl) manpage for more information +# about the syntax of this string. +# +# Note: this configuration applies only to <= TLSv1.2. +# +# tls-ciphers DEFAULT:!MEDIUM + +# Configure allowed TLSv1.3 ciphersuites. See the ciphers(1ssl) manpage for more +# information about the syntax of this string, and specifically for TLSv1.3 +# ciphersuites. +# +# tls-ciphersuites TLS_CHACHA20_POLY1305_SHA256 + +# When choosing a cipher, use the server's preference instead of the client +# preference. By default, the server follows the client's preference. +# +# tls-prefer-server-ciphers yes + +# By default, TLS session caching is enabled to allow faster and less expensive +# reconnections by clients that support it. Use the following directive to disable +# caching. +# +# tls-session-caching no + +# Change the default number of TLS sessions cached. A zero value sets the cache +# to unlimited size. The default size is 20480. +# +# tls-session-cache-size 5000 + +# Change the default timeout of cached TLS sessions. The default timeout is 300 +# seconds. +# +# tls-session-cache-timeout 60 + +################################# GENERAL ##################################### + +# By default Redis does not run as a daemon. Use 'yes' if you need it. +# Note that Redis will write a pid file in /usr/local/var/run/redis.pid when daemonized. +daemonize no + +# If you run Redis from upstart or systemd, Redis can interact with your +# supervision tree. Options: +# supervised no - no supervision interaction +# supervised upstart - signal upstart by putting Redis into SIGSTOP mode +# requires "expect stop" in your upstart job config +# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET +# supervised auto - detect upstart or systemd method based on +# UPSTART_JOB or NOTIFY_SOCKET environment variables +# Note: these supervision methods only signal "process is ready." +# They do not enable continuous pings back to your supervisor. +supervised no + +# If a pid file is specified, Redis writes it where specified at startup +# and removes it at exit. +# +# When the server runs non daemonized, no pid file is created if none is +# specified in the configuration. When the server is daemonized, the pid file +# is used even if not specified, defaulting to "/usr/local/var/run/redis.pid". +# +# Creating a pid file is best effort: if Redis is not able to create it +# nothing bad happens, the server will start and run normally. +pidfile "/var/run/redis_6380.pid" + +# Specify the server verbosity level. +# This can be one of: +# debug (a lot of information, useful for development/testing) +# verbose (many rarely useful info, but not a mess like the debug level) +# notice (moderately verbose, what you want in production probably) +# warning (only very important / critical messages are logged) +loglevel notice + +# Specify the log file name. Also the empty string can be used to force +# Redis to log on the standard output. Note that if you use standard +# output for logging but daemonize, logs will be sent to /dev/null +logfile "" + +# To enable logging to the system logger, just set 'syslog-enabled' to yes, +# and optionally update the other syslog parameters to suit your needs. +# syslog-enabled no + +# Specify the syslog identity. +# syslog-ident redis + +# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7. +# syslog-facility local0 + +# Set the number of databases. The default database is DB 0, you can select +# a different one on a per-connection basis using SELECT where +# dbid is a number between 0 and 'databases'-1 +databases 16 + +# By default Redis shows an ASCII art logo only when started to log to the +# standard output and if the standard output is a TTY. Basically this means +# that normally a logo is displayed only in interactive sessions. +# +# However it is possible to force the pre-4.0 behavior and always show a +# ASCII art logo in startup logs by setting the following option to yes. +always-show-logo yes + +################################ SNAPSHOTTING ################################ +# +# Save the DB on disk: +# +# save +# +# Will save the DB if both the given number of seconds and the given +# number of write operations against the DB occurred. +# +# In the example below the behavior will be to save: +# after 900 sec (15 min) if at least 1 key changed +# after 300 sec (5 min) if at least 10 keys changed +# after 60 sec if at least 10000 keys changed +# +# Note: you can disable saving completely by commenting out all "save" lines. +# +# It is also possible to remove all the previously configured save +# points by adding a save directive with a single empty string argument +# like in the following example: +# +# save "" + +save 900 1 +save 300 10 +save 60 10000 + +# By default Redis will stop accepting writes if RDB snapshots are enabled +# (at least one save point) and the latest background save failed. +# This will make the user aware (in a hard way) that data is not persisting +# on disk properly, otherwise chances are that no one will notice and some +# disaster will happen. +# +# If the background saving process will start working again Redis will +# automatically allow writes again. +# +# However if you have setup your proper monitoring of the Redis server +# and persistence, you may want to disable this feature so that Redis will +# continue to work as usual even if there are problems with disk, +# permissions, and so forth. +stop-writes-on-bgsave-error yes + +# Compress string objects using LZF when dump .rdb databases? +# By default compression is enabled as it's almost always a win. +# If you want to save some CPU in the saving child set it to 'no' but +# the dataset will likely be bigger if you have compressible values or keys. +rdbcompression yes + +# Since version 5 of RDB a CRC64 checksum is placed at the end of the file. +# This makes the format more resistant to corruption but there is a performance +# hit to pay (around 10%) when saving and loading RDB files, so you can disable it +# for maximum performances. +# +# RDB files created with checksum disabled have a checksum of zero that will +# tell the loading code to skip the check. +rdbchecksum yes + +# The filename where to dump the DB +dbfilename "dump.rdb" + +# Remove RDB files used by replication in instances without persistence +# enabled. By default this option is disabled, however there are environments +# where for regulations or other security concerns, RDB files persisted on +# disk by masters in order to feed replicas, or stored on disk by replicas +# in order to load them for the initial synchronization, should be deleted +# ASAP. Note that this option ONLY WORKS in instances that have both AOF +# and RDB persistence disabled, otherwise is completely ignored. +# +# An alternative (and sometimes better) way to obtain the same effect is +# to use diskless replication on both master and replicas instances. However +# in the case of replicas, diskless is not always an option. +rdb-del-sync-files no + +# The working directory. +# +# The DB will be written inside this directory, with the filename specified +# above using the 'dbfilename' configuration directive. +# +# The Append Only File will also be created inside this directory. +# +# Note that you must specify a directory here, not a file name. +dir "/Users/kimmking/logs/redis1" + +################################# REPLICATION ################################# + +# Master-Replica replication. Use replicaof to make a Redis instance a copy of +# another Redis server. A few things to understand ASAP about Redis replication. +# +# +------------------+ +---------------+ +# | Master | ---> | Replica | +# | (receive writes) | | (exact copy) | +# +------------------+ +---------------+ +# +# 1) Redis replication is asynchronous, but you can configure a master to +# stop accepting writes if it appears to be not connected with at least +# a given number of replicas. +# 2) Redis replicas are able to perform a partial resynchronization with the +# master if the replication link is lost for a relatively small amount of +# time. You may want to configure the replication backlog size (see the next +# sections of this file) with a sensible value depending on your needs. +# 3) Replication is automatic and does not need user intervention. After a +# network partition replicas automatically try to reconnect to masters +# and resynchronize with them. +# +# replicaof + +# If the master is password protected (using the "requirepass" configuration +# directive below) it is possible to tell the replica to authenticate before +# starting the replication synchronization process, otherwise the master will +# refuse the replica request. +# +# masterauth +# +# However this is not enough if you are using Redis ACLs (for Redis version +# 6 or greater), and the default user is not capable of running the PSYNC +# command and/or other commands needed for replication. In this case it's +# better to configure a special user to use with replication, and specify the +# masteruser configuration as such: +# +# masteruser +# +# When masteruser is specified, the replica will authenticate against its +# master using the new AUTH form: AUTH . + +# When a replica loses its connection with the master, or when the replication +# is still in progress, the replica can act in two different ways: +# +# 1) if replica-serve-stale-data is set to 'yes' (the default) the replica will +# still reply to client requests, possibly with out of date data, or the +# data set may just be empty if this is the first synchronization. +# +# 2) If replica-serve-stale-data is set to 'no' the replica will reply with +# an error "SYNC with master in progress" to all commands except: +# INFO, REPLICAOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG, SUBSCRIBE, +# UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB, COMMAND, POST, +# HOST and LATENCY. +# +replica-serve-stale-data yes + +# You can configure a replica instance to accept writes or not. Writing against +# a replica instance may be useful to store some ephemeral data (because data +# written on a replica will be easily deleted after resync with the master) but +# may also cause problems if clients are writing to it because of a +# misconfiguration. +# +# Since Redis 2.6 by default replicas are read-only. +# +# Note: read only replicas are not designed to be exposed to untrusted clients +# on the internet. It's just a protection layer against misuse of the instance. +# Still a read only replica exports by default all the administrative commands +# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve +# security of read only replicas using 'rename-command' to shadow all the +# administrative / dangerous commands. +replica-read-only yes + +# Replication SYNC strategy: disk or socket. +# +# New replicas and reconnecting replicas that are not able to continue the +# replication process just receiving differences, need to do what is called a +# "full synchronization". An RDB file is transmitted from the master to the +# replicas. +# +# The transmission can happen in two different ways: +# +# 1) Disk-backed: The Redis master creates a new process that writes the RDB +# file on disk. Later the file is transferred by the parent +# process to the replicas incrementally. +# 2) Diskless: The Redis master creates a new process that directly writes the +# RDB file to replica sockets, without touching the disk at all. +# +# With disk-backed replication, while the RDB file is generated, more replicas +# can be queued and served with the RDB file as soon as the current child +# producing the RDB file finishes its work. With diskless replication instead +# once the transfer starts, new replicas arriving will be queued and a new +# transfer will start when the current one terminates. +# +# When diskless replication is used, the master waits a configurable amount of +# time (in seconds) before starting the transfer in the hope that multiple +# replicas will arrive and the transfer can be parallelized. +# +# With slow disks and fast (large bandwidth) networks, diskless replication +# works better. +repl-diskless-sync no + +# When diskless replication is enabled, it is possible to configure the delay +# the server waits in order to spawn the child that transfers the RDB via socket +# to the replicas. +# +# This is important since once the transfer starts, it is not possible to serve +# new replicas arriving, that will be queued for the next RDB transfer, so the +# server waits a delay in order to let more replicas arrive. +# +# The delay is specified in seconds, and by default is 5 seconds. To disable +# it entirely just set it to 0 seconds and the transfer will start ASAP. +repl-diskless-sync-delay 5 + +# ----------------------------------------------------------------------------- +# WARNING: RDB diskless load is experimental. Since in this setup the replica +# does not immediately store an RDB on disk, it may cause data loss during +# failovers. RDB diskless load + Redis modules not handling I/O reads may also +# cause Redis to abort in case of I/O errors during the initial synchronization +# stage with the master. Use only if your do what you are doing. +# ----------------------------------------------------------------------------- +# +# Replica can load the RDB it reads from the replication link directly from the +# socket, or store the RDB to a file and read that file after it was completely +# received from the master. +# +# In many cases the disk is slower than the network, and storing and loading +# the RDB file may increase replication time (and even increase the master's +# Copy on Write memory and salve buffers). +# However, parsing the RDB file directly from the socket may mean that we have +# to flush the contents of the current database before the full rdb was +# received. For this reason we have the following options: +# +# "disabled" - Don't use diskless load (store the rdb file to the disk first) +# "on-empty-db" - Use diskless load only when it is completely safe. +# "swapdb" - Keep a copy of the current db contents in RAM while parsing +# the data directly from the socket. note that this requires +# sufficient memory, if you don't have it, you risk an OOM kill. +repl-diskless-load disabled + +# Replicas send PINGs to server in a predefined interval. It's possible to +# change this interval with the repl_ping_replica_period option. The default +# value is 10 seconds. +# +# repl-ping-replica-period 10 + +# The following option sets the replication timeout for: +# +# 1) Bulk transfer I/O during SYNC, from the point of view of replica. +# 2) Master timeout from the point of view of replicas (data, pings). +# 3) Replica timeout from the point of view of masters (REPLCONF ACK pings). +# +# It is important to make sure that this value is greater than the value +# specified for repl-ping-replica-period otherwise a timeout will be detected +# every time there is low traffic between the master and the replica. The default +# value is 60 seconds. +# +# repl-timeout 60 + +# Disable TCP_NODELAY on the replica socket after SYNC? +# +# If you select "yes" Redis will use a smaller number of TCP packets and +# less bandwidth to send data to replicas. But this can add a delay for +# the data to appear on the replica side, up to 40 milliseconds with +# Linux kernels using a default configuration. +# +# If you select "no" the delay for data to appear on the replica side will +# be reduced but more bandwidth will be used for replication. +# +# By default we optimize for low latency, but in very high traffic conditions +# or when the master and replicas are many hops away, turning this to "yes" may +# be a good idea. +repl-disable-tcp-nodelay no + +# Set the replication backlog size. The backlog is a buffer that accumulates +# replica data when replicas are disconnected for some time, so that when a +# replica wants to reconnect again, often a full resync is not needed, but a +# partial resync is enough, just passing the portion of data the replica +# missed while disconnected. +# +# The bigger the replication backlog, the longer the replica can endure the +# disconnect and later be able to perform a partial resynchronization. +# +# The backlog is only allocated if there is at least one replica connected. +# +# repl-backlog-size 1mb + +# After a master has no connected replicas for some time, the backlog will be +# freed. The following option configures the amount of seconds that need to +# elapse, starting from the time the last replica disconnected, for the backlog +# buffer to be freed. +# +# Note that replicas never free the backlog for timeout, since they may be +# promoted to masters later, and should be able to correctly "partially +# resynchronize" with other replicas: hence they should always accumulate backlog. +# +# A value of 0 means to never release the backlog. +# +# repl-backlog-ttl 3600 + +# The replica priority is an integer number published by Redis in the INFO +# output. It is used by Redis Sentinel in order to select a replica to promote +# into a master if the master is no longer working correctly. +# +# A replica with a low priority number is considered better for promotion, so +# for instance if there are three replicas with priority 10, 100, 25 Sentinel +# will pick the one with priority 10, that is the lowest. +# +# However a special priority of 0 marks the replica as not able to perform the +# role of master, so a replica with priority of 0 will never be selected by +# Redis Sentinel for promotion. +# +# By default the priority is 100. +replica-priority 100 + +# It is possible for a master to stop accepting writes if there are less than +# N replicas connected, having a lag less or equal than M seconds. +# +# The N replicas need to be in "online" state. +# +# The lag in seconds, that must be <= the specified value, is calculated from +# the last ping received from the replica, that is usually sent every second. +# +# This option does not GUARANTEE that N replicas will accept the write, but +# will limit the window of exposure for lost writes in case not enough replicas +# are available, to the specified number of seconds. +# +# For example to require at least 3 replicas with a lag <= 10 seconds use: +# +# min-replicas-to-write 3 +# min-replicas-max-lag 10 +# +# Setting one or the other to 0 disables the feature. +# +# By default min-replicas-to-write is set to 0 (feature disabled) and +# min-replicas-max-lag is set to 10. + +# A Redis master is able to list the address and port of the attached +# replicas in different ways. For example the "INFO replication" section +# offers this information, which is used, among other tools, by +# Redis Sentinel in order to discover replica instances. +# Another place where this info is available is in the output of the +# "ROLE" command of a master. +# +# The listed IP address and port normally reported by a replica is +# obtained in the following way: +# +# IP: The address is auto detected by checking the peer address +# of the socket used by the replica to connect with the master. +# +# Port: The port is communicated by the replica during the replication +# handshake, and is normally the port that the replica is using to +# listen for connections. +# +# However when port forwarding or Network Address Translation (NAT) is +# used, the replica may actually be reachable via different IP and port +# pairs. The following two options can be used by a replica in order to +# report to its master a specific set of IP and port, so that both INFO +# and ROLE will report those values. +# +# There is no need to use both the options if you need to override just +# the port or the IP address. +# +# replica-announce-ip 5.5.5.5 +# replica-announce-port 1234 + +############################### KEYS TRACKING ################################# + +# Redis implements server assisted support for client side caching of values. +# This is implemented using an invalidation table that remembers, using +# 16 millions of slots, what clients may have certain subsets of keys. In turn +# this is used in order to send invalidation messages to clients. Please +# check this page to understand more about the feature: +# +# https://redis.io/topics/client-side-caching +# +# When tracking is enabled for a client, all the read only queries are assumed +# to be cached: this will force Redis to store information in the invalidation +# table. When keys are modified, such information is flushed away, and +# invalidation messages are sent to the clients. However if the workload is +# heavily dominated by reads, Redis could use more and more memory in order +# to track the keys fetched by many clients. +# +# For this reason it is possible to configure a maximum fill value for the +# invalidation table. By default it is set to 1M of keys, and once this limit +# is reached, Redis will start to evict keys in the invalidation table +# even if they were not modified, just to reclaim memory: this will in turn +# force the clients to invalidate the cached values. Basically the table +# maximum size is a trade off between the memory you want to spend server +# side to track information about who cached what, and the ability of clients +# to retain cached objects in memory. +# +# If you set the value to 0, it means there are no limits, and Redis will +# retain as many keys as needed in the invalidation table. +# In the "stats" INFO section, you can find information about the number of +# keys in the invalidation table at every given moment. +# +# Note: when key tracking is used in broadcasting mode, no memory is used +# in the server side so this setting is useless. +# +# tracking-table-max-keys 1000000 + +################################## SECURITY ################################### + +# Warning: since Redis is pretty fast, an outside user can try up to +# 1 million passwords per second against a modern box. This means that you +# should use very strong passwords, otherwise they will be very easy to break. +# Note that because the password is really a shared secret between the client +# and the server, and should not be memorized by any human, the password +# can be easily a long string from /dev/urandom or whatever, so by using a +# long and unguessable password no brute force attack will be possible. + +# Redis ACL users are defined in the following format: +# +# user ... acl rules ... +# +# For example: +# +# user worker +@list +@connection ~jobs:* on >ffa9203c493aa99 +# +# The special username "default" is used for new connections. If this user +# has the "nopass" rule, then new connections will be immediately authenticated +# as the "default" user without the need of any password provided via the +# AUTH command. Otherwise if the "default" user is not flagged with "nopass" +# the connections will start in not authenticated state, and will require +# AUTH (or the HELLO command AUTH option) in order to be authenticated and +# start to work. +# +# The ACL rules that describe what a user can do are the following: +# +# on Enable the user: it is possible to authenticate as this user. +# off Disable the user: it's no longer possible to authenticate +# with this user, however the already authenticated connections +# will still work. +# + Allow the execution of that command +# - Disallow the execution of that command +# +@ Allow the execution of all the commands in such category +# with valid categories are like @admin, @set, @sortedset, ... +# and so forth, see the full list in the server.c file where +# the Redis command table is described and defined. +# The special category @all means all the commands, but currently +# present in the server, and that will be loaded in the future +# via modules. +# +|subcommand Allow a specific subcommand of an otherwise +# disabled command. Note that this form is not +# allowed as negative like -DEBUG|SEGFAULT, but +# only additive starting with "+". +# allcommands Alias for +@all. Note that it implies the ability to execute +# all the future commands loaded via the modules system. +# nocommands Alias for -@all. +# ~ Add a pattern of keys that can be mentioned as part of +# commands. For instance ~* allows all the keys. The pattern +# is a glob-style pattern like the one of KEYS. +# It is possible to specify multiple patterns. +# allkeys Alias for ~* +# resetkeys Flush the list of allowed keys patterns. +# > Add this password to the list of valid password for the user. +# For example >mypass will add "mypass" to the list. +# This directive clears the "nopass" flag (see later). +# < Remove this password from the list of valid passwords. +# nopass All the set passwords of the user are removed, and the user +# is flagged as requiring no password: it means that every +# password will work against this user. If this directive is +# used for the default user, every new connection will be +# immediately authenticated with the default user without +# any explicit AUTH command required. Note that the "resetpass" +# directive will clear this condition. +# resetpass Flush the list of allowed passwords. Moreover removes the +# "nopass" status. After "resetpass" the user has no associated +# passwords and there is no way to authenticate without adding +# some password (or setting it as "nopass" later). +# reset Performs the following actions: resetpass, resetkeys, off, +# -@all. The user returns to the same state it has immediately +# after its creation. +# +# ACL rules can be specified in any order: for instance you can start with +# passwords, then flags, or key patterns. However note that the additive +# and subtractive rules will CHANGE MEANING depending on the ordering. +# For instance see the following example: +# +# user alice on +@all -DEBUG ~* >somepassword +# +# This will allow "alice" to use all the commands with the exception of the +# DEBUG command, since +@all added all the commands to the set of the commands +# alice can use, and later DEBUG was removed. However if we invert the order +# of two ACL rules the result will be different: +# +# user alice on -DEBUG +@all ~* >somepassword +# +# Now DEBUG was removed when alice had yet no commands in the set of allowed +# commands, later all the commands are added, so the user will be able to +# execute everything. +# +# Basically ACL rules are processed left-to-right. +# +# For more information about ACL configuration please refer to +# the Redis web site at https://redis.io/topics/acl + +# ACL LOG +# +# The ACL Log tracks failed commands and authentication events associated +# with ACLs. The ACL Log is useful to troubleshoot failed commands blocked +# by ACLs. The ACL Log is stored in memory. You can reclaim memory with +# ACL LOG RESET. Define the maximum entry length of the ACL Log below. +acllog-max-len 128 + +# Using an external ACL file +# +# Instead of configuring users here in this file, it is possible to use +# a stand-alone file just listing users. The two methods cannot be mixed: +# if you configure users here and at the same time you activate the external +# ACL file, the server will refuse to start. +# +# The format of the external ACL user file is exactly the same as the +# format that is used inside redis.conf to describe users. +# +# aclfile /etc/redis/users.acl + +# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility +# layer on top of the new ACL system. The option effect will be just setting +# the password for the default user. Clients will still authenticate using +# AUTH as usually, or more explicitly with AUTH default +# if they follow the new protocol: both will work. +# +# requirepass foobared + +# Command renaming (DEPRECATED). +# +# ------------------------------------------------------------------------ +# WARNING: avoid using this option if possible. Instead use ACLs to remove +# commands from the default user, and put them only in some admin user you +# create for administrative purposes. +# ------------------------------------------------------------------------ +# +# It is possible to change the name of dangerous commands in a shared +# environment. For instance the CONFIG command may be renamed into something +# hard to guess so that it will still be available for internal-use tools +# but not available for general clients. +# +# Example: +# +# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52 +# +# It is also possible to completely kill a command by renaming it into +# an empty string: +# +# rename-command CONFIG "" +# +# Please note that changing the name of commands that are logged into the +# AOF file or transmitted to replicas may cause problems. + +################################### CLIENTS #################################### + +# Set the max number of connected clients at the same time. By default +# this limit is set to 10000 clients, however if the Redis server is not +# able to configure the process file limit to allow for the specified limit +# the max number of allowed clients is set to the current file limit +# minus 32 (as Redis reserves a few file descriptors for internal uses). +# +# Once the limit is reached Redis will close all the new connections sending +# an error 'max number of clients reached'. +# +# IMPORTANT: When Redis Cluster is used, the max number of connections is also +# shared with the cluster bus: every node in the cluster will use two +# connections, one incoming and another outgoing. It is important to size the +# limit accordingly in case of very large clusters. +# +# maxclients 10000 + +############################## MEMORY MANAGEMENT ################################ + +# Set a memory usage limit to the specified amount of bytes. +# When the memory limit is reached Redis will try to remove keys +# according to the eviction policy selected (see maxmemory-policy). +# +# If Redis can't remove keys according to the policy, or if the policy is +# set to 'noeviction', Redis will start to reply with errors to commands +# that would use more memory, like SET, LPUSH, and so on, and will continue +# to reply to read-only commands like GET. +# +# This option is usually useful when using Redis as an LRU or LFU cache, or to +# set a hard memory limit for an instance (using the 'noeviction' policy). +# +# WARNING: If you have replicas attached to an instance with maxmemory on, +# the size of the output buffers needed to feed the replicas are subtracted +# from the used memory count, so that network problems / resyncs will +# not trigger a loop where keys are evicted, and in turn the output +# buffer of replicas is full with DELs of keys evicted triggering the deletion +# of more keys, and so forth until the database is completely emptied. +# +# In short... if you have replicas attached it is suggested that you set a lower +# limit for maxmemory so that there is some free RAM on the system for replica +# output buffers (but this is not needed if the policy is 'noeviction'). +# +# maxmemory + +# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory +# is reached. You can select one from the following behaviors: +# +# volatile-lru -> Evict using approximated LRU, only keys with an expire set. +# allkeys-lru -> Evict any key using approximated LRU. +# volatile-lfu -> Evict using approximated LFU, only keys with an expire set. +# allkeys-lfu -> Evict any key using approximated LFU. +# volatile-random -> Remove a random key having an expire set. +# allkeys-random -> Remove a random key, any key. +# volatile-ttl -> Remove the key with the nearest expire time (minor TTL) +# noeviction -> Don't evict anything, just return an error on write operations. +# +# LRU means Least Recently Used +# LFU means Least Frequently Used +# +# Both LRU, LFU and volatile-ttl are implemented using approximated +# randomized algorithms. +# +# Note: with any of the above policies, Redis will return an error on write +# operations, when there are no suitable keys for eviction. +# +# At the date of writing these commands are: set setnx setex append +# incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd +# sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby +# zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby +# getset mset msetnx exec sort +# +# The default is: +# +# maxmemory-policy noeviction + +# LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated +# algorithms (in order to save memory), so you can tune it for speed or +# accuracy. By default Redis will check five keys and pick the one that was +# used least recently, you can change the sample size using the following +# configuration directive. +# +# The default of 5 produces good enough results. 10 Approximates very closely +# true LRU but costs more CPU. 3 is faster but not very accurate. +# +# maxmemory-samples 5 + +# Starting from Redis 5, by default a replica will ignore its maxmemory setting +# (unless it is promoted to master after a failover or manually). It means +# that the eviction of keys will be just handled by the master, sending the +# DEL commands to the replica as keys evict in the master side. +# +# This behavior ensures that masters and replicas stay consistent, and is usually +# what you want, however if your replica is writable, or you want the replica +# to have a different memory setting, and you are sure all the writes performed +# to the replica are idempotent, then you may change this default (but be sure +# to understand what you are doing). +# +# Note that since the replica by default does not evict, it may end using more +# memory than the one set via maxmemory (there are certain buffers that may +# be larger on the replica, or data structures may sometimes take more memory +# and so forth). So make sure you monitor your replicas and make sure they +# have enough memory to never hit a real out-of-memory condition before the +# master hits the configured maxmemory setting. +# +# replica-ignore-maxmemory yes + +# Redis reclaims expired keys in two ways: upon access when those keys are +# found to be expired, and also in background, in what is called the +# "active expire key". The key space is slowly and interactively scanned +# looking for expired keys to reclaim, so that it is possible to free memory +# of keys that are expired and will never be accessed again in a short time. +# +# The default effort of the expire cycle will try to avoid having more than +# ten percent of expired keys still in memory, and will try to avoid consuming +# more than 25% of total memory and to add latency to the system. However +# it is possible to increase the expire "effort" that is normally set to +# "1", to a greater value, up to the value "10". At its maximum value the +# system will use more CPU, longer cycles (and technically may introduce +# more latency), and will tolerate less already expired keys still present +# in the system. It's a tradeoff between memory, CPU and latency. +# +# active-expire-effort 1 + +############################# LAZY FREEING #################################### + +# Redis has two primitives to delete keys. One is called DEL and is a blocking +# deletion of the object. It means that the server stops processing new commands +# in order to reclaim all the memory associated with an object in a synchronous +# way. If the key deleted is associated with a small object, the time needed +# in order to execute the DEL command is very small and comparable to most other +# O(1) or O(log_N) commands in Redis. However if the key is associated with an +# aggregated value containing millions of elements, the server can block for +# a long time (even seconds) in order to complete the operation. +# +# For the above reasons Redis also offers non blocking deletion primitives +# such as UNLINK (non blocking DEL) and the ASYNC option of FLUSHALL and +# FLUSHDB commands, in order to reclaim memory in background. Those commands +# are executed in constant time. Another thread will incrementally free the +# object in the background as fast as possible. +# +# DEL, UNLINK and ASYNC option of FLUSHALL and FLUSHDB are user-controlled. +# It's up to the design of the application to understand when it is a good +# idea to use one or the other. However the Redis server sometimes has to +# delete keys or flush the whole database as a side effect of other operations. +# Specifically Redis deletes objects independently of a user call in the +# following scenarios: +# +# 1) On eviction, because of the maxmemory and maxmemory policy configurations, +# in order to make room for new data, without going over the specified +# memory limit. +# 2) Because of expire: when a key with an associated time to live (see the +# EXPIRE command) must be deleted from memory. +# 3) Because of a side effect of a command that stores data on a key that may +# already exist. For example the RENAME command may delete the old key +# content when it is replaced with another one. Similarly SUNIONSTORE +# or SORT with STORE option may delete existing keys. The SET command +# itself removes any old content of the specified key in order to replace +# it with the specified string. +# 4) During replication, when a replica performs a full resynchronization with +# its master, the content of the whole database is removed in order to +# load the RDB file just transferred. +# +# In all the above cases the default is to delete objects in a blocking way, +# like if DEL was called. However you can configure each case specifically +# in order to instead release memory in a non-blocking way like if UNLINK +# was called, using the following configuration directives. + +lazyfree-lazy-eviction no +lazyfree-lazy-expire no +lazyfree-lazy-server-del no +replica-lazy-flush no + +# It is also possible, for the case when to replace the user code DEL calls +# with UNLINK calls is not easy, to modify the default behavior of the DEL +# command to act exactly like UNLINK, using the following configuration +# directive: + +lazyfree-lazy-user-del no + +################################ THREADED I/O ################################# + +# Redis is mostly single threaded, however there are certain threaded +# operations such as UNLINK, slow I/O accesses and other things that are +# performed on side threads. +# +# Now it is also possible to handle Redis clients socket reads and writes +# in different I/O threads. Since especially writing is so slow, normally +# Redis users use pipelining in order to speed up the Redis performances per +# core, and spawn multiple instances in order to scale more. Using I/O +# threads it is possible to easily speedup two times Redis without resorting +# to pipelining nor sharding of the instance. +# +# By default threading is disabled, we suggest enabling it only in machines +# that have at least 4 or more cores, leaving at least one spare core. +# Using more than 8 threads is unlikely to help much. We also recommend using +# threaded I/O only if you actually have performance problems, with Redis +# instances being able to use a quite big percentage of CPU time, otherwise +# there is no point in using this feature. +# +# So for instance if you have a four cores boxes, try to use 2 or 3 I/O +# threads, if you have a 8 cores, try to use 6 threads. In order to +# enable I/O threads use the following configuration directive: +# +# io-threads 4 +# +# Setting io-threads to 1 will just use the main thread as usual. +# When I/O threads are enabled, we only use threads for writes, that is +# to thread the write(2) syscall and transfer the client buffers to the +# socket. However it is also possible to enable threading of reads and +# protocol parsing using the following configuration directive, by setting +# it to yes: +# +# io-threads-do-reads no +# +# Usually threading reads doesn't help much. +# +# NOTE 1: This configuration directive cannot be changed at runtime via +# CONFIG SET. Aso this feature currently does not work when SSL is +# enabled. +# +# NOTE 2: If you want to test the Redis speedup using redis-benchmark, make +# sure you also run the benchmark itself in threaded mode, using the +# --threads option to match the number of Redis threads, otherwise you'll not +# be able to notice the improvements. + +############################ KERNEL OOM CONTROL ############################## + +# On Linux, it is possible to hint the kernel OOM killer on what processes +# should be killed first when out of memory. +# +# Enabling this feature makes Redis actively control the oom_score_adj value +# for all its processes, depending on their role. The default scores will +# attempt to have background child processes killed before all others, and +# replicas killed before masters. + +oom-score-adj no + +# When oom-score-adj is used, this directive controls the specific values used +# for master, replica and background child processes. Values range -1000 to +# 1000 (higher means more likely to be killed). +# +# Unprivileged processes (not root, and without CAP_SYS_RESOURCE capabilities) +# can freely increase their value, but not decrease it below its initial +# settings. +# +# Values are used relative to the initial value of oom_score_adj when the server +# starts. Because typically the initial value is 0, they will often match the +# absolute values. + +oom-score-adj-values 0 200 800 + +############################## APPEND ONLY MODE ############################### + +# By default Redis asynchronously dumps the dataset on disk. This mode is +# good enough in many applications, but an issue with the Redis process or +# a power outage may result into a few minutes of writes lost (depending on +# the configured save points). +# +# The Append Only File is an alternative persistence mode that provides +# much better durability. For instance using the default data fsync policy +# (see later in the config file) Redis can lose just one second of writes in a +# dramatic event like a server power outage, or a single write if something +# wrong with the Redis process itself happens, but the operating system is +# still running correctly. +# +# AOF and RDB persistence can be enabled at the same time without problems. +# If the AOF is enabled on startup Redis will load the AOF, that is the file +# with the better durability guarantees. +# +# Please check http://redis.io/topics/persistence for more information. + +appendonly no + +# The name of the append only file (default: "appendonly.aof") + +appendfilename "appendonly.aof" + +# The fsync() call tells the Operating System to actually write data on disk +# instead of waiting for more data in the output buffer. Some OS will really flush +# data on disk, some other OS will just try to do it ASAP. +# +# Redis supports three different modes: +# +# no: don't fsync, just let the OS flush the data when it wants. Faster. +# always: fsync after every write to the append only log. Slow, Safest. +# everysec: fsync only one time every second. Compromise. +# +# The default is "everysec", as that's usually the right compromise between +# speed and data safety. It's up to you to understand if you can relax this to +# "no" that will let the operating system flush the output buffer when +# it wants, for better performances (but if you can live with the idea of +# some data loss consider the default persistence mode that's snapshotting), +# or on the contrary, use "always" that's very slow but a bit safer than +# everysec. +# +# More details please check the following article: +# http://antirez.com/post/redis-persistence-demystified.html +# +# If unsure, use "everysec". + +# appendfsync always +appendfsync everysec +# appendfsync no + +# When the AOF fsync policy is set to always or everysec, and a background +# saving process (a background save or AOF log background rewriting) is +# performing a lot of I/O against the disk, in some Linux configurations +# Redis may block too long on the fsync() call. Note that there is no fix for +# this currently, as even performing fsync in a different thread will block +# our synchronous write(2) call. +# +# In order to mitigate this problem it's possible to use the following option +# that will prevent fsync() from being called in the main process while a +# BGSAVE or BGREWRITEAOF is in progress. +# +# This means that while another child is saving, the durability of Redis is +# the same as "appendfsync none". In practical terms, this means that it is +# possible to lose up to 30 seconds of log in the worst scenario (with the +# default Linux settings). +# +# If you have latency problems turn this to "yes". Otherwise leave it as +# "no" that is the safest pick from the point of view of durability. + +no-appendfsync-on-rewrite no + +# Automatic rewrite of the append only file. +# Redis is able to automatically rewrite the log file implicitly calling +# BGREWRITEAOF when the AOF log size grows by the specified percentage. +# +# This is how it works: Redis remembers the size of the AOF file after the +# latest rewrite (if no rewrite has happened since the restart, the size of +# the AOF at startup is used). +# +# This base size is compared to the current size. If the current size is +# bigger than the specified percentage, the rewrite is triggered. Also +# you need to specify a minimal size for the AOF file to be rewritten, this +# is useful to avoid rewriting the AOF file even if the percentage increase +# is reached but it is still pretty small. +# +# Specify a percentage of zero in order to disable the automatic AOF +# rewrite feature. + +auto-aof-rewrite-percentage 100 +auto-aof-rewrite-min-size 64mb + +# An AOF file may be found to be truncated at the end during the Redis +# startup process, when the AOF data gets loaded back into memory. +# This may happen when the system where Redis is running +# crashes, especially when an ext4 filesystem is mounted without the +# data=ordered option (however this can't happen when Redis itself +# crashes or aborts but the operating system still works correctly). +# +# Redis can either exit with an error when this happens, or load as much +# data as possible (the default now) and start if the AOF file is found +# to be truncated at the end. The following option controls this behavior. +# +# If aof-load-truncated is set to yes, a truncated AOF file is loaded and +# the Redis server starts emitting a log to inform the user of the event. +# Otherwise if the option is set to no, the server aborts with an error +# and refuses to start. When the option is set to no, the user requires +# to fix the AOF file using the "redis-check-aof" utility before to restart +# the server. +# +# Note that if the AOF file will be found to be corrupted in the middle +# the server will still exit with an error. This option only applies when +# Redis will try to read more data from the AOF file but not enough bytes +# will be found. +aof-load-truncated yes + +# When rewriting the AOF file, Redis is able to use an RDB preamble in the +# AOF file for faster rewrites and recoveries. When this option is turned +# on the rewritten AOF file is composed of two different stanzas: +# +# [RDB file][AOF tail] +# +# When loading, Redis recognizes that the AOF file starts with the "REDIS" +# string and loads the prefixed RDB file, then continues loading the AOF +# tail. +aof-use-rdb-preamble yes + +################################ LUA SCRIPTING ############################### + +# Max execution time of a Lua script in milliseconds. +# +# If the maximum execution time is reached Redis will log that a script is +# still in execution after the maximum allowed time and will start to +# reply to queries with an error. +# +# When a long running script exceeds the maximum execution time only the +# SCRIPT KILL and SHUTDOWN NOSAVE commands are available. The first can be +# used to stop a script that did not yet call any write commands. The second +# is the only way to shut down the server in the case a write command was +# already issued by the script but the user doesn't want to wait for the natural +# termination of the script. +# +# Set it to 0 or a negative value for unlimited execution without warnings. +lua-time-limit 5000 + +################################ REDIS CLUSTER ############################### + +# Normal Redis instances can't be part of a Redis Cluster; only nodes that are +# started as cluster nodes can. In order to start a Redis instance as a +# cluster node enable the cluster support uncommenting the following: +# +# cluster-enabled yes + +# Every cluster node has a cluster configuration file. This file is not +# intended to be edited by hand. It is created and updated by Redis nodes. +# Every Redis Cluster node requires a different cluster configuration file. +# Make sure that instances running in the same system do not have +# overlapping cluster configuration file names. +# +# cluster-config-file nodes-6379.conf + +# Cluster node timeout is the amount of milliseconds a node must be unreachable +# for it to be considered in failure state. +# Most other internal time limits are a multiple of the node timeout. +# +# cluster-node-timeout 15000 + +# A replica of a failing master will avoid to start a failover if its data +# looks too old. +# +# There is no simple way for a replica to actually have an exact measure of +# its "data age", so the following two checks are performed: +# +# 1) If there are multiple replicas able to failover, they exchange messages +# in order to try to give an advantage to the replica with the best +# replication offset (more data from the master processed). +# Replicas will try to get their rank by offset, and apply to the start +# of the failover a delay proportional to their rank. +# +# 2) Every single replica computes the time of the last interaction with +# its master. This can be the last ping or command received (if the master +# is still in the "connected" state), or the time that elapsed since the +# disconnection with the master (if the replication link is currently down). +# If the last interaction is too old, the replica will not try to failover +# at all. +# +# The point "2" can be tuned by user. Specifically a replica will not perform +# the failover if, since the last interaction with the master, the time +# elapsed is greater than: +# +# (node-timeout * cluster-replica-validity-factor) + repl-ping-replica-period +# +# So for example if node-timeout is 30 seconds, and the cluster-replica-validity-factor +# is 10, and assuming a default repl-ping-replica-period of 10 seconds, the +# replica will not try to failover if it was not able to talk with the master +# for longer than 310 seconds. +# +# A large cluster-replica-validity-factor may allow replicas with too old data to failover +# a master, while a too small value may prevent the cluster from being able to +# elect a replica at all. +# +# For maximum availability, it is possible to set the cluster-replica-validity-factor +# to a value of 0, which means, that replicas will always try to failover the +# master regardless of the last time they interacted with the master. +# (However they'll always try to apply a delay proportional to their +# offset rank). +# +# Zero is the only value able to guarantee that when all the partitions heal +# the cluster will always be able to continue. +# +# cluster-replica-validity-factor 10 + +# Cluster replicas are able to migrate to orphaned masters, that are masters +# that are left without working replicas. This improves the cluster ability +# to resist to failures as otherwise an orphaned master can't be failed over +# in case of failure if it has no working replicas. +# +# Replicas migrate to orphaned masters only if there are still at least a +# given number of other working replicas for their old master. This number +# is the "migration barrier". A migration barrier of 1 means that a replica +# will migrate only if there is at least 1 other working replica for its master +# and so forth. It usually reflects the number of replicas you want for every +# master in your cluster. +# +# Default is 1 (replicas migrate only if their masters remain with at least +# one replica). To disable migration just set it to a very large value. +# A value of 0 can be set but is useful only for debugging and dangerous +# in production. +# +# cluster-migration-barrier 1 + +# By default Redis Cluster nodes stop accepting queries if they detect there +# is at least a hash slot uncovered (no available node is serving it). +# This way if the cluster is partially down (for example a range of hash slots +# are no longer covered) all the cluster becomes, eventually, unavailable. +# It automatically returns available as soon as all the slots are covered again. +# +# However sometimes you want the subset of the cluster which is working, +# to continue to accept queries for the part of the key space that is still +# covered. In order to do so, just set the cluster-require-full-coverage +# option to no. +# +# cluster-require-full-coverage yes + +# This option, when set to yes, prevents replicas from trying to failover its +# master during master failures. However the master can still perform a +# manual failover, if forced to do so. +# +# This is useful in different scenarios, especially in the case of multiple +# data center operations, where we want one side to never be promoted if not +# in the case of a total DC failure. +# +# cluster-replica-no-failover no + +# This option, when set to yes, allows nodes to serve read traffic while the +# the cluster is in a down state, as long as it believes it owns the slots. +# +# This is useful for two cases. The first case is for when an application +# doesn't require consistency of data during node failures or network partitions. +# One example of this is a cache, where as long as the node has the data it +# should be able to serve it. +# +# The second use case is for configurations that don't meet the recommended +# three shards but want to enable cluster mode and scale later. A +# master outage in a 1 or 2 shard configuration causes a read/write outage to the +# entire cluster without this option set, with it set there is only a write outage. +# Without a quorum of masters, slot ownership will not change automatically. +# +# cluster-allow-reads-when-down no + +# In order to setup your cluster make sure to read the documentation +# available at http://redis.io web site. + +########################## CLUSTER DOCKER/NAT support ######################## + +# In certain deployments, Redis Cluster nodes address discovery fails, because +# addresses are NAT-ted or because ports are forwarded (the typical case is +# Docker and other containers). +# +# In order to make Redis Cluster working in such environments, a static +# configuration where each node knows its public address is needed. The +# following two options are used for this scope, and are: +# +# * cluster-announce-ip +# * cluster-announce-port +# * cluster-announce-bus-port +# +# Each instructs the node about its address, client port, and cluster message +# bus port. The information is then published in the header of the bus packets +# so that other nodes will be able to correctly map the address of the node +# publishing the information. +# +# If the above options are not used, the normal Redis Cluster auto-detection +# will be used instead. +# +# Note that when remapped, the bus port may not be at the fixed offset of +# clients port + 10000, so you can specify any port and bus-port depending +# on how they get remapped. If the bus-port is not set, a fixed offset of +# 10000 will be used as usual. +# +# Example: +# +# cluster-announce-ip 10.1.1.5 +# cluster-announce-port 6379 +# cluster-announce-bus-port 6380 + +################################## SLOW LOG ################################### + +# The Redis Slow Log is a system to log queries that exceeded a specified +# execution time. The execution time does not include the I/O operations +# like talking with the client, sending the reply and so forth, +# but just the time needed to actually execute the command (this is the only +# stage of command execution where the thread is blocked and can not serve +# other requests in the meantime). +# +# You can configure the slow log with two parameters: one tells Redis +# what is the execution time, in microseconds, to exceed in order for the +# command to get logged, and the other parameter is the length of the +# slow log. When a new command is logged the oldest one is removed from the +# queue of logged commands. + +# The following time is expressed in microseconds, so 1000000 is equivalent +# to one second. Note that a negative number disables the slow log, while +# a value of zero forces the logging of every command. +slowlog-log-slower-than 10000 + +# There is no limit to this length. Just be aware that it will consume memory. +# You can reclaim memory used by the slow log with SLOWLOG RESET. +slowlog-max-len 128 + +################################ LATENCY MONITOR ############################## + +# The Redis latency monitoring subsystem samples different operations +# at runtime in order to collect data related to possible sources of +# latency of a Redis instance. +# +# Via the LATENCY command this information is available to the user that can +# print graphs and obtain reports. +# +# The system only logs operations that were performed in a time equal or +# greater than the amount of milliseconds specified via the +# latency-monitor-threshold configuration directive. When its value is set +# to zero, the latency monitor is turned off. +# +# By default latency monitoring is disabled since it is mostly not needed +# if you don't have latency issues, and collecting data has a performance +# impact, that while very small, can be measured under big load. Latency +# monitoring can easily be enabled at runtime using the command +# "CONFIG SET latency-monitor-threshold " if needed. +latency-monitor-threshold 0 + +############################# EVENT NOTIFICATION ############################## + +# Redis can notify Pub/Sub clients about events happening in the key space. +# This feature is documented at http://redis.io/topics/notifications +# +# For instance if keyspace events notification is enabled, and a client +# performs a DEL operation on key "foo" stored in the Database 0, two +# messages will be published via Pub/Sub: +# +# PUBLISH __keyspace@0__:foo del +# PUBLISH __keyevent@0__:del foo +# +# It is possible to select the events that Redis will notify among a set +# of classes. Every class is identified by a single character: +# +# K Keyspace events, published with __keyspace@__ prefix. +# E Keyevent events, published with __keyevent@__ prefix. +# g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ... +# $ String commands +# l List commands +# s Set commands +# h Hash commands +# z Sorted set commands +# x Expired events (events generated every time a key expires) +# e Evicted events (events generated when a key is evicted for maxmemory) +# t Stream commands +# m Key-miss events (Note: It is not included in the 'A' class) +# A Alias for g$lshzxet, so that the "AKE" string means all the events +# (Except key-miss events which are excluded from 'A' due to their +# unique nature). +# +# The "notify-keyspace-events" takes as argument a string that is composed +# of zero or multiple characters. The empty string means that notifications +# are disabled. +# +# Example: to enable list and generic events, from the point of view of the +# event name, use: +# +# notify-keyspace-events Elg +# +# Example 2: to get the stream of the expired keys subscribing to channel +# name __keyevent@0__:expired use: +# +# notify-keyspace-events Ex +# +# By default all notifications are disabled because most users don't need +# this feature and the feature has some overhead. Note that if you don't +# specify at least one of K or E, no events will be delivered. +notify-keyspace-events "" + +############################### GOPHER SERVER ################################# + +# Redis contains an implementation of the Gopher protocol, as specified in +# the RFC 1436 (https://www.ietf.org/rfc/rfc1436.txt). +# +# The Gopher protocol was very popular in the late '90s. It is an alternative +# to the web, and the implementation both server and client side is so simple +# that the Redis server has just 100 lines of code in order to implement this +# support. +# +# What do you do with Gopher nowadays? Well Gopher never *really* died, and +# lately there is a movement in order for the Gopher more hierarchical content +# composed of just plain text documents to be resurrected. Some want a simpler +# internet, others believe that the mainstream internet became too much +# controlled, and it's cool to create an alternative space for people that +# want a bit of fresh air. +# +# Anyway for the 10nth birthday of the Redis, we gave it the Gopher protocol +# as a gift. +# +# --- HOW IT WORKS? --- +# +# The Redis Gopher support uses the inline protocol of Redis, and specifically +# two kind of inline requests that were anyway illegal: an empty request +# or any request that starts with "/" (there are no Redis commands starting +# with such a slash). Normal RESP2/RESP3 requests are completely out of the +# path of the Gopher protocol implementation and are served as usual as well. +# +# If you open a connection to Redis when Gopher is enabled and send it +# a string like "/foo", if there is a key named "/foo" it is served via the +# Gopher protocol. +# +# In order to create a real Gopher "hole" (the name of a Gopher site in Gopher +# talking), you likely need a script like the following: +# +# https://github.com/antirez/gopher2redis +# +# --- SECURITY WARNING --- +# +# If you plan to put Redis on the internet in a publicly accessible address +# to server Gopher pages MAKE SURE TO SET A PASSWORD to the instance. +# Once a password is set: +# +# 1. The Gopher server (when enabled, not by default) will still serve +# content via Gopher. +# 2. However other commands cannot be called before the client will +# authenticate. +# +# So use the 'requirepass' option to protect your instance. +# +# Note that Gopher is not currently supported when 'io-threads-do-reads' +# is enabled. +# +# To enable Gopher support, uncomment the following line and set the option +# from no (the default) to yes. +# +# gopher-enabled no + +############################### ADVANCED CONFIG ############################### + +# Hashes are encoded using a memory efficient data structure when they have a +# small number of entries, and the biggest entry does not exceed a given +# threshold. These thresholds can be configured using the following directives. +hash-max-ziplist-entries 512 +hash-max-ziplist-value 64 + +# Lists are also encoded in a special way to save a lot of space. +# The number of entries allowed per internal list node can be specified +# as a fixed maximum size or a maximum number of elements. +# For a fixed maximum size, use -5 through -1, meaning: +# -5: max size: 64 Kb <-- not recommended for normal workloads +# -4: max size: 32 Kb <-- not recommended +# -3: max size: 16 Kb <-- probably not recommended +# -2: max size: 8 Kb <-- good +# -1: max size: 4 Kb <-- good +# Positive numbers mean store up to _exactly_ that number of elements +# per list node. +# The highest performing option is usually -2 (8 Kb size) or -1 (4 Kb size), +# but if your use case is unique, adjust the settings as necessary. +list-max-ziplist-size -2 + +# Lists may also be compressed. +# Compress depth is the number of quicklist ziplist nodes from *each* side of +# the list to *exclude* from compression. The head and tail of the list +# are always uncompressed for fast push/pop operations. Settings are: +# 0: disable all list compression +# 1: depth 1 means "don't start compressing until after 1 node into the list, +# going from either the head or tail" +# So: [head]->node->node->...->node->[tail] +# [head], [tail] will always be uncompressed; inner nodes will compress. +# 2: [head]->[next]->node->node->...->node->[prev]->[tail] +# 2 here means: don't compress head or head->next or tail->prev or tail, +# but compress all nodes between them. +# 3: [head]->[next]->[next]->node->node->...->node->[prev]->[prev]->[tail] +# etc. +list-compress-depth 0 + +# Sets have a special encoding in just one case: when a set is composed +# of just strings that happen to be integers in radix 10 in the range +# of 64 bit signed integers. +# The following configuration setting sets the limit in the size of the +# set in order to use this special memory saving encoding. +set-max-intset-entries 512 + +# Similarly to hashes and lists, sorted sets are also specially encoded in +# order to save a lot of space. This encoding is only used when the length and +# elements of a sorted set are below the following limits: +zset-max-ziplist-entries 128 +zset-max-ziplist-value 64 + +# HyperLogLog sparse representation bytes limit. The limit includes the +# 16 bytes header. When an HyperLogLog using the sparse representation crosses +# this limit, it is converted into the dense representation. +# +# A value greater than 16000 is totally useless, since at that point the +# dense representation is more memory efficient. +# +# The suggested value is ~ 3000 in order to have the benefits of +# the space efficient encoding without slowing down too much PFADD, +# which is O(N) with the sparse encoding. The value can be raised to +# ~ 10000 when CPU is not a concern, but space is, and the data set is +# composed of many HyperLogLogs with cardinality in the 0 - 15000 range. +hll-sparse-max-bytes 3000 + +# Streams macro node max size / items. The stream data structure is a radix +# tree of big nodes that encode multiple items inside. Using this configuration +# it is possible to configure how big a single node can be in bytes, and the +# maximum number of items it may contain before switching to a new node when +# appending new stream entries. If any of the following settings are set to +# zero, the limit is ignored, so for instance it is possible to set just a +# max entires limit by setting max-bytes to 0 and max-entries to the desired +# value. +stream-node-max-bytes 4kb +stream-node-max-entries 100 + +# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in +# order to help rehashing the main Redis hash table (the one mapping top-level +# keys to values). The hash table implementation Redis uses (see dict.c) +# performs a lazy rehashing: the more operation you run into a hash table +# that is rehashing, the more rehashing "steps" are performed, so if the +# server is idle the rehashing is never complete and some more memory is used +# by the hash table. +# +# The default is to use this millisecond 10 times every second in order to +# actively rehash the main dictionaries, freeing memory when possible. +# +# If unsure: +# use "activerehashing no" if you have hard latency requirements and it is +# not a good thing in your environment that Redis can reply from time to time +# to queries with 2 milliseconds delay. +# +# use "activerehashing yes" if you don't have such hard requirements but +# want to free memory asap when possible. +activerehashing yes + +# The client output buffer limits can be used to force disconnection of clients +# that are not reading data from the server fast enough for some reason (a +# common reason is that a Pub/Sub client can't consume messages as fast as the +# publisher can produce them). +# +# The limit can be set differently for the three different classes of clients: +# +# normal -> normal clients including MONITOR clients +# replica -> replica clients +# pubsub -> clients subscribed to at least one pubsub channel or pattern +# +# The syntax of every client-output-buffer-limit directive is the following: +# +# client-output-buffer-limit +# +# A client is immediately disconnected once the hard limit is reached, or if +# the soft limit is reached and remains reached for the specified number of +# seconds (continuously). +# So for instance if the hard limit is 32 megabytes and the soft limit is +# 16 megabytes / 10 seconds, the client will get disconnected immediately +# if the size of the output buffers reach 32 megabytes, but will also get +# disconnected if the client reaches 16 megabytes and continuously overcomes +# the limit for 10 seconds. +# +# By default normal clients are not limited because they don't receive data +# without asking (in a push way), but just after a request, so only +# asynchronous clients may create a scenario where data is requested faster +# than it can read. +# +# Instead there is a default limit for pubsub and replica clients, since +# subscribers and replicas receive data in a push fashion. +# +# Both the hard or the soft limit can be disabled by setting them to zero. +client-output-buffer-limit normal 0 0 0 +client-output-buffer-limit replica 256mb 64mb 60 +client-output-buffer-limit pubsub 32mb 8mb 60 + +# Client query buffers accumulate new commands. They are limited to a fixed +# amount by default in order to avoid that a protocol desynchronization (for +# instance due to a bug in the client) will lead to unbound memory usage in +# the query buffer. However you can configure it here if you have very special +# needs, such us huge multi/exec requests or alike. +# +# client-query-buffer-limit 1gb + +# In the Redis protocol, bulk requests, that are, elements representing single +# strings, are normally limited to 512 mb. However you can change this limit +# here, but must be 1mb or greater +# +# proto-max-bulk-len 512mb + +# Redis calls an internal function to perform many background tasks, like +# closing connections of clients in timeout, purging expired keys that are +# never requested, and so forth. +# +# Not all tasks are performed with the same frequency, but Redis checks for +# tasks to perform according to the specified "hz" value. +# +# By default "hz" is set to 10. Raising the value will use more CPU when +# Redis is idle, but at the same time will make Redis more responsive when +# there are many keys expiring at the same time, and timeouts may be +# handled with more precision. +# +# The range is between 1 and 500, however a value over 100 is usually not +# a good idea. Most users should use the default of 10 and raise this up to +# 100 only in environments where very low latency is required. +hz 10 + +# Normally it is useful to have an HZ value which is proportional to the +# number of clients connected. This is useful in order, for instance, to +# avoid too many clients are processed for each background task invocation +# in order to avoid latency spikes. +# +# Since the default HZ value by default is conservatively set to 10, Redis +# offers, and enables by default, the ability to use an adaptive HZ value +# which will temporarily raise when there are many connected clients. +# +# When dynamic HZ is enabled, the actual configured HZ will be used +# as a baseline, but multiples of the configured HZ value will be actually +# used as needed once more clients are connected. In this way an idle +# instance will use very little CPU time while a busy instance will be +# more responsive. +dynamic-hz yes + +# When a child rewrites the AOF file, if the following option is enabled +# the file will be fsync-ed every 32 MB of data generated. This is useful +# in order to commit the file to the disk more incrementally and avoid +# big latency spikes. +aof-rewrite-incremental-fsync yes + +# When redis saves RDB file, if the following option is enabled +# the file will be fsync-ed every 32 MB of data generated. This is useful +# in order to commit the file to the disk more incrementally and avoid +# big latency spikes. +rdb-save-incremental-fsync yes + +# Redis LFU eviction (see maxmemory setting) can be tuned. However it is a good +# idea to start with the default settings and only change them after investigating +# how to improve the performances and how the keys LFU change over time, which +# is possible to inspect via the OBJECT FREQ command. +# +# There are two tunable parameters in the Redis LFU implementation: the +# counter logarithm factor and the counter decay time. It is important to +# understand what the two parameters mean before changing them. +# +# The LFU counter is just 8 bits per key, it's maximum value is 255, so Redis +# uses a probabilistic increment with logarithmic behavior. Given the value +# of the old counter, when a key is accessed, the counter is incremented in +# this way: +# +# 1. A random number R between 0 and 1 is extracted. +# 2. A probability P is calculated as 1/(old_value*lfu_log_factor+1). +# 3. The counter is incremented only if R < P. +# +# The default lfu-log-factor is 10. This is a table of how the frequency +# counter changes with a different number of accesses with different +# logarithmic factors: +# +# +--------+------------+------------+------------+------------+------------+ +# | factor | 100 hits | 1000 hits | 100K hits | 1M hits | 10M hits | +# +--------+------------+------------+------------+------------+------------+ +# | 0 | 104 | 255 | 255 | 255 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# | 1 | 18 | 49 | 255 | 255 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# | 10 | 10 | 18 | 142 | 255 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# | 100 | 8 | 11 | 49 | 143 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# +# NOTE: The above table was obtained by running the following commands: +# +# redis-benchmark -n 1000000 incr foo +# redis-cli object freq foo +# +# NOTE 2: The counter initial value is 5 in order to give new objects a chance +# to accumulate hits. +# +# The counter decay time is the time, in minutes, that must elapse in order +# for the key counter to be divided by two (or decremented if it has a value +# less <= 10). +# +# The default value for the lfu-decay-time is 1. A special value of 0 means to +# decay the counter every time it happens to be scanned. +# +# lfu-log-factor 10 +# lfu-decay-time 1 + +########################### ACTIVE DEFRAGMENTATION ####################### +# +# What is active defragmentation? +# ------------------------------- +# +# Active (online) defragmentation allows a Redis server to compact the +# spaces left between small allocations and deallocations of data in memory, +# thus allowing to reclaim back memory. +# +# Fragmentation is a natural process that happens with every allocator (but +# less so with Jemalloc, fortunately) and certain workloads. Normally a server +# restart is needed in order to lower the fragmentation, or at least to flush +# away all the data and create it again. However thanks to this feature +# implemented by Oran Agra for Redis 4.0 this process can happen at runtime +# in a "hot" way, while the server is running. +# +# Basically when the fragmentation is over a certain level (see the +# configuration options below) Redis will start to create new copies of the +# values in contiguous memory regions by exploiting certain specific Jemalloc +# features (in order to understand if an allocation is causing fragmentation +# and to allocate it in a better place), and at the same time, will release the +# old copies of the data. This process, repeated incrementally for all the keys +# will cause the fragmentation to drop back to normal values. +# +# Important things to understand: +# +# 1. This feature is disabled by default, and only works if you compiled Redis +# to use the copy of Jemalloc we ship with the source code of Redis. +# This is the default with Linux builds. +# +# 2. You never need to enable this feature if you don't have fragmentation +# issues. +# +# 3. Once you experience fragmentation, you can enable this feature when +# needed with the command "CONFIG SET activedefrag yes". +# +# The configuration parameters are able to fine tune the behavior of the +# defragmentation process. If you are not sure about what they mean it is +# a good idea to leave the defaults untouched. + +# Enabled active defragmentation +# activedefrag no + +# Minimum amount of fragmentation waste to start active defrag +# active-defrag-ignore-bytes 100mb + +# Minimum percentage of fragmentation to start active defrag +# active-defrag-threshold-lower 10 + +# Maximum percentage of fragmentation at which we use maximum effort +# active-defrag-threshold-upper 100 + +# Minimal effort for defrag in CPU percentage, to be used when the lower +# threshold is reached +# active-defrag-cycle-min 1 + +# Maximal effort for defrag in CPU percentage, to be used when the upper +# threshold is reached +# active-defrag-cycle-max 25 + +# Maximum number of set/hash/zset/list fields that will be processed from +# the main dictionary scan +# active-defrag-max-scan-fields 1000 + +# Jemalloc background thread for purging will be enabled by default +jemalloc-bg-thread yes + +# It is possible to pin different threads and processes of Redis to specific +# CPUs in your system, in order to maximize the performances of the server. +# This is useful both in order to pin different Redis threads in different +# CPUs, but also in order to make sure that multiple Redis instances running +# in the same host will be pinned to different CPUs. +# +# Normally you can do this using the "taskset" command, however it is also +# possible to this via Redis configuration directly, both in Linux and FreeBSD. +# +# You can pin the server/IO threads, bio threads, aof rewrite child process, and +# the bgsave child process. The syntax to specify the cpu list is the same as +# the taskset command: +# +# Set redis server/io threads to cpu affinity 0,2,4,6: +# server_cpulist 0-7:2 +# +# Set bio threads to cpu affinity 1,3: +# bio_cpulist 1,3 +# +# Set aof rewrite child process to cpu affinity 8,9,10,11: +# aof_rewrite_cpulist 8-11 +# +# Set bgsave child process to cpu affinity 1,10,11 +# bgsave_cpulist 1,10-11 +# Generated by CONFIG REWRITE +user default on nopass sanitize-payload ~* &* +@all + diff --git a/08cache/ha/conf/sentinel0.conf b/08cache/ha/conf/sentinel0.conf new file mode 100644 index 00000000..eb9f8db1 --- /dev/null +++ b/08cache/ha/conf/sentinel0.conf @@ -0,0 +1,14 @@ +sentinel myid 8d992c54df8f8677b0b345825f61fb733c73d14c +sentinel deny-scripts-reconfig yes +sentinel monitor mymaster 127.0.0.1 6380 1 +sentinel down-after-milliseconds mymaster 2000 +# Generated by CONFIG REWRITE +protected-mode no +port 26379 +user default on nopass sanitize-payload ~* &* +@all +dir "/Users/kimmking/kimmking/JavaCourseCodes/08cache/ha/conf" +sentinel config-epoch mymaster 4 +sentinel leader-epoch mymaster 5 +sentinel known-sentinel mymaster 127.0.0.1 26380 8d992c54df8f8677b0b345825f61fb733c73d14d +sentinel current-epoch 5 +sentinel known-replica mymaster 127.0.0.1 6379 diff --git a/08cache/ha/conf/sentinel1.conf b/08cache/ha/conf/sentinel1.conf new file mode 100644 index 00000000..0c35a846 --- /dev/null +++ b/08cache/ha/conf/sentinel1.conf @@ -0,0 +1,14 @@ +sentinel myid 8d992c54df8f8677b0b345825f61fb733c73d14d +sentinel deny-scripts-reconfig yes +sentinel monitor mymaster 127.0.0.1 6380 1 +port 26380 +sentinel down-after-milliseconds mymaster 2000 +# Generated by CONFIG REWRITE +protected-mode no +user default on nopass sanitize-payload ~* &* +@all +dir "/Users/kimmking/kimmking/JavaCourseCodes/08cache/ha/conf" +sentinel config-epoch mymaster 4 +sentinel leader-epoch mymaster 5 +sentinel known-sentinel mymaster 127.0.0.1 26379 8d992c54df8f8677b0b345825f61fb733c73d14c +sentinel current-epoch 5 +sentinel known-replica mymaster 127.0.0.1 6379 diff --git a/08cache/redis/pom.xml b/08cache/redis/pom.xml new file mode 100644 index 00000000..6f0603dd --- /dev/null +++ b/08cache/redis/pom.xml @@ -0,0 +1,85 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.0.9.RELEASE + + + io.kimmking.08cache + redis + 0.0.1-SNAPSHOT + redis + Demo project for Spring Boot + + + 1.8 + + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + + + + redis.clients + jedis + + + + com.alibaba + fastjson + 1.2.70 + + + + org.redisson + redisson-spring-boot-starter + 3.14.1 + + + + com.hazelcast + hazelcast + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/08cache/redis/src/main/java/io/kimmking/cache/RedisApplication.java b/08cache/redis/src/main/java/io/kimmking/cache/RedisApplication.java new file mode 100644 index 00000000..f685a7b8 --- /dev/null +++ b/08cache/redis/src/main/java/io/kimmking/cache/RedisApplication.java @@ -0,0 +1,74 @@ +package io.kimmking.cache; + +import com.alibaba.fastjson.JSON; +import io.kimmking.cache.cluster.ClusterJedis; +import io.kimmking.cache.sentinel.SentinelJedis; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisCluster; + +import java.util.List; +import java.util.Map; + +@SpringBootApplication(scanBasePackages = "io.kimmking.cache") +public class RedisApplication { + + public static void main(String[] args) { + + // C1.最简单demo + Jedis jedis = new Jedis("localhost", 6379); + System.out.println(jedis.info()); + jedis.set("uptime", new Long(System.currentTimeMillis()).toString()); + System.out.println(jedis.get("uptime")); + jedis.set("teacher", "Cuijing"); + System.out.println(jedis.get("teacher")); +// +// // C2.基于sentinel和连接池的demo +// Jedis sjedis = SentinelJedis.getJedis(); +// System.out.println(sjedis.info()); +// sjedis.set("uptime2", new Long(System.currentTimeMillis()).toString()); +// System.out.println(sjedis.get("uptime2")); +// SentinelJedis.close(); +// +// // C3. 直接连接sentinel进行操作 +// Jedis jedisSentinel = new Jedis("localhost", 26379); // 连接到sentinel +// List> masters = jedisSentinel.sentinelMasters(); +// System.out.println(JSON.toJSONString(masters)); +// List> slaves = jedisSentinel.sentinelSlaves("mymaster"); +// System.out.println(JSON.toJSONString(slaves)); + + + // 作业: + // 1. 参考C2,实现基于Lettuce和Redission的Sentinel配置 + // 2. 实现springboot/spring data redis的sentinel配置 + // 3. 使用jedis命令,使用java代码手动切换 redis 主从 + // Jedis jedis1 = new Jedis("localhost", 6379); + // jedis1.info... + // jedis1.set xxx... + // Jedis jedis2 = new Jedis("localhost", 6380); + // jedis2.slaveof... + // jedis2.get xxx + // 4. 使用C3的方式,使用java代码手动操作sentinel + + + // C4. Redis Cluster + // 作业: + // 5.使用命令行配置Redis cluster: + // 1) 以cluster方式启动redis-server + // 2) 用meet,添加cluster节点,确认集群节点数目 + // 3) 分配槽位,确认分配成功 + // 4) 测试简单的get/set是否成功 + // 然后运行如下代码 +// JedisCluster cluster = ClusterJedis.getJedisCluster(); +// for (int i = 0; i < 100; i++) { +// cluster.set("cluster:" + i, "data:" + i); +// } +// System.out.println(cluster.get("cluster:92")); +// ClusterJedis.close(); + + //SpringApplication.run(RedisApplication.class, args); + + } + +} diff --git a/08cache/redis/src/main/java/io/kimmking/cache/cluster/ClusterJedis.java b/08cache/redis/src/main/java/io/kimmking/cache/cluster/ClusterJedis.java new file mode 100644 index 00000000..162284f7 --- /dev/null +++ b/08cache/redis/src/main/java/io/kimmking/cache/cluster/ClusterJedis.java @@ -0,0 +1,48 @@ +package io.kimmking.cache.cluster; + +import lombok.SneakyThrows; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.JedisPoolConfig; + +import java.util.HashSet; +import java.util.Set; + +public final class ClusterJedis { + + private static JedisCluster CLUSTER = createJedisCluster(); + + private static JedisCluster createJedisCluster() { + JedisCluster jedisCluster = null; + // 添加集群的服务节点Set集合 + Set hostAndPortsSet = new HashSet(); + // 添加节点 + hostAndPortsSet.add(new HostAndPort("127.0.0.1", 6379)); + hostAndPortsSet.add(new HostAndPort("127.0.0.1", 6380)); + // hostAndPortsSet.add(new HostAndPort("127.0.0.1", 6381)); + + // Jedis连接池配置 + JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); + // 最大空闲连接数, 默认8个 + jedisPoolConfig.setMaxIdle(12); + // 最大连接数, 默认8个 + jedisPoolConfig.setMaxTotal(16); + //最小空闲连接数, 默认0 + jedisPoolConfig.setMinIdle(4); + // 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1 + jedisPoolConfig.setMaxWaitMillis(2000); // 设置2秒 + //对拿到的connection进行validateObject校验 + jedisPoolConfig.setTestOnBorrow(true); + jedisCluster = new JedisCluster(hostAndPortsSet, jedisPoolConfig); + return jedisCluster; + } + + public static JedisCluster getJedisCluster() { + return CLUSTER; + } + + @SneakyThrows + public static void close(){ + CLUSTER.close(); + } +} diff --git a/08cache/redis/src/main/java/io/kimmking/cache/controller/UserController.java b/08cache/redis/src/main/java/io/kimmking/cache/controller/UserController.java new file mode 100644 index 00000000..3bbbf988 --- /dev/null +++ b/08cache/redis/src/main/java/io/kimmking/cache/controller/UserController.java @@ -0,0 +1,26 @@ +package io.kimmking.cache.controller; + +import io.kimmking.cache.entity.User; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; +import java.util.List; + +@RestController +@EnableAutoConfiguration +public class UserController { + + + @RequestMapping("/user/find") + User find(Integer id) { + return new User(1,"KK", 28); + } + + @RequestMapping("/user/list") + List list() { + return Arrays.asList(new User(1,"KK", 28), + new User(2,"CC", 18)); + } +} \ No newline at end of file diff --git a/08cache/redis/src/main/java/io/kimmking/cache/entity/User.java b/08cache/redis/src/main/java/io/kimmking/cache/entity/User.java new file mode 100644 index 00000000..e3b5dadd --- /dev/null +++ b/08cache/redis/src/main/java/io/kimmking/cache/entity/User.java @@ -0,0 +1,14 @@ +package io.kimmking.cache.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class User { + private Integer id; + private String name; + private Integer age; +} \ No newline at end of file diff --git a/08cache/redis/src/main/java/io/kimmking/cache/hazelcast/HazelcastDemo.java b/08cache/redis/src/main/java/io/kimmking/cache/hazelcast/HazelcastDemo.java new file mode 100644 index 00000000..919bd90b --- /dev/null +++ b/08cache/redis/src/main/java/io/kimmking/cache/hazelcast/HazelcastDemo.java @@ -0,0 +1,9 @@ +package io.kimmking.cache.hazelcast; + +public class HazelcastDemo { + + public static void main(String[] args) { + + } + +} diff --git a/08cache/redis/src/main/java/io/kimmking/cache/redission/RedissionDemo.java b/08cache/redis/src/main/java/io/kimmking/cache/redission/RedissionDemo.java new file mode 100644 index 00000000..bd86f2fb --- /dev/null +++ b/08cache/redis/src/main/java/io/kimmking/cache/redission/RedissionDemo.java @@ -0,0 +1,46 @@ +package io.kimmking.cache.redission; + +import lombok.SneakyThrows; +import org.redisson.Redisson; +import org.redisson.RedissonMap; +import org.redisson.api.RLock; +import org.redisson.api.RMap; +import org.redisson.api.RedissonClient; +import org.redisson.config.Config; + +public class RedissionDemo { + + @SneakyThrows + public static void main(String[] args) { + Config config = new Config(); + config.useSingleServer().setAddress("redis://127.0.0.1:6379"); + + final RedissonClient client = Redisson.create(config); + RMap rmap = client.getMap("map1"); + RLock lock = client.getLock("lock1"); + + try{ + lock.lock(); + + for (int i = 0; i < 15; i++) { + rmap.put("rkey:"+i, "111rvalue:"+i); + } + + // 如果代码块 W1 在这里会怎么样? + // 代码块 W1 + while(true) { + Thread.sleep(2000); + System.out.println(rmap.get("rkey:10")); + } + + }finally{ + lock.unlock(); + } + + + + } + + // 可参阅:https://www.jianshu.com/p/47fd7f86c848 + +} diff --git a/08cache/redis/src/main/java/io/kimmking/cache/redission/RedissionDemo1.java b/08cache/redis/src/main/java/io/kimmking/cache/redission/RedissionDemo1.java new file mode 100644 index 00000000..8534b3c8 --- /dev/null +++ b/08cache/redis/src/main/java/io/kimmking/cache/redission/RedissionDemo1.java @@ -0,0 +1,34 @@ +package io.kimmking.cache.redission; + +import org.redisson.Redisson; +import org.redisson.api.RLock; +import org.redisson.api.RMap; +import org.redisson.api.RedissonClient; +import org.redisson.config.Config; + +public class RedissionDemo1 { + + public static void main(String[] args) { + Config config = new Config(); + config.useSingleServer().setAddress("redis://127.0.0.1:6379"); + + final RedissonClient client = Redisson.create(config); + RLock lock = client.getLock("lock1"); + + try{ + lock.lock(); + + RMap rmap = client.getMap("map1"); + + for (int i = 0; i < 15; i++) { + rmap.put("rkey:"+i, "rvalue:22222-"+i); + } + + System.out.println(rmap.get("rkey:10")); + + }finally{ + lock.unlock(); + } + } + +} diff --git a/08cache/redis/src/main/java/io/kimmking/cache/sentinel/SentinelJedis.java b/08cache/redis/src/main/java/io/kimmking/cache/sentinel/SentinelJedis.java new file mode 100644 index 00000000..79a6819c --- /dev/null +++ b/08cache/redis/src/main/java/io/kimmking/cache/sentinel/SentinelJedis.java @@ -0,0 +1,66 @@ +package io.kimmking.cache.sentinel; + +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPoolConfig; +import redis.clients.jedis.JedisSentinelPool; + +import java.util.HashSet; +import java.util.Set; + +public final class SentinelJedis { + + //可用连接实例的最大数目,默认为8; + //如果赋值为-1,则表示不限制,如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽) + private static Integer MAX_TOTAL = 16; + //控制一个pool最多有多少个状态为idle(空闲)的jedis实例,默认值是8 + private static Integer MAX_IDLE = 12; + //等待可用连接的最大时间,单位是毫秒,默认值为-1,表示永不超时。 + //如果超过等待时间,则直接抛出JedisConnectionException + private static Integer MAX_WAIT_MILLIS = 10000; + //客户端超时时间配置 + private static Integer TIMEOUT = 10000; + //在borrow(用)一个jedis实例时,是否提前进行validate(验证)操作; + //如果为true,则得到的jedis实例均是可用的 + private static Boolean TEST_ON_BORROW = true; + //在空闲时检查有效性, 默认false + private static Boolean TEST_WHILE_IDLE = true; + //是否进行有效性检查 + private static Boolean TEST_ON_RETURN = true; + + private static JedisSentinelPool POOL = createJedisPool(); + + /** + * 创建连接池 + */ + private static JedisSentinelPool createJedisPool() { + JedisPoolConfig config = new JedisPoolConfig(); + /*注意: + 在高版本的jedis jar包,比如本版本2.9.0,JedisPoolConfig没有setMaxActive和setMaxWait属性了 + 这是因为高版本中官方废弃了此方法,用以下两个属性替换。 + maxActive ==> maxTotal + maxWait==> maxWaitMillis + */ + config.setMaxTotal(MAX_TOTAL); + config.setMaxIdle(MAX_IDLE); + config.setMaxWaitMillis(MAX_WAIT_MILLIS); + config.setTestOnBorrow(TEST_ON_BORROW); + config.setTestWhileIdle(TEST_WHILE_IDLE); + config.setTestOnReturn(TEST_ON_RETURN); + String masterName = "mymaster"; + Set sentinels = new HashSet(); + sentinels.add(new HostAndPort("127.0.0.1",26379).toString()); + sentinels.add(new HostAndPort("127.0.0.1",26380).toString()); + JedisSentinelPool pool = new JedisSentinelPool(masterName, sentinels, config, TIMEOUT, null); + return pool; + } + + public static Jedis getJedis() { + return POOL.getResource(); + } + + public static void close(){ + POOL.close(); + } + +} diff --git a/08cache/redis/src/main/resources/application.yml b/08cache/redis/src/main/resources/application.yml new file mode 100644 index 00000000..673cb414 --- /dev/null +++ b/08cache/redis/src/main/resources/application.yml @@ -0,0 +1,10 @@ +server: + port: 8080 + +logging: + level: + io: + kimmking: + cache : info + +# 作业,在这里使用spring boot配置各项内容, diff --git a/09mq/.gitignore b/09mq/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/09mq/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/09mq/.mvn/wrapper/MavenWrapperDownloader.java b/09mq/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..a45eb6ba --- /dev/null +++ b/09mq/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/09mq/.mvn/wrapper/maven-wrapper.jar b/09mq/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/09mq/.mvn/wrapper/maven-wrapper.jar differ diff --git a/09mq/.mvn/wrapper/maven-wrapper.properties b/09mq/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/09mq/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/09mq/activemq-demo/mvnw b/09mq/activemq-demo/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/09mq/activemq-demo/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/09mq/activemq-demo/mvnw.cmd b/09mq/activemq-demo/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/09mq/activemq-demo/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/09mq/activemq-demo/pom.xml b/09mq/activemq-demo/pom.xml new file mode 100644 index 00000000..c3352476 --- /dev/null +++ b/09mq/activemq-demo/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.0.RELEASE + + + io.kimmking.javacourse.mq + activemq-demo + 0.0.1-SNAPSHOT + activemq-demo + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.apache.activemq + activemq-all + 5.16.0 + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/09mq/activemq-demo/src/main/java/io/kimmking/javacourse/mq/activemq/ActiveMQServer.java b/09mq/activemq-demo/src/main/java/io/kimmking/javacourse/mq/activemq/ActiveMQServer.java new file mode 100644 index 00000000..b2b6a252 --- /dev/null +++ b/09mq/activemq-demo/src/main/java/io/kimmking/javacourse/mq/activemq/ActiveMQServer.java @@ -0,0 +1,9 @@ +package io.kimmking.javacourse.mq.activemq; + +public class ActiveMQServer { + + public static void main(String[] args) { + // 尝试用java代码启动一个ActiveMQ broker server + // 然后用前面的测试demo代码,连接这个嵌入式的server + } +} diff --git a/09mq/activemq-demo/src/main/java/io/kimmking/javacourse/mq/activemq/ActivemqApplication.java b/09mq/activemq-demo/src/main/java/io/kimmking/javacourse/mq/activemq/ActivemqApplication.java new file mode 100644 index 00000000..834f015f --- /dev/null +++ b/09mq/activemq-demo/src/main/java/io/kimmking/javacourse/mq/activemq/ActivemqApplication.java @@ -0,0 +1,74 @@ +package io.kimmking.javacourse.mq.activemq; + +import org.apache.activemq.ActiveMQConnection; +import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.command.ActiveMQQueue; +import org.apache.activemq.command.ActiveMQTopic; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import javax.jms.*; +import java.util.concurrent.atomic.AtomicInteger; + + +//@SpringBootApplication +public class ActivemqApplication { + + public static void main(String[] args) { + + // 定义Destination + Destination destination = new ActiveMQTopic("test.topic"); + // Destination destination = new ActiveMQQueue("test.queue"); + + testDestination(destination); + + //SpringApplication.run(ActivemqApplication.class, args); + } + + public static void testDestination(Destination destination) { + try { + // 创建连接和会话 + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"); + ActiveMQConnection conn = (ActiveMQConnection) factory.createConnection(); + conn.start(); + Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); + + // 创建消费者 + MessageConsumer consumer = session.createConsumer( destination ); + final AtomicInteger count = new AtomicInteger(0); + MessageListener listener = new MessageListener() { + public void onMessage(Message message) { + try { + // 打印所有的消息内容 + // Thread.sleep(); + System.out.println(count.incrementAndGet() + " => receive from " + destination.toString() + ": " + message); + // message.acknowledge(); // 前面所有未被确认的消息全部都确认。 + + } catch (Exception e) { + e.printStackTrace(); // 不要吞任何这里的异常, + } + } + }; + // 绑定消息监听器 + consumer.setMessageListener(listener); + + //consumer.receive() + + // 创建生产者,生产100个消息 + MessageProducer producer = session.createProducer(destination); + int index = 0; + while (index++ < 100) { + TextMessage message = session.createTextMessage(index + " message."); + producer.send(message); + } + + Thread.sleep(20000); + session.close(); + conn.close(); + + + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/09mq/activemq-demo/src/main/resources/application.properties b/09mq/activemq-demo/src/main/resources/application.properties new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/09mq/activemq-demo/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/09mq/activemq-demo/src/test/java/io/kimmking/javacourse/mq/activemq/ActivemqApplicationTests.java b/09mq/activemq-demo/src/test/java/io/kimmking/javacourse/mq/activemq/ActivemqApplicationTests.java new file mode 100644 index 00000000..b4559d51 --- /dev/null +++ b/09mq/activemq-demo/src/test/java/io/kimmking/javacourse/mq/activemq/ActivemqApplicationTests.java @@ -0,0 +1,13 @@ +//package io.kimmking.javacourse.mq.activemq; +// +//import org.junit.jupiter.api.Test; +//import org.springframework.boot.test.context.SpringBootTest; +// +//@SpringBootTest +//class ActivemqApplicationTests { +// +// @Test +// void contextLoads() { +// } +// +//} diff --git a/09mq/kafka-demo/.gitignore b/09mq/kafka-demo/.gitignore new file mode 100644 index 00000000..868f9075 --- /dev/null +++ b/09mq/kafka-demo/.gitignore @@ -0,0 +1,26 @@ +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +.idea +*.iml diff --git a/09mq/kafka-demo/LICENSE b/09mq/kafka-demo/LICENSE new file mode 100644 index 00000000..261eeb9e --- /dev/null +++ b/09mq/kafka-demo/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/09mq/kafka-demo/README.md b/09mq/kafka-demo/README.md new file mode 100644 index 00000000..e0bb2acc --- /dev/null +++ b/09mq/kafka-demo/README.md @@ -0,0 +1,37 @@ +# 消息队列培训-Kafka进阶 + +## 作业:具体场景下的Kafka接口设计实现 + +### 背景 +请各位同学结合定序 & 撮合的业务特点, 练习 Producer + Consuer 的用法: +- 定序要求: 数据有序并且不丢失 +- 撮合要求: 可重放, 顺序消费, 消息仅处理一次 + +### 消息格式 +消息的内容格式为:// 见本仓库的代码 +``` +public class Order{ +private Long id; +private Long ts; +private String symbol; +private Double price; + +// 省略setter getter... +} +``` + +### 作业要求 +1. `Producer`接口应该有序发送消息 +2. `Producer`接口应该不丢失消息 +3. `Consumer`接口应该顺序消费消息 +4. `Consumer`接口应该支持从offset重新接收消息 +5. `Consumer`接口应该支持消息的幂等处理,即根据id去重 +6. 设计挑战:尝试接口对外暴露的使用方式,完全屏蔽了kafka的原生接口和类 +7. 编码挑战:实现程序都正确,而且写了单元测试 + +### 提交方式:过程分(3分) +1. 设计一个 `io.kimmking.javacourse.自己姓名拼音.Producer` 接口,需满足以上要求,并尝试实现 +2. 设计一个 `io.kimmking.javacourse.自己姓名拼音.Consumer` 接口,需满足以上要求,并尝试实现 +3. 提交以上代码,并提交一个Pull Request到本仓库,在钉钉群回复已经完成,1分 + + diff --git a/09mq/kafka-demo/pom.xml b/09mq/kafka-demo/pom.xml new file mode 100644 index 00000000..87efa723 --- /dev/null +++ b/09mq/kafka-demo/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + + io.kimmking.javacourse + kafka-demo + 0.0.1-SNAPSHOT + jar + + kafka-demo + Demo project for training-mq-03 + + + org.springframework.boot + spring-boot-starter-parent + 2.0.4.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + + + + org.apache.kafka + kafka_2.12 + 2.6.0 + + + + org.apache.kafka + kafka-clients + 2.6.0 + + + + com.alibaba + fastjson + 1.2.58 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/KafkaConsumerDemo.java b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/KafkaConsumerDemo.java new file mode 100644 index 00000000..a950facc --- /dev/null +++ b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/KafkaConsumerDemo.java @@ -0,0 +1,16 @@ +package io.kimmking.javacourse.kafka; + +import io.kimmking.javacourse.kafka.kimmking.ConsumerImpl; + +public class KafkaConsumerDemo { + + public static void main(String[] args) { + testConsumer(); + } + + private static void testConsumer() { + ConsumerImpl consumer = new ConsumerImpl(); + consumer.consumeOrder(); + + } +} diff --git a/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/KafkaProducerDemo.java b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/KafkaProducerDemo.java new file mode 100644 index 00000000..1467492a --- /dev/null +++ b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/KafkaProducerDemo.java @@ -0,0 +1,18 @@ +package io.kimmking.javacourse.kafka; + +import io.kimmking.javacourse.kafka.kimmking.ProducerImpl; + +public class KafkaProducerDemo { + + public static void main(String[] args) { + testProducer(); + } + + private static void testProducer() { + ProducerImpl producer = new ProducerImpl(); + for (int i = 0; i < 1000; i++) { + producer.send(new Order(1000L + i,System.currentTimeMillis(),"USD2CNY", 6.5d)); + producer.send(new Order(2000L + i,System.currentTimeMillis(),"USD2CNY", 6.51d)); + } + } +} diff --git a/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/Order.java b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/Order.java new file mode 100644 index 00000000..43af1c02 --- /dev/null +++ b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/Order.java @@ -0,0 +1,19 @@ +package io.kimmking.javacourse.kafka; + + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + + +@NoArgsConstructor +@AllArgsConstructor +@Data +public class Order { // 此类型为需要使用的消息内容 + + private Long id; + private Long ts; + private String symbol; + private Double price; + +} diff --git a/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/Consumer.java b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/Consumer.java new file mode 100644 index 00000000..2c081d92 --- /dev/null +++ b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/Consumer.java @@ -0,0 +1,15 @@ +package io.kimmking.javacourse.kafka.kimmking; + +import io.kimmking.javacourse.kafka.Order; + +public interface Consumer { + + void consumeOrder(); + + void close(); + + // add your interface method here + + // and then implement it + +} diff --git a/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/ConsumerImpl.java b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/ConsumerImpl.java new file mode 100644 index 00000000..611c4de2 --- /dev/null +++ b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/ConsumerImpl.java @@ -0,0 +1,79 @@ +package io.kimmking.javacourse.kafka.kimmking; + +import com.alibaba.fastjson.JSON; +import io.kimmking.javacourse.kafka.Order; +import org.apache.kafka.clients.consumer.*; +import org.apache.kafka.common.TopicPartition; + +import java.time.Duration; +import java.util.*; + +public class ConsumerImpl implements Consumer { + private Properties properties; + private KafkaConsumer consumer; + private final String topic = "order-test1"; + private Map currentOffsets = new HashMap<>(); + private Set orderSet = new HashSet<>(); + private volatile boolean flag = true; + + public ConsumerImpl() { + properties = new Properties(); +// properties.put("enable.auto.commit", false); +// properties.put("isolation.level", "read_committed"); +// properties.put("auto.offset.reset", "latest"); + properties.put("group.id", "java1-kimmking"); + properties.put("bootstrap.servers", "localhost:9092"); + properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); + properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); + consumer = new KafkaConsumer(properties); + } + + @Override + public void consumeOrder() { + + consumer.subscribe(Collections.singletonList(topic)); + + try { + while (true) { //拉取数据 + ConsumerRecords poll = consumer.poll(Duration.ofSeconds(1)); + + for (ConsumerRecord o : poll) { + ConsumerRecord record = (ConsumerRecord) o; + Order order = JSON.parseObject(record.value(), Order.class); + System.out.println(" order = " + order); +// deduplicationOrder(order); +// currentOffsets.put(new TopicPartition(record.topic(), record.partition()), +// new OffsetAndMetadata(record.offset() + 1, "no matadata")); +// consumer.commitAsync(currentOffsets, new OffsetCommitCallback() { +// @Override +// public void onComplete(Map offsets, Exception exception) { +// if (exception != null) { +// exception.printStackTrace(); +// } +// } +// }); + } + } + } catch (CommitFailedException e) { + e.printStackTrace(); + } finally { + try { + consumer.commitSync();//currentOffsets); + } catch (Exception e) { + consumer.close(); + } + } + } + + @Override + public void close() { + if (this.flag) { + this.flag = false; + } + consumer.close(); + } + + private void deduplicationOrder(Order order) { + orderSet.add(order.getId().toString()); + } +} diff --git a/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/Producer.java b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/Producer.java new file mode 100644 index 00000000..2bf2d937 --- /dev/null +++ b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/Producer.java @@ -0,0 +1,15 @@ +package io.kimmking.javacourse.kafka.kimmking; + +import io.kimmking.javacourse.kafka.Order; + +public interface Producer { + + void send(Order order); + + void close(); + + // add your interface method here + + // and then implement it + +} diff --git a/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/ProducerImpl.java b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/ProducerImpl.java new file mode 100644 index 00000000..1c069263 --- /dev/null +++ b/09mq/kafka-demo/src/main/java/io/kimmking/javacourse/kafka/kimmking/ProducerImpl.java @@ -0,0 +1,55 @@ +package io.kimmking.javacourse.kafka.kimmking; + +import io.kimmking.javacourse.kafka.Order; + +import com.alibaba.fastjson.JSON; +import kafka.common.KafkaException; +import org.apache.kafka.clients.producer.KafkaProducer; +import org.apache.kafka.clients.producer.ProducerRecord; + +import java.util.Properties; + +public class ProducerImpl implements Producer { + private Properties properties; + private KafkaProducer producer; + private final String topic = "order-test1"; + + public ProducerImpl() { + properties = new Properties(); + properties.put("bootstrap.servers", "localhost:9092"); +// properties.put("queue.enqueue.timeout.ms", -1); +// properties.put("enable.idempotence", true); +// properties.put("transactional.id", "transactional_1"); +// properties.put("acks", "all"); +// properties.put("retries", "3"); +// properties.put("max.in.flight.requests.per.connection", 1); + properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); + properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); + producer = new KafkaProducer(properties); + //producer.initTransactions(); + } + + @Override + public void send(Order order) { + try { + //producer.beginTransaction(); + ProducerRecord record = new ProducerRecord(topic, order.getId().toString(), JSON.toJSONString(order)); + producer.send(record, (metadata, exception) -> { +// if (exception != null) { +// producer.abortTransaction(); +// throw new KafkaException(exception.getMessage() + " , data: " + record); +// } + }); + //producer.commitTransaction(); + + } catch (Throwable e) { + //producer.abortTransaction(); + } + //System.out.println("************" + json + "************"); + } + + @Override + public void close() { + producer.close(); + } +} \ No newline at end of file diff --git a/09mq/kafka-demo/src/main/resources/log4j.xml b/09mq/kafka-demo/src/main/resources/log4j.xml new file mode 100644 index 00000000..e7915d2e --- /dev/null +++ b/09mq/kafka-demo/src/main/resources/log4j.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/09mq/kmq-core/.gitignore b/09mq/kmq-core/.gitignore new file mode 100644 index 00000000..14fb3721 --- /dev/null +++ b/09mq/kmq-core/.gitignore @@ -0,0 +1,35 @@ + +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +*.dat diff --git a/09mq/kmq-core/README.md b/09mq/kmq-core/README.md new file mode 100644 index 00000000..5af5a6fd --- /dev/null +++ b/09mq/kmq-core/README.md @@ -0,0 +1,9 @@ +## 说明 + +第一个版本,完全基于内存queue的消息队列,实现了最基础的三个功能: + +- 创建topic +- 订阅topic和poll消息 +- 发送消息到topic + +具体参见demo.KmqDemo \ No newline at end of file diff --git a/09mq/kmq-core/pom.xml b/09mq/kmq-core/pom.xml new file mode 100644 index 00000000..f0dc62ef --- /dev/null +++ b/09mq/kmq-core/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.1 + + + io.kimmking.kmq + kmq-core + 0.0.1-SNAPSHOT + kmq-core + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.projectlombok + lombok + true + + + + com.alibaba + fastjson + 1.2.58 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/KmqApplication.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/KmqApplication.java new file mode 100644 index 00000000..76c7ed8e --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/KmqApplication.java @@ -0,0 +1,13 @@ +package io.kimmking.kmq; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class KmqApplication { + + public static void main(String[] args) { + SpringApplication.run(KmqApplication.class, args); + } + +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/Kmq.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/Kmq.java new file mode 100644 index 00000000..8989faab --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/Kmq.java @@ -0,0 +1,55 @@ +package io.kimmking.kmq.core; + +import lombok.SneakyThrows; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; + +public final class Kmq { + + public Kmq(String topic, int capacity) { + this.topic = topic; + this.capacity = capacity; + this.queue = new LinkedBlockingQueue(capacity); + } + +// public List consumers = new ArrayList<>(); + + private List listeners = new ArrayList<>(); + + private final String topic; + + private final int capacity; + + private LinkedBlockingQueue queue; + + public boolean send(KmqMessage message) { + boolean offered = queue.offer(message); + if(offered) { + listeners.forEach(listener -> { + try { + listener.onMessage(message); + } catch (Exception e) { + e.printStackTrace(); + } + }); + } + return offered; + } + + public KmqMessage poll() { + return queue.poll(); + } + + @SneakyThrows + public KmqMessage poll(long timeout) { + return queue.poll(timeout, TimeUnit.MILLISECONDS); + } + + public void addListener(MessageListener listener) { + listeners.add(listener); + } + +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqBroker.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqBroker.java new file mode 100644 index 00000000..c0aa344f --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqBroker.java @@ -0,0 +1,30 @@ +package io.kimmking.kmq.core; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; + +public final class KmqBroker { // Broker+Connection + + public static final int CAPACITY = 10000; + + private final Map kmqMap = new ConcurrentHashMap<>(64); + + public void createTopic(String name){ + kmqMap.putIfAbsent(name, new Kmq(name,CAPACITY)); + } + + public Kmq findKmq(String topic) { + return this.kmqMap.get(topic); + } + + public KmqProducer createProducer() { + return new KmqProducer(this); + } + + final AtomicInteger consumerId = new AtomicInteger(0); + public KmqConsumer createConsumer() { + return new KmqConsumer("CID" + consumerId.getAndIncrement(), this); + } + +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqConsumer.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqConsumer.java new file mode 100644 index 00000000..83ae7245 --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqConsumer.java @@ -0,0 +1,34 @@ +package io.kimmking.kmq.core; + +import lombok.Getter; + +public class KmqConsumer { + + private final KmqBroker broker; + + @Getter + private final String id; + + private Kmq kmq; + + public KmqConsumer(String id, KmqBroker broker) { + this.id = id; + this.broker = broker; + } + + public void subscribe(String topic) { + this.kmq = this.broker.findKmq(topic); + if (null == kmq) throw new RuntimeException("Topic[" + topic + "] doesn't exist."); + } + + public void subscribe(String topic, MessageListener listener) { + this.kmq = this.broker.findKmq(topic); + if (null == kmq) throw new RuntimeException("Topic[" + topic + "] doesn't exist."); + this.kmq.addListener(listener); + } + + public KmqMessage poll(long timeout) { + return kmq.poll(timeout); + } + +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqMessage.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqMessage.java new file mode 100644 index 00000000..73823c9d --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqMessage.java @@ -0,0 +1,32 @@ +package io.kimmking.kmq.core; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.HashMap; +import java.util.concurrent.atomic.AtomicLong; + +@AllArgsConstructor +@NoArgsConstructor +@Data +public class KmqMessage { + + static AtomicLong MID = new AtomicLong(0); + + private HashMap headers = new HashMap<>(); + private String topic; + private Long id; + private T body; + + public KmqMessage(String topic, T body) { + this.topic = topic; + this.body = body; + this.id = MID.getAndIncrement(); + } + + public static KmqMessage from(String topic, T body) { + return new KmqMessage<>(topic, body); + } + +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqProducer.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqProducer.java new file mode 100644 index 00000000..0adadd71 --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/KmqProducer.java @@ -0,0 +1,16 @@ +package io.kimmking.kmq.core; + +public class KmqProducer { + + private KmqBroker broker; + + public KmqProducer(KmqBroker broker) { + this.broker = broker; + } + + public boolean send(String topic, KmqMessage message) { + Kmq kmq = this.broker.findKmq(topic); + if (null == kmq) throw new RuntimeException("Topic[" + topic + "] doesn't exist."); + return kmq.send(message); + } +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/MessageListener.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/MessageListener.java new file mode 100644 index 00000000..4cfdbe08 --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/core/MessageListener.java @@ -0,0 +1,13 @@ +package io.kimmking.kmq.core; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/6/13 下午3:29 + */ +public interface MessageListener { + + void onMessage(KmqMessage message) throws Exception; + +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/demo/KmqDemo.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/demo/KmqDemo.java new file mode 100644 index 00000000..9921af3d --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/demo/KmqDemo.java @@ -0,0 +1,58 @@ +package io.kimmking.kmq.demo; + +import io.kimmking.kmq.core.KmqBroker; +import io.kimmking.kmq.core.KmqConsumer; +import io.kimmking.kmq.core.KmqMessage; +import io.kimmking.kmq.core.KmqProducer; + +import lombok.SneakyThrows; + +public class KmqDemo { + + @SneakyThrows + public static void main(String[] args) { + + String topic = "kk.test"; + KmqBroker broker = new KmqBroker(); + broker.createTopic(topic); + + KmqConsumer subscriber = broker.createConsumer(); + subscriber.subscribe(topic, (message) -> { + System.out.println(subscriber.getId() + " : " + message.getBody()); + }); + + KmqConsumer consumer = broker.createConsumer(); + consumer.subscribe(topic); + final boolean[] flag = new boolean[1]; + flag[0] = true; + new Thread(() -> { + while (flag[0]) { + KmqMessage message = consumer.poll(100); + if(null != message) { + System.out.println(consumer.getId() + " : " + message.getBody()); + } + } + System.out.println("程序退出。"); + }).start(); + + KmqProducer producer = broker.createProducer(); + for (int i = 0; i < 10; i++) { + Order order = new Order(1000L + i, System.currentTimeMillis(), "USD2CNY", 6.51d); + producer.send(topic, new KmqMessage(null, order)); + } + Thread.sleep(500); + System.out.println("点击任何键,发送一条消息;点击q或e,退出程序。"); + while (true) { + char c = (char) System.in.read(); + if(c > 20) { + System.out.println(c); + producer.send(topic, new KmqMessage(null, new Order(100000L + c, System.currentTimeMillis(), "USD2CNY", 6.52d))); + } + + if( c == 'q' || c == 'e') break; + } + + flag[0] = false; + + } +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/demo/Order.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/demo/Order.java new file mode 100644 index 00000000..2d81ec85 --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/demo/Order.java @@ -0,0 +1,17 @@ +package io.kimmking.kmq.demo; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@AllArgsConstructor +@Data +public class Order { + + private Long id; + private Long ts; + private String symbol; + private Double price; + +} \ No newline at end of file diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/FileDemo.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/FileDemo.java new file mode 100644 index 00000000..baccdaff --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/FileDemo.java @@ -0,0 +1,97 @@ +package io.kimmking.kmq.store; + +import com.alibaba.fastjson.JSON; +import io.kimmking.kmq.core.KmqMessage; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.Scanner; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/6/13 下午9:59 + */ +public class FileDemo { + + public static void main(String[] args) throws IOException { + + String content = "this is a good file.\r\n" + + "that is a new line.\r\n"; + String topic = "topicA"; + System.out.println(content.length()); + + File file = new File("store001.dat"); + if (!file.exists()) { + file.createNewFile(); + } + Path path = Paths.get(file.toURI()); + try(FileChannel channel = (FileChannel) Files.newByteChannel(path, + StandardOpenOption.READ, + StandardOpenOption.WRITE)) { + + MappedByteBuffer mappedByteBuffer = channel + .map(FileChannel.MapMode.READ_WRITE, 0, 10240); + + if (mappedByteBuffer != null) { + + System.out.println(Charset.forName("utf-8") + .decode(mappedByteBuffer.asReadOnlyBuffer())); + + for (int i = 0; i < 100; i++) { + KmqMessage km = KmqMessage.from(topic, content); + String message = encodeMessage(km); + Indexer.addEntry(topic, km.getId(), mappedByteBuffer.position(), message.length()); + int pos = write(mappedByteBuffer, message); + System.out.println("POS = " + pos); + } + + System.out.println(" ======== indexer ========= "); + System.out.println(Indexer.getEntries(topic)); + } + + ByteBuffer readOnlyBuffer = mappedByteBuffer.asReadOnlyBuffer(); + Scanner sc = new Scanner(System.in); + while (sc.hasNextLine()) { + String line = sc.nextLine(); + if (line.equals("exit")) { + break; + } + System.out.println("IN = "+line); + Long id = Long.valueOf(line); + Indexer.Entry entry = Indexer.getEntry(id); + System.out.println("EN = " + entry); + if(entry == null) { + System.out.println("!!!No entry for id=" + id); + } else { + readOnlyBuffer.position(entry.offset); + byte[] bytes = new byte[entry.length]; + readOnlyBuffer.get(bytes, 0, entry.length); + System.out.println("MSG = " + new String(bytes)); + } + } + + } + } + + private static String encodeMessage(KmqMessage message) { + return JSON.toJSONString(message); + } + + public static int write(MappedByteBuffer buffer, String content) throws IOException { + buffer.put( + Charset.forName("utf-8") + .encode(content)); + return buffer.position(); + } + +} diff --git a/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/Indexer.java b/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/Indexer.java new file mode 100644 index 00000000..792ac400 --- /dev/null +++ b/09mq/kmq-core/src/main/java/io/kimmking/kmq/store/Indexer.java @@ -0,0 +1,53 @@ +package io.kimmking.kmq.store; + +import lombok.Data; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Description for this class. + * + * @Author : kimmking(kimmking@apache.org) + * @create 2024/6/14 下午6:19 + */ + +@Data +public class Indexer { + + static Map> index = new HashMap<>(); + static Map mappings = new HashMap<>(); + + @Data + public static class Entry { + long id; + int offset; + int length; + + public Entry(long id, int offset, int length) { + this.offset = offset; + this.length = length; + } + } + + public static void addEntry(String topic, long id, int offset, int length) { + List entries = index.get(topic); + if(entries == null) { + entries = new java.util.ArrayList<>(); + index.put(topic, entries); + } + Entry e = new Entry(id, offset, length); + entries.add(e); + mappings.put(id, e); + } + + public static List getEntries(String topic) { + return index.get(topic); + } + + public static Entry getEntry(long id) { + return mappings.get(id); + } + +} diff --git a/09mq/kmq-core/src/main/resources/application.properties b/09mq/kmq-core/src/main/resources/application.properties new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/09mq/kmq-core/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/09mq/kmq-core/src/test/java/io/kimmking/kmq/core/KmqApplicationTests.java b/09mq/kmq-core/src/test/java/io/kimmking/kmq/core/KmqApplicationTests.java new file mode 100644 index 00000000..19ef07aa --- /dev/null +++ b/09mq/kmq-core/src/test/java/io/kimmking/kmq/core/KmqApplicationTests.java @@ -0,0 +1,13 @@ +package io.kimmking.kmq.core; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class KmqApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/09mq/pulsar/.gitignore b/09mq/pulsar/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/09mq/pulsar/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/09mq/pulsar/.mvn/wrapper/MavenWrapperDownloader.java b/09mq/pulsar/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..a45eb6ba --- /dev/null +++ b/09mq/pulsar/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/09mq/pulsar/.mvn/wrapper/maven-wrapper.jar b/09mq/pulsar/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/09mq/pulsar/.mvn/wrapper/maven-wrapper.jar differ diff --git a/09mq/pulsar/.mvn/wrapper/maven-wrapper.properties b/09mq/pulsar/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/09mq/pulsar/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/09mq/pulsar/mvnw b/09mq/pulsar/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/09mq/pulsar/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/09mq/pulsar/mvnw.cmd b/09mq/pulsar/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/09mq/pulsar/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/09mq/pulsar/pom.xml b/09mq/pulsar/pom.xml new file mode 100644 index 00000000..6382a1b4 --- /dev/null +++ b/09mq/pulsar/pom.xml @@ -0,0 +1,71 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.1 + + + io.kimmking.mq + pulsar + 0.0.1-SNAPSHOT + pulsar + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.apache.pulsar + pulsar-client + 2.7.0 + + + + + + + + + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/Config.java b/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/Config.java new file mode 100644 index 00000000..7d62c9c3 --- /dev/null +++ b/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/Config.java @@ -0,0 +1,15 @@ +package io.kimmking.mq.pulsar; + +import lombok.SneakyThrows; +import org.apache.pulsar.client.api.PulsarClient; + +public class Config { + + @SneakyThrows + public static PulsarClient createClient() { + return PulsarClient.builder() + .serviceUrl("pulsar://localhost:6650") + .build(); + } + +} diff --git a/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/ConsumerDemo.java b/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/ConsumerDemo.java new file mode 100644 index 00000000..7a1ac56d --- /dev/null +++ b/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/ConsumerDemo.java @@ -0,0 +1,136 @@ +package io.kimmking.mq.pulsar; + +import lombok.SneakyThrows; +import org.apache.pulsar.client.api.Consumer; +import org.apache.pulsar.client.api.Message; +import org.springframework.stereotype.Component; + +@Component +public class ConsumerDemo { + + @SneakyThrows + public void consume() { + + Consumer consumer = Config.createClient().newConsumer() + .topic("my-kk") + .subscriptionName("my-subscription") + .subscribe(); + + while (true) { + // Wait for a message + Message msg = consumer.receive(); + + try { + // Do something with the message + System.out.printf("Message received from pulsar: %s \n", new String(msg.getData())); + + // Acknowledge the message so that it can be deleted by the message broker + consumer.acknowledge(msg); + } catch (Exception e) { + // Message failed to process, redeliver later + consumer.negativeAcknowledge(msg); + } + } + +// +// client.newConsumer() +// .deadLetterPolicy(DeadLetterPolicy.builder().maxRedeliverCount(10) +// .deadLetterTopic("your-topic-name").build()) +// .subscribe(); + + +// Consumer consumer = client.newConsumer() +// .topic("my-topic") +// .subscriptionName("my-subscription") +// .ackTimeout(10, TimeUnit.SECONDS) +// .subscriptionType(SubscriptionType.Exclusive) +// .subscribe(); + + +// +// CompletableFuture asyncMessage = consumer.receiveAsync(); +// +// Messages messages = consumer.batchReceive(); +// for (Object message : messages) { +// // do something +// } +// consumer.acknowledge(messages); +// +// BatchReceivePolicy.builder() +// .maxNumMessage(-1) +// .maxNumBytes(10 * 1024 * 1024) +// .timeout(100, TimeUnit.MILLISECONDS) +// .build(); +// +// Consumer consumer = client.newConsumer().topic("my-topic").subscriptionName("my-subscription") +// .batchReceivePolicy(BatchReceivePolicy.builder(). +// maxNumMessages(100).maxNumBytes(1024 * 1024) +// .timeout(200, TimeUnit.MILLISECONDS) +// .build()).subscribe(); + + +// +// ConsumerBuilder consumerBuilder = pulsarClient.newConsumer() +// .subscriptionName(subscription); +// +//// Subscribe to all topics in a namespace +// Pattern allTopicsInNamespace = Pattern.compile("public/default/.*"); +// Consumer allTopicsConsumer = consumerBuilder +// .topicsPattern(allTopicsInNamespace) +// .subscribe(); +// +//// Subscribe to a subsets of topics in a namespace, based on regex +// Pattern someTopicsInNamespace = Pattern.compile("public/default/foo.*"); +// Consumer allTopicsConsumer = consumerBuilder +// .topicsPattern(someTopicsInNamespace) +// .subscribe(); +// In the above example, the consumer subscribes to the persistent topics that can match the topic name pattern. If you want the consumer subscribes to all persistent and non-persistent topics that can match the topic name pattern, set subscriptionTopicsMode to RegexSubscriptionMode.AllTopics. +// +// Pattern pattern = Pattern.compile("public/default/.*"); +// pulsarClient.newConsumer() +// .subscriptionName("my-sub") +// .topicsPattern(pattern) +// .subscriptionTopicsMode(RegexSubscriptionMode.AllTopics) +// .subscribe(); +// Note +// By default, the subscriptionTopicsMode of the consumer is PersistentOnly. Available options of subscriptionTopicsMode are PersistentOnly, NonPersistentOnly, and AllTopics. +// +// 你还可以订阅明确的主题列表 (如果愿意, 可跨命名空间): +// +// List topics = Arrays.asList( +// "topic-1", +// "topic-2", +// "topic-3" +// ); +// +// Consumer multiTopicConsumer = consumerBuilder +// .topics(topics) +// .subscribe(); +// +//// Alternatively: +// Consumer multiTopicConsumer = consumerBuilder +// .topic( +// "topic-1", +// "topic-2", +// "topic-3" +// ) +// .subscribe(); +// You can also subscribe to multiple topics asynchronously using the subscribeAsync method rather than the synchronous subscribe method. The following is an example. +// +// Pattern allTopicsInNamespace = Pattern.compile("persistent://public/default.*"); +// consumerBuilder +// .topics(topics) +// .subscribeAsync() +// .thenAccept(this::receiveMessageFromConsumer); +// +// private void receiveMessageFromConsumer(Object consumer) { +// ((Consumer)consumer).receiveAsync().thenAccept(message -> { +// // Do something with the received message +// receiveMessageFromConsumer(consumer); +// }); +// } + + + } + +} diff --git a/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/ProducerDemo.java b/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/ProducerDemo.java new file mode 100644 index 00000000..9c1c6bf4 --- /dev/null +++ b/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/ProducerDemo.java @@ -0,0 +1,60 @@ +package io.kimmking.mq.pulsar; + +import lombok.SneakyThrows; +import org.apache.pulsar.client.api.Producer; +import org.apache.pulsar.client.api.Schema; +import org.springframework.stereotype.Component; + +@Component +public class ProducerDemo { + Producer stringProducer; + + @SneakyThrows + public ProducerDemo(){ + stringProducer = Config.createClient().newProducer(Schema.STRING) + .topic("my-kk") + .create(); + } + + @SneakyThrows + public void sendMessage() { + for (int i = 0; i < 1000; i++) { + stringProducer.send(i + " message from pulsar."); + } + } + + +// //关闭操作也可以是异步的: +// +// producer.closeAsync() +// .thenRun(() -> System.out.println("Producer closed")); +// .exceptionally((ex) -> { +// System.err.println("Failed to close producer: " + ex); +// return ex; +// }); + + // 控制发送行为 +// Producer producer = client.newProducer() +// .topic("my-topic") +// .batchingMaxPublishDelay(10, TimeUnit.MILLISECONDS) +// .sendTimeout(10, TimeUnit.SECONDS) +// .blockIfQueueFull(true) +// .create(); + + + //异步发送 +// producer.sendAsync("my-async-message".getBytes()).thenAccept(msgId -> { +// System.out.printf("Message with ID %s successfully sent", msgId); +// }); + + + // 添加参数 +// producer.newMessage() +// .key("my-message-key") +// .value("my-async-message".getBytes()) +// .property("my-key", "my-value") +// .property("my-other-key", "my-other-value") +// .send(); + + +} diff --git a/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/PulsarApplication.java b/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/PulsarApplication.java new file mode 100644 index 00000000..89938c6a --- /dev/null +++ b/09mq/pulsar/src/main/java/io/kimmking/mq/pulsar/PulsarApplication.java @@ -0,0 +1,36 @@ +package io.kimmking.mq.pulsar; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class PulsarApplication { + + public static void main(String[] args) { + SpringApplication.run(PulsarApplication.class, args); + } + + + @Autowired + ProducerDemo producer; + + @Autowired + ConsumerDemo consumer; + + @Bean + ApplicationRunner run() { + return args -> { + new Thread(() -> { + consumer.consume(); + }).start(); + + producer.sendMessage(); + + }; + } + + +} diff --git a/09mq/pulsar/src/main/resources/application.properties b/09mq/pulsar/src/main/resources/application.properties new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/09mq/pulsar/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/09mq/pulsar/src/test/java/io/kimmking/mq/pulsar/PulsarApplicationTests.java b/09mq/pulsar/src/test/java/io/kimmking/mq/pulsar/PulsarApplicationTests.java new file mode 100644 index 00000000..8d9659b7 --- /dev/null +++ b/09mq/pulsar/src/test/java/io/kimmking/mq/pulsar/PulsarApplicationTests.java @@ -0,0 +1,13 @@ +package io.kimmking.mq.pulsar; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class PulsarApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/09mq/rabbit/.gitignore b/09mq/rabbit/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/09mq/rabbit/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/09mq/rabbit/.mvn/wrapper/MavenWrapperDownloader.java b/09mq/rabbit/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..a45eb6ba --- /dev/null +++ b/09mq/rabbit/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/09mq/rabbit/.mvn/wrapper/maven-wrapper.jar b/09mq/rabbit/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/09mq/rabbit/.mvn/wrapper/maven-wrapper.jar differ diff --git a/09mq/rabbit/.mvn/wrapper/maven-wrapper.properties b/09mq/rabbit/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/09mq/rabbit/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/09mq/rabbit/mvnw b/09mq/rabbit/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/09mq/rabbit/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/09mq/rabbit/mvnw.cmd b/09mq/rabbit/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/09mq/rabbit/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/09mq/rabbit/pom.xml b/09mq/rabbit/pom.xml new file mode 100644 index 00000000..ae17ee14 --- /dev/null +++ b/09mq/rabbit/pom.xml @@ -0,0 +1,82 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.1 + + + io.kimmking.mq + rabbit + 0.0.1-SNAPSHOT + rabbit + Demo project for Spring Boot + + + 1.8 + + + + + + + + + org.springframework.boot + spring-boot-starter-amqp + + + + + + + + + + + + + + + + + + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.amqp + spring-rabbit-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/09mq/rabbit/src/main/java/io/kimmking/mq/camel/README.md b/09mq/rabbit/src/main/java/io/kimmking/mq/camel/README.md new file mode 100644 index 00000000..29f5cd93 --- /dev/null +++ b/09mq/rabbit/src/main/java/io/kimmking/mq/camel/README.md @@ -0,0 +1 @@ +使用代码方式,实现camel的操作。 diff --git a/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageProducer.java b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageProducer.java new file mode 100644 index 00000000..2480046b --- /dev/null +++ b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageProducer.java @@ -0,0 +1,43 @@ +package io.kimmking.mq.rabbit; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.rabbit.connection.CorrelationData; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.UUID; + +@Component +@Slf4j +public class MessageProducer implements RabbitTemplate.ConfirmCallback { + + //由于rabbitTemplate的scope属性设置为ConfigurableBeanFactory.SCOPE_PROTOTYPE,所以不能自动注入 + private RabbitTemplate rabbitTemplate; + /** + * 构造方法注入rabbitTemplate + */ + @Autowired + public MessageProducer(RabbitTemplate rabbitTemplate) { + this.rabbitTemplate = rabbitTemplate; + rabbitTemplate.setConfirmCallback(this); //rabbitTemplate如果为单例的话,那回调就是最后设置的内容 + } + + public void sendMessage(String content) { + CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString()); + //把消息放入ROUTINGKEY_A对应的队列当中去,对应的是队列A + rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_A, RabbitConfig.ROUTINGKEY_B, content, correlationId); + } + /** + * 回调 + */ + @Override + public void confirm(CorrelationData correlationData, boolean ack, String cause) { + log.info(" 回调id:" + correlationData); + if (ack) { + log.info("消息成功消费!!!!!" + correlationData); + } else { + log.info("消息消费失败:" + cause + correlationData); + } + } +} \ No newline at end of file diff --git a/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageReceiverA.java b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageReceiverA.java new file mode 100644 index 00000000..6cea0d3a --- /dev/null +++ b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageReceiverA.java @@ -0,0 +1,18 @@ +package io.kimmking.mq.rabbit; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.rabbit.annotation.RabbitHandler; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.stereotype.Component; + +@Component +@RabbitListener(queues = RabbitConfig.QUEUE_A) +@Slf4j +public class MessageReceiverA { //guava , EventBus 的一些语法糖 + + @RabbitHandler + public void process(String content) { + log.info("接收处理队列A当中的消息: " + content); + } + +} \ No newline at end of file diff --git a/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageReceiverB.java b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageReceiverB.java new file mode 100644 index 00000000..7b0e81db --- /dev/null +++ b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/MessageReceiverB.java @@ -0,0 +1,18 @@ +package io.kimmking.mq.rabbit; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.rabbit.annotation.RabbitHandler; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.stereotype.Component; + +@Component +@RabbitListener(queues = RabbitConfig.QUEUE_B) +@Slf4j +public class MessageReceiverB { + + @RabbitHandler + public void process(String content) { + log.info("接收处理队列B当中的消息: " + content); + } + +} \ No newline at end of file diff --git a/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/RabbitApplication.java b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/RabbitApplication.java new file mode 100644 index 00000000..787c57a3 --- /dev/null +++ b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/RabbitApplication.java @@ -0,0 +1,28 @@ +package io.kimmking.mq.rabbit; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class RabbitApplication { + + public static void main(String[] args) { + SpringApplication.run(RabbitApplication.class, args); + } + + @Autowired + MessageProducer producer; + + @Bean + ApplicationRunner run() { + return args -> { + for (int i = 0; i < 1000; i++) { + producer.sendMessage(i+" message by cuicuilaoshi."); + } + }; + } + +} diff --git a/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/RabbitConfig.java b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/RabbitConfig.java new file mode 100644 index 00000000..440db166 --- /dev/null +++ b/09mq/rabbit/src/main/java/io/kimmking/mq/rabbit/RabbitConfig.java @@ -0,0 +1,108 @@ +package io.kimmking.mq.rabbit; + +import org.springframework.amqp.core.Binding; +import org.springframework.amqp.core.BindingBuilder; +import org.springframework.amqp.core.DirectExchange; +import org.springframework.amqp.core.Queue; +import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; +import org.springframework.amqp.rabbit.connection.ConnectionFactory; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; + +@Configuration +public class RabbitConfig { + + @Value("${spring.rabbitmq.host}") + private String host; + + @Value("${spring.rabbitmq.port}") + private int port; + + @Value("${spring.rabbitmq.username}") + private String username; + + @Value("${spring.rabbitmq.password}") + private String password; + + + public static final String EXCHANGE_A = "my-mq-exchange_A"; + public static final String EXCHANGE_B = "my-mq-exchange_B"; + public static final String EXCHANGE_C = "my-mq-exchange_C"; + + + public static final String QUEUE_A = "QUEUE_A"; + public static final String QUEUE_B = "QUEUE_B"; + public static final String QUEUE_C = "QUEUE_C"; + + public static final String ROUTINGKEY_A = "spring-boot-routingKey_A"; + public static final String ROUTINGKEY_B = "spring-boot-routingKey_B"; + public static final String ROUTINGKEY_C = "spring-boot-routingKey_C"; + + @Bean + public ConnectionFactory connectionFactory() { + CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host,port); + connectionFactory.setUsername(username); + connectionFactory.setPassword(password); + connectionFactory.setVirtualHost("/"); + connectionFactory.setPublisherConfirms(true); + return connectionFactory; + } + + @Bean + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public RabbitTemplate rabbitTemplate() { + RabbitTemplate template = new RabbitTemplate(connectionFactory()); + return template; + } + + /** + * 针对消费者配置 + * 1. 设置交换机类型 + * 2. 将队列绑定到交换机 + FanoutExchange: 将消息分发到所有的绑定队列,无routingkey的概念 + HeadersExchange :通过添加属性key-value匹配 + DirectExchange:按照routingkey分发到指定队列 + TopicExchange:多关键字匹配 + */ + @Bean + public DirectExchange defaultExchange() { + return new DirectExchange(EXCHANGE_A); + } + + @Bean + public Queue queueA() { + return new Queue(QUEUE_A, true); //队列持久 + } + + @Bean + public Queue queueB() { + return new Queue(QUEUE_B, true); //队列持久 + } + + @Bean + public Queue queueC() { + return new Queue(QUEUE_C, true); //队列持久 + } + + @Bean + public Binding bindingA() { + return BindingBuilder.bind(queueA()).to(defaultExchange()).with(RabbitConfig.ROUTINGKEY_A); + } + @Bean + public Binding bindingB() { + return BindingBuilder.bind(queueB()).to(defaultExchange()).with(RabbitConfig.ROUTINGKEY_B); + } + @Bean + public Binding bindingC(){ + return BindingBuilder.bind(queueC()).to(defaultExchange()).with(RabbitConfig.ROUTINGKEY_C); + } + + // 详细的使用可以参考: + // https://blog.csdn.net/qq_38455201/article/details/80308771 + // https://www.cnblogs.com/handsomeye/p/9135623.html + +} \ No newline at end of file diff --git a/09mq/rabbit/src/main/resources/application.yaml b/09mq/rabbit/src/main/resources/application.yaml new file mode 100644 index 00000000..48574487 --- /dev/null +++ b/09mq/rabbit/src/main/resources/application.yaml @@ -0,0 +1,7 @@ +spring: + rabbitmq: + host: localhost + username: admin + password: admin + port: 5672 + diff --git a/09mq/rabbit/src/test/java/io/kimmking/mq/rabbit/RabbitApplicationTests.java b/09mq/rabbit/src/test/java/io/kimmking/mq/rabbit/RabbitApplicationTests.java new file mode 100644 index 00000000..adf34994 --- /dev/null +++ b/09mq/rabbit/src/test/java/io/kimmking/mq/rabbit/RabbitApplicationTests.java @@ -0,0 +1,13 @@ +package io.kimmking.mq.rabbit; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class RabbitApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/09mq/rocket/.gitignore b/09mq/rocket/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/09mq/rocket/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/09mq/rocket/.mvn/wrapper/MavenWrapperDownloader.java b/09mq/rocket/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..a45eb6ba --- /dev/null +++ b/09mq/rocket/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/09mq/rocket/.mvn/wrapper/maven-wrapper.jar b/09mq/rocket/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/09mq/rocket/.mvn/wrapper/maven-wrapper.jar differ diff --git a/09mq/rocket/.mvn/wrapper/maven-wrapper.properties b/09mq/rocket/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/09mq/rocket/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/09mq/rocket/mvnw b/09mq/rocket/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/09mq/rocket/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/09mq/rocket/mvnw.cmd b/09mq/rocket/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/09mq/rocket/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/09mq/rocket/pom.xml b/09mq/rocket/pom.xml new file mode 100644 index 00000000..d2931a7e --- /dev/null +++ b/09mq/rocket/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.1 + + + io.kimmking.mq + rocket + 0.0.1-SNAPSHOT + rocket + Demo project for Spring Boot + + + 1.8 + + + + + + + + + + org.apache.rocketmq + rocketmq-spring-boot-starter + 2.1.1 + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/09mq/rocket/src/main/java/io/kimmking/mq/rocket/Order.java b/09mq/rocket/src/main/java/io/kimmking/mq/rocket/Order.java new file mode 100644 index 00000000..02aec9f7 --- /dev/null +++ b/09mq/rocket/src/main/java/io/kimmking/mq/rocket/Order.java @@ -0,0 +1,16 @@ +package io.kimmking.mq.rocket; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class Order { + + private long id; + private String symbol; + private double price; + +} diff --git a/09mq/rocket/src/main/java/io/kimmking/mq/rocket/OrderConsumerDemo.java b/09mq/rocket/src/main/java/io/kimmking/mq/rocket/OrderConsumerDemo.java new file mode 100644 index 00000000..2c4ba4ed --- /dev/null +++ b/09mq/rocket/src/main/java/io/kimmking/mq/rocket/OrderConsumerDemo.java @@ -0,0 +1,17 @@ +package io.kimmking.mq.rocket; + +import org.apache.rocketmq.spring.annotation.ConsumeMode; +import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; +import org.apache.rocketmq.spring.core.RocketMQReplyListener; +import org.springframework.stereotype.Component; + +@Component +@RocketMQMessageListener(consumerGroup = "test2", topic = "test-k2") +public class OrderConsumerDemo implements RocketMQReplyListener { + + @Override + public String onMessage(Order order) { // request-response + System.out.println(this.getClass().getName() + " -> " + order); + return "Process&Return [" + order + "]."; + } +} diff --git a/09mq/rocket/src/main/java/io/kimmking/mq/rocket/RocketApplication.java b/09mq/rocket/src/main/java/io/kimmking/mq/rocket/RocketApplication.java new file mode 100644 index 00000000..13d6c89c --- /dev/null +++ b/09mq/rocket/src/main/java/io/kimmking/mq/rocket/RocketApplication.java @@ -0,0 +1,60 @@ +package io.kimmking.mq.rocket; + +import org.apache.rocketmq.client.producer.SendCallback; +import org.apache.rocketmq.client.producer.SendResult; +import org.apache.rocketmq.spring.core.RocketMQTemplate; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.messaging.MessageHeaders; +import org.springframework.messaging.support.MessageBuilder; +import org.springframework.util.MimeTypeUtils; + +import javax.annotation.Resource; +import java.math.BigDecimal; +import java.util.List; + +@SpringBootApplication +public class RocketApplication implements CommandLineRunner { + + public static void main(String[] args) { + SpringApplication.run(RocketApplication.class, args); + } + + @Resource + private RocketMQTemplate rocketMQTemplate; + + @Override + public void run(String... args) throws Exception { + + String topic = "test-k1"; + SendResult sendResult = rocketMQTemplate.syncSend(topic, "Hello, World!" + System.currentTimeMillis()); + System.out.printf("syncSend1 to topic %s sendResult=%s %n", topic, sendResult); + + sendResult = rocketMQTemplate.syncSend(topic, MessageBuilder.withPayload( + new Order(System.currentTimeMillis(),"CNY2USD", 0.1501d)) + .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON_VALUE).build()); + System.out.printf("syncSend1 to topic %s sendResult=%s %n", topic, sendResult); + + String topic1 = "test-k2"; + + String result = rocketMQTemplate.sendAndReceive(topic1, new Order(System.currentTimeMillis(),"CNY2USD", 0.1502d), String.class); + System.out.println(" consumer result => " + result); + +// rocketMQTemplate.asyncSend(topic1, new Order(System.currentTimeMillis(),"CNY2USD", 0.1502d), new SendCallback() { +// @Override +// public void onSuccess(SendResult result) { +// System.out.printf("async onSucess SendResult=%s %n", result); +// } +// +// @Override +// public void onException(Throwable throwable) { +// System.out.printf("async onException Throwable=%s %n", throwable); +// } +// +// }); + + + } + +} diff --git a/09mq/rocket/src/main/java/io/kimmking/mq/rocket/StringConsumerDemo.java b/09mq/rocket/src/main/java/io/kimmking/mq/rocket/StringConsumerDemo.java new file mode 100644 index 00000000..f2534bdb --- /dev/null +++ b/09mq/rocket/src/main/java/io/kimmking/mq/rocket/StringConsumerDemo.java @@ -0,0 +1,15 @@ +package io.kimmking.mq.rocket; + +import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; +import org.apache.rocketmq.spring.core.RocketMQListener; +import org.springframework.stereotype.Component; + +@Component +@RocketMQMessageListener(consumerGroup = "test1", topic = "test-k1") +public class StringConsumerDemo implements RocketMQListener { + + @Override + public void onMessage(String message) { // one way + System.out.println(this.getClass().getName() + " -> " + message); + } +} diff --git a/09mq/rocket/src/main/resources/application.properties b/09mq/rocket/src/main/resources/application.properties new file mode 100644 index 00000000..513b78b4 --- /dev/null +++ b/09mq/rocket/src/main/resources/application.properties @@ -0,0 +1,5 @@ + +rocketmq.name-server=localhost:9876 +rocketmq.producer.group=my-group1 +rocketmq.producer.sendMessageTimeout=300000 + diff --git a/09mq/rocket/src/test/java/io/kimmking/mq/rocket/RocketApplicationTests.java b/09mq/rocket/src/test/java/io/kimmking/mq/rocket/RocketApplicationTests.java new file mode 100644 index 00000000..80c1f88e --- /dev/null +++ b/09mq/rocket/src/test/java/io/kimmking/mq/rocket/RocketApplicationTests.java @@ -0,0 +1,13 @@ +package io.kimmking.mq.rocket; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class RocketApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/09mq/spring-kafka-demo/.gitignore b/09mq/spring-kafka-demo/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/09mq/spring-kafka-demo/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/09mq/spring-kafka-demo/.mvn/wrapper/MavenWrapperDownloader.java b/09mq/spring-kafka-demo/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..a45eb6ba --- /dev/null +++ b/09mq/spring-kafka-demo/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/09mq/spring-kafka-demo/.mvn/wrapper/maven-wrapper.jar b/09mq/spring-kafka-demo/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/09mq/spring-kafka-demo/.mvn/wrapper/maven-wrapper.jar differ diff --git a/09mq/spring-kafka-demo/.mvn/wrapper/maven-wrapper.properties b/09mq/spring-kafka-demo/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/09mq/spring-kafka-demo/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/09mq/spring-kafka-demo/mvnw b/09mq/spring-kafka-demo/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/09mq/spring-kafka-demo/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/09mq/spring-kafka-demo/mvnw.cmd b/09mq/spring-kafka-demo/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/09mq/spring-kafka-demo/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/09mq/spring-kafka-demo/pom.xml b/09mq/spring-kafka-demo/pom.xml new file mode 100644 index 00000000..7c3d6f57 --- /dev/null +++ b/09mq/spring-kafka-demo/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.1 + + + io.kimmking.javacourse + kafka-demo + 0.0.1-SNAPSHOT + kafka-demo + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.kafka + spring-kafka + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.kafka + spring-kafka-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/09mq/spring-kafka-demo/src/main/java/io/kimmking/javacourse/kafkademo/KafkaDemoApplication.java b/09mq/spring-kafka-demo/src/main/java/io/kimmking/javacourse/kafkademo/KafkaDemoApplication.java new file mode 100644 index 00000000..738ba47c --- /dev/null +++ b/09mq/spring-kafka-demo/src/main/java/io/kimmking/javacourse/kafkademo/KafkaDemoApplication.java @@ -0,0 +1,13 @@ +package io.kimmking.javacourse.kafkademo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class KafkaDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(KafkaDemoApplication.class, args); + } + +} diff --git a/09mq/spring-kafka-demo/src/main/resources/application.properties b/09mq/spring-kafka-demo/src/main/resources/application.properties new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/09mq/spring-kafka-demo/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/09mq/spring-kafka-demo/src/test/java/io/kimmking/javacourse/kafkademo/KafkaDemoApplicationTests.java b/09mq/spring-kafka-demo/src/test/java/io/kimmking/javacourse/kafkademo/KafkaDemoApplicationTests.java new file mode 100644 index 00000000..e70ed750 --- /dev/null +++ b/09mq/spring-kafka-demo/src/test/java/io/kimmking/javacourse/kafkademo/KafkaDemoApplicationTests.java @@ -0,0 +1,13 @@ +package io.kimmking.javacourse.kafkademo; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class KafkaDemoApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/11dfs/dfs/.gitignore b/11dfs/dfs/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/11dfs/dfs/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/11dfs/dfs/.mvn/wrapper/maven-wrapper.jar b/11dfs/dfs/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..c1dd12f1 Binary files /dev/null and b/11dfs/dfs/.mvn/wrapper/maven-wrapper.jar differ diff --git a/11dfs/dfs/.mvn/wrapper/maven-wrapper.properties b/11dfs/dfs/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..b7cb93e7 --- /dev/null +++ b/11dfs/dfs/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.4/apache-maven-3.8.4-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar diff --git a/11dfs/dfs/app/pic/1.txt b/11dfs/dfs/app/pic/1.txt new file mode 100644 index 00000000..9d07aa0d --- /dev/null +++ b/11dfs/dfs/app/pic/1.txt @@ -0,0 +1 @@ +111 \ No newline at end of file diff --git a/11dfs/dfs/app/pic/1644038685842-moon.jpeg b/11dfs/dfs/app/pic/1644038685842-moon.jpeg new file mode 100644 index 00000000..39a70e03 Binary files /dev/null and b/11dfs/dfs/app/pic/1644038685842-moon.jpeg differ diff --git a/11dfs/dfs/app/pic/1644048593861-moon.jpeg b/11dfs/dfs/app/pic/1644048593861-moon.jpeg new file mode 100644 index 00000000..39a70e03 Binary files /dev/null and b/11dfs/dfs/app/pic/1644048593861-moon.jpeg differ diff --git a/11dfs/dfs/app/pic/1644051049289-IMG_0987.JPG b/11dfs/dfs/app/pic/1644051049289-IMG_0987.JPG new file mode 100644 index 00000000..e6d9aae4 Binary files /dev/null and b/11dfs/dfs/app/pic/1644051049289-IMG_0987.JPG differ diff --git a/11dfs/dfs/app/pic/1644051171701-houyiqibing.jpeg b/11dfs/dfs/app/pic/1644051171701-houyiqibing.jpeg new file mode 100644 index 00000000..1a21b07f Binary files /dev/null and b/11dfs/dfs/app/pic/1644051171701-houyiqibing.jpeg differ diff --git a/11dfs/dfs/app/pic/1644051228263-houyiqibing.jpeg b/11dfs/dfs/app/pic/1644051228263-houyiqibing.jpeg new file mode 100644 index 00000000..1a21b07f Binary files /dev/null and b/11dfs/dfs/app/pic/1644051228263-houyiqibing.jpeg differ diff --git a/11dfs/dfs/app/pic/1644051550427-houyiqibing.jpeg b/11dfs/dfs/app/pic/1644051550427-houyiqibing.jpeg new file mode 100644 index 00000000..1a21b07f Binary files /dev/null and b/11dfs/dfs/app/pic/1644051550427-houyiqibing.jpeg differ diff --git a/11dfs/dfs/app/pic/1646313820045-image1.png b/11dfs/dfs/app/pic/1646313820045-image1.png new file mode 100644 index 00000000..54495982 Binary files /dev/null and b/11dfs/dfs/app/pic/1646313820045-image1.png differ diff --git a/11dfs/dfs/app/pic/1646485079290-renshijian.jpeg b/11dfs/dfs/app/pic/1646485079290-renshijian.jpeg new file mode 100644 index 00000000..78d018a0 Binary files /dev/null and b/11dfs/dfs/app/pic/1646485079290-renshijian.jpeg differ diff --git a/11dfs/dfs/app/pic/1646485131332-renshijian.jpeg b/11dfs/dfs/app/pic/1646485131332-renshijian.jpeg new file mode 100644 index 00000000..78d018a0 Binary files /dev/null and b/11dfs/dfs/app/pic/1646485131332-renshijian.jpeg differ diff --git a/11dfs/dfs/app/pic/1646490487023-renshijian.jpeg b/11dfs/dfs/app/pic/1646490487023-renshijian.jpeg new file mode 100644 index 00000000..78d018a0 Binary files /dev/null and b/11dfs/dfs/app/pic/1646490487023-renshijian.jpeg differ diff --git a/11dfs/dfs/app/pic/1646490663637-yushengjiexuan7.jpeg b/11dfs/dfs/app/pic/1646490663637-yushengjiexuan7.jpeg new file mode 100644 index 00000000..61366514 Binary files /dev/null and b/11dfs/dfs/app/pic/1646490663637-yushengjiexuan7.jpeg differ diff --git a/11dfs/dfs/app/pic/1646491143115-yushengjiexuan7.jpeg b/11dfs/dfs/app/pic/1646491143115-yushengjiexuan7.jpeg new file mode 100644 index 00000000..61366514 Binary files /dev/null and b/11dfs/dfs/app/pic/1646491143115-yushengjiexuan7.jpeg differ diff --git a/11dfs/dfs/app/pic/jiajia.txt b/11dfs/dfs/app/pic/jiajia.txt new file mode 100644 index 00000000..fc139c7d --- /dev/null +++ b/11dfs/dfs/app/pic/jiajia.txt @@ -0,0 +1 @@ +jiajia,nihao \ No newline at end of file diff --git a/11dfs/dfs/mvnw b/11dfs/dfs/mvnw new file mode 100755 index 00000000..8a8fb228 --- /dev/null +++ b/11dfs/dfs/mvnw @@ -0,0 +1,316 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /usr/local/etc/mavenrc ] ; then + . /usr/local/etc/mavenrc + fi + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`\\unset -f command; \\command -v java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + $MAVEN_DEBUG_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" \ + "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/11dfs/dfs/mvnw.cmd b/11dfs/dfs/mvnw.cmd new file mode 100644 index 00000000..1d8ab018 --- /dev/null +++ b/11dfs/dfs/mvnw.cmd @@ -0,0 +1,188 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* +if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %* +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + +FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% ^ + %JVM_CONFIG_MAVEN_PROPS% ^ + %MAVEN_OPTS% ^ + %MAVEN_DEBUG_OPTS% ^ + -classpath %WRAPPER_JAR% ^ + "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^ + %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" +if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%"=="on" pause + +if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE% + +cmd /C exit /B %ERROR_CODE% diff --git a/11dfs/dfs/pom.xml b/11dfs/dfs/pom.xml new file mode 100644 index 00000000..92586ab7 --- /dev/null +++ b/11dfs/dfs/pom.xml @@ -0,0 +1,92 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.3 + + + io.github.kimmking.javacourse + dfs + 0.0.1-SNAPSHOT + dfs + DFS Demo project for Spring Boot + + 11 + + + + org.springframework.boot + spring-boot-starter-web + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + + com.alibaba + druid-spring-boot-starter + 1.1.17 + + + + org.springframework + spring-jdbc + + + + com.github.tobato + fastdfs-client + 1.26.4 + + + + commons-fileupload + commons-fileupload + 1.4 + + + + mysql + mysql-connector-java + runtime + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/DfsApplication.java b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/DfsApplication.java new file mode 100644 index 00000000..9ace7029 --- /dev/null +++ b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/DfsApplication.java @@ -0,0 +1,15 @@ +package io.github.kimmking.javacourse.dfs; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@MapperScan("io.github.kimmking.javacourse.dfs.mapper") +@SpringBootApplication +public class DfsApplication { + + public static void main(String[] args) { + SpringApplication.run(DfsApplication.class, args); + } + +} diff --git a/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/FastDfsClientConfig.java b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/FastDfsClientConfig.java new file mode 100644 index 00000000..7ee6d867 --- /dev/null +++ b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/FastDfsClientConfig.java @@ -0,0 +1,14 @@ +package io.github.kimmking.javacourse.dfs; + +import com.github.tobato.fastdfs.FdfsClientConfig; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableMBeanExport; +import org.springframework.context.annotation.Import; +import org.springframework.jmx.support.RegistrationPolicy; + +@Configuration +@Import(FdfsClientConfig.class) +@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) +public class FastDfsClientConfig { + +} diff --git a/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/FileConfig.java b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/FileConfig.java new file mode 100644 index 00000000..79bc164a --- /dev/null +++ b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/FileConfig.java @@ -0,0 +1,40 @@ +package io.github.kimmking.javacourse.dfs; + +import org.springframework.context.annotation.Configuration; +import org.springframework.util.ResourceUtils; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; + +import java.io.File; +import java.io.FileNotFoundException; + +@Configuration +public class FileConfig extends WebMvcConfigurationSupport { + + public static final String PATH = new File("app").getAbsolutePath(); + public static final String PIC_PATH=PATH + "/pic/"; + static { + File picFile = new File(PIC_PATH); + if (!picFile.exists()) { + picFile.mkdirs(); + } + System.out.println("PIC_PATH => " + PIC_PATH); + } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { +// File path = null; +// try { +// path = new File(ResourceUtils.getURL("classpath:").getPath()); +// System.out.println("ResourceUtils Path: "+ path); +// } catch (FileNotFoundException e) { +// e.printStackTrace(); +// } +// String picPath = path.getParentFile().getParentFile().getParent() + File.separator + "app" + File.separator + "pic" + File.separator; + String picPath = PIC_PATH; + System.out.println("addResource for: "+picPath); + registry.addResourceHandler("/pic/**").addResourceLocations("file:"+picPath); + registry.addResourceHandler("/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/"); + super.addResourceHandlers(registry); + } +} diff --git a/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/JavaConfig.java b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/JavaConfig.java new file mode 100644 index 00000000..2672afac --- /dev/null +++ b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/JavaConfig.java @@ -0,0 +1,17 @@ +package io.github.kimmking.javacourse.dfs; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.multipart.commons.CommonsMultipartResolver; + +@Configuration +public class JavaConfig { + @Bean + CommonsMultipartResolver createCommonsMultipartResolver() { + CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(); + commonsMultipartResolver.setMaxUploadSize(4*1024*1024); + commonsMultipartResolver.setMaxInMemorySize(4*1024*1024); + commonsMultipartResolver.setDefaultEncoding("UTF-8"); + return commonsMultipartResolver; + } +} diff --git a/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/controller/FileController.java b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/controller/FileController.java new file mode 100644 index 00000000..a7a0fb6f --- /dev/null +++ b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/controller/FileController.java @@ -0,0 +1,76 @@ +package io.github.kimmking.javacourse.dfs.controller; + +import io.github.kimmking.javacourse.dfs.FileConfig; +import io.github.kimmking.javacourse.dfs.JavaConfig; +import io.github.kimmking.javacourse.dfs.model.User; +import io.github.kimmking.javacourse.dfs.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.commons.CommonsMultipartFile; +import com.github.tobato.fastdfs.domain.StorePath; +import com.github.tobato.fastdfs.service.FastFileStorageClient; + +import java.io.File; +import java.io.IOException; +import java.io.FileInputStream; + +@RestController +@RequestMapping("/file") +public class FileController { + + @Autowired + UserService userService; + + @Autowired + FastFileStorageClient storageClient; + + @GetMapping("/test") + public User createUser() { + return new User(1L,"KK01", "null-"+System.currentTimeMillis()); + } + + @PostMapping("/") + public User upload(@RequestParam("file") CommonsMultipartFile file, @RequestParam("name") String name) throws IOException { + long startTime=System.currentTimeMillis(); + System.out.println("文件名称:" + file.getOriginalFilename()); + String fileName= startTime + "-" + file.getOriginalFilename(); + File newFile=new File(FileConfig.PIC_PATH + "/" +fileName); + System.out.println("文件路径:" + newFile.getAbsolutePath()); + file.transferTo(newFile); + long endTime=System.currentTimeMillis(); + System.out.println("文件处理时间:"+(endTime-startTime)+"ms"); + User user = new User(startTime, name, "http://localhost:8011/pic/"+fileName); + return userService.create(user); + } + + @PostMapping("/uploadDfs") + public User uploadDfs(@RequestParam("file") CommonsMultipartFile file, @RequestParam("name") String name) throws IOException { + long startTime=System.currentTimeMillis(); + System.out.println("文件名称:" + file.getOriginalFilename()); + String fileName= startTime + "-" + file.getOriginalFilename(); + File newFile=new File(FileConfig.PIC_PATH + "/" +fileName); + System.out.println("文件路径:" + newFile.getAbsolutePath()); + file.transferTo(newFile); + + FileInputStream is = new FileInputStream(newFile); + StorePath storePath = storageClient.uploadFile(is, newFile.length(), org.apache.commons.io.FilenameUtils.getExtension(newFile.getName()), null); + is.close(); + String fullPath = storePath.getFullPath(); + System.out.println("FastDFS Path = " + fullPath); + + long endTime=System.currentTimeMillis(); + System.out.println("文件处理时间:"+(endTime-startTime)+"ms"); + User user = new User(startTime, name, fullPath); + return userService.create(user); + } + + @RequestMapping("/findById") + public User findById(@RequestParam("id") Long id){ + return userService.findById(id); + } + + // get pic + + + +} diff --git a/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/mapper/UserMapper.java b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/mapper/UserMapper.java new file mode 100644 index 00000000..11ece03b --- /dev/null +++ b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/mapper/UserMapper.java @@ -0,0 +1,10 @@ +package io.github.kimmking.javacourse.dfs.mapper; + +import io.github.kimmking.javacourse.dfs.model.User; +import org.springframework.stereotype.Repository; + +@Repository +public interface UserMapper { + int create(User user); + User findById(Long id); +} diff --git a/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/model/User.java b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/model/User.java new file mode 100644 index 00000000..bb601bb6 --- /dev/null +++ b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/model/User.java @@ -0,0 +1,14 @@ +package io.github.kimmking.javacourse.dfs.model; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class User { + private Long id; + private String name; + private String picPath; +} diff --git a/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/service/UserService.java b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/service/UserService.java new file mode 100644 index 00000000..595cc672 --- /dev/null +++ b/11dfs/dfs/src/main/java/io/github/kimmking/javacourse/dfs/service/UserService.java @@ -0,0 +1,23 @@ +package io.github.kimmking.javacourse.dfs.service; + +import io.github.kimmking.javacourse.dfs.mapper.UserMapper; +import io.github.kimmking.javacourse.dfs.model.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class UserService { + + @Autowired + UserMapper userMapper; + + public User create(User user){ + int result = this.userMapper.create(user); + System.out.println("create user[" + user + "],result=" + result); + return result>0 ? user : null; + } + + public User findById(Long id){ + return this.userMapper.findById(id); + } +} diff --git a/11dfs/dfs/src/main/resources/application.yml b/11dfs/dfs/src/main/resources/application.yml new file mode 100644 index 00000000..d662bf93 --- /dev/null +++ b/11dfs/dfs/src/main/resources/application.yml @@ -0,0 +1,32 @@ +server: + port: 8011 +# tomcat: +# basedir: app + +spring: +# mvc: +# static-path-pattern: /** +# web: +# resources: +# static-locations: file:/Users/kimmking/kimmking/JavaCourseCodes/11dfs/dfs/pic/ +# # ## classpath:/META-INF/static/,classpath:/META-INF/public/,classpath:/META-INF/resources/, + datasource: + url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useServerPrepStmts=true&cachePrepStmts=true + username: root + password: + druid: + initial-size: 5 + max-active: 5 + +mybatis: + mapper-locations: classpath:mapper/*Mapper.xml + +logging: + level: + root: info + +fdfs: + so-timeout: 3000 + connect-timeout: 1000 + tracker-list: + - 62.234.122.77:22122 diff --git a/11dfs/dfs/src/main/resources/mapper/userMapper.xml b/11dfs/dfs/src/main/resources/mapper/userMapper.xml new file mode 100644 index 00000000..9008ebb8 --- /dev/null +++ b/11dfs/dfs/src/main/resources/mapper/userMapper.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + insert into user(id,name,pic_path) values(#{id},#{name},#{picPath}); + + + \ No newline at end of file diff --git a/11dfs/dfs/src/main/resources/static/index-dfs.html b/11dfs/dfs/src/main/resources/static/index-dfs.html new file mode 100644 index 00000000..72427468 --- /dev/null +++ b/11dfs/dfs/src/main/resources/static/index-dfs.html @@ -0,0 +1,17 @@ + + + + + 测试上传文件 + + +
+

请选择上传文件

+ +
+ +
+ +
+ + \ No newline at end of file diff --git a/11dfs/dfs/src/main/resources/static/index.html b/11dfs/dfs/src/main/resources/static/index.html new file mode 100644 index 00000000..36710e1e --- /dev/null +++ b/11dfs/dfs/src/main/resources/static/index.html @@ -0,0 +1,17 @@ + + + + + 测试上传文件 + + +
+

请选择上传文件

+ +
+ +
+ +
+ + \ No newline at end of file diff --git a/11dfs/dfs/src/test/java/io/github/kimmking/javacourse/dfs/DfsApplicationTests.java b/11dfs/dfs/src/test/java/io/github/kimmking/javacourse/dfs/DfsApplicationTests.java new file mode 100644 index 00000000..2d184891 --- /dev/null +++ b/11dfs/dfs/src/test/java/io/github/kimmking/javacourse/dfs/DfsApplicationTests.java @@ -0,0 +1,13 @@ +package io.github.kimmking.javacourse.dfs; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class DfsApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/README.md b/README.md index e6a677d6..252447c6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,14 @@ # JavaCourse -JavaCourse +Java进阶训练营示例代码。此处代码,一方面作为作业的基本版本,另一方面需要大家通过调整加深自己对技术点的认识。 + +本课程的三个要素: + +1)40%是课程,包括预习、听课、复习总结,形成自己对知识体系的认识和经验,这是最基本的学习方法。 + +2)30%是作业,作业包括基础版本的必做作业,补充的选做作业,高难度的挑战作业。基本上完成必做可以通过P6的技术面试,完成选做可以通过P7的技术面试,高难度的话可以达到P7+/P8的技术面试水平。这是通过练习,得到第一手的体验。 + +3)30%是活动,包括且不限于源码分析学习小组,技术文章活动,读书活动,线下技术沙龙,线上技术分享等。通过一群愿意学习的人,在更好的学习氛围中,实现长期深入学习。 + +## 挑战作业 + +每个模块的挑战作业:[homework2.0.md](./homework2.0.md) , 能做出来70%的题目,直接联系我,给你推荐一线大厂工作。 \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/app/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/app/.mvn/wrapper/MavenWrapperDownloader.java b/app/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..a45eb6ba --- /dev/null +++ b/app/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/app/.mvn/wrapper/maven-wrapper.jar b/app/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/app/.mvn/wrapper/maven-wrapper.jar differ diff --git a/app/.mvn/wrapper/maven-wrapper.properties b/app/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/app/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/app/mvnw b/app/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/app/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/app/mvnw.cmd b/app/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/app/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/app/pom.xml b/app/pom.xml new file mode 100644 index 00000000..3d06aa6c --- /dev/null +++ b/app/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.4 + + + com.example + app + 0.0.1-SNAPSHOT + app + Demo project for Spring Boot + + 1.8 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/app/src/main/java/com/example/app/App.java b/app/src/main/java/com/example/app/App.java new file mode 100644 index 00000000..bcd641a1 --- /dev/null +++ b/app/src/main/java/com/example/app/App.java @@ -0,0 +1,26 @@ +package com.example.app; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class App { + + public static void main(String[] args) { + SpringApplication.run(App.class, args); + } + + + + // ==== 测试自动配置 ==== + @Autowired + WebInfo info; + + @Bean + public void printInfo(){ + System.out.println(info.getName()); + } + +} diff --git a/app/src/main/java/com/example/app/WebAutoConfiguration.java b/app/src/main/java/com/example/app/WebAutoConfiguration.java new file mode 100644 index 00000000..e63d295f --- /dev/null +++ b/app/src/main/java/com/example/app/WebAutoConfiguration.java @@ -0,0 +1,25 @@ +package com.example.app; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +@Configuration +@Import(WebConfiguration.class) +@EnableConfigurationProperties(WebProperties.class) +public class WebAutoConfiguration { + + @Autowired + WebProperties properties; + + @Autowired + WebConfiguration configuration; + + @Bean + public WebInfo creatInfo(){ + return new WebInfo(configuration.name + "-"+properties.getA()); + } + +} diff --git a/app/src/main/java/com/example/app/WebConfiguration.java b/app/src/main/java/com/example/app/WebConfiguration.java new file mode 100644 index 00000000..d645d77b --- /dev/null +++ b/app/src/main/java/com/example/app/WebConfiguration.java @@ -0,0 +1,10 @@ +package com.example.app; + +import org.springframework.context.annotation.Configuration; + +@Configuration +public class WebConfiguration { + + public String name = "java"; + +} diff --git a/app/src/main/java/com/example/app/WebInfo.java b/app/src/main/java/com/example/app/WebInfo.java new file mode 100644 index 00000000..5fd4345b --- /dev/null +++ b/app/src/main/java/com/example/app/WebInfo.java @@ -0,0 +1,19 @@ +package com.example.app; + +public class WebInfo { + + public WebInfo(String name) { + this.name = name; + } + + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/app/src/main/java/com/example/app/WebProperties.java b/app/src/main/java/com/example/app/WebProperties.java new file mode 100644 index 00000000..713fbeed --- /dev/null +++ b/app/src/main/java/com/example/app/WebProperties.java @@ -0,0 +1,17 @@ +package com.example.app; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "web") +public class WebProperties { + + private String a = "aaa"; + + public String getA() { + return a; + } + + public void setA(String a) { + this.a = a; + } +} diff --git a/app/src/main/resources/META-INF/spring.factories b/app/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..e69de29b diff --git a/app/src/main/resources/application.properties b/app/src/main/resources/application.properties new file mode 100644 index 00000000..5167366f --- /dev/null +++ b/app/src/main/resources/application.properties @@ -0,0 +1 @@ +web.a=kimmking diff --git a/app/src/test/java/com/example/app/AppApplicationTests.java b/app/src/test/java/com/example/app/AppApplicationTests.java new file mode 100644 index 00000000..a9afa13c --- /dev/null +++ b/app/src/test/java/com/example/app/AppApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.app; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class AppApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/demo/.gitignore b/demo/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/demo/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/demo/.mvn/wrapper/MavenWrapperDownloader.java b/demo/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..e76d1f32 --- /dev/null +++ b/demo/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,117 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if(mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if(mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if(!outputFile.getParentFile().exists()) { + if(!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/demo/.mvn/wrapper/maven-wrapper.jar b/demo/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/demo/.mvn/wrapper/maven-wrapper.jar differ diff --git a/demo/.mvn/wrapper/maven-wrapper.properties b/demo/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/demo/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/demo/mvnw b/demo/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/demo/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/demo/mvnw.cmd b/demo/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/demo/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/demo/pom.xml b/demo/pom.xml new file mode 100644 index 00000000..52076da9 --- /dev/null +++ b/demo/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.4 + + + com.example + demo + 0.0.1-SNAPSHOT + demo + Demo project for Spring Boot + + 1.8 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/demo/src/main/java/com/example/demo/DemoApplication.java b/demo/src/main/java/com/example/demo/DemoApplication.java new file mode 100644 index 00000000..64b538a1 --- /dev/null +++ b/demo/src/main/java/com/example/demo/DemoApplication.java @@ -0,0 +1,13 @@ +package com.example.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DemoApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + +} diff --git a/demo/src/main/java/com/example/demo/controller/DemoController.java b/demo/src/main/java/com/example/demo/controller/DemoController.java new file mode 100644 index 00000000..10cac5a7 --- /dev/null +++ b/demo/src/main/java/com/example/demo/controller/DemoController.java @@ -0,0 +1,17 @@ +package com.example.demo.controller; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@EnableAutoConfiguration +@RequestMapping("/demo") +public class DemoController { + + @RequestMapping("/hello") + public String hello() { + return "KK-" + System.currentTimeMillis(); + } + +} diff --git a/demo/src/main/java/com/example/demo/controller/UserController.java b/demo/src/main/java/com/example/demo/controller/UserController.java new file mode 100644 index 00000000..6a9e87f9 --- /dev/null +++ b/demo/src/main/java/com/example/demo/controller/UserController.java @@ -0,0 +1,17 @@ +package com.example.demo.controller; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@EnableAutoConfiguration +@RequestMapping("/user") +public class UserController { + + @RequestMapping("/list") + public String list() { + return "KK-" + System.currentTimeMillis(); + } + +} diff --git a/demo/src/main/resources/application.yml b/demo/src/main/resources/application.yml new file mode 100644 index 00000000..a7afc92b --- /dev/null +++ b/demo/src/main/resources/application.yml @@ -0,0 +1,2 @@ +server: + port: 8080 diff --git a/demo/src/test/java/com/example/demo/DemoApplicationTests.java b/demo/src/test/java/com/example/demo/DemoApplicationTests.java new file mode 100644 index 00000000..2778a6a7 --- /dev/null +++ b/demo/src/test/java/com/example/demo/DemoApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.demo; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class DemoApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/demoidea/.gitignore b/demoidea/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/demoidea/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/demoidea/.mvn/wrapper/MavenWrapperDownloader.java b/demoidea/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..a45eb6ba --- /dev/null +++ b/demoidea/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/demoidea/.mvn/wrapper/maven-wrapper.jar b/demoidea/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/demoidea/.mvn/wrapper/maven-wrapper.jar differ diff --git a/demoidea/.mvn/wrapper/maven-wrapper.properties b/demoidea/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/demoidea/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/demoidea/mvnw b/demoidea/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/demoidea/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/demoidea/mvnw.cmd b/demoidea/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/demoidea/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/demoidea/pom.xml b/demoidea/pom.xml new file mode 100644 index 00000000..4ca50254 --- /dev/null +++ b/demoidea/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.4 + + + io.github.kimmking + demoidea + 0.0.1-SNAPSHOT + demoidea + Demo project for Spring Boot + + 1.8 + + + + org.springframework.boot + spring-boot-starter-activemq + + + org.springframework.boot + spring-boot-starter-data-jdbc + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/demoidea/src/main/java/io/github/kimmking/demoidea/DemoideaApplication.java b/demoidea/src/main/java/io/github/kimmking/demoidea/DemoideaApplication.java new file mode 100644 index 00000000..ba5c61cf --- /dev/null +++ b/demoidea/src/main/java/io/github/kimmking/demoidea/DemoideaApplication.java @@ -0,0 +1,13 @@ +package io.github.kimmking.demoidea; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DemoideaApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoideaApplication.class, args); + } + +} diff --git a/demoidea/src/main/resources/application.properties b/demoidea/src/main/resources/application.properties new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/demoidea/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/demoidea/src/test/java/io/github/kimmking/demoidea/DemoideaApplicationTests.java b/demoidea/src/test/java/io/github/kimmking/demoidea/DemoideaApplicationTests.java new file mode 100644 index 00000000..428c4548 --- /dev/null +++ b/demoidea/src/test/java/io/github/kimmking/demoidea/DemoideaApplicationTests.java @@ -0,0 +1,13 @@ +package io.github.kimmking.demoidea; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class DemoideaApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/homework2.0.md b/homework2.0.md new file mode 100644 index 00000000..ab6193f2 --- /dev/null +++ b/homework2.0.md @@ -0,0 +1,197 @@ +以下附加作业是针对目前已经符合P6+/P7水平的同学,并且有时间可以挑战一下自己的。能做出来90分以上,直接联系我。 + +### 1. JVM + +从Classloader到模块化,动态加载的插件机制。 + +1. 10-使用自定义Classloader机制,实现xlass的加载:xlass是作业材料。 +2. 20-实现xlass打包的xar(类似class文件打包的jar)的加载:xar里是xlass。 +3. 30-基于自定义Classloader实现类的动态加载和卸载:需要设计加载和卸载。 +4. 30-基于自定义Classloader实现模块化机制:需要设计模块化机制。 +5. 30-使用xar作为模块,实现xar动态加载和卸载:综合应用前面的内容。 + +### 2. NIO + +实现一个http 文件服务器和一个ftp文件服务器。 +1. 10-实现文件列表展示:http直接网页展示列表即可。ftp支持cd、ls命令。 +2. 20-实现文件上传下载:http上传不需要支持multi-part,直接post文件内容即可。ftp只需要支持主动模式或被动模式的一种。 +3. 30-支持断点续传:http下载需要实现range,上传需要自己设计服务器端的分片方式并记录。ftp需要实现retr,stor,rest命令。 +4. 30-实现多线程文件上传下载:基于断点续传,需考虑客户端分片方式,多线程调度。 +5. 30-实现爬虫爬取前面实现的服务器上所有文件:需要考虑html解析,记录多个文件的传输进度,位置等。 + +### 3. 并发 + +#### 3.1-侧重集合: + +1. 10-基于基本类型和数组,实现ArrayList/LinkedList,支持自动扩容和迭代器 +2. 20-基于基本类型和数组和List,HashMap/LinkedHashMap功能,处理hash冲突和扩容 +3. 30-考虑List和Map的并发安全问题,基于读写锁改进安全问题 +4. 30-考虑List和Map的并发安全问题,基于AQS改进安全问题 +5. 30-编写测试代码比较它们与java-util/JUC集合类的性能和并发安全性 + +#### 3.2-侧重应用: + +1. 10-根据课程提供的场景,实现一个订单处理Service,模拟处理100万订单:后面提供模拟数据 +2. 20-使用多线程方法优化订单处理,对比处理性能 +3. 30-使用并发工具和集合类改进订单Service,对比处理性能 +4. 30-使用分布式集群+分库分表方式处理拆分订单,对比处理性能:第6模块讲解分库分表 +5. 30-使用读写分离和分布式缓存优化订单的读性能:第6、8模块讲解读写分离和缓存 + +### 4. 框架 + +#### 4.1 Spring AOP + +1. 10-讲网关的frontend/backend/filter/router/线程池都改造成Spring配置方式 +2. 20-基于AOP改造Netty网关,filter和router使用AOP方式实现 +3. 30-基于前述改造,将网关请求前后端分离,中级使用JMS传递消息 +4. 30-尝试使用ByteBuddy实现一个简单的基于类的AOP +5. 30-尝试使用ByteBuddy与Instrument实现一个简单JavaAgent实现无侵入下的AOP + +#### 4.2 Spring ORM + +1. 基于AOP和自定义注解,实现@MyCache(60)对于指定方法返回值缓存60秒 +2. 自定义实现一个数据库连接池,并整合Hibernate/Mybatis/Spring/SpringBoot +3. 基于MyBatis实现一个简单的分库分表+读写分离+分布式ID生成方案 + +### 5. 数据库与性能 + +1. 模拟1000万订单数据,测试不同方式下导入导出(数据备份还原)MySQL的速度,包括jdbc程序处理和命令行处理,思考和实践,如何提升处理效率 +2. 对MySQL配置不同的数据库连接池(DBCP、C3P0、Druid、Hikari),测试增删改查100万次,对比性能,生成报告 +3. 尝试自己做一个ID生成器(可以模拟Seq或Snowflake) +4. 尝试实现或改造一个非精确分页的组件,思考是否可以用于改造自己的业务系统 +5. 基于必做作业2.0版本,实现读写分离-数据库中间件版本3.0 + +### 6. 分库分表 + +1. 思考总结常用的数据拆分和数据迁移同步方案,以及它们的优势劣势,适用场景,考虑是否可以引入到自己的工作中 +2. 设计实现一个简单的XA分布式事务框架demo,只需要能管理和调用2个MySQL的本地事务即可,不需要考虑全局事务的持久化和恢复、高可用等 +3. 设计实现一个TCC分布式事务框架的简单Demo,需要实现事务管理器,不需要实现全局事务的持久化和恢复、高可用等 +4. 设计实现一个AT分布式事务框架的简单Demo,仅需要支持根据主键id进行的单个删改操作的SQL或插入操作的事务 + +### 7. RPC与分布式服务化 + +#### 7.1 RPC与Dubbo + +1. 升级作业中的自定义RPC程序: +- 尝试使用压测并分析优化RPC性能 +- 尝试使用Netty+TCP作为两端传输方式 +- 尝试自定义二进制序列化或者使用kyro/fst等 +- 尝试压测改进后的RPC并分析优化,有问题欢迎群里讨论 +- 尝试将fastjson改成xstream +- 尝试使用字节码生成方式代替服务端反射 + +2. 尝试扩展Dubbo +- 基于上次作业的自定义序列化,实现Dubbo的序列化扩展; +- 基于上次作业的自定义RPC,实现Dubbo的RPC扩展; +- 在Dubbo的filter机制上,实现REST权限控制,可参考dubbox; +- 实现自定义Dubbo的Cluster/Loadbalance扩展,如果一分钟内调用某个服务/提供者超过10次,则拒绝提供服务直到下一分钟; +- 整合Dubbo+Sentinel,实现限流功能; +- 整合Dubbo与Skywalking,实现全链路性能监控。 + +#### 7.2 自定义RPC + +1. rpcfx1.1: 给自定义RPC实现简单的分组(group)和版本(version)。 + +2. rpcfx2.0: 给自定义RPC实现: + - 基于zookeeper的注册中心,消费者和生产者可以根据注册中心查找可用服务进行调用(直接选择列表里的最后一个)。 + - 当有生产者启动或者下线时,通过zookeeper通知并更新各个消费者,使得各个消费者可以调用新生产者或者不调用下线生产者。 + +3. 在2.0的基础上继续增强rpcfx实现: +- 3.0: 实现基于zookeeper的配置中心,消费者和生产者可以根据配置中心配置参数(分组,版本,线程池大小等)。 +- 3.1:实现基于zookeeper的元数据中心,将服务描述元数据保存到元数据中心。 +- 3.2:实现基于etcd/nacos/apollo等基座的配置/注册/元数据中心。 + +4. 在3.2的基础上继续增强rpcfx实现: +- 4.0:实现基于tag的简单路由; +- 4.1:实现基于Weight/ConsistentHash的负载均衡; +- 4.2:实现基于IP黑名单的简单流控; +- 4.3:完善RPC框架里的超时处理,增加重试参数; + +5. 在4.3的基础上继续增强rpcfx实现: +- 5.0:实现利用HTTP头跨进程传递Context参数(隐式传参); +- 5.1:实现消费端mock一个指定对象的功能(Mock功能); +- 5.2:实现消费端可以通过一个泛化接口调用不同服务(泛化调用); +- 5.3:实现基于Weight/ConsistentHash的负载均衡; +- 5.4:实现基于单位时间调用次数的流控,可以基于令牌桶等算法; + +6. 实现最终版本6.0:压测并分析调优5.4版本。 + +### 8. 分布式缓存 + +1. 基于其他各类场景,设计并在示例代码中实现简单demo: +- 实现分数排名或者排行榜; +- 实现全局ID生成; +- 基于Bitmap实现id去重; +- 基于HLL实现点击量计数。 +- 以redis作为数据库,模拟使用lua脚本实现前面课程的外汇交易事务。 + +2. 升级改造项目: +- 实现guava cache的spring cache适配; +- 替换jackson序列化为fastjson或者fst,kryo; +- 对项目进行分析和性能调优。 + +3. 以redis作为基础实现上个模块的自定义rpc的注册中心; +4. 练习redission的各种功能; +5. 练习hazelcast的各种功能; +6. 搭建hazelcast 3节点集群,写入100万数据到一个map,模拟和演示高可用,测试一下性能。 + +### 9. 分布式消息 + +#### 9.1 消息队列原理与应用 + +1. 基于数据库的订单表,模拟消息队列处理订单: +- 一个程序往表里写新订单,标记状态为未处理(status=0); +- 另一个程序每隔100ms定时从表里读取所有status=0的订单,打印一下订单数据,然后改成完成status=1; +- 考虑失败重试策略,考虑多个消费程序如何协作; +- 将上述订单处理场景,改成使用ActiveMQ发送消息处理模式; +- 使用java代码,创建一个ActiveMQ Broker Server,并测试它; + +2. ActiveMQ/RabbitMQ作业 +- 搭建ActiveMQ的network集群和master-slave主从结构; +- 基于ActiveMQ的MQTT实现简单的聊天功能或者Android消息推送; +- 创建一个RabbitMQ,用Java代码实现简单的AMQP协议操作; +- 搭建RabbitMQ集群,重新实现前面的订单处理; +- 使用Apache Camel打通上述ActiveMQ集群和RabbitMQ集群,实现所有写入到ActiveMQ上的一个队列q24的消息,自动转发到RabbitMQ; +- 压测ActiveMQ和RabbitMQ的性能; + +3. 演练本课提及的各种生产者和消费者特性。 + +4. Kafka金融领域实战:在证券或者外汇、数字货币类金融核心交易系统里,对于订单的处理,大概可以分为收单、定序、撮合、清算等步骤。其中我们一般可以用mq来实现订单定序,然后将订单发送给撮合模块。 +- 收单:请实现一个订单的rest接口,能够接收一个订单Order对象; +- 定序:将Order对象写入到kafka集群的order.usd2cny队列,要求数据有序并且不丢失; +- 撮合:模拟撮合程序(不需要实现撮合逻辑),从kafka获取order数据,并打印订单信息,要求可重放, 顺序消费, 消息仅处理一次。 + +#### 9.2 自定义消息中间件 + +1. v1.0-内存队列:基于内存Queue实现生产和消费API(示例代码已经完成) +- 创建内存BlockingQueue,作为底层消息存储 +- 定义Topic,支持多个Topic +- 定义Producer,支持Send消息 +- 定义Consumer,支持Poll消息 + +2. v2.0-自定义队列:去掉内存Queue,设计自定义Queue,实现消息确认和消费offset +- 自定义内存Message数组模拟Queue。 +- 使用指针记录当前消息写入位置。 +- 对于每个命名消费者,用指针记录消费位置。 + +3. v3.0-基于SpringMVC实现MQServer:拆分broker和client(包括producer和consumer),从单机走向服务器模式。 +- 将Queue保存到web server端 +- 设计消息读写API接口,确认接口,提交offset接口 +- producer和consumer通过httpclient访问Queue +- 实现消息确认,offset提交 +- 实现consumer从offset增量拉取 + +4. v4.0-功能全面:增加多种策略(各条之间没有关系,可以任意选择实现),基于TCP实现server->client,从而实现 PUSH模式 +- 考虑实现消息过期,消息重试,消息定时投递等策略 +- 考虑批量操作,包括读写,可以打包和压缩 +- 考虑消息清理策略,包括定时清理,按容量清理、LRU等 +- 考虑消息持久化,存入数据库,或WAL日志文件,或BookKeeper +- 考虑将spring mvc替换成netty下的tcp传输协议,rsocket/websocket + +5. v5.0-优化完善:对接各种技术(各条之间没有关系,可以任意选择实现) +- 考虑封装 JMS 1.1 接口规范 +- 考虑实现 STOMP 消息规范 +- 考虑实现消息事务机制与事务管理器 +- 对接Spring +- 对接Camel或Spring Integration +- 优化内存和磁盘的使用 diff --git a/java11/.gitignore b/java11/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/java11/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/java11/.mvn/wrapper/MavenWrapperDownloader.java b/java11/.mvn/wrapper/MavenWrapperDownloader.java new file mode 100644 index 00000000..a45eb6ba --- /dev/null +++ b/java11/.mvn/wrapper/MavenWrapperDownloader.java @@ -0,0 +1,118 @@ +/* + * Copyright 2007-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.net.*; +import java.io.*; +import java.nio.channels.*; +import java.util.Properties; + +public class MavenWrapperDownloader { + + private static final String WRAPPER_VERSION = "0.5.6"; + /** + * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. + */ + private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" + + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; + + /** + * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to + * use instead of the default one. + */ + private static final String MAVEN_WRAPPER_PROPERTIES_PATH = + ".mvn/wrapper/maven-wrapper.properties"; + + /** + * Path where the maven-wrapper.jar will be saved to. + */ + private static final String MAVEN_WRAPPER_JAR_PATH = + ".mvn/wrapper/maven-wrapper.jar"; + + /** + * Name of the property which should be used to override the default download url for the wrapper. + */ + private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; + + public static void main(String args[]) { + System.out.println("- Downloader started"); + File baseDirectory = new File(args[0]); + System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); + + // If the maven-wrapper.properties exists, read it and check if it contains a custom + // wrapperUrl parameter. + File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); + String url = DEFAULT_DOWNLOAD_URL; + if (mavenWrapperPropertyFile.exists()) { + FileInputStream mavenWrapperPropertyFileInputStream = null; + try { + mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); + Properties mavenWrapperProperties = new Properties(); + mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); + url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); + } catch (IOException e) { + System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); + } finally { + try { + if (mavenWrapperPropertyFileInputStream != null) { + mavenWrapperPropertyFileInputStream.close(); + } + } catch (IOException e) { + // Ignore ... + } + } + } + System.out.println("- Downloading from: " + url); + + File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); + if (!outputFile.getParentFile().exists()) { + if (!outputFile.getParentFile().mkdirs()) { + System.out.println( + "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); + } + } + System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); + try { + downloadFileFromURL(url, outputFile); + System.out.println("Done"); + System.exit(0); + } catch (Throwable e) { + System.out.println("- Error downloading"); + e.printStackTrace(); + System.exit(1); + } + } + + private static void downloadFileFromURL(String urlString, File destination) throws Exception { + if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { + String username = System.getenv("MVNW_USERNAME"); + char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); + Authenticator.setDefault(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + } + URL website = new URL(urlString); + ReadableByteChannel rbc; + rbc = Channels.newChannel(website.openStream()); + FileOutputStream fos = new FileOutputStream(destination); + fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + fos.close(); + rbc.close(); + } + +} diff --git a/java11/.mvn/wrapper/maven-wrapper.jar b/java11/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 00000000..2cc7d4a5 Binary files /dev/null and b/java11/.mvn/wrapper/maven-wrapper.jar differ diff --git a/java11/.mvn/wrapper/maven-wrapper.properties b/java11/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..642d572c --- /dev/null +++ b/java11/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,2 @@ +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar diff --git a/java11/klass.png b/java11/klass.png new file mode 100644 index 00000000..264750de Binary files /dev/null and b/java11/klass.png differ diff --git a/java11/list.png b/java11/list.png new file mode 100644 index 00000000..02b2f4f7 Binary files /dev/null and b/java11/list.png differ diff --git a/java11/map.png b/java11/map.png new file mode 100644 index 00000000..034e4917 Binary files /dev/null and b/java11/map.png differ diff --git a/java11/mvnw b/java11/mvnw new file mode 100755 index 00000000..a16b5431 --- /dev/null +++ b/java11/mvnw @@ -0,0 +1,310 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/java11/mvnw.cmd b/java11/mvnw.cmd new file mode 100644 index 00000000..c8d43372 --- /dev/null +++ b/java11/mvnw.cmd @@ -0,0 +1,182 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM https://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + +FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/java11/pom.xml b/java11/pom.xml new file mode 100644 index 00000000..19b803dc --- /dev/null +++ b/java11/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.4 + + + com.example + demo + 0.0.1-SNAPSHOT + demo + Demo project for Spring Boot + + 11 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.openjdk.jol + jol-core + 0.16 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/java11/src/main/java/com/example/demo/DemoApplication.java b/java11/src/main/java/com/example/demo/DemoApplication.java new file mode 100644 index 00000000..f314b278 --- /dev/null +++ b/java11/src/main/java/com/example/demo/DemoApplication.java @@ -0,0 +1,146 @@ +package com.example.demo; + +import org.openjdk.jol.info.ClassLayout; +import org.openjdk.jol.info.GraphLayout; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import java.io.IOException; +import java.util.*; + +@SpringBootApplication +public class DemoApplication implements ApplicationRunner { + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + + @Override + public void run(ApplicationArguments args) throws Exception { + +// ClassLoader app = this.getClass().getClassLoader(); +// System.out.println(" APP Classloader => " + app.getName()); +// for (Package definedPackage : app.getDefinedPackages()) { +// System.out.println(definedPackage.getName()); +// } + + + printObject(); + printString(); + printIntArray(); + + printUser(); + + printListGraph(); + } + + private void printListGraph() { + + Random random = new Random(); + List list = new ArrayList<>(128); + for (int i = 0; i < 128; i++) { + list.add(i, random.nextInt(128)); + } + + try { + System.out.println(" ===> GraphLayout.parseInstance(list).toImage(\"list.png\")"); + GraphLayout.parseInstance(list).toImage("list.png"); + } catch (IOException e) { + throw new RuntimeException(e); + } + + Map map = new HashMap(256); + for (int i = 0; i < 128; i++) { + map.put("K" + i, i); + } + + try { + System.out.println(" ===> GraphLayout.parseInstance(map).toImage(\"map.png\"))"); + GraphLayout.parseInstance(map).toImage("map.png"); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + private void printUser() { + + System.out.println(" ===> ClassLayout.parseInstance(user).toPrintable()"); + User user = new User(true, (byte) 65,12345, 8888, 12345678L, 9999999L, "a"); + System.out.println(ClassLayout.parseInstance(user).toPrintable()); + + System.out.println(" ===> GraphLayout.parseInstance(user).toPrintable()"); + System.out.println(GraphLayout.parseInstance(user).toPrintable()); + + System.out.println(" ===> GraphLayout.parseInstance(user).toFootprint()"); + System.out.println(GraphLayout.parseInstance(user).toFootprint()); + +// try { +// GraphLayout.parseInstance(user).toImage("user.png"); +// } catch (IOException e) { +// e.printStackTrace(); +// } + + User user1 = new User(false, (byte) 66,12346, 5555, 12345679L, 888888L, "b"); + Klass klass = new Klass("Klass1", Arrays.asList(user,user1)); + + System.out.println(" ===> ClassLayout.parseInstance(klass).toPrintable()"); + System.out.println(ClassLayout.parseInstance(klass).toPrintable()); + + System.out.println(" ===> GraphLayout.parseInstance(klass).toImage(\"klass.png\")"); + try { + GraphLayout.parseInstance(klass,user,user1).toImage("klass.png"); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + private void printObject() { + System.out.println(ClassLayout.parseInstance(new Object()).toPrintable()); + } + + private void printString() { + System.out.println(ClassLayout.parseInstance("abcde12345").toPrintable()); + } + + private void printIntArray() { + System.out.println(ClassLayout.parseInstance(new int[2]).toPrintable()); + System.out.println(ClassLayout.parseInstance(new int[12]).toPrintable()); + System.out.println(ClassLayout.parseInstance(new int[2][50]).toPrintable()); + System.out.println(ClassLayout.parseInstance(new int[50][2]).toPrintable()); + } + + public static class User { + public User(boolean b1, byte b2, int a, Integer b, long c, Long d, String s) { + this.b1 = b1; + this.b2 = b2; + this.a = a; + this.b = b; + this.c = c; + this.d = d; + this.s = s; + } + + private boolean b1; + private byte b2; + private int a; + private Integer b; + private long c; + private Long d; + private String s; + } + + public static class Klass { + private String klassName; + private List users; + + public Klass(String klassName, List users) { + this.klassName = klassName; + this.users = users; + } + } + +} diff --git a/java11/src/main/resources/application.properties b/java11/src/main/resources/application.properties new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/java11/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/java11/src/test/java/com/example/demo/DemoApplicationTests.java b/java11/src/test/java/com/example/demo/DemoApplicationTests.java new file mode 100644 index 00000000..de52af5b --- /dev/null +++ b/java11/src/test/java/com/example/demo/DemoApplicationTests.java @@ -0,0 +1,13 @@ +//package com.example.demo; +// +//import org.junit.jupiter.api.Test; +//import org.springframework.boot.test.context.SpringBootTest; +// +//@SpringBootTest +//class DemoApplicationTests { +// +// @Test +// void contextLoads() { +// } +// +//} diff --git a/java11/user.png b/java11/user.png new file mode 100644 index 00000000..c7c20997 Binary files /dev/null and b/java11/user.png differ diff --git a/test.txt b/test.txt new file mode 100644 index 00000000..880d7876 --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +nohup java -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnSafepointTimeout -XX:+ShowMessageBoxOnError -Xmx1g -Xms1g -XX:+SafepointTimeout -XX:SafepointTimeoutDelay=100 -Xlog:gc*=trace,heap*=trace,safepoint*=trace,logging*=trace:file=gc-%p-%t.log:pid,utctime,level,tags:filecount=100,filesize=10m -jar gateway-server-0.0.1-SNAPSHOT.jar & \ No newline at end of file diff --git "a/\344\275\234\344\270\232\346\263\250\346\204\217\344\272\213\351\241\271.md" "b/\344\275\234\344\270\232\346\263\250\346\204\217\344\272\213\351\241\271.md" new file mode 100644 index 00000000..46e0b9aa --- /dev/null +++ "b/\344\275\234\344\270\232\346\263\250\346\204\217\344\272\213\351\241\271.md" @@ -0,0 +1,315 @@ +# 作业注意事项 + + +> 课程内容: + +课程1、Java中的多线程与并发编程,某一线公司技术专家任富飞。 +Part1:11-22,周一,18:45-19:45,主要内容: + +Java多线程原理、各种锁的机制和应用(重量级锁、轻量级锁,可重入锁,偏向锁,公平锁,自旋锁,读写锁,乐观锁等)、四种线程池的原理、类型、配置参数和经验,Spring中线程池的应用,几种常用原子类的原理和使用场景。 + +Part2:11-26,周五,18:45-19:45,主要内容:AQS原理,三种常用并发工具类(CountdownLatch/Semaphore/CyclicBarrier)使用场景示例,线程安全编程和优化经验。 + +课程2、JVM的体系发展与优化经验,长亮科技北京平台团队负责人秦金卫。 +Part1:11-29,周一,18:45-19:45,主要内容:JVM基础(字节码、内存模型、类加载器),各种不同的GC(串行GC、并行GC、CMS、G1、ZGC)的基本原理、常用参数和分析经验。 +Part2:12-03,周五,18:45-19:45,主要内容:常见JVM命令行工具、可视化工具,以及高级工具的使用,通过GC日志、线程堆栈、飞行记录以及可视化分析等方式,实现JVM运行状态的分析和优化,常见的JVM问题排查。 + +课程3:事务原理与Spring事务机制,京东科技技术专家、Apache Shenyu项目负责人、《分布式事务原理与实战》作者肖宇。 +Part1:12-06,周一,18:45-19:45,主要内容:关系数据库的事务机制和隔离级别(读未提交/读已提交/可重复读/串行化),Spring TX事务管理器的功能特性、使用方式。 +Part2:12-10,周五,18:45-19:45,主要内容:Spring TX事务管理器的原理设计,相关的API和注解,传播特性,跟其他框架的集成,以及常见问题处理经验。 + +课程4、网络编程NIO与Netty实践,长亮科技北京平台团队负责人秦金卫。 +Part1:12-13,周一,18:45-19:45,主要内容:Java网络编程模型,BIO/NIO/AIO,Reactor/Proactor网络模型,select/epoll机制; +Part2:12-17,周五,18:45-19:45,主要内容:Netty的基本原理、常见用法与网络编程优化经验,Channel/EventLoop/Handler/Adapter + + +> 批阅原则: + +- 1. 认可与表扬认真学习的同学, 尽量给与正面反馈, 加油打气。 +- 2. 对一些问题和不足进行友善的提示, 都是成年人, 大家都要面子。 +- 3. 引导同学们学习各种进阶知识,拓展知识点。 + + +简介: + +``` +铁锚:系统架构师,Java性能调优专家。 +CSDN博客专家: https://renfufei.blog.csdn.net/ +热爱程序开发和设计; 积极应对各种情境和挑战; +喜欢钻研新技术, 闲暇时喜欢翻译和分析英文文档/技术博客。 +技术文章翻译仓库: https://github.com/cncounter/translation/ +``` + + + +## 1周 + + +提示: +- 尝试自己从头绘制一幅图形,完善后向自己的同事或者朋友介绍,加深自己的印象和理解 +- JDK7及之前的版本是永久代, 对应到JDK8及以后是Meta区, class信息保存着方法区之中。 +- 常量池位于方法区/Meta区之中, Mata区归属于非堆内存(Non-Heap),不是堆外内存,也不是直接内存 +- CCS是开启指针压缩时才有的, 关闭指针压缩则此空间的大小为0 +- 直接内存(Direct)属于堆外内存,不属于非堆 +- 堆外内存可简单划分为(Direct, Native), 本次作业对应的所有内存区域都在JVM进程内部 +- JIT编译后的机器代码缓存到 code cache/非堆之中; +- `-Xmx` 和 `-Xms` 设置的是整个堆内存的大小,不是老年代。 +- 和年轻代(young/nursery)不一样,新生代实际上是 Eden 区, 由 `-Xmn` 与存活区的比例来控制 +- 栈内存不在堆内存之中,也不属于堆外内存,是一个独立的部分。 +- 注意区分线程栈以及内部的栈帧结构; `-Xss` 控制单个线程的栈空间最大大小 +- 编码风格: 建议增加中间变量,增加可读性,可读性和可维护性是构建大型复杂系统的基石。 +- 注意关闭输入输出流,最好是谁控制着打开的,就由谁在同一个方法内将其关闭。 +- 在开发中可以打断点调试,查看class加载时的调用栈和调用关系。 +- 作业参考链接: https://github.com/JavaCourse00/JavaCourseCodes/tree/main/01jvm +- 《Java资料链接汇总》: +- 秦老师推荐书单: +- 铁锚的翻译仓库: + + + +## 2周 + +认真完成作业和整理笔记 + +- 在finally中关闭输入输出流是一个好习惯; 每次提交认真写提交日志也是一个好习惯。 +- 分辨HTTP1.0与HTTP1.1的区别; +- 可以尝试分析G1中混合模式 mixed GC的威力; 选择哪种垃圾收集器,主要考虑的是业务特征(实时/非实时),跟内存大小关系并不太大。 +- 与外部系统的交互,在允许的情况下,最好是输出详细的交互日志, 包括: [uri,请求参数/body,响应信息,耗时信息]等等。 +- 性能测试时需要排除各种干扰,只改变单个变量然后进行对比,例如: 客户端线程数,并发连接数/用户数,去除随机数的随机性(指定random种子)等等。 +- 编码风格: 建议增加中间变量,增加可读性,可读性和可维护性是构建大型复杂系统的基石。 +- 建议: 适当使用一些通用工具类,例如 apache的commons-io等等,里面的 IOUtils之类的工具很好用。 +- 建议:趁机使用和学习 markdown 文件,做好目录和链接引用,在互联网上很方便,而且可以导出为各种格式。常用工具: Typora 等。 + +- 往期优秀作业链接: +- 作业参考链接: +- 《Java资料链接汇总》: +- 秦老师推荐书单: +- 铁锚的翻译仓库: 【可以找感兴趣的技术文章看看】 + + + + + +## 3周 + + +提示: + +- 梳理一下网关需要执行的操作和处理逻辑,对比一下Nginx是怎么处理的。 +- 使用自己的包名, 来标识自己的作品 +- 通过 .gitignore 文件来忽略某些文件。 +- 性能测试时需要排除各种干扰,只改变单个因素, 然后进行对比,例如: 客户端线程数,并发连接数/用户数等等。 +- 不要用实例变量来存储某些信息,避免多线程环境下的出错,以及多次请求互相污染。 +- 适当增加必要的注释来增加可读性,说明某段代码的作用和实现逻辑,但不要太多或者太少 ^_^ +- 响应头之中也可以加上某些 X-开始的header,方便调试和追踪,更像一个网关 +- 响应消息写入完成后, 记得 flush 和 close。推荐在finally中关闭,避免中间过程抛异常导致资源不关闭,引发内存泄漏, 在高并发场景中造成问题。 +- 可以了解一下 ByteArrayOutputStream,挺好用. +- 注意Java输入流的特征,以及基于buffer的读取和写入方法, 不要写多了, 也不要读漏了. +- 注意区分 HTTP1.0 和 HTTP1.1的差异 +- 所有使用 getBytes 和String相关的方法都需要指定具体的编码, 避免默认语言问题。 + +- 往期优秀作业链接: +- 《Java资料链接汇总》: +- 秦老师推荐书单: +- 铁锚的技术文章翻译: + + + +## 4周 + +笔记整理。 + +提示: +- 请复习创建线程池的方法, 分辨 cached 方式的线程池是先加队列还是先创将cache线程? 提示: core,queue,cache,reject +- 多线程代码中,共享状态的变量需要特殊处理,比如使用 volatile 来强制从内存读取最新值,或者直接使用原子类来保存。 +- 思考各种线程协同方式的本质是什么? +- 想想怎么让线程处于各种不同的状态, 以及怎么获取这些状态信息 +- 线程最重要的属性是哪些? +- 尝试使用反射方式在main方法中将这些方法全部调用起来 +- 往期优秀作业链接: +- 《Java资料链接汇总》: +- 秦老师推荐书单: +- 铁锚的技术文章翻译: + +并发相关的面试题,在【第一课】【预习资料以及环境准备】里面的压缩包之中。 + + + + +## 5周 + + +提示: +- 枚举实际上是可以通过反射往里面加东西的,当然,在编译期间不可见,属于运行时hack修改。 +- 反编译工具可以使用: jd-gui, 或者 jclasslib +- 往期优秀作业链接: +- 《Java资料链接汇总》: +- 秦老师推荐书单: +- 深入系列: InnoDB存储引擎: + + + + +## 6 周 + +提示: +- 建议: 在Table级别使用 utf8mb4 字符集 +- 设计合理的SQL语句和注释,并通过Git形成版本化。 +- 使用BIGINT来存储Long类型的时间戳timeMillis,减少对时区的依赖。 +- 可以根据业务需要, 增加联合索引。 +- 可以结合自己在网上购物的经历, 思考如何设计订单表。 +- 往期优秀作业链接: +- 《Java资料链接汇总》: +- [InnoDB的锁和事务模型](https://github.com/cncounter/translation/blob/master/tiemao_2020/44_innodb-storage-engine/14.7_innodb-locking-transaction-model_CN.md) + + +通过上周的课程学习, 经过本次作业实践, 请思考以下问题: +1. MySQL数据库中, int(2) 和 int(10) 有什么区别? +2. 订单表中的【创建时间】字段采用以下类型, 各自的优缺点是什么? +- A、 datetime +- B、 timestamp +- C、 bigint +- D、 int +- E、 varchar + + +## 7周 + + +提示: +0. 想一想限制数据库TPS的瓶颈点有哪些? 分析整个请求执行链路上的各个环节, 并尝试解决各种环节下的瓶颈。 +1. 可以使用aop来做动态切换,根据sql类型来做数据源切换不能很好支持强制走主库查询的情况 +2. 注解时考虑使用数据源名称,而不是master-slave;应用内可能有多个数据库组(比如账号数据一套主从,订单数据一套主从) +3. 考虑多个从库的情况 +4. 注解加到mapper(dao层),而不是service层;具体实现可以考虑各种情景确定是否采用注解赋予的建议。 +5. aop可以看下AbstractPointcutAdvisor +6. 《Java资料链接汇总》: +7. 可以参考的作业实现: + - + - +8. 秦老师推荐书单: +9. 铁锚的技术文章翻译: + + + +## 8周 + + +提示: + +- 想想什么情况下适合使用分表,什么情况下适合使用分库,各有什么优缺点? +- 注意分库与分表的表达式, 避免分布不均匀或者导致空表 +- 适当增减注释来说明每一块代码的意图。 +- 《Java资料链接汇总》: +- 秦老师推荐书单: +- 铁锚的技术文章翻译: +- CSDN博客: https://renfufei.blog.csdn.net/ +- 优秀作业参考: + - + - + - + + + + +## 9周 + + +提示: + +- id 可以使用 BIGINT(20), 更加专业一些。 +- 根据实际情况, 可以在某些情况下, 使用 BIGINT 来存储金额,单位则约定为"分", 由程序进行换算, 避免程序层面出现精度丢失。 +- 如果使用BigDecimal计算, 则一定要指定精度和四舍五入规则; 并且与数据库字段的精度对应。 +- 《Java资料链接汇总》: +- 往期优秀作业链接: +- 李睿老师的完整RPC实现: +- 秦老师推荐书单: +- 铁锚的技术文章翻译: + + + + + +## 10周 + + +请思考以下问题: + +- 通过这个作业有哪些收获? +- RPC通信中有哪些需要注意的问题? +- 序列化和反序列化框架有哪些?需要注意什么? +- 往期优秀作业链接: +- 秦老师推荐书单: +- Java资料链接汇总: +- 李睿老师的完整实现: +- 铁锚的技术文章翻译: + + + +## 11周 + + +提示: +1. 锁的实现需要考虑加锁时的失败自动重试机制以及自动过期 +2. 正常释放锁时要判断lock值是否一致,最好是通过 lua 脚本来保证操作的原子性。 +3. 推荐使用 redisson,封装的 api 简单易用。 +4. 分布式减库存操作需要加锁,而且需要在库存上加锁,不能使用 userId 等 +5. 推荐阅读 《Redis 操作手册; 基于Redis 5版本,微信读书可以免费阅读 +6. 往期优秀作业链接: https://github.com/lw1243925457/JAVA-000 +7. 《Java资料链接汇总: https://shimo.im/docs/YGjGgTWwgD6V3wkp/ +8. 秦老师推荐书单: https://kimmking.github.io/ +9. Redis集群入门简介[系列文章]: https://github.com/cncounter/translation/tree/master/tiemao_2021/31_redis_cluster +10. 免费好用的Redis图形界面客户端: Another: https://gitee.com/qishibo/AnotherRedisDesktopManager/releases + + + +## 12周 + +锻炼动手能力, 加油! + +提示: +1. 尝试分析一下本地缓存与分布式缓存的适用场景。 +2. 尝试分析缓存数据的特征: 变化频率, 数据量, 不一致的容忍时间, 网络带宽占用压力,延迟以及并发请求数... +3. 总结消息队列的优缺点: 异步解耦、削峰填谷、增强并发性能和横向扩展能力... + +- 往期优秀作业链接: +- 《Java资料链接汇总: +- Redis集群入门教程(中文翻译): +- 免费好用的Redis图形界面客户端: Another: https://gitee.com/qishibo/AnotherRedisDesktopManager/releases + + + + +## 13周 + + +请思考以下问题: + +- 生产端如何保证消息的顺序性? +- 消费端如何保证消息的顺序性? +- 如何防止消息的重复消费? +- 如何防止消费失败或者消息丢失? +- 幂等性是怎么回事?如何保证?在哪个层级保证? + +- 往期优秀作业链接: +- 《Java资料链接汇总》: +- 秦老师推荐书单: https://kimmking.github.io/ +- 铁锚的翻译仓库: + + + +## 14周 + + +- 往期优秀作业链接: +- 《Java资料链接汇总: +- 秦老师推荐书单: https://kimmking.github.io/ +- 铁锚的翻译仓库: + + + +## 毕业总结: + +恭喜你完成学业,祝未来的工作和学习旅程越来越顺利! +认真总结,这份坚持和努力值得肯定。 +提示: 很多东西用自己的语言来转述一遍,收获和理解会更深刻。 聚沙成塔的同时,也需要梳理知识体系,形成自己的核心脉络与钢筋铁骨。