Upgrade to Pro — share decks privately, control downloads, hide ads and more …

EventSourcing & CQRS

EventSourcing & CQRS

Avatar for TEBOURBI Maher

TEBOURBI Maher

June 13, 2013

More Decks by TEBOURBI Maher

Other Decks in Technology

Transcript

  1. Producer 1 2 3 4 5 6 7 8 9

    10 11 12 Handler 1 Handler n Handler 2 Consumer Thursday, June 27, 13
  2. Input Buffer Output Buffer Business Logic Event Business Processor Évènements

    d'entrée • Domain Model • Logique métier • Consumers Évènements de sortie: - persistence - appels WS externes Business Processor - Intro Thursday, June 27, 13
  3. @Immutable public  class  RawDatapointEvent  {      private  final  String

     sensorId;      private  final  DateTime  measureTime;      private  final  double  value;      public  RawDatapointEvent(String  sensorId,  DateTime  measureTime,  double  value)  {            this.sensorId  =  sensorId;            this.measureTime  =  measureTime;            this.value  =  value;      }      public  String  getSensorId()  {  return  sensorId;  }      public  DateTime  getMeasureTime()  {  return  measureTime;  }      public  double  getValue()  {  return  value;  } } Business Processor - Évènement d'entrée Thursday, June 27, 13
  4. public  class  RawDatapointEventConsumer  implements  EventConsumer<RawDatapointEvent>  {      @Inject  

       DatapointPreprocessingService  preprocessingService;      @Override      public  void  consume(RawDatapointEvent  rawEvent,                                              Model  model,                                              EventProcessor  eventProcessor)  {            PreprocessingResult  result  =  preprocessingService.preprocess(model,  rawEvent);            if  (result.hasError())  {                  eventProcessor.sendReply(new  FailedDatapointResult(result.getErrorMsg()));            }            eventProcessor.sendReply(new  SuccessfulDatapointResult());            eventProcessor.process(                  new  AddDatapointToTimeSeriesEvent(result.getAccount(),                                                                                      result.getSensor().getTimeSeries(),                                                                                      result.getDatapoint()                  )            );      } } Business Processor - Consumer Thursday, June 27, 13
  5. @Immutable public  class  RawDatapointEvent  {      //  fields,  constructor,

     getters...      public  interface  Result  {}      public  static  class  SuccessfulDatapointResult  implements  Result  {}      public  static  class  FailedDatapointResult  implements  Result  {                private  final  String  errorMessage;                public  FailedDatapointResult(String  errorMessage)  {                      this.errorMessage  =  errorMessage;              }                public  String  getErrorMessage()  {                      return  errorMessage;              }      } } Business Processor - Évènement de résultat Thursday, June 27, 13
  6. RawDatapointEvent  rawEvent  =  createRawEventFromJson(httpRequest.getContent());   ListenableFuture<RawDatapointEvent.Result>  future  =  client.sendEvent(rawEvent);  

    Futures.addCallback(future,  new  FutureCallback<RawDatapointEvent.Result>()  {      @Override      public  void  onSuccess(RawDatapointEvent.Result  result)  {              ...      }        @Override      public  void  onFailure(Throwable  t)  {              ...      } }); Business Processor - Code client Thursday, June 27, 13
  7. In-Memory Model - Data structures ArrayList HashMap HashSet BiMap Multimap

    ConcurrentHashMap CopyOnWriteArrayList ReentrantLock Thursday, June 27, 13
  8. @Immutable public  final  class  Money  {        //

     I  swear,  those  are  final!      private  BigDecimal  amount;      private  Currency  currency;      //  For  Hibernate  only!      Money()  {}      public  Money(BigDecimal  amount,  Currency  currency)  {            this.amount  =  amount;            this.currency  =  currency;      }      public  BigDecimal  getAmount()  {  return  amount;  }      public  Currency  getCurrency()  {  return  currency;  }      //  For  Hibernate  only!      void  setAmount(BigDecimal  amount)  {  this.amount  =  amount;  }      void  setCurrency(Currency  currency)  {  this.currency  =  currency;  } } In-Memory Model - Immutability Thursday, June 27, 13
  9. @Immutable public  final  class  Money  {        private

     final  BigDecimal  amount;        private  final  Currency  currency;        public  Money(BigDecimal  amount,  Currency  currency)  {              this.amount  =  amount;              this.currency  =  currency;      }        public  BigDecimal  getAmount()  {              return  amount;      }        public  Currency  getCurrency()  {              return  currency;      }   } In-Memory Model - Immutability Thursday, June 27, 13
  10. public  class  UserManager  {        private  final  Map<String,

     User>  users  =  Maps.newHashMap();        public  void  add(@Nonnull  User  user)  {              users.put(user.primaryKey(),  user);      }        public  void  remove(@Nonnull  String  userId)  {              User  removedUser  =  users.remove(userId);              if  (removedUser  ==  null)  throw  new  UserNotFoundException(userId);        }        public  User  getById(@Nonnull  String  userId)  {              User  user  =  users.get(userId);              if  (user  ==  null)  throw  new  UserNotFoundException(userId);              return  user;      }        public  List<User>  getAll()  {              return  ImmutableList.copyOf(users.values());      }   } In-Memory Model - Managers Thursday, June 27, 13
  11. Snapshots - Migration Snapshot V4 Snapshot V5 Messages Protobuf V5

    Messages Protobuf V4 Thursday, June 27, 13
  12. Data BV1 BV2 BV3 RTV2 RTV3 Event Source Server Speed

    Layer Batch Layer Event V2 Thursday, June 27, 13