Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit after moving codebase from cqrs4j (http://cqrs4j.googl…

  • Loading branch information...
commit 89f40710038e40d7f77a3f51a2aa4dc9f1af9a3a 1 parent 6e21183
Allard Buijze abuijze authored
Showing with 12,738 additions and 0 deletions.
  1. +202 −0 LICENSE.txt
  2. +29 −0 core/pom.xml
  3. +130 −0 core/src/main/java/org/axonframework/core/AbstractAggregateRoot.java
  4. +55 −0 core/src/main/java/org/axonframework/core/AggregateRoot.java
  5. +149 −0 core/src/main/java/org/axonframework/core/DomainEvent.java
  6. +144 −0 core/src/main/java/org/axonframework/core/EventContainer.java
  7. +38 −0 core/src/main/java/org/axonframework/core/EventSourcedAggregateRoot.java
  8. +58 −0 core/src/main/java/org/axonframework/core/EventStream.java
  9. +111 −0 core/src/main/java/org/axonframework/core/SimpleEventStream.java
  10. +36 −0 core/src/main/java/org/axonframework/core/VersionedAggregateRoot.java
  11. +159 −0 core/src/main/java/org/axonframework/core/eventhandler/AsyncEventBus.java
  12. +59 −0 core/src/main/java/org/axonframework/core/eventhandler/EventBus.java
  13. +137 −0 core/src/main/java/org/axonframework/core/eventhandler/EventHandlingSequenceManager.java
  14. +57 −0 core/src/main/java/org/axonframework/core/eventhandler/EventListener.java
  15. +211 −0 core/src/main/java/org/axonframework/core/eventhandler/EventProcessingScheduler.java
  16. +45 −0 core/src/main/java/org/axonframework/core/eventhandler/EventSequencingPolicy.java
  17. +36 −0 core/src/main/java/org/axonframework/core/eventhandler/FullConcurrencyPolicy.java
  18. +55 −0 core/src/main/java/org/axonframework/core/eventhandler/MessageHandlerAdapter.java
  19. +37 −0 core/src/main/java/org/axonframework/core/eventhandler/SequentialPerAggregatePolicy.java
  20. +39 −0 core/src/main/java/org/axonframework/core/eventhandler/SequentialPolicy.java
  21. +84 −0 core/src/main/java/org/axonframework/core/eventhandler/SpringIntegrationEventBus.java
  22. +84 −0 core/src/main/java/org/axonframework/core/eventhandler/SynchronousEventBus.java
  23. +59 −0 core/src/main/java/org/axonframework/core/eventhandler/TransactionAware.java
  24. +199 −0 core/src/main/java/org/axonframework/core/eventhandler/TransactionStatus.java
  25. +39 −0 core/src/main/java/org/axonframework/core/eventhandler/YieldPolicy.java
  26. +90 −0 core/src/main/java/org/axonframework/core/eventhandler/annotation/AbstractAnnotatedAggregateRoot.java
  27. +53 −0 core/src/main/java/org/axonframework/core/eventhandler/annotation/AfterTransaction.java
  28. +326 −0 core/src/main/java/org/axonframework/core/eventhandler/annotation/AnnotationEventHandlerInvoker.java
  29. +210 −0 core/src/main/java/org/axonframework/core/eventhandler/annotation/AnnotationEventListenerAdapter.java
  30. +49 −0 core/src/main/java/org/axonframework/core/eventhandler/annotation/BeforeTransaction.java
  31. +51 −0 core/src/main/java/org/axonframework/core/eventhandler/annotation/ConcurrentEventListener.java
  32. +56 −0 core/src/main/java/org/axonframework/core/eventhandler/annotation/EventHandler.java
  33. +52 −0 core/src/main/java/org/axonframework/core/eventhandler/annotation/EventHandlingRejectedException.java
  34. +37 −0 core/src/main/java/org/axonframework/core/eventhandler/annotation/TransactionMethodExecutionException.java
  35. +55 −0 core/src/main/java/org/axonframework/core/eventhandler/annotation/UnhandledEventException.java
  36. +54 −0 core/src/main/java/org/axonframework/core/eventhandler/annotation/UnsupportedHandlerMethodException.java
  37. +37 −0 core/src/main/java/org/axonframework/core/eventhandler/annotation/UnsupportedPolicyException.java
  38. +42 −0 .../org/axonframework/core/eventhandler/annotation/postprocessor/AnnotationEventListenerBeanPostProcessor.java
  39. +37 −0 core/src/main/java/org/axonframework/core/eventhandler/annotation/postprocessor/AxonNamingPolicy.java
  40. +193 −0 .../axonframework/core/eventhandler/annotation/postprocessor/BaseAnnotationEventListenerBeanPostProcessor.java
  41. +36 −0 ...c/main/java/org/axonframework/core/eventhandler/annotation/postprocessor/EventListenerAdapterException.java
  42. +105 −0 core/src/main/java/org/axonframework/core/repository/AbstractRepository.java
  43. +36 −0 core/src/main/java/org/axonframework/core/repository/ConcurrencyException.java
  44. +55 −0 core/src/main/java/org/axonframework/core/repository/LockManager.java
  45. +151 −0 core/src/main/java/org/axonframework/core/repository/LockingRepository.java
  46. +44 −0 core/src/main/java/org/axonframework/core/repository/LockingStrategy.java
  47. +107 −0 core/src/main/java/org/axonframework/core/repository/OptimisticLockManager.java
  48. +148 −0 core/src/main/java/org/axonframework/core/repository/PessimisticLockManager.java
  49. +47 −0 core/src/main/java/org/axonframework/core/repository/Repository.java
  50. +101 −0 core/src/main/java/org/axonframework/core/repository/eventsourcing/CachingEventSourcingRepository.java
  51. +115 −0 core/src/main/java/org/axonframework/core/repository/eventsourcing/EventSourcingRepository.java
  52. +36 −0 core/src/main/java/org/axonframework/core/repository/eventsourcing/EventStorageException.java
  53. +49 −0 core/src/main/java/org/axonframework/core/repository/eventsourcing/EventStore.java
  54. +201 −0 core/src/main/java/org/axonframework/core/repository/eventsourcing/NoCache.java
  55. +106 −0 core/src/main/java/org/axonframework/core/repository/eventsourcing/ObjectInputStreamAdapter.java
  56. +191 −0 core/src/main/java/org/axonframework/core/repository/eventsourcing/XStreamFileSystemEventStore.java
  57. +51 −0 core/src/main/java/org/axonframework/core/util/Assert.java
  58. +81 −0 core/src/test/java/org/axonframework/core/AbstractAggregateRootTest.java
  59. +101 −0 core/src/test/java/org/axonframework/core/DomainEventTest.java
  60. +87 −0 core/src/test/java/org/axonframework/core/EventContainerTest.java
  61. +47 −0 core/src/test/java/org/axonframework/core/StubAggregate.java
  62. +52 −0 core/src/test/java/org/axonframework/core/StubDomainEvent.java
  63. +94 −0 core/src/test/java/org/axonframework/core/eventhandler/AsyncEventBusTest.java
  64. +107 −0 core/src/test/java/org/axonframework/core/eventhandler/EventHandlingSequenceManagerTest.java
  65. +39 −0 core/src/test/java/org/axonframework/core/eventhandler/FullConcurrencyPolicyTest.java
  66. +137 −0 core/src/test/java/org/axonframework/core/eventhandler/ListenerManagerTest.java
  67. +47 −0 core/src/test/java/org/axonframework/core/eventhandler/MessageHandlerAdapterTest.java
  68. +44 −0 core/src/test/java/org/axonframework/core/eventhandler/SequentialPerAggregatePolicyTest.java
  69. +45 −0 core/src/test/java/org/axonframework/core/eventhandler/SequentialPolicyTest.java
  70. +72 −0 core/src/test/java/org/axonframework/core/eventhandler/SimpleEventBusTest.java
  71. +85 −0 core/src/test/java/org/axonframework/core/eventhandler/SpringIntegrationEventBusTest.java
  72. +100 −0 core/src/test/java/org/axonframework/core/eventhandler/TransactionStatusTest.java
  73. +68 −0 core/src/test/java/org/axonframework/core/eventhandler/annotation/AbstractAnnotatedAggregateRootTest.java
  74. +266 −0 core/src/test/java/org/axonframework/core/eventhandler/annotation/AnnotationEventHandlerInvokerTest.java
  75. +155 −0 core/src/test/java/org/axonframework/core/eventhandler/annotation/AnnotationEventListenerAdapterTest.java
  76. +37 −0 core/src/test/java/org/axonframework/core/eventhandler/annotation/UnhandledEventExceptionTest.java
  77. +100 −0 ...g/axonframework/core/eventhandler/annotation/postprocessor/AnnotationEventHandlerBeanPostProcessorTest.java
  78. +37 −0 .../axonframework/core/eventhandler/annotation/postprocessor/AnnotationEventListenerBeanPostProcessorTest.java
  79. +100 −0 ...nframework/core/eventhandler/annotation/postprocessor/BaseAnnotationEventListenerBeanPostProcessorTest.java
  80. +137 −0 core/src/test/java/org/axonframework/core/repository/LockingRepositoryTest.java
  81. +61 −0 core/src/test/java/org/axonframework/core/repository/OptimisticLockManagerTest.java
  82. +44 −0 core/src/test/java/org/axonframework/core/repository/PessimisticLockManagerTest.java
  83. +120 −0 core/src/test/java/org/axonframework/core/repository/eventsourcing/CachingEventSourcingRepositoryTest.java
  84. +188 −0 core/src/test/java/org/axonframework/core/repository/eventsourcing/EventSourcingRepositoryIntegrationTest.java
  85. +115 −0 core/src/test/java/org/axonframework/core/repository/eventsourcing/EventSourcingRepositoryTest.java
  86. +62 −0 core/src/test/java/org/axonframework/core/repository/eventsourcing/NoCacheTest.java
  87. +45 −0 core/src/test/java/org/axonframework/core/util/AssertTest.java
  88. +43 −0 core/src/test/resources/ehcache.xml
  89. +24 −0 core/src/test/resources/log4j.properties
  90. +233 −0 pom.xml
  91. +168 −0 reference-guide/pom.xml
  92. +186 −0 reference-guide/src/main/docbook/en-US/1-getting-started.xml
  93. +94 −0 reference-guide/src/main/docbook/en-US/2-annotation-support.xml
  94. +51 −0 reference-guide/src/main/docbook/en-US/3-event-sourcing-support.xml
  95. +44 −0 reference-guide/src/main/docbook/en-US/4-transaction-support.xml
  96. +60 −0 reference-guide/src/main/docbook/en-US/master.xml
  97. +38 −0 reference-guide/src/main/docbook/en-US/programlistingsample.txt
  98. BIN  reference-guide/src/main/resources/images/callouts/1.gif
  99. BIN  reference-guide/src/main/resources/images/callouts/1.png
  100. +15 −0 reference-guide/src/main/resources/images/callouts/1.svg
  101. BIN  reference-guide/src/main/resources/images/callouts/10.gif
  102. BIN  reference-guide/src/main/resources/images/callouts/10.png
  103. +18 −0 reference-guide/src/main/resources/images/callouts/10.svg
  104. BIN  reference-guide/src/main/resources/images/callouts/11.gif
  105. BIN  reference-guide/src/main/resources/images/callouts/11.png
  106. +16 −0 reference-guide/src/main/resources/images/callouts/11.svg
  107. BIN  reference-guide/src/main/resources/images/callouts/12.gif
  108. BIN  reference-guide/src/main/resources/images/callouts/12.png
  109. +18 −0 reference-guide/src/main/resources/images/callouts/12.svg
  110. BIN  reference-guide/src/main/resources/images/callouts/13.gif
  111. BIN  reference-guide/src/main/resources/images/callouts/13.png
  112. +20 −0 reference-guide/src/main/resources/images/callouts/13.svg
  113. BIN  reference-guide/src/main/resources/images/callouts/14.gif
  114. BIN  reference-guide/src/main/resources/images/callouts/14.png
  115. +17 −0 reference-guide/src/main/resources/images/callouts/14.svg
  116. BIN  reference-guide/src/main/resources/images/callouts/15.gif
  117. BIN  reference-guide/src/main/resources/images/callouts/15.png
  118. +19 −0 reference-guide/src/main/resources/images/callouts/15.svg
  119. +20 −0 reference-guide/src/main/resources/images/callouts/16.svg
  120. +17 −0 reference-guide/src/main/resources/images/callouts/17.svg
  121. +21 −0 reference-guide/src/main/resources/images/callouts/18.svg
  122. +20 −0 reference-guide/src/main/resources/images/callouts/19.svg
  123. BIN  reference-guide/src/main/resources/images/callouts/2.gif
  124. BIN  reference-guide/src/main/resources/images/callouts/2.png
  125. +17 −0 reference-guide/src/main/resources/images/callouts/2.svg
  126. +20 −0 reference-guide/src/main/resources/images/callouts/20.svg
  127. +18 −0 reference-guide/src/main/resources/images/callouts/21.svg
  128. +20 −0 reference-guide/src/main/resources/images/callouts/22.svg
  129. +22 −0 reference-guide/src/main/resources/images/callouts/23.svg
  130. +19 −0 reference-guide/src/main/resources/images/callouts/24.svg
  131. +21 −0 reference-guide/src/main/resources/images/callouts/25.svg
  132. +22 −0 reference-guide/src/main/resources/images/callouts/26.svg
  133. +19 −0 reference-guide/src/main/resources/images/callouts/27.svg
  134. +23 −0 reference-guide/src/main/resources/images/callouts/28.svg
  135. +22 −0 reference-guide/src/main/resources/images/callouts/29.svg
  136. BIN  reference-guide/src/main/resources/images/callouts/3.gif
  137. BIN  reference-guide/src/main/resources/images/callouts/3.png
  138. +19 −0 reference-guide/src/main/resources/images/callouts/3.svg
  139. +22 −0 reference-guide/src/main/resources/images/callouts/30.svg
  140. BIN  reference-guide/src/main/resources/images/callouts/4.gif
  141. BIN  reference-guide/src/main/resources/images/callouts/4.png
  142. +16 −0 reference-guide/src/main/resources/images/callouts/4.svg
  143. BIN  reference-guide/src/main/resources/images/callouts/5.gif
  144. BIN  reference-guide/src/main/resources/images/callouts/5.png
  145. +18 −0 reference-guide/src/main/resources/images/callouts/5.svg
  146. BIN  reference-guide/src/main/resources/images/callouts/6.gif
  147. BIN  reference-guide/src/main/resources/images/callouts/6.png
  148. +19 −0 reference-guide/src/main/resources/images/callouts/6.svg
  149. BIN  reference-guide/src/main/resources/images/callouts/7.gif
  150. BIN  reference-guide/src/main/resources/images/callouts/7.png
  151. +16 −0 reference-guide/src/main/resources/images/callouts/7.svg
  152. BIN  reference-guide/src/main/resources/images/callouts/8.gif
  153. BIN  reference-guide/src/main/resources/images/callouts/8.png
  154. +20 −0 reference-guide/src/main/resources/images/callouts/8.svg
  155. BIN  reference-guide/src/main/resources/images/callouts/9.gif
  156. BIN  reference-guide/src/main/resources/images/callouts/9.png
  157. +19 −0 reference-guide/src/main/resources/images/callouts/9.svg
  158. BIN  reference-guide/src/main/resources/images/logo.png
  159. +123 −0 reference-guide/src/main/xslt/basic.xsl
  160. +230 −0 reference-guide/src/main/xslt/borders.xsl
  161. +278 −0 reference-guide/src/main/xslt/coverpages.xsl
  162. +259 −0 reference-guide/src/main/xslt/coverpages_html.xsl
  163. +109 −0 reference-guide/src/main/xslt/highlight.xsl
  164. +108 −0 reference-guide/src/main/xslt/highlight_html.xsl
  165. +16 −0 reference-guide/src/main/xslt/highlighting/README
  166. +105 −0 reference-guide/src/main/xslt/highlighting/c-hl.xml
  167. +140 −0 reference-guide/src/main/xslt/highlighting/common.xsl
  168. +137 −0 reference-guide/src/main/xslt/highlighting/cpp-hl.xml
  169. +174 −0 reference-guide/src/main/xslt/highlighting/csharp-hl.xml
  170. +186 −0 reference-guide/src/main/xslt/highlighting/delphi-hl.xml
  171. +31 −0 reference-guide/src/main/xslt/highlighting/ini-hl.xml
  172. +103 −0 reference-guide/src/main/xslt/highlighting/java-hl.xml
  173. +133 −0 reference-guide/src/main/xslt/highlighting/javascript-hl.xml
  174. +76 −0 reference-guide/src/main/xslt/highlighting/m2-hl.xml
  175. +132 −0 reference-guide/src/main/xslt/highlighting/myxml-hl.xml
  176. +106 −0 reference-guide/src/main/xslt/highlighting/perl-hl.xml
  177. +135 −0 reference-guide/src/main/xslt/highlighting/php-hl.xml
  178. +86 −0 reference-guide/src/main/xslt/highlighting/python-hl.xml
  179. +95 −0 reference-guide/src/main/xslt/highlighting/ruby-hl.xml
  180. +167 −0 reference-guide/src/main/xslt/highlighting/tcl-hl.xml
  181. +34 −0 reference-guide/src/main/xslt/highlighting/xslthl-config.xml
  182. +36 −0 reference-guide/src/main/xslt/main-html.xsl
  183. +35 −0 reference-guide/src/main/xslt/main-html_single.xsl
  184. +344 −0 reference-guide/src/main/xslt/main-pdf.xsl
202 LICENSE.txt
View
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
29 core/pom.xml
View
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Copyright (c) 2010. Axon Framework
+ ~
+ ~ Licensed under the Apache License, Version 2.0 (the "License");
+ ~ you may not use this file except in compliance with the License.
+ ~ You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <groupId>org.axonframework</groupId>
+ <artifactId>axon</artifactId>
+ <version>0.3-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>axon-core</artifactId>
+ <name>Axon Framework core</name>
+
+</project>
130 core/src/main/java/org/axonframework/core/AbstractAggregateRoot.java
View
@@ -0,0 +1,130 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core;
+
+import org.axonframework.core.util.Assert;
+
+import java.util.UUID;
+
+/**
+ * Abstract convenience class to be extended by all aggregate roots. The AbstractAggregateRoot tracks all uncommitted
+ * events. It also provides convenience methods to initialize the state of the aggregate root based on an {@link
+ * org.axonframework.core.EventStream}, which can be used for event sourcing.
+ *
+ * @author Allard Buijze
+ * @since 0.1
+ */
+public abstract class AbstractAggregateRoot implements EventSourcedAggregateRoot {
+
+ private final EventContainer uncommittedEvents;
+ private final UUID identifier;
+ private volatile Long lastCommitted;
+
+ /**
+ * Initializes the aggregate root using a random aggregate identifier.
+ */
+ protected AbstractAggregateRoot() {
+ this(UUID.randomUUID());
+ }
+
+ /**
+ * Initializes the aggregate root using the provided aggregate identifier.
+ *
+ * @param identifier the identifier of this aggregate
+ */
+ protected AbstractAggregateRoot(UUID identifier) {
+ uncommittedEvents = new EventContainer(identifier);
+ this.identifier = identifier;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void initializeState(EventStream eventStream) {
+ Assert.state(uncommittedEvents.size() == 0, "Aggregate is already initialized");
+ long lastSequenceNumber = -1;
+ while (eventStream.hasNext()) {
+ DomainEvent event = eventStream.next();
+ lastSequenceNumber = event.getSequenceNumber();
+ handle(event);
+ }
+ uncommittedEvents.setFirstSequenceNumber(lastSequenceNumber + 1);
+ lastCommitted = lastSequenceNumber >= 0 ? lastSequenceNumber : null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Long getLastCommittedEventSequenceNumber() {
+ return lastCommitted;
+ }
+
+ /**
+ * Apply the provided event. Applying events means they are added to the uncommitted event queue and forwarded to
+ * the {@link #handle(DomainEvent) event handler method} for processing.
+ *
+ * @param event The event to apply
+ */
+ protected void apply(DomainEvent event) {
+ uncommittedEvents.addEvent(event);
+ handle(event);
+ }
+
+ /**
+ * Apply state changes based on the given event.
+ * <p/>
+ * Note: Implementations of this method should *not* perform validation.
+ *
+ * @param event The event to handle
+ */
+ protected abstract void handle(DomainEvent event);
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public EventStream getUncommittedEvents() {
+ return uncommittedEvents.getInputStream();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public UUID getIdentifier() {
+ return identifier;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void commitEvents() {
+ lastCommitted = uncommittedEvents.getLastSequenceNumber();
+ uncommittedEvents.clear();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int getUncommittedEventCount() {
+ return uncommittedEvents.size();
+ }
+}
55 core/src/main/java/org/axonframework/core/AggregateRoot.java
View
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core;
+
+import java.util.UUID;
+
+/**
+ * Interface defining a contract for entities that represent the aggregate root.
+ *
+ * @author Allard Buijze
+ * @see org.axonframework.core.AbstractAggregateRoot
+ * @since 0.1
+ */
+public interface AggregateRoot {
+
+ /**
+ * Returns an EventStream to the events in the aggregate that have been raised since creation or the last commit.
+ *
+ * @return the EventStream to the uncommitted events.
+ */
+ EventStream getUncommittedEvents();
+
+ /**
+ * Returns the identifier of this aggregate
+ *
+ * @return the identifier of this aggregate
+ */
+ UUID getIdentifier();
+
+ /**
+ * Clears the events currently marked as "uncommitted".
+ */
+ void commitEvents();
+
+ /**
+ * Returns the number of uncommitted events currently available in the aggregate.
+ *
+ * @return the number of uncommitted events currently available in the aggregate.
+ */
+ int getUncommittedEventCount();
+}
149 core/src/main/java/org/axonframework/core/DomainEvent.java
View
@@ -0,0 +1,149 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core;
+
+import org.joda.time.LocalDateTime;
+
+import java.util.UUID;
+
+/**
+ * Base class for all Domain Events. This class contains the basic behavior expected from any event to be processed by
+ * event sourcing engines or dispatchers.
+ *
+ * @author Allard Buijze
+ * @since 0.1
+ */
+public abstract class DomainEvent {
+
+ private volatile Long sequenceNumber;
+ private volatile UUID aggregateIdentifier;
+
+ private final LocalDateTime createDate;
+ private final UUID eventIdentifier;
+
+ /**
+ * Initialize the domain event. Will set the current time stamp and generate a random event identifier.
+ */
+ protected DomainEvent() {
+ createDate = new LocalDateTime();
+ eventIdentifier = UUID.randomUUID();
+ }
+
+ /**
+ * Returns the identifier of this event.
+ *
+ * @return the identifier of this event.
+ */
+ public UUID getEventIdentifier() {
+ return eventIdentifier;
+ }
+
+ /**
+ * Returns the sequence number of this event, if available. Will return null if this event has not been added to an
+ * {@link org.axonframework.core.EventContainer}.
+ *
+ * @return the sequence number of this event, or null if unknown.
+ */
+ public Long getSequenceNumber() {
+ return sequenceNumber;
+ }
+
+ /**
+ * Returns the identifier of the aggregate that reported this event
+ *
+ * @return the identifier of the aggregate that reported this event
+ */
+ public UUID getAggregateIdentifier() {
+ return aggregateIdentifier;
+ }
+
+ /**
+ * Sets the sequence number of this event. May only be set once.
+ *
+ * @param sequenceNumber the sequence number to assign to this event
+ * @throws IllegalStateException if a sequence number was already assigned
+ */
+ void setSequenceNumber(long sequenceNumber) {
+ if (this.sequenceNumber != null) {
+ throw new IllegalStateException("Sequence number may not be applied more than once.");
+ }
+ this.sequenceNumber = sequenceNumber;
+ }
+
+ /**
+ * Sets the aggregate identifier. May only be set once.
+ *
+ * @param aggregateIdentifier the aggregate identifier
+ * @throws IllegalStateException if an aggregate identifier was already assigned
+ */
+ void setAggregateIdentifier(UUID aggregateIdentifier) {
+ if (this.aggregateIdentifier != null) {
+ throw new IllegalStateException("An aggregateIdentifier can not be applied more than once.");
+ }
+ this.aggregateIdentifier = aggregateIdentifier;
+ }
+
+ /**
+ * Checks for equality of two events. Two events are equal when they have the same type, aggregate identifier, time
+ * stamp and sequence number. This allows to test for equality after one or more instances have been serialized and
+ * deserialized.
+ *
+ * @param o the other DomainEvent
+ * @return true when equals, otherwise false
+ */
+ @SuppressWarnings({"RedundantIfStatement"})
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ DomainEvent that = (DomainEvent) o;
+
+ if (!createDate.equals(that.createDate)) {
+ return false;
+ }
+
+ if (aggregateIdentifier != null ? !aggregateIdentifier.equals(that.aggregateIdentifier) :
+ that.aggregateIdentifier != null) {
+ return false;
+ }
+
+ if (this.sequenceNumber == null || that.sequenceNumber == null) {
+ return false;
+ }
+
+ if (!sequenceNumber.equals(that.sequenceNumber)) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ int result = createDate != null ? createDate.hashCode() : 0;
+ result = 31 * result + (eventIdentifier != null ? eventIdentifier.hashCode() : 0);
+ return result;
+ }
+}
144 core/src/main/java/org/axonframework/core/EventContainer.java
View
@@ -0,0 +1,144 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core;
+
+import org.axonframework.core.util.Assert;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.UUID;
+
+/**
+ * Container for events related to a single aggregate. All events added to this container will automatically be assigned
+ * the aggregate identifier and a sequence number.
+ *
+ * @author Allard Buijze
+ * @see org.axonframework.core.DomainEvent
+ * @see org.axonframework.core.AbstractAggregateRoot
+ * @since 0.1
+ */
+class EventContainer {
+
+ private final List<DomainEvent> events = new LinkedList<DomainEvent>();
+ private final UUID aggregateIdentifier;
+ private Long lastSequenceNumber;
+ private long firstSequenceNumber = 0;
+
+ /**
+ * Initialize an EventContainer for an aggregate with the given <code>aggregateIdentifier</code>. This identifier
+ * will be attached to all incoming events.
+ *
+ * @param aggregateIdentifier the aggregate identifier to assign to this container
+ */
+ public EventContainer(UUID aggregateIdentifier) {
+ this.aggregateIdentifier = aggregateIdentifier;
+ }
+
+ /**
+ * Add an event to this container.
+ * <p/>
+ * Events should either be already assigned to the aggregate with the same identifier as this container, or have no
+ * aggregate assigned yet. If an event has a sequence number assigned, it must follow directly upon the sequence
+ * number of the event that was previously added.
+ *
+ * @param event the event to add to this container
+ */
+ public void addEvent(DomainEvent event) {
+ Assert.isTrue(event.getSequenceNumber() == null
+ || lastSequenceNumber == null
+ || event.getSequenceNumber().equals(lastSequenceNumber + 1),
+ "The given event's sequence number is discontinuous");
+
+ Assert.isTrue(event.getAggregateIdentifier() == null
+ || aggregateIdentifier.equals(event.getAggregateIdentifier()),
+ "The Identifier of the event does not match the Identifier of the EventContainer");
+
+ if (event.getAggregateIdentifier() == null) {
+ event.setAggregateIdentifier(aggregateIdentifier);
+ }
+
+ if (event.getSequenceNumber() == null) {
+ event.setSequenceNumber(newSequenceNumber());
+ } else {
+ lastSequenceNumber = event.getSequenceNumber();
+ }
+
+ events.add(event);
+ }
+
+ /**
+ * Read the events inside this container using an {@link org.axonframework.core.EventStream}.
+ *
+ * @return an EventStream providing access to the events in this container
+ */
+ public EventStream getInputStream() {
+ return new SimpleEventStream(events, aggregateIdentifier);
+ }
+
+ /**
+ * Returns the aggregate identifier assigned to this container.
+ *
+ * @return the aggregate identifier assigned to this container
+ */
+ public UUID getAggregateIdentifier() {
+ return aggregateIdentifier;
+ }
+
+ /**
+ * Sets the first sequence number that should be assigned to an incoming event.
+ *
+ * @param firstSequenceNumber the sequence number to assign to the first incoming event
+ */
+ public void setFirstSequenceNumber(long firstSequenceNumber) {
+ Assert.state(events.size() == 0, "Cannot set first sequence number if events have already been added");
+ this.firstSequenceNumber = firstSequenceNumber;
+ }
+
+ /**
+ * Returns the sequence number of the event last added to this container
+ *
+ * @return the sequence number of the last event
+ */
+ public Long getLastSequenceNumber() {
+ return lastSequenceNumber;
+ }
+
+ /**
+ * Clears the events in this container. The sequence number is not modified by this call.
+ */
+ public void clear() {
+ events.clear();
+ }
+
+ /**
+ * Returns the number of events currently inside this container
+ *
+ * @return the number of events in this container
+ */
+ public int size() {
+ return events.size();
+ }
+
+ private long newSequenceNumber() {
+ if (lastSequenceNumber == null) {
+ lastSequenceNumber = firstSequenceNumber;
+ } else {
+ lastSequenceNumber++;
+ }
+ return lastSequenceNumber;
+ }
+}
38 core/src/main/java/org/axonframework/core/EventSourcedAggregateRoot.java
View
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core;
+
+/**
+ * Aggregate that can be initialized using an {@link org.axonframework.core.EventStream}. Aggregates that are initialized
+ * using Event Sourcing should implement this interface.
+ *
+ * @author Allard Buijze
+ * @see org.axonframework.core.repository.eventsourcing.EventSourcingRepository
+ * @since 0.3
+ */
+public interface EventSourcedAggregateRoot extends VersionedAggregateRoot {
+
+ /**
+ * Initialize the state of this aggregate using the events in the provided {@link EventStream}. A call to this
+ * method on an aggregate that has already been initialized will result in an {@link IllegalStateException}.
+ *
+ * @param eventStream the event stream containing the events that describe the state changes of this aggregate
+ * @throws IllegalStateException if this aggregate was already initialized.
+ */
+ void initializeState(EventStream eventStream);
+
+}
58 core/src/main/java/org/axonframework/core/EventStream.java
View
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core;
+
+import java.util.UUID;
+
+/**
+ * The EventStream represents a stream of historical events. The order of events in this stream must represent the
+ * actual chronological order in which the events happened. An EventStream may provide access to all events (from the
+ * first to the most recent) or any subset of these, as long as the stream is continuous.
+ * <p/>
+ * A stream is continuous if each call to obtain the <code>next</code> stream will return the event that immediately
+ * followed the previous one. No events may be <i>skipped</i>.
+ *
+ * @author Allard Buijze
+ * @since 0.1
+ */
+public interface EventStream {
+
+ /**
+ * Returns the aggregate identifier of the aggregate on which these events apply.
+ *
+ * @return the identifier of the aggregate to which events in this stream apply.
+ */
+ UUID getAggregateIdentifier();
+
+ /**
+ * Returns <code>true</code> if the stream has more events, meaning that a call to <code>next()</code> will not
+ * result in an exception. If a call to this method returns <code>false</code>, there is no guarantee about the
+ * result of a consecutive call to <code>next()</code>
+ *
+ * @return <tt>true</tt> if the stream contains more events.
+ */
+ boolean hasNext();
+
+ /**
+ * Returns the next events in the stream, if available. Use <code>hasNext()</code> to obtain a guarantee about the
+ * availability of any next event.
+ *
+ * @return the next event in the stream.
+ */
+ DomainEvent next();
+
+}
111 core/src/main/java/org/axonframework/core/SimpleEventStream.java
View
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.UUID;
+
+/**
+ * Creates an EventStream that streams the contents of a list.
+ *
+ * @author Allard Buijze
+ * @since 0.1
+ */
+public class SimpleEventStream implements EventStream {
+
+ private volatile DomainEvent nextEvent;
+ private final Iterator<DomainEvent> iterator;
+ private final UUID identifier;
+
+ /**
+ * Initialize the event stream using the given List of DomainEvent and aggregate identifier. The List may be empty.
+ *
+ * @param domainEvents the list of domain events to stream
+ * @param aggregateIdentifier the aggregate identifier that the events apply to
+ */
+ public SimpleEventStream(List<DomainEvent> domainEvents, UUID aggregateIdentifier) {
+ this.iterator = domainEvents.iterator();
+ if (iterator.hasNext()) {
+ nextEvent = iterator.next();
+ }
+ identifier = aggregateIdentifier;
+ }
+
+ /**
+ * Initialize the event stream using the given List of DomainEvent and aggregate identifier. The aggregate
+ * identifier is initialized by reading it from the first event available. Therefore, you must ensure that there is
+ * at least one event in the provided list.
+ *
+ * @param domainEvents the list of domain events to stream
+ * @throws IllegalArgumentException if the given list is empty
+ */
+ public SimpleEventStream(List<DomainEvent> domainEvents) {
+ this.iterator = domainEvents.iterator();
+ if (iterator.hasNext()) {
+ nextEvent = iterator.next();
+ identifier = nextEvent.getAggregateIdentifier();
+ } else {
+ throw new IllegalArgumentException("Must provide at least one event");
+ }
+ }
+
+ /**
+ * Initialize the event stream using the given {@link org.axonframework.core.DomainEvent}s and aggregate identifier.
+ * The aggregate identifier is initialized by reading it from the first event available. Therefore, you must provide
+ * at least one event.
+ *
+ * @param events the list of domain events to stream
+ * @throws IllegalArgumentException if no events are supplied
+ */
+ public SimpleEventStream(DomainEvent... events) {
+ this(Arrays.asList(events));
+ }
+
+ /**
+ * Returns the aggregate identifier that the events in this stream apply to. Will never return null.
+ *
+ * @return the aggregate identifier of the events in this stream
+ */
+ @Override
+ public UUID getAggregateIdentifier() {
+ return identifier;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean hasNext() {
+ return nextEvent != null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public DomainEvent next() {
+ DomainEvent next = nextEvent;
+ if (iterator.hasNext()) {
+ nextEvent = iterator.next();
+ } else {
+ nextEvent = null;
+ }
+ return next;
+ }
+}
36 core/src/main/java/org/axonframework/core/VersionedAggregateRoot.java
View
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core;
+
+/**
+ * Interface for aggregate roots that provide their version number. This version number is used to implement locking
+ * strategies and efficient detection of changes.
+ *
+ * @author Allard Buijze
+ * @see org.axonframework.core.repository.LockingRepository
+ * @since 0.3
+ */
+public interface VersionedAggregateRoot extends AggregateRoot {
+
+ /**
+ * Returns the sequence number of the last committed event on this aggregate or <code>null</code> if events were
+ * ever committed. This sequence number can be used to implement optimistic locking strategies.
+ *
+ * @return the sequence number of the last committed event or <code>null</code> if no events were ever committed
+ */
+ Long getLastCommittedEventSequenceNumber();
+}
159 core/src/main/java/org/axonframework/core/eventhandler/AsyncEventBus.java
View
@@ -0,0 +1,159 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core.eventhandler;
+
+import org.axonframework.core.DomainEvent;
+import org.axonframework.core.util.Assert;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * EventBus implementation that uses an ExecutorService to dispatch events asynchronously. This dispatcher takes into
+ * account the {@link EventSequencingPolicy} provided by the {@link EventListener} for sequential handling
+ * requirements.
+ *
+ * @author Allard Buijze
+ * @see EventSequencingPolicy
+ * @see org.axonframework.core.eventhandler.EventListener
+ * @since 0.3
+ */
+public class AsyncEventBus implements EventBus {
+
+ private final static int DEFAULT_CORE_POOL_SIZE = 5;
+ private final static int DEFAULT_MAX_POOL_SIZE = 25;
+ private final static long DEFAULT_KEEP_ALIVE_TIME = 5;
+ private final static TimeUnit DEFAULT_TIME_UNIT = TimeUnit.MINUTES;
+
+ private ExecutorService executorService;
+ private final ConcurrentMap<EventListener, EventHandlingSequenceManager> listenerManagers =
+ new ConcurrentHashMap<EventListener, EventHandlingSequenceManager>();
+ private boolean shutdownExecutorServiceOnStop = false;
+ private AtomicBoolean running = new AtomicBoolean(false);
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void publish(DomainEvent event) {
+ Assert.state(running.get(), "The EventBus is currently not running.");
+ for (EventHandlingSequenceManager eventHandlingSequencing : listenerManagers.values()) {
+ eventHandlingSequencing.addEvent(event);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void subscribe(EventListener eventListener) {
+ Assert.state(running.get(), "The EventBus is currently not running.");
+ if (!listenerManagers.containsKey(eventListener)) {
+ listenerManagers.putIfAbsent(eventListener, newEventHandlingSequenceManager(eventListener));
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void unsubscribe(EventListener eventListener) {
+ listenerManagers.remove(eventListener);
+ }
+
+ /**
+ * Starts the EventBus, opening it for incoming subscription requests and events.
+ * <p/>
+ * Will configure a default executor service if none has been wired. This method must be called after initialization
+ * of all properties.
+ * <p/>
+ * {@inheritDoc}
+ */
+ @PostConstruct
+ public void start() {
+ running.set(true);
+ if (executorService == null) {
+ shutdownExecutorServiceOnStop = true;
+ executorService = new ThreadPoolExecutor(DEFAULT_CORE_POOL_SIZE, DEFAULT_MAX_POOL_SIZE,
+ DEFAULT_KEEP_ALIVE_TIME, DEFAULT_TIME_UNIT,
+ new LinkedBlockingQueue<Runnable>());
+ }
+ }
+
+ /**
+ * Stops this event bus. All subscriptions are removed and incoming events are rejected.
+ */
+ @PreDestroy
+ public void stop() {
+ running.set(false);
+ listenerManagers.clear();
+ if (executorService != null && shutdownExecutorServiceOnStop) {
+ executorService.shutdown();
+ }
+ }
+
+ /**
+ * Creates a new EventHandlingSequenceManager for the given event listener.
+ *
+ * @param eventListener The event listener that the EventHandlingSequenceManager should manage
+ * @return a new EventHandlingSequenceManager instance
+ */
+ protected EventHandlingSequenceManager newEventHandlingSequenceManager(EventListener eventListener) {
+ return new EventHandlingSequenceManager(eventListener, getExecutorService());
+ }
+
+ /**
+ * Accessor for the executor service used to dispatch events in this event bus
+ *
+ * @return the executor service used to dispatch events in this event bus
+ */
+ protected ExecutorService getExecutorService() {
+ return executorService;
+ }
+
+ /**
+ * Sets the ExecutorService instance to use to handle events. Typically, this will be a ThreadPoolExecutor
+ * implementation with an unbounded blocking queue.
+ * <p/>
+ * Defaults to a ThreadPoolExecutor with 5 core threads and a max pool size of 25 threads with a timeout of 5
+ * minutes.
+ *
+ * @param executorService the executor service to use for event handling
+ */
+ public void setExecutorService(ExecutorService executorService) {
+ this.executorService = executorService;
+ }
+
+ /**
+ * Defines whether or not to shutdown the executor service when the EventBus is stopped. This value is ignored when
+ * the default ExecutorService is used. Defaults to <code>false</code> if a custom executor service is defined using
+ * the {@link #setExecutorService(java.util.concurrent.ExecutorService)} method.
+ *
+ * @param shutdownExecutorServiceOnStop Whether or not to shutdown the executor service when the event bus is
+ * stopped
+ */
+ public void setShutdownExecutorServiceOnStop(boolean shutdownExecutorServiceOnStop) {
+ this.shutdownExecutorServiceOnStop = shutdownExecutorServiceOnStop;
+ }
+}
59 core/src/main/java/org/axonframework/core/eventhandler/EventBus.java
View
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core.eventhandler;
+
+import org.axonframework.core.DomainEvent;
+
+/**
+ * Specification of the mechanism on which the Event Listeners can subscribe for events and event publishers can publish
+ * their events.
+ * <p/>
+ * Implementations may or may not dispatch the events to event listeners in the dispatching thread.
+ *
+ * @author Allard Buijze
+ * @see EventListener
+ * @see SynchronousEventBus
+ * @see AsyncEventBus
+ * @see SpringIntegrationEventBus
+ * @since 0.1
+ */
+public interface EventBus {
+
+ /**
+ * Publish an event on this bus. It is dispatched to all subscribed event listeners.
+ *
+ * @param event the event to publish
+ */
+ void publish(DomainEvent event);
+
+ /**
+ * Subscribe the given <code>eventListener</code> to this bus. When subscribed, it will receive all events published
+ * to this bus.
+ *
+ * @param eventListener The event listener to subscribe
+ */
+ void subscribe(EventListener eventListener);
+
+ /**
+ * Unsubscribe the given <code>eventListener</code> to this bus. When unsubscribed, it will no longer receive events
+ * published to this bus.
+ *
+ * @param eventListener The event listener to unsubscribe
+ */
+ void unsubscribe(EventListener eventListener);
+
+}
137 core/src/main/java/org/axonframework/core/eventhandler/EventHandlingSequenceManager.java
View
@@ -0,0 +1,137 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core.eventhandler;
+
+import org.axonframework.core.DomainEvent;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ExecutorService;
+
+/**
+ * The EventHandlingSequenceManager is responsible for delegating each incoming event to the relevant {@link
+ * EventProcessingScheduler} for processing, depending on the sequencing identifier of the event.
+ *
+ * @author Allard Buijze
+ * @since 0.3
+ */
+public class EventHandlingSequenceManager {
+
+ private final EventListener eventListener;
+ private final ExecutorService executorService;
+ private final ConcurrentMap<Object, EventProcessingScheduler> transactions =
+ new ConcurrentHashMap<Object, EventProcessingScheduler>();
+ private final EventSequencingPolicy eventSequencingPolicy;
+
+ /**
+ * Initialize the EventHandlingSequenceManager for the given <code>eventListener</code> using the given
+ * <code>executorService</code>.
+ *
+ * @param eventListener The event listener this instance manages
+ * @param executorService The executorService that processes the events
+ */
+ public EventHandlingSequenceManager(EventListener eventListener, ExecutorService executorService) {
+ this.eventListener = eventListener;
+ this.executorService = executorService;
+ this.eventSequencingPolicy = eventListener.getEventSequencingPolicy();
+ }
+
+ /**
+ * Adds an event to the relevant scheduler.
+ *
+ * @param event The event to schedule
+ */
+ public void addEvent(DomainEvent event) {
+ if (eventListener.canHandle(event.getClass())) {
+ final Object policy = eventSequencingPolicy.getSequenceIdentifierFor(event);
+ if (policy == null) {
+ executorService.submit(new SingleEventHandlerInvocationTask(eventListener, event));
+ } else {
+ scheduleEvent(event, policy);
+ }
+ }
+ }
+
+ private void scheduleEvent(DomainEvent event, Object policy) {
+ boolean eventScheduled = false;
+ while (!eventScheduled) {
+ EventProcessingScheduler currentScheduler = transactions.get(policy);
+ if (currentScheduler == null) {
+ transactions.putIfAbsent(policy, newProcessingScheduler(new TransactionCleanUp(policy)));
+ } else {
+ eventScheduled = currentScheduler.scheduleEvent(event);
+ if (!eventScheduled) {
+ // we know it can be cleaned up.
+ transactions.remove(policy, currentScheduler);
+ }
+ }
+ }
+ }
+
+ /**
+ * Creates a new scheduler instance for the eventListener that schedules events on the executor service for the
+ * managed EventListener.
+ *
+ * @param shutDownCallback The callback that needs to be notified when the scheduler stops processing.
+ * @return a new scheduler instance
+ */
+ protected EventProcessingScheduler newProcessingScheduler(TransactionCleanUp shutDownCallback) {
+ return new EventProcessingScheduler(eventListener, executorService, shutDownCallback);
+ }
+
+ private static class SingleEventHandlerInvocationTask implements Runnable {
+
+ private final EventListener eventListener;
+ private final DomainEvent event;
+
+ /**
+ * Configures a task to invoke a single event on an event listener
+ *
+ * @param eventListener The event listener to invoke the event handler on
+ * @param event the event to send to the event listener
+ */
+ public SingleEventHandlerInvocationTask(EventListener eventListener, DomainEvent event) {
+ this.eventListener = eventListener;
+ this.event = event;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void run() {
+ eventListener.handle(event);
+ }
+ }
+
+ private final class TransactionCleanUp implements EventProcessingScheduler.ShutdownCallback {
+
+ private final Object policy;
+
+ private TransactionCleanUp(Object policy) {
+ this.policy = policy;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void afterShutdown(EventProcessingScheduler scheduler) {
+ transactions.remove(policy, scheduler);
+ }
+ }
+}
57 core/src/main/java/org/axonframework/core/eventhandler/EventListener.java
View
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core.eventhandler;
+
+import org.axonframework.core.DomainEvent;
+
+/**
+ * Interface to be implemented by classes that can handle events.
+ *
+ * @author Allard Buijze
+ * @see org.axonframework.core.eventhandler.EventBus
+ * @see org.axonframework.core.DomainEvent
+ * @see org.axonframework.core.eventhandler.annotation.EventHandler
+ * @since 0.1
+ */
+public interface EventListener {
+
+ /**
+ * Indicates whether this event listener can handle events of the given type. This method is used as an early
+ * detection during the dispatching process.
+ *
+ * @param eventType the type of event
+ * @return true if this event listener can handle the event, false otherwise
+ */
+ boolean canHandle(Class<? extends DomainEvent> eventType);
+
+ /**
+ * Process the given event. There are no guarantees that this method is not called with types for which {@link
+ * #canHandle(Class)} returned false. In such case, this method should return normally (typically without doing
+ * anything).
+ *
+ * @param event the event to handle
+ */
+ void handle(DomainEvent event);
+
+ /**
+ * The Event sequencing policy applicable to this event listener. This policy defines which Events must be processed
+ * sequentially, and which may run in parallel.
+ *
+ * @return the Event sequencing policy applicable to this event listener
+ */
+ EventSequencingPolicy getEventSequencingPolicy();
+}
211 core/src/main/java/org/axonframework/core/eventhandler/EventProcessingScheduler.java
View
@@ -0,0 +1,211 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core.eventhandler;
+
+import org.axonframework.core.DomainEvent;
+
+import java.util.LinkedList;
+import java.util.Queue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.RejectedExecutionException;
+
+import static org.axonframework.core.eventhandler.YieldPolicy.DO_NOT_YIELD;
+
+/**
+ * The EventProcessingScheduler is responsible for scheduling all events within the same SequencingIdentifier in an
+ * ExecutorService. It will only handle events that were present in the queue at the moment processing started. Any
+ * events added later will be rescheduled automatically.
+ *
+ * @author Allard Buijze
+ * @since 0.3
+ */
+public class EventProcessingScheduler implements Runnable {
+
+ private final EventListener eventListener;
+ private final ShutdownCallback shutDownCallback;
+ private final TransactionAware transactionListener;
+ private final ExecutorService executorService;
+
+ // guarded by "this"
+ private final Queue<DomainEvent> events = new LinkedList<DomainEvent>();
+ // guarded by "this"
+ private boolean isScheduled = false;
+ private boolean cleanedUp;
+
+ /**
+ * Initialize a scheduler for the given <code>eventListener</code> using the given <code>executorService</code>.
+ *
+ * @param eventListener The event listener for which this scheduler schedules events
+ * @param executorService The executor service that will process the events
+ * @param shutDownCallback The callback to notify when the scheduler finishes processing events
+ */
+ public EventProcessingScheduler(EventListener eventListener, ExecutorService executorService,
+ ShutdownCallback shutDownCallback) {
+ this.eventListener = eventListener;
+ this.shutDownCallback = shutDownCallback;
+ if (eventListener instanceof TransactionAware) {
+ this.transactionListener = (TransactionAware) eventListener;
+ } else {
+ this.transactionListener = new TransactionIgnoreAdapter();
+ }
+ this.executorService = executorService;
+ }
+
+ /**
+ * Schedules an event for processing. Will schedule a new invoker task if none is currently active.
+ * <p/>
+ * If the current scheduler is in the process of being shut down, this method will return false.
+ * <p/>
+ * This method is thread safe
+ *
+ * @param event the event to schedule
+ * @return true if the event was scheduled successfully, false if this scheduler is not available to process events
+ */
+ public synchronized boolean scheduleEvent(DomainEvent event) {
+ if (cleanedUp) {
+ return false;
+ }
+ events.add(event);
+ scheduleIfNecessary();
+ return true;
+ }
+
+ /**
+ * Returns the next event in the queue, if available. If returns false if no further events are available for
+ * processing. In that case, it will also set the scheduled status to false.
+ * <p/>
+ * This method is thread safe
+ *
+ * @return the next DomainEvent for processing, of null if none is available
+ */
+ protected synchronized DomainEvent nextEvent() {
+ return events.poll();
+ }
+
+ /**
+ * Tries to yield to other threads be rescheduling processing of any further queued events. If rescheduling fails,
+ * this call returns false, indicating that processing should continue in the current thread.
+ * <p/>
+ * This method is thread safe
+ *
+ * @return true if yielding succeeded, false otherwise.
+ */
+ protected synchronized boolean yield() {
+ if (events.size() > 0) {
+ isScheduled = true;
+ try {
+ executorService.submit((Runnable) this);
+ }
+ catch (RejectedExecutionException e) {
+ return false;
+ }
+ } else {
+ cleanUp();
+ }
+ return true;
+ }
+
+ /**
+ * Will look at the current scheduling status and schedule an EventHandlerInvokerTask if none is already active.
+ * <p/>
+ * This method is thread safe
+ */
+ protected synchronized void scheduleIfNecessary() {
+ if (!isScheduled) {
+ isScheduled = true;
+ executorService.submit(this);
+ }
+ }
+
+ /**
+ * Returns the number of events currently queued for processing.
+ *
+ * @return the number of events currently queued for processing.
+ */
+ protected synchronized int queuedEventCount() {
+ return events.size();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void run() {
+ DomainEvent event;
+ boolean mayContinue = true;
+ final TransactionStatusImpl status = new TransactionStatusImpl(queuedEventCount());
+ TransactionStatus.set(status);
+ while (mayContinue) {
+ transactionListener.beforeTransaction(status);
+ // TODO: Implement transaction rollback and retry mechanism
+ while (!status.isTransactionSizeReached() && (event = nextEvent()) != null) {
+ eventListener.handle(event);
+ status.recordEventProcessed();
+ }
+ transactionListener.afterTransaction(status);
+ mayContinue = (queuedEventCount() > 0 && DO_NOT_YIELD.equals(status.getYieldPolicy())) || !yield();
+ status.resetTransactionStatus();
+ }
+ TransactionStatus.clear();
+ }
+
+ private synchronized void cleanUp() {
+ isScheduled = false;
+ cleanedUp = true;
+ shutDownCallback.afterShutdown(this);
+ }
+
+ private static class TransactionStatusImpl extends TransactionStatus {
+
+ public TransactionStatusImpl(int transactionSize) {
+ setMaxTransactionSize(transactionSize);
+ }
+ }
+
+ private static class TransactionIgnoreAdapter implements TransactionAware {
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void beforeTransaction(TransactionStatus transactionStatus) {
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void afterTransaction(TransactionStatus transactionStatus) {
+ }
+
+ }
+
+ /**
+ * Callback that allows the SequenceManager to receive a notification when this scheduler finishes processing
+ * events.
+ */
+ interface ShutdownCallback {
+
+ /**
+ * Called when event processing is complete. This means that there are no more events waiting and the last
+ * transactional batch has been committed successfully.
+ *
+ * @param scheduler the scheduler that completed processing.
+ */
+ void afterShutdown(EventProcessingScheduler scheduler);
+ }
+}
45 core/src/main/java/org/axonframework/core/eventhandler/EventSequencingPolicy.java
View
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core.eventhandler;
+
+import org.axonframework.core.DomainEvent;
+
+/**
+ * Interface to a policy definition for concurrent event handling.
+ * <p/>
+ * Some implementations are provided by default: <ul> <li>{@link SequentialPolicy}: Default policy. Requires that all
+ * events are handled in the order they arrive at the event handler. This also means that at most 1 thread is processing
+ * events for this handler at any time. <li>{@link FullConcurrencyPolicy}: Allows each event to be handled independently
+ * of any other events. Event processing will typically start in the same order the events were dispatched in.
+ * <li>{@link SequentialPerAggregatePolicy}: Will force events generated by the same aggregate to be handled
+ * sequentially. At most one thread will be processing events of a single aggregate at any time</ul>
+ *
+ * @author Allard Buijze
+ * @since 0.3
+ */
+public interface EventSequencingPolicy {
+
+ /**
+ * Returns the sequence identifier for the given <code>event</code>. When two events have the same identifier (as
+ * defined by their equals method), they will be executed sequentially. A <code>null</code> value indicates that
+ * there are no sequencing requirements for the handling of this event.
+ *
+ * @param event the event for which to get the sequencing identifier
+ * @return a sequence identifier for the given event
+ */
+ Object getSequenceIdentifierFor(DomainEvent event);
+}
36 core/src/main/java/org/axonframework/core/eventhandler/FullConcurrencyPolicy.java
View
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core.eventhandler;
+
+import org.axonframework.core.DomainEvent;
+
+/**
+ * EventSequencingPolicy that does not enforce any sequencing requirements on event processing.
+ *
+ * @author Allard Buijze
+ * @since 0.3
+ */
+public class FullConcurrencyPolicy implements EventSequencingPolicy {
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Object getSequenceIdentifierFor(DomainEvent event) {
+ return null;
+ }
+}
55 core/src/main/java/org/axonframework/core/eventhandler/MessageHandlerAdapter.java
View
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core.eventhandler;
+
+import org.axonframework.core.DomainEvent;
+import org.springframework.integration.core.Message;
+import org.springframework.integration.message.MessageDeliveryException;
+import org.springframework.integration.message.MessageHandler;
+import org.springframework.integration.message.MessageHandlingException;
+
+/**
+ * Adapter that allows an EventListener to be registered as a Spring Integration {@link
+ * org.springframework.integration.message.MessageHandler}.
+ *
+ * @author Allard Buijze
+ * @since 0.1
+ */
+class MessageHandlerAdapter implements MessageHandler {
+
+ private final EventListener eventListener;
+
+ /**
+ * Initialize an adapter for the given <code>eventListener</code>
+ *
+ * @param eventListener the event listener to adapt
+ */
+ public MessageHandlerAdapter(EventListener eventListener) {
+ this.eventListener = eventListener;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void handleMessage(Message<?> message) throws MessageHandlingException, MessageDeliveryException {
+ DomainEvent event = (DomainEvent) message.getPayload();
+ if (eventListener.canHandle(event.getClass())) {
+ eventListener.handle(event);
+ }
+ }
+}
37 core/src/main/java/org/axonframework/core/eventhandler/SequentialPerAggregatePolicy.java
View
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core.eventhandler;
+
+import org.axonframework.core.DomainEvent;
+
+/**
+ * Concurrency policy that requires sequential processing of events raised by the same aggregate. Events from different
+ * aggregates may be processed in different threads.
+ *
+ * @author Allard Buijze
+ * @since 0.3
+ */
+public class SequentialPerAggregatePolicy implements EventSequencingPolicy {
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Object getSequenceIdentifierFor(DomainEvent event) {
+ return event.getAggregateIdentifier();
+ }
+}
39 core/src/main/java/org/axonframework/core/eventhandler/SequentialPolicy.java
View
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core.eventhandler;
+
+import org.axonframework.core.DomainEvent;
+
+/**
+ * EventSequencingPolicy that requires serialized handling of all events delivered to an event handler. This is the
+ * default policy for event handlers.
+ *
+ * @author Allard Buijze
+ * @since 0.3
+ */
+public class SequentialPolicy implements EventSequencingPolicy {
+
+ private static final Object FULL_SEQUENTIAL_POLICY = new Object();
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Object getSequenceIdentifierFor(DomainEvent event) {
+ return FULL_SEQUENTIAL_POLICY;
+ }
+}
84 core/src/main/java/org/axonframework/core/eventhandler/SpringIntegrationEventBus.java
View
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core.eventhandler;
+
+import org.axonframework.core.DomainEvent;
+import org.springframework.integration.channel.SubscribableChannel;
+import org.springframework.integration.message.GenericMessage;
+import org.springframework.integration.message.MessageHandler;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+/**
+ * {@link org.axonframework.core.eventhandler.EventBus} implementation that delegates all subscription and publishing
+ * requests to a {@link org.springframework.integration.channel.SubscribableChannel Spring Integration channel}.
+ * <p/>
+ * Use {@link #setChannel(org.springframework.integration.channel.SubscribableChannel)} to set the channel to delegate
+ * all the requests to.
+ * <p/>
+ * This EventBus will automatically wrap and unwrap events in {@link org.springframework.integration.core.Message
+ * Messages} and {@link org.axonframework.core.eventhandler.EventListener EventListeners} in {@link
+ * org.springframework.integration.message.MessageHandler MessageHandlers}.
+ * <p/>
+ * This implementation expects the Spring Integration to be configured to handle messages asynchronously.
+ *
+ * @author Allard Buijze
+ * @since 0.1
+ */
+public class SpringIntegrationEventBus implements EventBus {
+
+ private SubscribableChannel channel;
+ private final ConcurrentMap<EventListener, MessageHandler> handlers = new ConcurrentHashMap<EventListener, MessageHandler>();
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void unsubscribe(EventListener eventListener) {
+ MessageHandler messageHandler = handlers.remove(eventListener);
+ channel.unsubscribe(messageHandler);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void subscribe(EventListener eventListener) {
+ MessageHandler messagehandler = new MessageHandlerAdapter(eventListener);
+ handlers.putIfAbsent(eventListener, messagehandler);
+ channel.subscribe(messagehandler);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void publish(DomainEvent event) {
+ channel.send(new GenericMessage<Object>(event));
+ }
+
+ /**
+ * Sets the Spring Integration Channel that this event bus should publish events to.
+ *
+ * @param channel the channel to publish events to
+ */
+ public void setChannel(SubscribableChannel channel) {
+ this.channel = channel;
+ }
+
+}
84 core/src/main/java/org/axonframework/core/eventhandler/SynchronousEventBus.java
View
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core.eventhandler;
+
+import org.axonframework.core.DomainEvent;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Set;
+import java.util.concurrent.CopyOnWriteArraySet;
+
+/**
+ * Implementation of the {@link org.axonframework.core.eventhandler.EventBus} that directly forwards all published events
+ * (in the callers' thread) to all subscribed listeners.
+ * <p/>
+ * Listeners are expected to implement asynchronous handling themselves.
+ *
+ * @author Allard Buijze
+ * @since 0.1
+ */
+public class SynchronousEventBus implements EventBus {
+
+ private final Set<EventListener> listeners = new CopyOnWriteArraySet<EventListener>();
+ private static final Logger logger = LoggerFactory.getLogger(SynchronousEventBus.class);
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void unsubscribe(EventListener eventListener) {
+ if (listeners.remove(eventListener)) {
+ logger.debug("EventListener {} unsubscribed successfully", eventListener.getClass().getSimpleName());
+ } else {
+ logger.info("EventListener {} not removed. It was already unsubscribed",
+ eventListener.getClass().getSimpleName());
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void subscribe(EventListener eventListener) {
+ if (listeners.add(eventListener)) {
+ logger.debug("EventListener [{}] subscribed successfully", eventListener.getClass().getSimpleName());
+ } else {
+ logger.info("EventListener [{}] not added. It was already subscribed",
+ eventListener.getClass().getSimpleName());
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void publish(DomainEvent event) {
+ for (EventListener listener : listeners) {
+ if (listener.canHandle(event.getClass())) {
+ logger.debug("Dispatching Event [{}] to EventListener [{}]",
+ event.getClass().getSimpleName(),
+ listener.getClass().getSimpleName());
+ listener.handle(event);
+ } else {
+ logger.debug("Dispatching of Event [{}] skipped for EventListener [{}]",
+ event.getClass().getSimpleName(),
+ listener.getClass().getSimpleName());
+ }
+ }
+ }
+}
59 core/src/main/java/org/axonframework/core/eventhandler/TransactionAware.java
View
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core.eventhandler;
+
+/**
+ * Extension on the {@link org.axonframework.core.eventhandler.EventListener} interface that provides implementations the
+ * ability to do work at the start and end of a transaction.
+ * <p/>
+ * Typically, this will involve opening database transactions or connecting to external systems.
+ *
+ * @author Allard Buijze
+ * @since 0.3
+ */
+public interface TransactionAware {
+
+ /**
+ * Invoked by the EventProcessingScheduler before processing a series of events. The given {@link
+ * org.axonframework.core.eventhandler.TransactionStatus} may be used to set the maximum batch size for the current
+ * transaction.
+ *
+ * @param transactionStatus The current status of the transaction
+ * @see #afterTransaction(TransactionStatus)
+ * @see org.axonframework.core.eventhandler.TransactionStatus
+ */
+ void beforeTransaction(TransactionStatus transactionStatus);
+
+ /**
+ * Invoked by the EventProcessingScheduler after a series of events is processed. The given {@link
+ * org.axonframework.core.eventhandler.TransactionStatus} may be used to indicate whether the scheduler should yield to
+ * other event processing schedulers or not.
+ * <p/>
+ * This method is always called once for each invocation to {@link #beforeTransaction(TransactionStatus)}, even if
+ * no events were processed at all.
+ * <p/>
+ * Note that this method is called when a transactional batch was handled successfully, as well as when an error
+ * occurred. Use the {@link org.axonframework.core.eventhandler.TransactionStatus} object to find information about
+ * transaction status and (when failed) the cause of the failure.
+ *
+ * @param transactionStatus The current status of the transaction
+ * @see #beforeTransaction(TransactionStatus)
+ * @see org.axonframework.core.eventhandler.TransactionStatus
+ */
+ void afterTransaction(TransactionStatus transactionStatus);
+
+}
199 core/src/main/java/org/axonframework/core/eventhandler/TransactionStatus.java
View
@@ -0,0 +1,199 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core.eventhandler;
+
+/**
+ * Provides details about the current status of an event handling transaction. This method is typically accessed through
+ * the {@link TransactionAware#beforeTransaction(TransactionStatus) beforeTransaction} and {@link
+ * TransactionAware#afterTransaction(TransactionStatus) afterTransaction} methods on {@link TransactionAware}, but may
+ * also be obtained through the static {@link TransactionStatus#current()} method.
+ * <p/>
+ * This class is meant to be used in a single thread and is therefore not thread-safe.
+ *
+ * @author Allard Buijze
+ * @since 0.3
+ */
+public abstract class TransactionStatus {
+
+ private static ThreadLocal<TransactionStatus> current = new ThreadLocal<TransactionStatus>();
+
+ private YieldPolicy yieldPolicy = YieldPolicy.YIELD_AFTER_TRANSACTION;
+ private int eventsProcessedSinceLastYield = 0;
+ private int eventsProcessedInTransaction = 0;
+ private int maxTransactionSize = 50;
+ private Throwable exception;
+
+ /**
+ * Returns the TransactionStatus object related to a transaction running on the current thread. Returns
+ * <code>null</code> if no transaction is running on the current thread.
+ *
+ * @return the currently active TransactionStatus, or <code>null</code> if none is present.
+ */
+ public static TransactionStatus current() {
+ return current.get();
+ }
+
+ /**
+ * Clears the TransactionStatus related to the current thread.
+ */
+ static void clear() {
+ current.remove();
+ }
+
+ /**
+ * Sets the TransactionStatus object related to the transaction running in the current thread. If a previous value
+ * exists, it is overwritten.
+ *
+ * @param newStatus The TransactionStatus for the current transaction
+ */
+ static void set(TransactionStatus newStatus) {
+ current.set(newStatus);
+ }
+
+ /**
+ * Returns the number of events processed (so far) in the current transaction.
+ *
+ * @return the number of events processed (so far) in the current transaction.
+ */
+ public int getEventsProcessedInTransaction() {
+ return eventsProcessedInTransaction;
+ }
+
+ /**
+ * Returns the number of events processed (so far) since the scheduler last yielded to other threads. If the
+ * scheduler never yielded, it indicates the total number of events processed.
+ *
+ * @return the number of events processed (so far) since the scheduler last yielded
+ */
+ public int getEventsProcessedSinceLastYield() {
+ return eventsProcessedSinceLastYield;
+ }
+
+ /**
+ * Sets the YieldPolicy for the current transaction. Defaults to {@link YieldPolicy#YIELD_AFTER_TRANSACTION
+ * YIELD_AFTER_TRANSACTION}.
+ *
+ * @param yieldPolicy The YieldPolicy to use for the current transaction
+ */
+ public void setYieldPolicy(YieldPolicy yieldPolicy) {
+ this.yieldPolicy = yieldPolicy;
+ }
+
+ /**
+ * Returns the YieldPolicy applicable to the current transaction.
+ *
+ * @return the YieldPolicy applicable to the current transaction
+ */
+ public YieldPolicy getYieldPolicy() {
+ return yieldPolicy;
+ }
+
+ /**
+ * Forces the EventProcessingScheduler to immediately yield to other schedulers after processing this event. The
+ * current transaction will be closed normally.
+ */
+ public void requestImmediateYield() {
+ requestImmediateCommit();
+ setYieldPolicy(YieldPolicy.YIELD_AFTER_TRANSACTION);
+ }
+
+ /**
+ * Requests the EventProcessingScheduler to commit the transaction immediately. Note that if this method is called
+ * before any events have been processed, the transaction will close without processing any events.
+ */
+ public void requestImmediateCommit() {
+ maxTransactionSize = eventsProcessedInTransaction;
+ }
+
+ /**
+ * Returns the maximum number of events that may be processed inside the current transaction.
+ *
+ * @return the maximum number of events in the current transaction
+ */
+ public int getMaxTransactionSize() {
+ return maxTransactionSize;
+ }
+
+ /**
+ * Sets the maximum number of events to process inside the current transaction. The scheduler will commit a
+ * transaction if this number (or more) events have been processed inside the current transaction.
+ * <p/>
+ * Defaults to the number of events in the queue at the moment the transaction started.
+ *
+ * @param maxTransactionSize The number of events to process in the current transaction
+ */
+ public void setMaxTransactionSize(int maxTransactionSize) {
+ this.maxTransactionSize = maxTransactionSize;
+ }
+
+ /**
+ * Record the fact that an event has been processed. This will increase the number of events processed in current
+ * transaction as well as the number of events since last yield.
+ */
+ protected void recordEventProcessed() {
+ eventsProcessedSinceLastYield++;
+ eventsProcessedInTransaction++;
+ }
+
+ /**
+ * Resets the event count for current transaction to 0 and sets the YieldPolicy to the default value
+ * (YIELD_AFTER_TRANSACTION).
+ */
+ protected void resetTransactionStatus() {
+ eventsProcessedInTransaction = 0;
+ yieldPolicy = YieldPolicy.YIELD_AFTER_TRANSACTION;
+ }
+
+ /**
+ * Indicates whether or not the maximum amount of events have been processed in this transaction.
+ *
+ * @return true if the maximum amount of events was handled, otherwise false.
+ */
+ protected boolean isTransactionSizeReached() {
+ return eventsProcessedInTransaction >= maxTransactionSize;
+ }
+
+ /**
+ * Indicates whether the current transactional batch is executed successfully. If a batch is currently in progress,
+ * this will indicate if an error has been discovered so far.
+ *
+ * @return whether the current transaction is successful or not.
+ */
+ public boolean isSuccessful() {
+ return exception == null;
+ }
+
+ /**
+ * Returns the exception that caused the transaction to be marked as failed. Returns null if transaction is
+ * successful. Use {@link #isSuccessful()} to find out if transaction was successful or not.
+ *
+ * @return the exception that caused the transaction to fail
+ */
+ public Throwable getException() {
+ return exception;
+ }
+
+ /**
+ * Mark the current transaction as failed.
+ *
+ * @param exception the exception that caused the transaction to fail
+ */
+ void markFailed(Throwable exception) {
+ this.exception = exception;
+ }
+
+}
39 core/src/main/java/org/axonframework/core/eventhandler/YieldPolicy.java
View
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2010. Axon Framework
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.axonframework.core.eventhandler;
+
+/**
+ * The yielding policy for transactions. This policy tells the EventProcessingScheduler what to do when a transaction is
+ * finished.
+ *
+ * @author Allard Buijze
+ * @since 0.3
+ */
+public enum YieldPolicy {
+
+ /**
+ * Continue event processing in the same thread. This option can be used for event listeners that need high-priority