├── .gitignore ├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── LICENSE ├── general ├── pom.xml └── src │ └── main │ └── java │ └── KeepRunning.java ├── mvnw ├── mvnw.cmd ├── pom.xml ├── reactor ├── pom.xml └── src │ ├── main │ └── java │ │ └── .gitkeep │ └── test │ └── java │ └── workshop │ ├── Step1Mono.java │ ├── Step2Flux.java │ ├── Step3StepVerifier.java │ ├── Step4Transformations.java │ ├── Step5ErrorHandling.java │ ├── Step6Adapters.java │ ├── Step7BlockingAndAsync.java │ ├── Step8Context.java │ └── Verifications.java ├── readme.md ├── spring-data ├── pom.xml └── src │ └── main │ └── java │ └── workshop │ ├── LoginEvent.java │ ├── LoginEventController.java │ ├── LoginEventRepository.java │ ├── PeopleController.java │ ├── Person.java │ ├── PersonRepository.java │ └── WorkshopApplication.java └── spring-webflux ├── pom.xml └── src └── main ├── java └── workshop │ ├── client │ ├── WorkshopApiClient.java │ └── WorkshopSseClient.java │ └── server │ ├── IndexController.java │ ├── SseController.java │ ├── WebConfig.java │ ├── WorkshopApplication.java │ └── WorkshopController.java └── resources ├── application.properties ├── logback.xml └── templates └── index.ftl /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mp911de/reactive-spring/614d05cfc71c6d71e9828f70227873ad911d2536/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.3/apache-maven-3.5.3-bin.zip 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /general/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 4.0.0 7 | 8 | biz.paluch.reactive.spring 9 | general 10 | 1.0-SNAPSHOT 11 | 12 | 13 | -------------------------------------------------------------------------------- /general/src/main/java/KeepRunning.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /** 18 | * @author Mark Paluch 19 | */ 20 | public class KeepRunning { 21 | 22 | public static class Test extends Thread { 23 | 24 | boolean keepRunning = true; 25 | 26 | public void run() { 27 | long count = 0; 28 | while (keepRunning) { 29 | count++; 30 | } 31 | System.out.println("Thread terminated after cycles: " + count); 32 | } 33 | } 34 | 35 | public static void main(String[] args) throws InterruptedException { 36 | Test t = new Test(); 37 | t.start(); 38 | System.out.println("Started"); 39 | Thread.sleep(1000); 40 | t.keepRunning = false; 41 | System.out.println("keepRunning set to false."); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # 58 | # Look for the Apple JDKs first to preserve the existing behaviour, and then look 59 | # for the new JDKs provided by Oracle. 60 | # 61 | if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK ] ; then 62 | # 63 | # Apple JDKs 64 | # 65 | export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home 66 | fi 67 | 68 | if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Java/JavaVirtualMachines/CurrentJDK ] ; then 69 | # 70 | # Apple JDKs 71 | # 72 | export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home 73 | fi 74 | 75 | if [ -z "$JAVA_HOME" ] && [ -L "/Library/Java/JavaVirtualMachines/CurrentJDK" ] ; then 76 | # 77 | # Oracle JDKs 78 | # 79 | export JAVA_HOME=/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home 80 | fi 81 | 82 | if [ -z "$JAVA_HOME" ] && [ -x "/usr/libexec/java_home" ]; then 83 | # 84 | # Apple JDKs 85 | # 86 | export JAVA_HOME=`/usr/libexec/java_home` 87 | fi 88 | ;; 89 | esac 90 | 91 | if [ -z "$JAVA_HOME" ] ; then 92 | if [ -r /etc/gentoo-release ] ; then 93 | JAVA_HOME=`java-config --jre-home` 94 | fi 95 | fi 96 | 97 | if [ -z "$M2_HOME" ] ; then 98 | ## resolve links - $0 may be a link to maven's home 99 | PRG="$0" 100 | 101 | # need this for relative symlinks 102 | while [ -h "$PRG" ] ; do 103 | ls=`ls -ld "$PRG"` 104 | link=`expr "$ls" : '.*-> \(.*\)$'` 105 | if expr "$link" : '/.*' > /dev/null; then 106 | PRG="$link" 107 | else 108 | PRG="`dirname "$PRG"`/$link" 109 | fi 110 | done 111 | 112 | saveddir=`pwd` 113 | 114 | M2_HOME=`dirname "$PRG"`/.. 115 | 116 | # make it fully qualified 117 | M2_HOME=`cd "$M2_HOME" && pwd` 118 | 119 | cd "$saveddir" 120 | # echo Using m2 at $M2_HOME 121 | fi 122 | 123 | # For Cygwin, ensure paths are in UNIX format before anything is touched 124 | if $cygwin ; then 125 | [ -n "$M2_HOME" ] && 126 | M2_HOME=`cygpath --unix "$M2_HOME"` 127 | [ -n "$JAVA_HOME" ] && 128 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 129 | [ -n "$CLASSPATH" ] && 130 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 131 | fi 132 | 133 | # For Migwn, ensure paths are in UNIX format before anything is touched 134 | if $mingw ; then 135 | [ -n "$M2_HOME" ] && 136 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 137 | [ -n "$JAVA_HOME" ] && 138 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 139 | # TODO classpath? 140 | fi 141 | 142 | if [ -z "$JAVA_HOME" ]; then 143 | javaExecutable="`which javac`" 144 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 145 | # readlink(1) is not available as standard on Solaris 10. 146 | readLink=`which readlink` 147 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 148 | if $darwin ; then 149 | javaHome="`dirname \"$javaExecutable\"`" 150 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 151 | else 152 | javaExecutable="`readlink -f \"$javaExecutable\"`" 153 | fi 154 | javaHome="`dirname \"$javaExecutable\"`" 155 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 156 | JAVA_HOME="$javaHome" 157 | export JAVA_HOME 158 | fi 159 | fi 160 | fi 161 | 162 | if [ -z "$JAVACMD" ] ; then 163 | if [ -n "$JAVA_HOME" ] ; then 164 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 165 | # IBM's JDK on AIX uses strange locations for the executables 166 | JAVACMD="$JAVA_HOME/jre/sh/java" 167 | else 168 | JAVACMD="$JAVA_HOME/bin/java" 169 | fi 170 | else 171 | JAVACMD="`which java`" 172 | fi 173 | fi 174 | 175 | if [ ! -x "$JAVACMD" ] ; then 176 | echo "Error: JAVA_HOME is not defined correctly." >&2 177 | echo " We cannot execute $JAVACMD" >&2 178 | exit 1 179 | fi 180 | 181 | if [ -z "$JAVA_HOME" ] ; then 182 | echo "Warning: JAVA_HOME environment variable is not set." 183 | fi 184 | 185 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 186 | 187 | # For Cygwin, switch paths to Windows format before running java 188 | if $cygwin; then 189 | [ -n "$M2_HOME" ] && 190 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 191 | [ -n "$JAVA_HOME" ] && 192 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 193 | [ -n "$CLASSPATH" ] && 194 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 195 | fi 196 | 197 | # traverses directory structure from process work directory to filesystem root 198 | # first directory with .mvn subdirectory is considered project base directory 199 | find_maven_basedir() { 200 | local basedir=$(pwd) 201 | local wdir=$(pwd) 202 | while [ "$wdir" != '/' ] ; do 203 | if [ -d "$wdir"/.mvn ] ; then 204 | basedir=$wdir 205 | break 206 | fi 207 | wdir=$(cd "$wdir/.."; pwd) 208 | done 209 | echo "${basedir}" 210 | } 211 | 212 | # concatenates all lines of a file 213 | concat_lines() { 214 | if [ -f "$1" ]; then 215 | echo "$(tr -s '\n' ' ' < "$1")" 216 | fi 217 | } 218 | 219 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-$(find_maven_basedir)} 220 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 221 | 222 | # Provide a "standardized" way to retrieve the CLI args that will 223 | # work with both Windows and non-Windows executions. 224 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 225 | export MAVEN_CMD_LINE_ARGS 226 | 227 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 228 | 229 | exec "$JAVACMD" \ 230 | $MAVEN_OPTS \ 231 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 232 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 233 | ${WRAPPER_LAUNCHER} "$@" 234 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 40 | 41 | @REM set %HOME% to equivalent of $HOME 42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 43 | 44 | @REM Execute a user defined script before this one 45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 49 | :skipRcPre 50 | 51 | @setlocal 52 | 53 | set ERROR_CODE=0 54 | 55 | @REM To isolate internal variables from possible post scripts, we use another setlocal 56 | @setlocal 57 | 58 | @REM ==== START VALIDATION ==== 59 | if not "%JAVA_HOME%" == "" goto OkJHome 60 | 61 | echo. 62 | echo Error: JAVA_HOME not found in your environment. >&2 63 | echo Please set the JAVA_HOME variable in your environment to match the >&2 64 | echo location of your Java installation. >&2 65 | echo. 66 | goto error 67 | 68 | :OkJHome 69 | if exist "%JAVA_HOME%\bin\java.exe" goto init 70 | 71 | echo. 72 | echo Error: JAVA_HOME is set to an invalid directory. >&2 73 | echo JAVA_HOME = "%JAVA_HOME%" >&2 74 | echo Please set the JAVA_HOME variable in your environment to match the >&2 75 | echo location of your Java installation. >&2 76 | echo. 77 | goto error 78 | 79 | @REM ==== END VALIDATION ==== 80 | 81 | :init 82 | 83 | set MAVEN_CMD_LINE_ARGS=%* 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | 121 | set WRAPPER_JAR="".\.mvn\wrapper\maven-wrapper.jar"" 122 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 123 | 124 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CMD_LINE_ARGS% 125 | if ERRORLEVEL 1 goto error 126 | goto end 127 | 128 | :error 129 | set ERROR_CODE=1 130 | 131 | :end 132 | @endlocal & set ERROR_CODE=%ERROR_CODE% 133 | 134 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 135 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 136 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 137 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 138 | :skipRcPost 139 | 140 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 141 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 142 | 143 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 144 | 145 | exit /B %ERROR_CODE% -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 4.0.0 7 | 8 | biz.paluch.reactive.spring 9 | reactive-spring-workshop 10 | 1.0-SNAPSHOT 11 | pom 12 | 13 | 14 | general 15 | reactor 16 | spring-webflux 17 | spring-data 18 | 19 | 20 | 21 | 1.8 22 | 1.8 23 | 24 | 25 | 26 | 27 | spring-snapshots 28 | Spring Snapshots 29 | https://repo.spring.io/snapshot 30 | 31 | true 32 | 33 | 34 | 35 | spring-milestones 36 | Spring Milestones 37 | https://repo.spring.io/milestone 38 | 39 | false 40 | 41 | 42 | 43 | 44 | 45 | spring-snapshots 46 | Spring Snapshots 47 | https://repo.spring.io/snapshot 48 | 49 | true 50 | 51 | 52 | 53 | spring-milestones 54 | Spring Milestones 55 | https://repo.spring.io/milestone 56 | 57 | false 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /reactor/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 4.0.0 7 | 8 | 9 | biz.paluch.reactive.spring 10 | reactive-spring-workshop 11 | 1.0-SNAPSHOT 12 | 13 | 14 | reactor 15 | 16 | 17 | 18 | 19 | io.projectreactor 20 | reactor-bom 21 | Dysprosium-RC1 22 | import 23 | pom 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | io.projectreactor 32 | reactor-core 33 | 34 | 35 | 36 | io.reactivex 37 | rxjava 38 | 1.3.8 39 | 40 | 41 | 42 | io.reactivex 43 | rxjava-reactive-streams 44 | 1.2.1 45 | 46 | 47 | 48 | io.reactivex.rxjava2 49 | rxjava 50 | 2.2.12 51 | 52 | 53 | 54 | io.projectreactor 55 | reactor-test 56 | test 57 | 58 | 59 | 60 | junit 61 | junit 62 | 4.12 63 | test 64 | 65 | 66 | 67 | org.assertj 68 | assertj-core 69 | 3.13.2 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /reactor/src/main/java/.gitkeep: -------------------------------------------------------------------------------- 1 | keep 2 | -------------------------------------------------------------------------------- /reactor/src/test/java/workshop/Step1Mono.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop; 17 | 18 | import static workshop.Verifications.*; 19 | 20 | import reactor.core.publisher.Mono; 21 | import reactor.core.scheduler.Scheduler; 22 | import reactor.core.scheduler.Schedulers; 23 | import reactor.test.StepVerifier; 24 | 25 | import java.util.concurrent.Callable; 26 | 27 | import org.junit.Test; 28 | 29 | /** 30 | * Tests to get comfortable with {@link Mono}. 31 | * 32 | * @author Mark Paluch 33 | */ 34 | public class Step1Mono { 35 | 36 | @Test 37 | public void monoShouldCreateScalarValue() { 38 | 39 | String jesse = "Jesse"; 40 | 41 | // TODO: Replace this line with an implementation creates a Mono from the value above 42 | Mono mono = Mono.empty(); 43 | 44 | verify(mono, jesse); 45 | } 46 | 47 | @Test 48 | public void monoShouldDeferCallable() { 49 | 50 | String marie = "Marie"; 51 | 52 | Callable myValue = () -> marie; 53 | 54 | // TODO: Replace this line by creating a Mono from the Callable above 55 | Mono mono = Mono.empty(); 56 | 57 | verify(mono, marie); 58 | } 59 | 60 | @Test 61 | public void monoShouldExecuteOnADifferentThread() { 62 | 63 | String walter = "Walter"; 64 | 65 | Scheduler elastic = Schedulers.elastic(); 66 | 67 | // TODO: Replace this line by creating a Mono that publishes its value on a 68 | // different thread applying the Scheduler from above 69 | Mono mono = Mono.just(walter); 70 | 71 | verifyDifferentThread(mono, walter); 72 | } 73 | 74 | @Test 75 | public void monoShouldEmitAnError() { 76 | 77 | IllegalStateException exception = new IllegalStateException("Skyler found out about weed"); 78 | 79 | // TODO: Replace this line by creating a Mono that publishes an error 80 | Mono mono = Mono.empty(); 81 | 82 | mono.as(StepVerifier::create).expectError(IllegalStateException.class).verify(); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /reactor/src/test/java/workshop/Step2Flux.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop; 17 | 18 | import reactor.core.publisher.Flux; 19 | import reactor.test.StepVerifier; 20 | 21 | import java.time.Duration; 22 | import java.util.Arrays; 23 | import java.util.List; 24 | import java.util.stream.Stream; 25 | 26 | import org.junit.Test; 27 | 28 | /** 29 | * Tests to get comfortable with {@link Flux}. 30 | * 31 | * @author Mark Paluch 32 | */ 33 | public class Step2Flux { 34 | 35 | @Test 36 | public void verifyFluxEmission() { 37 | 38 | // TODO: Replace this line by creating a Flux that publishes "Skyler" and "Walter" elements 39 | Flux flux = Flux.empty(); 40 | 41 | flux.as(StepVerifier::create).expectNext("Skyler", "Walter").verifyComplete(); 42 | } 43 | 44 | @Test 45 | public void verifyInfiniteStreamEmission() { 46 | 47 | Stream stream = Stream.generate(Math::random); 48 | 49 | // TODO: Replace this line by creating a Flux that generates values from a Java 8 Stream 50 | Flux flux = Flux.empty(); 51 | 52 | flux.as(StepVerifier::create).expectNextCount(5).thenCancel().verify(); 53 | } 54 | 55 | @Test 56 | public void fluxShouldCreateHelloWorldFromIterable() { 57 | 58 | List strings = Arrays.asList("Saul", "Mike"); 59 | 60 | // TODO: Replace this line by creating a Flux from a resolved collection 61 | Flux flux = Flux.empty(); 62 | 63 | flux.as(StepVerifier::create).expectNext("Saul", "Mike").verifyComplete(); 64 | } 65 | 66 | @Test 67 | public void fluxShouldEmitItemsOverTime() { 68 | 69 | Duration duration = Duration.ofSeconds(2); 70 | 71 | // TODO: Replace this line by creating a Flux publishing items over time 72 | Flux flux = Flux.empty(); 73 | 74 | StepVerifier.create(flux.doOnNext(System.out::println).take(4)).expectNext(0L, 1L, 2L, 3L).verifyComplete(); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /reactor/src/test/java/workshop/Step3StepVerifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop; 17 | 18 | import reactor.core.publisher.Flux; 19 | import reactor.core.publisher.Mono; 20 | 21 | import java.time.Duration; 22 | import java.util.stream.Stream; 23 | 24 | import org.junit.Test; 25 | 26 | /** 27 | * Testing reactive sequences. 28 | * 29 | * @author Mark Paluch 30 | */ 31 | public class Step3StepVerifier { 32 | 33 | @Test 34 | public void verifyMonoEmission() { 35 | 36 | Mono mono = Mono.just("Hello, World").doOnSuccess(System.out::println); 37 | 38 | // Use StepVerifier to verify Mono emission 39 | } 40 | 41 | @Test 42 | public void verifyFluxEmission() { 43 | 44 | Flux flux = Flux.just("Hello", "World").doOnNext(System.out::println); 45 | 46 | // Use StepVerifier to verify Flux emission 47 | } 48 | 49 | @Test 50 | public void verifyInfiniteStreamEmission() { 51 | 52 | Flux flux = Flux.fromStream(Stream.generate(Math::random)).doOnNext(System.out::println); 53 | 54 | // Use StepVerifier to verify Flux emission 55 | } 56 | 57 | @Test 58 | public void verifyDelayedEmission() { 59 | 60 | Flux flux = Flux.interval(Duration.ofSeconds(5)).take(10).doOnNext(System.out::println); 61 | 62 | // Use StepVerifier to verify Flux emission, not spending 5 seconds per emission 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /reactor/src/test/java/workshop/Step4Transformations.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop; 17 | 18 | import reactor.core.publisher.Flux; 19 | import reactor.core.publisher.Mono; 20 | import reactor.test.StepVerifier; 21 | 22 | import org.junit.Test; 23 | 24 | /** 25 | * Testing reactive transformations. 26 | * 27 | * @author Mark Paluch 28 | */ 29 | public class Step4Transformations { 30 | 31 | @Test 32 | public void monoShouldMapValueToUpperCaseHelloWorld() { 33 | 34 | Mono mono = Mono.just("Heisenberg"); 35 | 36 | // TODO: Replace this line to transform the emitted element to uppercase 37 | // mono = … 38 | 39 | StepVerifier.create(mono).expectNext("HEISENBERG").verifyComplete(); 40 | } 41 | 42 | @Test 43 | public void fluxShouldMapValueToUpperCaseHelloWorld() { 44 | 45 | Flux flux = Flux.just("Mike", "Gustavo"); 46 | 47 | // TODO: Replace this line to transform the emitted element to uppercase 48 | // flux = … 49 | 50 | flux.as(StepVerifier::create).expectNext("MIKE", "GUSTAVO").verifyComplete(); 51 | } 52 | 53 | @Test 54 | public void monoShouldEmitIndividualCharactersAsString() { 55 | 56 | Mono mono = Mono.just("Schraderbräu"); 57 | 58 | // TODO: Replace this line to emit each character as single element 59 | Flux flux = Flux.empty(); 60 | 61 | flux.as(StepVerifier::create).expectNext("S", "c", "h", "r", "a", "d", "e", "r", "b", "r", "ä", "u").verifyComplete(); 62 | } 63 | 64 | @Test 65 | public void fluxShouldConcatTwoMonos() { 66 | 67 | Mono hello = Mono.just("Breaking"); 68 | Mono world = Mono.just("Bad"); 69 | 70 | // TODO: Replace this line to compose a stream from the two Monos 71 | Flux flux = Flux.empty(); 72 | 73 | flux.as(StepVerifier::create).expectNext("Breaking", "Bad").verifyComplete(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /reactor/src/test/java/workshop/Step5ErrorHandling.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop; 17 | 18 | import reactor.core.publisher.Mono; 19 | import reactor.test.StepVerifier; 20 | 21 | import org.junit.Test; 22 | 23 | /** 24 | * Tests to handle error cases. 25 | * 26 | * @author Mark Paluch 27 | */ 28 | public class Step5ErrorHandling { 29 | 30 | @Test 31 | public void monoShouldEmitAFallbackValueOnError() { 32 | 33 | Mono failedMono = Mono.error(new IllegalStateException("Something bad happened!")); 34 | 35 | String recoveryValue = "Recovered!"; 36 | 37 | // TODO: Replace this line with a fallback value in an error happens 38 | Mono withErrorHandling = failedMono; 39 | 40 | withErrorHandling.as(StepVerifier::create).expectNext("Recovered!").verifyComplete(); 41 | } 42 | 43 | @Test 44 | public void monoShouldEmitADeferredFallbackValueOnError() { 45 | 46 | Mono failedMono = Mono.error(new IllegalStateException("Something bad happened!")); 47 | 48 | Mono recoveryValue = Mono.just("Recovered!"); 49 | 50 | // TODO: Replace this line with a fallback that is computed in a deferred way 51 | Mono withErrorHandling = failedMono; 52 | 53 | withErrorHandling.as(StepVerifier::create).expectNext("Recovered!").verifyComplete(); 54 | } 55 | 56 | @Test 57 | public void monoShouldEmitAFallbackValueIfEmpty() { 58 | 59 | Mono emptyMono = Mono.empty(); 60 | 61 | Mono recoveryValue = Mono.just("Recovered!"); 62 | 63 | // TODO: Replace this line with a fallback that is computed in a deferred way 64 | Mono withErrorHandling = emptyMono; 65 | 66 | withErrorHandling.as(StepVerifier::create).expectNext("Recovered!").verifyComplete(); 67 | } 68 | 69 | @Test 70 | public void monoShouldTranslateException() { 71 | 72 | Mono failedMono = Mono.error(new IllegalStateException("Something bad happened!")); 73 | 74 | // TODO: Replace this line with a Mono translating IllegalStateException to MyBusinessException 75 | Mono withErrorHandling = failedMono; 76 | 77 | withErrorHandling.as(StepVerifier::create).expectError(MyBusinessException.class).verify(); 78 | } 79 | 80 | static class MyBusinessException extends Exception { 81 | 82 | public MyBusinessException(Throwable cause) { 83 | super(cause); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /reactor/src/test/java/workshop/Step6Adapters.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop; 17 | 18 | import io.reactivex.Flowable; 19 | import reactor.core.publisher.Flux; 20 | import reactor.core.publisher.Mono; 21 | import reactor.test.StepVerifier; 22 | import rx.Observable; 23 | import rx.Single; 24 | 25 | import org.junit.Test; 26 | 27 | /** 28 | * Using adapters to interop between reactive composition libraries. 29 | * 30 | * @author Mark Paluch 31 | */ 32 | public class Step6Adapters { 33 | 34 | @Test 35 | public void adoptRxJava1ObservableToFlux() { 36 | 37 | Observable people = Observable.just("Jesse", "Hank"); 38 | 39 | // TODO: Use RxReactiveStreams to create a Flux from Observable. 40 | Flux flux = Flux.empty(); 41 | 42 | flux.as(StepVerifier::create).expectNext("Jesse", "Hank").verifyComplete(); 43 | } 44 | 45 | @Test 46 | public void adoptRxJava1SingleToMono() { 47 | 48 | Single jesse = Single.just("Jesse"); 49 | 50 | // TODO: Use RxReactiveStreams to create a Flux from Observable. 51 | Mono mono = Mono.empty(); 52 | 53 | mono.as(StepVerifier::create).expectNext("Jesse").verifyComplete(); 54 | } 55 | 56 | @Test 57 | public void adoptRxJava1EmptySingleToMono() { 58 | 59 | Single empty = Observable. empty().toSingle(); 60 | 61 | // TODO: Use RxReactiveStreams to create a Flux from Observable. 62 | Mono mono = Mono.empty(); 63 | 64 | // Expect a surprise here 65 | mono.as(StepVerifier::create).verifyComplete(); 66 | } 67 | 68 | @Test 69 | public void adoptRxJava2ObservableToFlux() { 70 | 71 | io.reactivex.Observable jesse = io.reactivex.Observable.just("Jesse"); 72 | 73 | // TODO: Use RxJava 2's Flowable to create a Flux 74 | Flux flux = Flux.empty(); 75 | 76 | flux.as(StepVerifier::create).expectNext("Jesse").verifyComplete(); 77 | } 78 | 79 | @Test 80 | public void adoptFluxToRxJavaObservable() throws Exception { 81 | 82 | Flux people = Flux.just("Jesse", "Hank"); 83 | 84 | // TODO: Use RxReactiveStreams to create an Observable from Flux. 85 | Observable observable = Observable.empty(); 86 | 87 | observable.test().awaitTerminalEvent().assertResult("Jesse", "Hank"); 88 | } 89 | 90 | @Test 91 | public void adoptFluxToRxJava2Flowable() throws Exception { 92 | 93 | Flux people = Flux.just("Jesse", "Hank"); 94 | 95 | // TODO: Use RxReactiveStreams to create an Flowable from Flux. 96 | Flowable flowable = Flowable.empty(); 97 | 98 | flowable.test().await().assertResult("Jesse", "Hank").awaitTerminalEvent(); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /reactor/src/test/java/workshop/Step7BlockingAndAsync.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop; 17 | 18 | import static org.assertj.core.api.Assertions.*; 19 | 20 | import reactor.core.publisher.Flux; 21 | import reactor.core.publisher.Mono; 22 | import reactor.core.scheduler.Schedulers; 23 | import reactor.test.StepVerifier; 24 | 25 | import java.util.Collections; 26 | import java.util.List; 27 | import java.util.concurrent.Callable; 28 | import java.util.concurrent.CompletableFuture; 29 | import java.util.concurrent.atomic.AtomicLong; 30 | 31 | import org.junit.Test; 32 | 33 | /** 34 | * Tests to explore async to blocking synchronisation and blocking through reactive APIs. 35 | * 36 | * @author Mark Paluch 37 | */ 38 | public class Step7BlockingAndAsync { 39 | 40 | @Test 41 | public void synchronizeMono() { 42 | 43 | Mono tuco = Mono.just("Tuco").subscribeOn(Schedulers.elastic()); 44 | 45 | // TODO: Obtain this value from the Mono above using hard synchronization 46 | String result = ""; 47 | 48 | assertThat(result).isEqualTo("Tuco"); 49 | } 50 | 51 | @Test 52 | public void synchronizeFlux() { 53 | 54 | Flux salamancas = Flux.just("Hector", "Tuco"); 55 | 56 | // TODO: Obtain this value from the Flux above using hard synchronization 57 | List result = Collections.emptyList(); 58 | 59 | assertThat(result).contains("Hector", "Tuco"); 60 | } 61 | 62 | @Test 63 | public void adoptToCompletableFuture() { 64 | 65 | Mono tuco = Mono.just("Tuco").subscribeOn(Schedulers.elastic()); 66 | 67 | // TODO: Replace this line to create a CompletableFuture from Mono 68 | CompletableFuture future = CompletableFuture.completedFuture("foo"); 69 | 70 | future.thenAccept(s -> assertThat(s).isEqualTo("Tuco")).join(); 71 | } 72 | 73 | @Test 74 | public void blockingToReactive() { 75 | 76 | Callable takesAWhile = () -> { 77 | Thread.sleep(2000); 78 | return "Mike"; 79 | }; 80 | 81 | // TODO: Observe the output sequence. Use subscribeOn and publishOn to adjust context switching behavior 82 | Mono naive = Mono.fromCallable(takesAWhile) 83 | .doOnSubscribe(it -> System.out.println(Thread.currentThread().getName() + ": Subscribe")) 84 | .doOnSuccess(it -> System.out.println(Thread.currentThread().getName() + ": Success")); 85 | 86 | System.out.println("Before subscribe"); 87 | naive.subscribe(); 88 | System.out.println("After subscribe"); 89 | } 90 | 91 | @Test 92 | public void reactivePushFutureToMono() { 93 | 94 | CompletableFuture saul = new CompletableFuture<>(); 95 | 96 | // TODO: Create a Mono from the CompletableFuture. 97 | Mono naive = Mono.empty(); 98 | 99 | System.out.println("Before subscribe"); 100 | naive.doOnSuccess(System.out::println).subscribe(); 101 | System.out.println("After subscribe"); 102 | 103 | // TODO: Move the completion before Mono.subscribe to explore the behavior 104 | saul.complete("Saul"); 105 | } 106 | 107 | @Test 108 | public void reactivePullGenerator() { 109 | 110 | Flux doubleStream = Flux.generate(AtomicLong::new, (o, synchronousSink) -> { 111 | 112 | return o; 113 | }); 114 | 115 | doubleStream.as(StepVerifier::create).expectNextCount(10).verifyComplete(); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /reactor/src/test/java/workshop/Step8Context.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop; 17 | 18 | import static org.assertj.core.api.Assertions.*; 19 | 20 | import reactor.core.publisher.Flux; 21 | import reactor.core.publisher.Mono; 22 | import reactor.test.StepVerifier; 23 | import reactor.util.context.Context; 24 | 25 | import java.util.Map.Entry; 26 | import java.util.function.Function; 27 | 28 | import org.junit.Test; 29 | 30 | /** 31 | * Testing Reactor {@link reactor.util.context.Context}. 32 | * 33 | * @author Mark Paluch 34 | */ 35 | public class Step8Context { 36 | 37 | @Test 38 | public void verifyContext() { 39 | 40 | Context context = Context.empty(); 41 | 42 | assertThat((String) context.get("username")).isEqualTo("HAL"); 43 | } 44 | 45 | @Test 46 | public void verifyContextUsage() { 47 | 48 | Context context = Context.empty(); 49 | 50 | Mono.subscriberContext().map(it -> it.get("username")) // 51 | // TODO: Attach context here 52 | .as(StepVerifier::create) // 53 | .expectNext("HAL") // 54 | .verifyComplete(); 55 | } 56 | 57 | @Test 58 | public void verifyContextUsageDirection() { 59 | 60 | Flux contextKeys = Mono.subscriberContext() 61 | .flatMapMany(it -> Flux.fromStream(it.stream().map(Entry::getKey))); 62 | 63 | Context context1 = Context.of("username", "foo"); 64 | Context context2 = Context.of("password", "bar"); 65 | 66 | contextKeys.map(it -> "First: " + it) // 67 | .subscriberContext(context1) // 68 | .concatWith(contextKeys.map(it -> "Second: " + it)).subscriberContext(context2) // 69 | .as(StepVerifier::create) // 70 | // TODO Complete expectation .expectNext("First: …") // 71 | // TODO Complete expectation .expectNext("First: …") // 72 | // TODO Complete expectation .expectNext("Second: …") // 73 | .verifyComplete(); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /reactor/src/test/java/workshop/Verifications.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package workshop; 18 | 19 | import static org.assertj.core.api.Assertions.*; 20 | 21 | import reactor.core.publisher.Mono; 22 | import reactor.test.StepVerifier; 23 | 24 | import java.time.Duration; 25 | import java.util.function.Supplier; 26 | 27 | import org.reactivestreams.Publisher; 28 | 29 | /** 30 | * @author Mark Paluch 31 | */ 32 | class Verifications { 33 | 34 | static void verifyDifferentThread(Mono mono, String expectation) { 35 | 36 | mono.as(StepVerifier::create).consumeNextWith(s -> { 37 | 38 | assertThat(s).isEqualTo(expectation); 39 | assertThat(Thread.currentThread().getName()).isNotEqualTo("main"); 40 | 41 | }).verifyComplete(); 42 | } 43 | 44 | static void verify(Mono mono, String expectedValue) { 45 | mono.as(StepVerifier::create).expectNext(expectedValue).verifyComplete(); 46 | } 47 | 48 | static void verifyDelayedEmission(Supplier> supplier) { 49 | 50 | StepVerifier.withVirtualTime(supplier).thenAwait(Duration.ofSeconds(50)) // 51 | .expectNextCount(10) // 52 | .verifyComplete(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Reactive Spring Workshop 2 | 3 | This project is the skeleton (and solution) for the Reactive Spring workshop. The slides are available from https://mp911.de/reactive-spring. 4 | 5 | This workshop is designed to help you to learn easily the Reactive API provided by [Reactor Core 3.x](https://github.com/reactor/reactor-core/) and to discover Spring's Reactive features through a practical example. 6 | 7 | You will mostly need these 3 classes Javadoc: 8 | 9 | - [Flux](http://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html) 10 | - [Mono](http://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html) 11 | - [StepVerifier](http://projectreactor.io/docs/test/release/api/reactor/test/StepVerifier.html) 12 | 13 | To do this workshop, you just have to: 14 | 15 | - Have [Java 8](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) and a Java IDE ([IntelliJ IDEA](https://www.jetbrains.com/idea/) for example) installed with Maven support 16 | - Clone/[Download](https://github.com/mp911de/reactive-spring/archive/initial.zip) this repository (or your fork) 17 | - Import the project as a Maven one in your IDE 18 | - Make sure that the language level is set to Java 8 in your IDE project settings 19 | - Fix the TODO one by one in StepX test classes to make unit tests green 20 | 21 | Once you mastered the basics, you can continue with Spring WebFlux and Spring Data examples that explain reactive 22 | programming models with web applications and NoSQL data access. 23 | 24 | The solution is available in the [`main`](https://github.com/mp911de/reactive-spring/tree/main) branch ([download](https://github.com/mp911de/reactive-spring/archive/main.zip)) to compare, when you have finished, with what you have done. 25 | 26 | ## License 27 | 28 | This code is licensed under the [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0). 29 | -------------------------------------------------------------------------------- /spring-data/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | org.springframework.boot 9 | spring-boot-starter-parent 10 | 2.2.0.M6 11 | 12 | 13 | 14 | spring-data 15 | 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-webflux 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-data-mongodb 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-mongodb-reactive 31 | 32 | 33 | 34 | de.flapdoodle.embed 35 | de.flapdoodle.embed.mongo 36 | 37 | 38 | 39 | io.reactivex 40 | rxjava 41 | 42 | 43 | 44 | io.reactivex 45 | rxjava-reactive-streams 46 | 47 | 48 | 49 | io.reactivex.rxjava2 50 | rxjava 51 | 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-starter-test 56 | test 57 | 58 | 59 | 60 | org.springframework.boot 61 | spring-boot-devtools 62 | true 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | org.springframework.boot 71 | spring-boot-maven-plugin 72 | 73 | 74 | 75 | 76 | 77 | 78 | spring-snapshots 79 | Spring Snapshots 80 | https://repo.spring.io/snapshot 81 | 82 | true 83 | 84 | 85 | 86 | spring-milestones 87 | Spring Milestones 88 | https://repo.spring.io/milestone 89 | 90 | false 91 | 92 | 93 | 94 | 95 | 96 | 97 | spring-snapshots 98 | Spring Snapshots 99 | https://repo.spring.io/snapshot 100 | 101 | true 102 | 103 | 104 | 105 | spring-milestones 106 | Spring Milestones 107 | https://repo.spring.io/milestone 108 | 109 | false 110 | 111 | 112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /spring-data/src/main/java/workshop/LoginEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop; 17 | 18 | import java.time.Instant; 19 | 20 | import org.springframework.data.mongodb.core.mapping.Document; 21 | 22 | /** 23 | * @author Mark Paluch 24 | */ 25 | @Document 26 | public class LoginEvent { 27 | 28 | String id; 29 | final Person person; 30 | final Instant loginDate; 31 | 32 | public LoginEvent(Person person, Instant loginDate) { 33 | this.person = person; 34 | this.loginDate = loginDate; 35 | } 36 | 37 | public String getId() { 38 | return id; 39 | } 40 | 41 | public void setId(String id) { 42 | this.id = id; 43 | } 44 | 45 | public Person getPerson() { 46 | return person; 47 | } 48 | 49 | public Instant getLoginDate() { 50 | return loginDate; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /spring-data/src/main/java/workshop/LoginEventController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop; 17 | 18 | import reactor.core.publisher.Flux; 19 | 20 | import java.time.Duration; 21 | import java.time.Instant; 22 | import java.util.concurrent.ThreadLocalRandom; 23 | 24 | import javax.annotation.PostConstruct; 25 | 26 | import org.springframework.data.mongodb.core.CollectionOptions; 27 | import org.springframework.data.mongodb.core.MongoTemplate; 28 | import org.springframework.data.mongodb.core.ReactiveMongoTemplate; 29 | import org.springframework.http.MediaType; 30 | import org.springframework.web.bind.annotation.GetMapping; 31 | import org.springframework.web.bind.annotation.RestController; 32 | 33 | /** 34 | * @author Mark Paluch 35 | */ 36 | @RestController 37 | public class LoginEventController { 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /spring-data/src/main/java/workshop/LoginEventRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop; 17 | 18 | import org.springframework.data.repository.reactive.ReactiveCrudRepository; 19 | 20 | /** 21 | * @author Mark Paluch 22 | */ 23 | public interface LoginEventRepository extends ReactiveCrudRepository { 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-data/src/main/java/workshop/PeopleController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop; 17 | 18 | import io.reactivex.Maybe; 19 | import reactor.core.publisher.Flux; 20 | 21 | import org.springframework.web.bind.annotation.RestController; 22 | 23 | /** 24 | * @author Mark Paluch 25 | */ 26 | @RestController 27 | public class PeopleController { 28 | 29 | } 30 | -------------------------------------------------------------------------------- /spring-data/src/main/java/workshop/Person.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop; 17 | 18 | import org.springframework.data.mongodb.core.mapping.Document; 19 | 20 | /** 21 | * @author Mark Paluch 22 | */ 23 | @Document 24 | public class Person { 25 | 26 | String id; 27 | final String name; 28 | 29 | public Person(String name) { 30 | this.name = name; 31 | } 32 | 33 | public String getId() { 34 | return id; 35 | } 36 | 37 | public void setId(String id) { 38 | this.id = id; 39 | } 40 | 41 | public String getName() { 42 | return name; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /spring-data/src/main/java/workshop/PersonRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop; 17 | 18 | import org.springframework.data.repository.reactive.ReactiveCrudRepository; 19 | 20 | /** 21 | * @author Mark Paluch 22 | */ 23 | public interface PersonRepository extends ReactiveCrudRepository { 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-data/src/main/java/workshop/WorkshopApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop; 17 | 18 | 19 | import java.io.IOException; 20 | 21 | import org.springframework.boot.CommandLineRunner; 22 | import org.springframework.boot.SpringApplication; 23 | import org.springframework.boot.autoconfigure.SpringBootApplication; 24 | import org.springframework.data.mongodb.core.MongoOperations; 25 | 26 | /** 27 | * @author Mark Paluch 28 | */ 29 | @SpringBootApplication 30 | public class WorkshopApplication implements CommandLineRunner { 31 | 32 | final MongoOperations mongoOperations; 33 | 34 | public WorkshopApplication(MongoOperations mongoOperations) { 35 | this.mongoOperations = mongoOperations; 36 | } 37 | 38 | public static void main(String[] args) throws IOException { 39 | SpringApplication.run(WorkshopApplication.class, args); 40 | } 41 | 42 | @Override 43 | public void run(String... strings) throws Exception { 44 | 45 | mongoOperations.save(new Person("Hank")); 46 | mongoOperations.save(new Person("Marie")); 47 | mongoOperations.save(new Person("Jesse")); 48 | mongoOperations.save(new Person("Walter")); 49 | mongoOperations.save(new Person("Skyler")); 50 | mongoOperations.save(new Person("Flynn")); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /spring-webflux/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | org.springframework.boot 9 | spring-boot-starter-parent 10 | 2.2.0.M6 11 | 12 | 13 | 14 | spring-webflux 15 | 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-webflux 21 | 22 | 23 | 24 | org.freemarker 25 | freemarker 26 | 27 | 28 | 29 | org.springframework 30 | spring-context-support 31 | 32 | 33 | 34 | de.flapdoodle.embed 35 | de.flapdoodle.embed.mongo 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-devtools 47 | true 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-maven-plugin 57 | 58 | workshop.server.WorkshopApplication 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | spring-snapshots 67 | Spring Snapshots 68 | https://repo.spring.io/snapshot 69 | 70 | true 71 | 72 | 73 | 74 | spring-milestones 75 | Spring Milestones 76 | https://repo.spring.io/milestone 77 | 78 | false 79 | 80 | 81 | 82 | 83 | 84 | 85 | spring-snapshots 86 | Spring Snapshots 87 | https://repo.spring.io/snapshot 88 | 89 | true 90 | 91 | 92 | 93 | spring-milestones 94 | Spring Milestones 95 | https://repo.spring.io/milestone 96 | 97 | false 98 | 99 | 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /spring-webflux/src/main/java/workshop/client/WorkshopApiClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop.client; 17 | 18 | /** 19 | * Reactive client for a web API. 20 | * 21 | * @author Mark Paluch 22 | */ 23 | public class WorkshopApiClient { 24 | 25 | public static void main(String[] args) { 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-webflux/src/main/java/workshop/client/WorkshopSseClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop.client; 17 | 18 | import reactor.core.Disposable; 19 | import reactor.core.publisher.Flux; 20 | 21 | import java.io.IOException; 22 | 23 | /** 24 | * Reactive client for a SSE endpoint. 25 | * 26 | * @author Mark Paluch 27 | */ 28 | public class WorkshopSseClient { 29 | 30 | public static void main(String[] args) throws IOException { 31 | 32 | Flux flux = Flux.empty(); 33 | 34 | System.out.println("Subscribing to SSE"); 35 | Disposable subscription = flux.subscribe(); 36 | 37 | System.out.println("Press any key to terminate subscription..."); 38 | System.in.read(); 39 | 40 | subscription.dispose(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /spring-webflux/src/main/java/workshop/server/IndexController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop.server; 17 | 18 | import org.springframework.stereotype.Controller; 19 | import org.springframework.ui.Model; 20 | import org.springframework.web.bind.annotation.GetMapping; 21 | 22 | /** 23 | * Controller with MVC support. 24 | * 25 | * @author Mark Paluch 26 | */ 27 | @Controller 28 | public class IndexController { 29 | 30 | @GetMapping(path = "/") 31 | String index(Model model) { 32 | 33 | // model.addAttribute("people", people); 34 | 35 | return "index"; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /spring-webflux/src/main/java/workshop/server/SseController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop.server; 17 | 18 | import java.time.Duration; 19 | 20 | import reactor.core.publisher.Flux; 21 | 22 | import org.springframework.http.MediaType; 23 | import org.springframework.web.bind.annotation.GetMapping; 24 | import org.springframework.web.bind.annotation.RequestMapping; 25 | import org.springframework.web.bind.annotation.RestController; 26 | 27 | /** 28 | * Controller exposing a SSE endpoint. 29 | * 30 | * @author Mark Paluch 31 | */ 32 | @RestController 33 | @RequestMapping("sse") 34 | public class SseController { 35 | 36 | @GetMapping(value = "messages", produces = MediaType.APPLICATION_STREAM_JSON_VALUE) 37 | public Flux getMessages() { 38 | return Flux.interval(Duration.ofSeconds(1)).map(it -> new Person("" + it)); 39 | } 40 | 41 | static class Person { 42 | 43 | final String name; 44 | 45 | public Person(String name) { 46 | this.name = name; 47 | } 48 | 49 | public String getName() { 50 | return name; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /spring-webflux/src/main/java/workshop/server/WebConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop.server; 17 | 18 | import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext; 19 | import org.springframework.context.annotation.Bean; 20 | import org.springframework.context.annotation.Configuration; 21 | import org.springframework.web.reactive.config.ViewResolverRegistry; 22 | import org.springframework.web.reactive.config.WebFluxConfigurer; 23 | import org.springframework.web.reactive.result.view.freemarker.FreeMarkerConfigurer; 24 | 25 | @Configuration 26 | public class WebConfig implements WebFluxConfigurer { 27 | 28 | @Bean 29 | public FreeMarkerConfigurer freeMarkerConfigurer(ReactiveWebApplicationContext applicationContext) { 30 | 31 | FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); 32 | 33 | configurer.setTemplateLoaderPath("classpath:/templates/"); 34 | configurer.setResourceLoader(applicationContext); 35 | 36 | return configurer; 37 | } 38 | 39 | @Override 40 | public void configureViewResolvers(ViewResolverRegistry registry) { 41 | registry.freeMarker(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /spring-webflux/src/main/java/workshop/server/WorkshopApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop.server; 17 | 18 | import org.springframework.boot.SpringApplication; 19 | import org.springframework.boot.autoconfigure.SpringBootApplication; 20 | 21 | /** 22 | * @author Mark Paluch 23 | */ 24 | @SpringBootApplication 25 | public class WorkshopApplication { 26 | 27 | public static void main(String[] args) { 28 | SpringApplication.run(WorkshopApplication.class, args); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /spring-webflux/src/main/java/workshop/server/WorkshopController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2017 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package workshop.server; 17 | 18 | import reactor.core.publisher.Flux; 19 | import reactor.core.publisher.Mono; 20 | 21 | import org.springframework.core.io.buffer.DataBuffer; 22 | import org.springframework.core.io.buffer.DataBufferFactory; 23 | import org.springframework.core.io.buffer.DefaultDataBufferFactory; 24 | import org.springframework.http.HttpStatus; 25 | import org.springframework.web.bind.annotation.GetMapping; 26 | import org.springframework.web.bind.annotation.RequestMapping; 27 | import org.springframework.web.bind.annotation.RequestParam; 28 | import org.springframework.web.bind.annotation.RestController; 29 | import org.springframework.web.server.ServerWebExchange; 30 | 31 | /** 32 | * Simple controller with a return body. 33 | * 34 | * @author Mark Paluch 35 | */ 36 | @RestController 37 | @RequestMapping("api") 38 | public class WorkshopController { 39 | 40 | private final DataBufferFactory factory = new DefaultDataBufferFactory(); 41 | 42 | static class Person { 43 | 44 | final String name; 45 | 46 | public Person(String name) { 47 | this.name = name; 48 | } 49 | 50 | public String getName() { 51 | return name; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /spring-webflux/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mp911de/reactive-spring/614d05cfc71c6d71e9828f70227873ad911d2536/spring-webflux/src/main/resources/application.properties -------------------------------------------------------------------------------- /spring-webflux/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | %d %5p %40.40c:%4L - %m%n 7 | 8 | 9 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /spring-webflux/src/main/resources/templates/index.ftl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Reactive Spring 10 | 11 | 12 | 13 | 14 | 17 | 18 | 19 | 20 | 21 | 22 |
23 |
24 |

Reactive Spring using Freemarker

25 |

This is a template.

26 |
27 |
28 | 29 |
30 | 31 |
32 |
33 |

Breaking Bad

34 |

35 | 36 |

    37 | <#list people as person> 38 |
  • 39 | ${person} 40 |
  • 41 | 42 |
43 |

44 |
45 |
46 | 47 |
48 | 49 |
50 | 51 | 52 | 54 | 55 | 58 | 59 | 62 | 63 | 64 | 65 | 66 | 67 | --------------------------------------------------------------------------------