kcqrs

CQRS In Kotlin

Лицензия

Лицензия

Группа

Группа

com.clouway.kcqrs
Идентификатор

Идентификатор

kcqrs-core
Последняя версия

Последняя версия

0.0.1
Дата

Дата

Тип

Тип

jar
Описание

Описание

kcqrs
CQRS In Kotlin
Ссылка на сайт

Ссылка на сайт

https://github.com/clouway/kcqrs
Система контроля версий

Система контроля версий

https://github.com/clouway/kcqrs

Скачать kcqrs-core

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/com.clouway.kcqrs/kcqrs-core/ -->
<dependency>
    <groupId>com.clouway.kcqrs</groupId>
    <artifactId>kcqrs-core</artifactId>
    <version>0.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.clouway.kcqrs/kcqrs-core/
implementation 'com.clouway.kcqrs:kcqrs-core:0.0.1'
// https://jarcasting.com/artifacts/com.clouway.kcqrs/kcqrs-core/
implementation ("com.clouway.kcqrs:kcqrs-core:0.0.1")
'com.clouway.kcqrs:kcqrs-core:jar:0.0.1'
<dependency org="com.clouway.kcqrs" name="kcqrs-core" rev="0.0.1">
  <artifact name="kcqrs-core" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.clouway.kcqrs', module='kcqrs-core', version='0.0.1')
)
libraryDependencies += "com.clouway.kcqrs" % "kcqrs-core" % "0.0.1"
[com.clouway.kcqrs/kcqrs-core "0.0.1"]

Зависимости

compile (1)

Идентификатор библиотеки Тип Версия
org.jetbrains.kotlin : kotlin-stdlib-jre8 jar 1.2.10

test (2)

Идентификатор библиотеки Тип Версия
com.clouway.kcqrs : kcqrs-testing jar 0.0.1
junit : junit jar 4.12

Модули Проекта

Данный проект не имеет модулей.

kcqrs

CQRS library build in Kotlin for better development experience.

Build Status

Build Status

Architecture

Architecture

Use Case

class Invoice private constructor(@JvmField var customerName: String, @JvmField val amount: BigDecimal) : AggregateRootBase() {

  // Required for the serialization 
  constructor() : this("", BigDecimal.ZERO)

  constructor(id: UUID, customerName: String, amount: BigDecimal) : this(customerName, amount) {
    applyChange(InvoiceCreatedEvent(id, customerName))
  }

  fun changeCustomerName(customerName: String) {
    applyChange(InvoiceCustomerNameChangedEvent(getId()!!, customerName))
  }

  fun apply(event: InvoiceCreatedEvent) {
    uuid = event.invoiceId
    customerName = event.customerName    
  }

  fun apply(event: InvoiceCustomerNameChangedEvent) {
    customerName = event.newCustomerName
  }
}
    
data class InvoiceCreatedEvent(@JvmField val invoiceId: UUID, @JvmField val customerName: String) : Event
    
data class InvoiceCustomerNameChangedEvent(@JvmField val invoiceId: UUID, @JvmField val newCustomerName: String) : Event

// ...
// Usage 
val invoice = Invoice(UUID.randomUUID(), "John", BigDecimal(30))        
eventRepository.save(invoice)

invoice.changeCustomerName("Peter")
eventRepository.save(invoice)

// Load Invoice document from events
val updateInvoice = eventRepository.getById(invoice.getId()!!, Invoice::class.java)

Handling Commands

val messageBus = cqrs.messageBus()

// On App Initialization 
messageBus.registerCommandHandler(ChangeCustomerName::class.java, ChangeCustomerNameHandler())

// Dispatch User Action in Servlet or REST Api Call 
fun registerNewCustomer(req: HttpServletRequest, HttpServletResponse) {     
  msgBus.send(RegisterNewCustomer(req.getParameter("name")))      
} 

Handling Events

val messageBus = cqrs.messageBus()

// On App Initialization 
msgBus.registerEventHandler(CustomerRegisteredEvent::class.java, CustomerRegisteredEventHandler(InMemoryCustomerRepository()))

// Handle registration event      
msgBus.handle(CustomerRegisteredEvent("Any Customer Name"))       

Interceptors

Interceptors are a powerful mechanism that can monitor, rewrite, and retry calls. Here's a simple interceptor that publishes received event.

val msgBus = cqrs.messageBus()
val pubSubPublisher = object : Interceptor {
   override fun intercept(chain: Interceptor.Chain) {     
     // make sure that event is processed by other handlers                                 
     chain.proceed(chain.event())
     
     val pubSubEvent = adapt(chain.event())     
     pubSubPublisher.publish(pubSubEvent)
  }
}
msgBus.registerInterceptor(pubSubPublisher)

// ....
msgBus.handle(event)                

App Engine Adapter

Registering Event Handler

class KCqrsEventHandler : AbstractEventHandlerServlet() {
    private val gson = Gson()

    override fun decode(inputStream: InputStream, type: Class<*>): Event {
        val event = gson.fromJson(InputStreamReader(inputStream, "UTF-8"), type)
        return event as Event
    }

    override fun messageBus(): MessageBus {
        return CQRSContext.messageBus()
    }
    
}

Adding it to web.xml

  <servlet>
    <servlet-name>kcqrsEventHandler</servlet-name>
    <servlet-class>com.clouway.kcqrs.example.KCqrsEventHandler</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>kcqrsEventHandler</servlet-name>
    <url-pattern>/worker/kcqrs</url-pattern>
  </servlet-mapping>

  <security-constraint>
    <web-resource-collection>
      <web-resource-name>worker</web-resource-name>
      <url-pattern>/worker/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
      <role-name>admin</role-name>
    </auth-constraint>
  </security-constraint>
// Initialize KCQRS for GAE
private var cqrs = AppEngineKcqrs.Builder(configuration, messageFormatFactory).build {
    kind = "Event"
    kcqrsHandlerEndpoint = "/worker/kcqrs"
    identityProvider = IdentityProvider.Default()
    requestInitializer = HttpRequestInitializer { 
        it.headers.setAuthorization("Bearer ${securityProvider.getAuthorizationToken()}")
    }
} 

// Accessing message bus
cqrs.messageBus()
// Accessing repository 
cqrs.repository()

Maven dependency

    <dependency>
      <groupId>com.clouway.kcqrs</groupId>
      <artifactId>kcqrs-core</artifactId>
      <version>0.0.1</version>
    </dependency>
    
   <dependency>
      <groupId>com.clouway.kcqrs.adapters</groupId>
      <artifactId>kcqrs-appengine</artifactId>
      <version>0.0.1</version>
    </dependency>

TODO

  • MongoDB Adapter

License

Copyright 2018 clouWay ood.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

com.clouway.kcqrs

clouWay ood

Версии библиотеки

Версия
0.0.1
v1.3.0