Spring One live blog – state or events

State or Events?
Speaker: Kenny Bastani & Jakub Pilimon

For more posts from Spring One 2017, see the Spring One blog table of contents

Live coding

  • Showed Groovy code using Spock for tests. [didn’t actually mention it was Spock, but easy enough to recognize]
  • Wrote tests first for a CreditCard object [nice advantage of tests in Groovy. The methods don’t need to exist and you don’t get red errors as you type so not tempted to write the methods]
  • The CreditCard implementation was logical with instance variables.
  • Noted that the method to assign the limit is a command with an invariant.
  • CQS – command query segregation

Refactoring to events

  • Create domain events
    • For name, pick a past tense description of what it does. For example “limitAssigned”. Used same name for method and domain event object (which is passed to method as parameter).
    • Add timestamp to event so know order. That way can determine current state of object.
    • Also need former instance variables (limit) and parameters (amount) as attributes of the new domain event.
    • Similarly refactored to have withdraw(CardWithdraw c) and repay(CardRepay c)
  • Keep track of changes and store
    • Dirty context – when change object but haven’t saved yet.
    • Add instance variable for list of domain events. This keeps track of the pending/dirty events
  • Create Repository object
    • instance variable with map of unique credit card ids to a list of domain events
    • method to save to map
    • method to load credit cards by replaying all events. Used left fold. “It’s so fancy that Java 8 doesn’t support it out of the box”. Used javaslang.collection.List API to get it.
  • Update CreditCard three handler methods to return a references to “this” so can chain
  • Added Kafka so could store events (via Topics)
    • Added spring-kafka and kafka-streams to pom
    • Created config class with @EnableKafka
    • Update save method to use Kafka class
  • Then switched to Kafka streams
    • @EnableKafkaStreams
    • Created new bean for stream configuration
    • return KTable – key table – aggregate of events from object
  • Create controller
    • method to get list of credit cards
  • Created scheduler for testing a bunch of credit card transactions

[this was a full house; I barely got a seat. Pivotal handed that well. They found all the empty seats and directed people do them. Even so, there are a good number of people in the back.]

Leave a Reply

Your email address will not be published. Required fields are marked *