├── .gitignore ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src └── main ├── java ├── com │ └── github │ │ └── pgasync │ │ └── impl │ │ └── netty │ │ └── RatpackPgProtocolStream.java └── springone │ ├── Main.java │ ├── ProductService.java │ ├── RatpackPgConnectionPool.java │ ├── config │ └── DbConfig.java │ └── model │ └── Product.java └── resources └── application.yml /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | out/ 3 | build/ 4 | .gradle/ 5 | .idea/ 6 | *.iml 7 | *.ipr 8 | *.iws 9 | .DS_Store 10 | *.log 11 | *.orig 12 | *.swp 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Ratpack + Spring Boot + Async PostgreSQL 2 | === 3 | 4 | This is a demonstration of a Ratpack application that leverages Spring Boot and makes an asynchronous database call to PostgreSQL. This is from the Spring One Platform 2016 talk, "High Performance Microservices with Ratpack and Spring Boot". This talk demonstrates leveraging Ratpack as the high throughput, non-blocking web layer to an application that is built with the comforts and familiarities of Spring Boot. 5 | 6 | Performance Testing 7 | === 8 | 9 | With no additional server configuration or tuning, the following results were recorded: 10 | 11 | Trial 1 12 | --- 13 | 14 | | EC2 Instance Type | RDS Instance Type | Max Req/Sec Recorded | 15 | |-------------------|-------------------|----------------------| 16 | | t2.micro | t2.micro | 9,529 | 17 | 18 | ``` 19 | # ./wrk -t4 -c72 -d60s -R10000 http://localhost:5050 20 | Running 1m test @ http://localhost:5050 21 | 4 threads and 72 connections 22 | Thread calibration: mean lat.: 171.367ms, rate sampling interval: 930ms 23 | Thread calibration: mean lat.: 199.882ms, rate sampling interval: 1000ms 24 | Thread calibration: mean lat.: 204.732ms, rate sampling interval: 1061ms 25 | Thread calibration: mean lat.: 187.197ms, rate sampling interval: 983ms 26 | Thread Stats Avg Stdev Max +/- Stdev 27 | Latency 1.45s 1.13s 4.34s 59.59% 28 | Req/Sec 2.39k 102.57 2.52k 82.91% 29 | 571768 requests in 1.00m, 190.30MB read 30 | Requests/sec: 9529.38 31 | Transfer/sec: 3.17MB 32 | ``` 33 | 34 | Trial 2 35 | --- 36 | 37 | | EC2 Instance Type | RDS Instance Type | Max Req/Sec Recorded | 38 | |-------------------|-------------------|----------------------| 39 | | t2.medium | t2.micro | 10,259 | 40 | 41 | ``` 42 | # ./wrk -t4 -c72 -d60s -R15000 http://localhost:5050 43 | Running 1m test @ http://localhost:5050 44 | 4 threads and 72 connections 45 | Thread calibration: mean lat.: 1557.261ms, rate sampling interval: 5705ms 46 | Thread calibration: mean lat.: 1553.418ms, rate sampling interval: 5693ms 47 | Thread calibration: mean lat.: 1555.895ms, rate sampling interval: 5701ms 48 | Thread calibration: mean lat.: 1557.657ms, rate sampling interval: 5709ms 49 | Thread Stats Avg Stdev Max +/- Stdev 50 | Latency 11.04s 4.57s 19.22s 57.52% 51 | Req/Sec 2.56k 22.48 2.60k 75.00% 52 | 615528 requests in 1.00m, 204.87MB read 53 | Requests/sec: 10258.71 54 | Transfer/sec: 3.41MB 55 | ``` 56 | 57 | Trial 3 58 | --- 59 | 60 | | EC2 Instance Type | RDS Instance Type | Max Req/Sec Recorded | 61 | |-------------------|-------------------|----------------------| 62 | | t2.medium | t2.medium | 17,399 | 63 | 64 | ``` 65 | # ./wrk -t4 -c72 -d60s -R20000 http://localhost:5050 66 | Running 1m test @ http://localhost:5050 67 | 4 threads and 72 connections 68 | Thread calibration: mean lat.: 668.483ms, rate sampling interval: 2269ms 69 | Thread calibration: mean lat.: 666.548ms, rate sampling interval: 2248ms 70 | Thread calibration: mean lat.: 677.119ms, rate sampling interval: 2285ms 71 | Thread calibration: mean lat.: 669.698ms, rate sampling interval: 2271ms 72 | Thread Stats Avg Stdev Max +/- Stdev 73 | Latency 4.33s 1.84s 8.08s 57.61% 74 | Req/Sec 4.36k 120.14 4.52k 67.06% 75 | 1043910 requests in 1.00m, 347.45MB read 76 | Requests/sec: 17398.57 77 | Transfer/sec: 5.79MB 78 | ``` 79 | 80 | Trial 4 81 | --- 82 | 83 | | EC2 Instance Type | RDS Instance Type | Max Req/Sec Recorded | 84 | |-------------------|-------------------|----------------------| 85 | | m4.large | t2.medium | 13,884 | 86 | 87 | ``` 88 | # ./wrk -t4 -c72 -d60s -R20000 http://localhost:5050 89 | Running 1m test @ http://localhost:5050 90 | 4 threads and 72 connections 91 | Thread calibration: mean lat.: 1405.393ms, rate sampling interval: 4927ms 92 | Thread calibration: mean lat.: 1663.260ms, rate sampling interval: 6381ms 93 | Thread calibration: mean lat.: 1610.518ms, rate sampling interval: 5992ms 94 | Thread calibration: mean lat.: 1506.068ms, rate sampling interval: 5328ms 95 | Thread Stats Avg Stdev Max +/- Stdev 96 | Latency 10.48s 4.99s 23.63s 65.29% 97 | Req/Sec 3.49k 113.93 3.69k 55.88% 98 | 833028 requests in 1.00m, 277.26MB read 99 | Requests/sec: 13884.09 100 | Transfer/sec: 4.62MB 101 | ``` 102 | 103 | _Note_: It shouldn't be surprising that performance is less on AWS's memory-intensive instance class, because Ratpack is CPU-bound. It's worth keeping in mind that this is the case and that you need to do some work to find out the proper instance class for your application's use case. 104 | 105 | Trail 5 106 | --- 107 | 108 | | EC2 Instance Type | RDS Instance Type | Max Req/Sec Recorded | 109 | |-------------------|-------------------|----------------------| 110 | | c4.large | t2.medium | 15,373 | 111 | 112 | ``` 113 | # ./wrk -t4 -c72 -d60s -R20000 http://localhost:5050 114 | Running 1m test @ http://localhost:5050 115 | 4 threads and 72 connections 116 | Thread calibration: mean lat.: 1257.768ms, rate sampling interval: 5152ms 117 | Thread calibration: mean lat.: 1201.606ms, rate sampling interval: 5210ms 118 | Thread calibration: mean lat.: 1223.431ms, rate sampling interval: 5107ms 119 | Thread calibration: mean lat.: 1079.383ms, rate sampling interval: 4616ms 120 | Thread Stats Avg Stdev Max +/- Stdev 121 | Latency 7.88s 4.21s 19.32s 64.80% 122 | Req/Sec 3.86k 79.60 4.04k 62.16% 123 | 922323 requests in 1.00m, 306.98MB read 124 | Requests/sec: 15372.73 125 | Transfer/sec: 5.12MB 126 | ``` 127 | 128 | Trial 6 129 | --- 130 | 131 | | EC2 Instance Type | RDS Instance Type | Max Req/Sec Recorded | 132 | |-------------------|-------------------|----------------------| 133 | | c4.large | t2.large | 16,085 | 134 | 135 | ``` 136 | # ./wrk -t4 -c72 -d60s -R20000 http://localhost:5050 137 | Running 1m test @ http://localhost:5050 138 | 4 threads and 72 connections 139 | Thread calibration: mean lat.: 938.818ms, rate sampling interval: 4073ms 140 | Thread calibration: mean lat.: 943.311ms, rate sampling interval: 4079ms 141 | Thread calibration: mean lat.: 1007.105ms, rate sampling interval: 4089ms 142 | Thread calibration: mean lat.: 975.187ms, rate sampling interval: 4182ms 143 | Thread Stats Avg Stdev Max +/- Stdev 144 | Latency 6.57s 4.36s 14.74s 60.80% 145 | Req/Sec 4.03k 36.11 4.10k 70.21% 146 | 965082 requests in 1.00m, 321.21MB read 147 | Requests/sec: 16085.08 148 | Transfer/sec: 5.35MB 149 | ``` 150 | 151 | Trial 7 152 | --- 153 | 154 | | EC2 Instance Type | RDS Instance Type | Max Req/Sec Recorded | 155 | |-------------------|-------------------|----------------------| 156 | | c4.xlarge | t2.large | 23,149 | 157 | 158 | ``` 159 | # ./wrk -t4 -c100 -d60s -R25000 http://localhost:5050 160 | Running 1m test @ http://localhost:5050 161 | 4 threads and 100 connections 162 | Thread calibration: mean lat.: 346.475ms, rate sampling interval: 1235ms 163 | Thread calibration: mean lat.: 347.531ms, rate sampling interval: 1232ms 164 | Thread calibration: mean lat.: 347.625ms, rate sampling interval: 1254ms 165 | Thread calibration: mean lat.: 347.428ms, rate sampling interval: 1245ms 166 | Thread Stats Avg Stdev Max +/- Stdev 167 | Latency 2.54s 1.13s 5.64s 59.59% 168 | Req/Sec 5.80k 71.22 5.89k 83.02% 169 | 1388845 requests in 1.00m, 462.25MB read 170 | Requests/sec: 23148.93 171 | Transfer/sec: 7.70MB 172 | ``` 173 | 174 | _Note_: The first thing I'd like to note is that this combo went from a cold JVM to 20,000 rps without breaking a sweat. Normally, I run the test over 5 mins to ensure the JVM is warm (usually doesn't improve dramatically after the first round of calls), but this combo was seeing performance right out of the gates. The second thing I'd like to point out is that I increased the number of persistent connections to see if I could squeeze a few more RPS out of the run, so here you'll see 4 x 100 instead of my normal 4 x 72. 175 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | jcenter() 4 | } 5 | dependencies { 6 | classpath 'io.ratpack:ratpack-gradle:1.4.0-rc-2' 7 | classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3' 8 | } 9 | } 10 | 11 | apply plugin: 'io.ratpack.ratpack-java' 12 | apply plugin: 'idea' 13 | apply plugin: 'com.github.johnrengelman.shadow' 14 | 15 | repositories { 16 | jcenter() 17 | } 18 | 19 | dependencies { 20 | compile ratpack.dependency('spring-boot') 21 | compile ratpack.dependency('rx') 22 | compile 'com.github.alaisi.pgasync:postgres-async-driver:0.9' 23 | } 24 | 25 | mainClassName = "springone.Main" 26 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danveloper/s1p-high-perf-microservices/17531f9302e4f8a59a8def83f5b77914778b0447/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Jul 31 12:26:13 CDT 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.0-milestone-2-bin.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn ( ) { 37 | echo "$*" 38 | } 39 | 40 | die ( ) { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 165 | if [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]]; then 166 | cd "$(dirname "$0")" 167 | fi 168 | 169 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 170 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /src/main/java/com/github/pgasync/impl/netty/RatpackPgProtocolStream.java: -------------------------------------------------------------------------------- 1 | package com.github.pgasync.impl.netty; 2 | 3 | import com.github.pgasync.SqlException; 4 | import com.github.pgasync.impl.message.ErrorResponse; 5 | import com.github.pgasync.impl.message.Message; 6 | import com.github.pgasync.impl.message.StartupMessage; 7 | import io.netty.bootstrap.Bootstrap; 8 | import io.netty.channel.EventLoopGroup; 9 | import ratpack.util.internal.ChannelImplDetector; 10 | import rx.Observable; 11 | import rx.Subscriber; 12 | 13 | import java.net.SocketAddress; 14 | 15 | public class RatpackPgProtocolStream extends NettyPgProtocolStream { 16 | 17 | public RatpackPgProtocolStream(EventLoopGroup group, SocketAddress address, boolean useSsl, boolean pipeline) { 18 | super(group, address, useSsl, pipeline); 19 | } 20 | 21 | @Override 22 | public Observable connect(StartupMessage startup) { 23 | return Observable.create(subscriber -> { 24 | 25 | pushSubscriber(subscriber); 26 | new Bootstrap() 27 | .group(group) 28 | .channel(ChannelImplDetector.getSocketChannelImpl()) 29 | .handler(newProtocolInitializer(newStartupHandler(startup))) 30 | .connect(address) 31 | .addListener(onError); 32 | 33 | }).flatMap(this::throwErrorResponses); 34 | } 35 | 36 | private void pushSubscriber(Subscriber subscriber) { 37 | if(!subscribers.offer(subscriber)) { 38 | throw new IllegalStateException("Pipelining not enabled " + subscribers.peek()); 39 | } 40 | } 41 | 42 | private Observable throwErrorResponses(Object message) { 43 | return message instanceof ErrorResponse 44 | ? Observable.error(toSqlException((ErrorResponse) message)) 45 | : Observable.just((Message) message); 46 | } 47 | 48 | private static SqlException toSqlException(ErrorResponse error) { 49 | return new SqlException(error.getLevel().name(), error.getCode(), error.getMessage()); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/springone/Main.java: -------------------------------------------------------------------------------- 1 | package springone; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.github.pgasync.ConnectionPoolBuilder; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.context.annotation.Bean; 7 | import ratpack.registry.Registry; 8 | import ratpack.rx.RxRatpack; 9 | import ratpack.server.RatpackServer; 10 | import ratpack.spring.Spring; 11 | import springone.config.DbConfig; 12 | 13 | import static ratpack.jackson.Jackson.json; 14 | 15 | @SpringBootApplication 16 | public class Main { 17 | 18 | @Bean 19 | public RatpackPgConnectionPool ratpackPgConnectionPool(DbConfig dbConfig) { 20 | System.out.println("Captured keys: " + dbConfig.keySet()); 21 | ConnectionPoolBuilder.PoolProperties props = new ObjectMapper().convertValue(dbConfig, ConnectionPoolBuilder.PoolProperties.class); 22 | return new RatpackPgConnectionPool(props); 23 | } 24 | 25 | public static void main(String[] args) throws Exception { 26 | RxRatpack.initialize(); 27 | 28 | Registry registry = Spring.spring(Main.class); 29 | RatpackServer.start(spec -> spec 30 | .registry(registry) 31 | .handlers(chain -> chain 32 | .get(ctx -> { 33 | ProductService productService = ctx.get(ProductService.class); 34 | RxRatpack.promise(productService.getProducts()).then(products -> ctx.render(json(products))); 35 | }) 36 | ) 37 | ); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/springone/ProductService.java: -------------------------------------------------------------------------------- 1 | package springone; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | import rx.Observable; 6 | import springone.model.Product; 7 | 8 | import java.math.BigDecimal; 9 | 10 | @Service 11 | public class ProductService { 12 | 13 | private final RatpackPgConnectionPool pool; 14 | 15 | @Autowired 16 | public ProductService(RatpackPgConnectionPool pool) { 17 | this.pool = pool; 18 | } 19 | 20 | public Observable getProducts() { 21 | return pool.queryRows("select * from product").map(row -> { 22 | Long id = row.getLong("product_id"); 23 | String name = row.getString("name"); 24 | String description = row.getString("description"); 25 | Boolean available = row.getBoolean("is_available"); 26 | BigDecimal price = row.getBigDecimal("price"); 27 | return new Product(id, name, description, available, price); 28 | }).doOnError(Throwable::printStackTrace); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/springone/RatpackPgConnectionPool.java: -------------------------------------------------------------------------------- 1 | package springone; 2 | 3 | import com.github.pgasync.ConnectionPoolBuilder; 4 | import com.github.pgasync.impl.PgConnectionPool; 5 | import com.github.pgasync.impl.PgProtocolStream; 6 | import com.github.pgasync.impl.netty.RatpackPgProtocolStream; 7 | import io.netty.channel.EventLoopGroup; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Component; 10 | import ratpack.exec.Execution; 11 | import ratpack.service.Service; 12 | import ratpack.service.StartEvent; 13 | 14 | import java.net.InetSocketAddress; 15 | 16 | @Component 17 | public class RatpackPgConnectionPool extends PgConnectionPool implements Service { 18 | 19 | private final boolean useSsl; 20 | private final boolean usePipeline; 21 | 22 | private EventLoopGroup eventLoopGroup; 23 | 24 | @Autowired 25 | public RatpackPgConnectionPool(ConnectionPoolBuilder.PoolProperties poolProperties) { 26 | super(poolProperties); 27 | this.useSsl = poolProperties.getUseSsl(); 28 | this.usePipeline = poolProperties.getUsePipelining(); 29 | } 30 | 31 | @Override 32 | public void onStart(StartEvent e) { 33 | this.eventLoopGroup = Execution.current().getEventLoop(); 34 | } 35 | 36 | @Override 37 | protected PgProtocolStream openStream(InetSocketAddress address) { 38 | return new RatpackPgProtocolStream(eventLoopGroup, address, useSsl, usePipeline); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/springone/config/DbConfig.java: -------------------------------------------------------------------------------- 1 | package springone.config; 2 | 3 | import org.springframework.boot.context.properties.ConfigurationProperties; 4 | import org.springframework.stereotype.Component; 5 | 6 | import java.util.HashMap; 7 | 8 | @Component 9 | @ConfigurationProperties("db") 10 | public class DbConfig extends HashMap { 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/springone/model/Product.java: -------------------------------------------------------------------------------- 1 | package springone.model; 2 | 3 | import java.math.BigDecimal; 4 | 5 | public class Product { 6 | private Long id; 7 | private String name; 8 | private String description; 9 | private Boolean available; 10 | private BigDecimal price; 11 | 12 | public Product(Long id, String name, String description, Boolean available, BigDecimal price) { 13 | this.id = id; 14 | this.name = name; 15 | this.description = description; 16 | this.available = available; 17 | this.price = price; 18 | } 19 | 20 | public Long getId() { 21 | return id; 22 | } 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public String getDescription() { 29 | return description; 30 | } 31 | 32 | public Boolean getAvailable() { 33 | return available; 34 | } 35 | 36 | public BigDecimal getPrice() { 37 | return price; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | db: 2 | username: danw 3 | password: foo 4 | database: danw 5 | poolSize: 4 --------------------------------------------------------------------------------