├── .gitignore ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src └── main ├── java └── org │ └── reveno │ └── article │ ├── Main.java │ ├── Utils.java │ ├── commands │ ├── AdjustOrder.java │ ├── CancellOrder.java │ ├── ChangeBalance.java │ ├── CreateAccount.java │ ├── ExecuteOrder.java │ └── MakeOrder.java │ ├── events │ └── BalanceChangedEvent.java │ ├── model │ ├── Order.java │ └── TradeAccount.java │ └── view │ ├── OrderView.java │ └── TradeAccountView.java └── resources └── log4j.properties /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.log 3 | .DS_Store 4 | 5 | # Build directory# 6 | build/ 7 | bin/ 8 | 9 | # sbt specific 10 | dist/* 11 | target/ 12 | lib_managed/ 13 | src_managed/ 14 | project/boot/ 15 | project/plugins/project/ 16 | 17 | # Scala-IDE specific 18 | .scala_dependencies 19 | 20 | # Gradle 21 | .gradle 22 | 23 | # Package Files # 24 | *.war 25 | *.ear 26 | 27 | # Eclipse Project Files # 28 | .settings 29 | .cache 30 | .classpath 31 | .project 32 | .metadata/ 33 | 34 | # IDEA Project Files # 35 | .idea/* 36 | *.iml 37 | *.ipr 38 | *.iws 39 | out/ -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | group 'reveno-article' 2 | version '1.0' 3 | 4 | apply plugin: 'java' 5 | apply plugin: 'maven' 6 | apply plugin: 'idea' 7 | 8 | repositories { 9 | mavenCentral() 10 | } 11 | 12 | dependencies { 13 | testCompile "junit:junit:4.12" 14 | 15 | compile "org.reveno:reveno-core:1.22.1" 16 | compile "org.reveno:reveno-metrics:1.22.1" 17 | compile "it.unimi.dsi:fastutil:7.0.9" 18 | } 19 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dmart28/reveno-article-demo/74edb732b04239439a54e5499b40799e3f2b583e/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Dec 19 23:32:37 MSK 2015 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-2.2-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 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 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 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 Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'reveno-article' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/org/reveno/article/Main.java: -------------------------------------------------------------------------------- 1 | package org.reveno.article; 2 | 3 | import org.reveno.article.commands.*; 4 | import org.reveno.article.events.BalanceChangedEvent; 5 | import org.reveno.article.model.Order; 6 | import org.reveno.article.model.TradeAccount; 7 | import org.reveno.article.view.OrderView; 8 | import org.reveno.article.view.TradeAccountView; 9 | import org.reveno.atp.api.Configuration; 10 | import org.reveno.atp.api.Reveno; 11 | import org.reveno.atp.core.Engine; 12 | import org.reveno.atp.metrics.RevenoMetrics; 13 | import org.reveno.atp.utils.MeasureUtils; 14 | 15 | import static org.reveno.article.Utils.*; 16 | 17 | public class Main { 18 | 19 | public static void main(String[] args) throws Exception { 20 | Reveno reveno = new Engine(args[0]); 21 | reveno.config().cpuConsumption(Configuration.CpuConsumption.HIGH); 22 | 23 | // Commands declarations 24 | declareCommands(reveno); 25 | 26 | // Transaction actions declarations 27 | declareTransactionActions(reveno); 28 | 29 | // Views mapping 30 | reveno.domain().viewMapper(TradeAccount.class, TradeAccountView.class, (id,e,r) -> 31 | new TradeAccountView(fromLong(e.balance), r.linkSet(e.orders(), OrderView.class))); 32 | reveno.domain().viewMapper(Order.class, OrderView.class, (id,e,r) -> 33 | new OrderView(fromLong(e.price), e.size, e.symbol, r.get(TradeAccountView.class, e.accountId))); 34 | 35 | reveno.events().eventHandler(BalanceChangedEvent.class, (e, m) -> { 36 | if (!m.isRestore()) { 37 | TradeAccountView account = reveno.query().find(TradeAccountView.class, e.accountId); 38 | System.out.println(String.format("New balance of account %s from event is: %s", e.accountId, account.balance)); 39 | } 40 | }); 41 | 42 | reveno.startup(); 43 | 44 | long accountId = reveno.executeSync(new CreateAccount("USD", 5.15)); 45 | long orderId = reveno.executeSync(new MakeOrder(accountId, "EUR/USD", 1, 1.213)); 46 | 47 | // circular dynamic view references works this way 48 | // the balance is expected to be 5.15 49 | System.out.println(reveno.query().find(TradeAccountView.class, accountId).orders.iterator().next().account.balance); 50 | 51 | reveno.executeSync(new ExecuteOrder(orderId)); 52 | 53 | // the balance is expected to be 3.937, after order successfully executed 54 | System.out.println(reveno.query().find(TradeAccountView.class, accountId).balance); 55 | 56 | long orderId1 = reveno.executeSync(new MakeOrder(accountId, "RUB/GPB", 3, 0.0096)); 57 | long orderId2 = reveno.executeSync(new MakeOrder(accountId, "EUR/USD", 1, 1.314)); 58 | 59 | // expected to have 2 orders 60 | System.out.println(reveno.query().find(TradeAccountView.class, accountId).orders.size()); 61 | 62 | reveno.executeSync(new CancellOrder(orderId1)); 63 | 64 | // expected to have null, as we already cancelled this order 65 | System.out.println(reveno.query().find(OrderView.class, orderId1)); 66 | // expected to have 1.314 for second order 67 | System.out.println(reveno.query().find(OrderView.class, orderId2).price); 68 | 69 | /* 70 | Uncomment next lines to get performance metrics output 71 | */ 72 | // RevenoMetrics metrics = new RevenoMetrics(); 73 | // metrics.config().sendToLog(true); 74 | // metrics.config().hostName("localhost"); 75 | // metrics.config().instanceName("test"); 76 | // metrics.listen((Engine) reveno); 77 | 78 | // metrics.config().metricBufferSize(MeasureUtils.mb(2)); 79 | 80 | // Object command = new ChangeBalance(accountId, 10); 81 | // #### Warmup #### 82 | // for (int i = 0; i < 10; i++) { 83 | // for (int j = 0; j < 1_000_000; j++) { 84 | // reveno.executeCommand(command); 85 | // } 86 | // } 87 | // 88 | // #### Measurement #### 89 | // for (int i = 0; i < 45; i++) { 90 | // for (int j = 0; j < 1_000_000; j++) { 91 | // reveno.executeCommand(command); 92 | // } 93 | // } 94 | //// just to see last results since metrics sends to sink every 15 seconds by default. 95 | // System.in.read(); 96 | 97 | reveno.shutdown(); 98 | } 99 | 100 | protected static void declareCommands(Reveno reveno) { 101 | reveno.domain().command(CreateAccount.class, long.class, (c, ctx) -> { 102 | long accountId = ctx.id(TradeAccount.class); 103 | ctx.executeTxAction(new CreateAccount.CreateAccountAction(c, accountId)); 104 | if (c.initialBalance > 0) { 105 | ctx.executeTxAction(new ChangeBalance(accountId, toLong(c.initialBalance))); 106 | } 107 | return accountId; 108 | }); 109 | reveno.domain().command(ChangeBalance.class, (c, ctx) -> { 110 | if (ctx.repo().has(TradeAccount.class, c.accountId)) { 111 | TradeAccount account = ctx.repo().get(TradeAccount.class, c.accountId); 112 | if (c.amount < 0 && account.balance < Math.abs(c.amount)) { 113 | throw new IllegalArgumentException("Can't withdraw from account - not enough money."); 114 | } 115 | ctx.executeTxAction(c); 116 | } else throw new RuntimeException("No account " + c.accountId + " found!"); 117 | }); 118 | reveno.domain().command(MakeOrder.class, long.class, (c, ctx) -> { 119 | if (c.size > 0 && !eq(c.price, 0) && ctx.repo().has(TradeAccount.class, c.accountId)) { 120 | if (c.price < 0 && ctx.repo().get(TradeAccount.class, c.accountId).balance < Math.abs(c.price)) { 121 | throw new RuntimeException("Not sufficient finance!"); 122 | } 123 | long orderId = ctx.id(Order.class); 124 | ctx.executeTxAction(new MakeOrder.MakeOrderAction(orderId, toLong(c.price), c)); 125 | return orderId; 126 | } else { 127 | throw new IllegalArgumentException("One of the order command arguments are not correct."); 128 | } 129 | }); 130 | reveno.domain().command(CancellOrder.class, (c, ctx) -> { 131 | if (ctx.repo().has(Order.class, c.orderId)) { 132 | ctx.executeTxAction(c); 133 | } else { 134 | throw new IllegalArgumentException("Order with id=" + c.orderId + " not found."); 135 | } 136 | }); 137 | reveno.domain().command(AdjustOrder.class, (c, ctx) -> { 138 | if (ctx.repo().has(Order.class, c.orderId)) { 139 | if (c.newSize <= 0) { 140 | ctx.executeTxAction(new CancellOrder(c.orderId)); 141 | } else { 142 | ctx.executeTxAction(new AdjustOrder.AdjustOrderAction(c, toLong(c.newPrice))); 143 | } 144 | } else { 145 | throw new IllegalArgumentException("Order with id=" + c.orderId + " not found."); 146 | } 147 | }); 148 | // we don't even need here special Transaction Action, since 149 | // order execution is naturally conjunction of two transaction actions: balance change and order remove 150 | reveno.domain().command(ExecuteOrder.class, (c, ctx) -> { 151 | if (ctx.repo().has(Order.class, c.orderId)) { 152 | Order order = ctx.repo().get(Order.class, c.orderId); 153 | ctx.executeTxAction(new ChangeBalance(order.accountId, -order.price)); 154 | ctx.executeTxAction(new CancellOrder(c.orderId)); 155 | } else { 156 | throw new IllegalArgumentException("Order with id=" + c.orderId + " not found."); 157 | } 158 | }); 159 | } 160 | 161 | protected static void declareTransactionActions(Reveno reveno) { 162 | reveno.domain().transactionAction(CreateAccount.CreateAccountAction.class, (a, ctx) -> 163 | ctx.repo().store(a.id, new TradeAccount(a.id, a.info.currency))); 164 | 165 | reveno.domain().transactionAction(ChangeBalance.class, (a, ctx) -> { 166 | ctx.repo().remap(a.accountId, TradeAccount.class, (id, e) -> e.addBalance(a.amount)); 167 | // uncomment to check that events are published 168 | //ctx.eventBus().publishEvent(new BalanceChangedEvent(a.accountId)); 169 | }); 170 | 171 | reveno.domain().transactionAction(MakeOrder.MakeOrderAction.class, (a, ctx) -> { 172 | Order order = new Order(a.id, a.command.accountId, a.command.symbol, a.command.size, a.price); 173 | ctx.repo().store(a.id, order); 174 | ctx.repo().remap(a.command.accountId, TradeAccount.class, (id, e) -> e.addOrder(a.id)); 175 | }); 176 | 177 | reveno.domain().transactionAction(CancellOrder.class, (a, ctx) -> { 178 | Order order = ctx.repo().remove(Order.class, a.orderId); 179 | ctx.repo().remap(order.accountId, TradeAccount.class, (id, e) -> e.removeOrder(a.orderId)); 180 | }); 181 | 182 | reveno.domain().transactionAction(AdjustOrder.AdjustOrderAction.class, (a, ctx) -> 183 | ctx.repo().remap(a.cmd.orderId, Order.class, (id, e) -> e.adjust(a.cmd.newSize, a.newPrice))); 184 | } 185 | 186 | } 187 | -------------------------------------------------------------------------------- /src/main/java/org/reveno/article/Utils.java: -------------------------------------------------------------------------------- 1 | package org.reveno.article; 2 | 3 | public class Utils { 4 | private static final long DECIMAL_POWER = (long) Math.pow(10, 6); 5 | 6 | public static long toLong(double value) { 7 | return (long)(value * DECIMAL_POWER); 8 | } 9 | 10 | public static double fromLong(long value) { 11 | return (double)value / DECIMAL_POWER; 12 | } 13 | 14 | public static boolean eq(double a, double b) { 15 | final double epsilon = 1 / DECIMAL_POWER; 16 | final double absA = Math.abs(a); 17 | final double absB = Math.abs(b); 18 | final double diff = Math.abs(a - b); 19 | 20 | if (a == b) { 21 | return true; 22 | } else if (a == 0 || b == 0 || diff < Double.MIN_NORMAL) { 23 | return diff < (epsilon * Double.MIN_NORMAL); 24 | } else { 25 | return diff / (absA + absB) < epsilon; 26 | } 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/reveno/article/commands/AdjustOrder.java: -------------------------------------------------------------------------------- 1 | package org.reveno.article.commands; 2 | 3 | public class AdjustOrder { 4 | 5 | public final long orderId; 6 | public final int newSize; 7 | public final double newPrice; 8 | 9 | public AdjustOrder(long orderId, int newSize, double newPrice) { 10 | this.orderId = orderId; 11 | this.newSize = newSize; 12 | this.newPrice = newPrice; 13 | } 14 | 15 | public static class AdjustOrderAction { 16 | public final AdjustOrder cmd; 17 | public final long newPrice; 18 | 19 | public AdjustOrderAction(AdjustOrder cmd, long newPrice) { 20 | this.cmd = cmd; 21 | this.newPrice = newPrice; 22 | } 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/reveno/article/commands/CancellOrder.java: -------------------------------------------------------------------------------- 1 | package org.reveno.article.commands; 2 | 3 | public class CancellOrder { 4 | 5 | public final long orderId; 6 | 7 | public CancellOrder(long orderId) { 8 | this.orderId = orderId; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/org/reveno/article/commands/ChangeBalance.java: -------------------------------------------------------------------------------- 1 | package org.reveno.article.commands; 2 | 3 | public class ChangeBalance { 4 | 5 | public final long accountId; 6 | public final long amount; 7 | 8 | public ChangeBalance(long accountId, long amount) { 9 | this.accountId = accountId; 10 | this.amount = amount; 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/reveno/article/commands/CreateAccount.java: -------------------------------------------------------------------------------- 1 | package org.reveno.article.commands; 2 | 3 | public class CreateAccount { 4 | 5 | public final String currency; 6 | public final double initialBalance; 7 | 8 | public CreateAccount(String currency, double initialBalance) { 9 | this.currency = currency; 10 | this.initialBalance = initialBalance; 11 | } 12 | 13 | public static class CreateAccountAction { 14 | public final CreateAccount info; 15 | public final long id; 16 | 17 | public CreateAccountAction(CreateAccount info, long id) { 18 | this.info = info; 19 | this.id = id; 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/org/reveno/article/commands/ExecuteOrder.java: -------------------------------------------------------------------------------- 1 | package org.reveno.article.commands; 2 | 3 | public class ExecuteOrder { 4 | 5 | public final long orderId; 6 | 7 | public ExecuteOrder(long orderId) { 8 | this.orderId = orderId; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/org/reveno/article/commands/MakeOrder.java: -------------------------------------------------------------------------------- 1 | package org.reveno.article.commands; 2 | 3 | public class MakeOrder { 4 | 5 | public final long accountId; 6 | public final String symbol; 7 | public final int size; 8 | public final double price; 9 | 10 | public MakeOrder(long accountId, String symbol, int size, double price) { 11 | this.accountId = accountId; 12 | this.symbol = symbol; 13 | this.size = size; 14 | this.price = price; 15 | } 16 | 17 | public static class MakeOrderAction { 18 | public final long id; 19 | public final long price; 20 | public final MakeOrder command; 21 | 22 | public MakeOrderAction(long id, long price, MakeOrder command) { 23 | this.id = id; 24 | this.price = price; 25 | this.command = command; 26 | } 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/reveno/article/events/BalanceChangedEvent.java: -------------------------------------------------------------------------------- 1 | package org.reveno.article.events; 2 | 3 | public class BalanceChangedEvent { 4 | 5 | public final long accountId; 6 | 7 | public BalanceChangedEvent(long accountId) { 8 | this.accountId = accountId; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/org/reveno/article/model/Order.java: -------------------------------------------------------------------------------- 1 | package org.reveno.article.model; 2 | 3 | public class Order { 4 | 5 | public final long id; 6 | public final long accountId; 7 | public final String symbol; 8 | public final int size; 9 | public final long price; 10 | 11 | public Order(long id, long accountId, String symbol, int size, long price) { 12 | this.id = id; 13 | this.accountId = accountId; 14 | this.symbol = symbol; 15 | this.size = size; 16 | this.price = price; 17 | } 18 | 19 | public Order adjust(int size, long price) { 20 | return new Order(id, accountId, symbol, size, price); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/org/reveno/article/model/TradeAccount.java: -------------------------------------------------------------------------------- 1 | package org.reveno.article.model; 2 | 3 | import it.unimi.dsi.fastutil.longs.LongCollection; 4 | import it.unimi.dsi.fastutil.longs.LongIterator; 5 | import it.unimi.dsi.fastutil.longs.LongOpenHashSet; 6 | import it.unimi.dsi.fastutil.longs.LongSet; 7 | 8 | public class TradeAccount { 9 | public final long id; 10 | public final long balance; 11 | public final String currency; 12 | private final LongSet orders; 13 | 14 | public TradeAccount(long id, String currency) { 15 | this(id, 0, currency, new LongOpenHashSet()); 16 | } 17 | 18 | private TradeAccount(long id, long balance, String currency, LongSet orders) { 19 | this.id = id; 20 | this.balance = balance; 21 | this.currency = currency; 22 | this.orders = orders; 23 | } 24 | 25 | public TradeAccount addBalance(long amount) { 26 | return new TradeAccount(id, balance + amount, currency, orders); 27 | } 28 | 29 | public TradeAccount addOrder(long orderId) { 30 | LongSet orders = new LongOpenHashSet(this.orders); 31 | orders.add(orderId); 32 | return new TradeAccount(id, balance, currency, orders); 33 | } 34 | 35 | public TradeAccount removeOrder(long orderId) { 36 | LongSet orders = new LongOpenHashSet(this.orders); 37 | orders.remove(orderId); 38 | return new TradeAccount(id, balance, currency, orders); 39 | } 40 | 41 | public LongCollection orders() { 42 | return new LongOpenHashSet(orders); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/reveno/article/view/OrderView.java: -------------------------------------------------------------------------------- 1 | package org.reveno.article.view; 2 | 3 | public class OrderView { 4 | 5 | public final double price; 6 | public final int size; 7 | public final String symbol; 8 | public final TradeAccountView account; 9 | 10 | public OrderView(double price, int size, String symbol, TradeAccountView account) { 11 | this.price = price; 12 | this.size = size; 13 | this.account = account; 14 | this.symbol = symbol; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/reveno/article/view/TradeAccountView.java: -------------------------------------------------------------------------------- 1 | package org.reveno.article.view; 2 | 3 | import java.util.Set; 4 | 5 | public class TradeAccountView { 6 | public final double balance; 7 | public final Set orders; 8 | 9 | public TradeAccountView(double balance, Set orders) { 10 | this.balance = balance; 11 | this.orders = orders; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO, A1 2 | 3 | log4j.appender.A1=org.apache.log4j.ConsoleAppender 4 | log4j.appender.A1.layout=org.apache.log4j.PatternLayout 5 | log4j.appender.A1.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss.SSS} %p [%c{1}] %m%n --------------------------------------------------------------------------------