{"id":338574,"date":"2022-09-21T15:00:13","date_gmt":"2022-09-21T15:00:13","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=338574"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=338574","title":{"rendered":"<span>Event sourcing+CQRS \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Spring<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0448\u0435\u043b \u043a\u0430\u043a-\u0442\u043e \u043d\u0430 \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442, \u0430 \u0442\u0430\u043c Event sourcing+CQRS. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043b \u043a\u0430\u043a \u044d\u0442\u0430 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u0430 <a href=\"https:\/\/habr.com\/ru\/post\/146429\/\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a> \u0438 <a href=\"https:\/\/habr.com\/ru\/post\/149464\/\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a> \u0438 \u043e\u0447\u0435\u043d\u044c \u0432\u0434\u043e\u0445\u043d\u043e\u0432\u0438\u043b\u0441\u044f. \u041d\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0431\u044b\u043b\u0438 \u0434\u0436\u0430\u0432\u0430 \u0438 \u0441\u043f\u0440\u0438\u043d\u0433\u043e\u043c, \u043d\u043e \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u043b\u0430\u0441\u044c \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u0440\u0443\u0447\u043d\u0443\u044e.<\/p>\n<p>\u041a\u0430\u043a \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u044d\u0442\u043e \u0445\u043e\u0440\u043e\u0448\u043e. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u043e\u0431\u0440\u0430\u0442\u044c handler\u2019\u044b \u0438 applier\u2019\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0445 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0439.<\/p>\n<p>\u0417\u0430\u0434\u0430\u0447\u0430 &#8212; \u043c\u043e\u0434\u0443\u043b\u044c \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u0430 \u0434\u0435\u043d\u0435\u0433 \u0437\u0430 \u043f\u0438\u0446\u0446\u0443. \u041a\u0442\u043e-\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043a\u0443\u043f\u0438\u0442\u044c \u043f\u0438\u0446\u0446\u0443, \u043a\u0442\u043e-\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u044a\u0435\u0441\u0442\u044c \u043a\u0443\u0441\u043e\u0447\u0435\u043a \u043f\u0438\u0446\u0446\u044b. \u041a\u0443\u043f\u0438\u0432\u0448\u0438\u0439 \u0441\u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438 \u043a\u0442\u043e \u0435\u043c\u0443 \u0434\u043e\u043b\u0436\u0435\u043d, \u0430 \u0434\u043e\u043b\u0436\u043d\u0438\u043a\u0438 \u043c\u043e\u0433\u0443\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u0435\u0440\u043d\u0443\u043b\u0438 \u0435\u043c\u0443 \u0434\u0435\u043d\u044c\u0433\u0438.<\/p>\n<h2>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/h2>\n<p>\u041f\u0440\u043e\u0435\u043a\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0432 \u0434\u0432\u0443\u0445 \u043c\u043e\u0434\u0443\u043b\u044f\u0445. \u041c\u043e\u0434\u0443\u043b\u044c event-sourcing \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u0430 handler\u2019\u043e\u0432 \u0438 applier\u2019\u043e\u0432. \u0410 \u0432 \u043c\u043e\u0434\u0443\u043b\u0435  api \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u044d\u0442\u0443 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443. \u041d\u0443 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0431\u0443\u0434\u0435\u043c \u0432 \u0442\u0435\u0441\u0442\u0435. \u0421\u0440\u0430\u0437\u0443 \u0432\u043e\u0442 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0430 <a href=\"https:\/\/github.com\/AVTarasov210\/es_cqrs\" rel=\"noopener noreferrer nofollow\">\u0433\u0438\u0442\u0435<\/a><\/p>\n<h2>Wright \u043c\u043e\u0434\u0435\u043b\u044c<\/h2>\n<p>\u0412 \u0440\u0430\u043c\u043a\u0430\u0445 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043e\u0431\u044b\u0442\u0438\u0439. \u0417\u0430 \u0441\u0447\u0435\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0438 \u043a\u043e\u043c\u0430\u043d\u0434 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e \u0431\u0438\u0437\u043d\u0435\u0441-\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c. \u041a\u043e\u043c\u0430\u043d\u0434\u0430 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u044f. \u041a\u043e\u043c\u0430\u043d\u0434\u0430 \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u0443\u0435\u0442 \u043a\u0430\u043a\u0443\u044e-\u043b\u0438\u0431\u043e \u043b\u043e\u0433\u0438\u043a\u0443, \u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 &#8212; \u044d\u0442\u043e \u043a\u043e\u043c\u0430\u043d\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438 \u0432\u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0438 \u043f\u043e \u0441\u043f\u0438\u0441\u043a\u0443 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432 \u043c\u043e\u0434\u0443\u043b\u0435 event-sourcing. \u041e\u0442 \u043d\u0435\u0433\u043e \u043e \u0431\u0443\u0434\u0443\u0442 \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u0441\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u044b\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b. \u0410 \u043a\u043b\u0430\u0441\u0441 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u0442\u044c\u0441\u044f \u0432 \u043c\u043e\u0434\u0443\u043b\u0435 api. \u042d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043b\u043e\u0433\u0438\u043a\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u043e\u0439. \u041a\u043b\u0430\u0441\u0441 <code>Event<\/code> \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0438 id \u0441\u043e\u0431\u044b\u0442\u0438\u044f.<\/p>\n<pre><code>@SuperBuilder @AllArgsConstructor @NoArgsConstructor @Data public abstract class Event implements Command {     private UUID id;     private Long eventId;     @Builder.Default     private Instant createAt = Instant.now(); }  <\/code><\/pre>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a\u0443\u044e \u043f\u0438\u0446\u0446\u0443 \u043a\u0443\u043f\u0438\u043b \u0447\u0435\u043b\u043e\u0432\u0435\u043a. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0432\u0435\u043d\u0442 <code>BuyPizza<\/code> \u0441 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u043e \u0442\u043e\u043c \u043a\u0442\u043e \u043a\u0443\u043f\u0438\u043b, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0438\u0446\u0446\u0430 \u0441\u0442\u043e\u0438\u043b\u0430 \u0438 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u0443\u0441\u043a\u043e\u0432 \u0432 \u043d\u0435\u0439.<\/p>\n<pre><code>@Data @SuperBuilder public class BuyPizza extends Event {     private Integer countOfPieces;     private Integer price;     private String username; }  <\/code><\/pre>\n<p>\u0422\u0430\u043a \u0436\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u044f. \u041f\u0443\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043b\u0438\u0441\u0442 \u043f\u043e\u0440\u043e\u0436\u0434\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u043c\u0430\u043d\u0434.  \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c, \u0438 \u0443\u0432\u0435\u0434\u043e\u043c\u0438\u043c \u0432\u0441\u0435\u0445 \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u0438\u0446\u0446\u0430 \u043a\u0443\u043f\u043b\u0435\u043d\u0430. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u0435\u0440\u043d\u0435\u043c \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 <code>SendPizzaNotification<\/code> \u0438 \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u0442\u043e\u0436\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a.<\/p>\n<pre><code>@Handler public List&lt;Command> handle(BuyPizza command){         if (command.getUsername()!=null &amp;&amp; command.getUsername().isBlank())             throw new RuntimeException(\"\u0438\u043c\u044f \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043e\");         if (command.getCountOfPieces() == null || command.getCountOfPieces() &lt;= 0)             throw new RuntimeException(\"\u043a\u0443\u0441\u043e\u0447\u043a\u043e\u0432 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u0431\u044b\u0442\u044c\");         if (command.getPrice() == null || command.getPrice() &lt;= 0)             throw new RuntimeException(\"\u043f\u0438\u0446\u0446\u0430 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0447\u0435\u0433\u043e-\u0442\u043e \u0441\u0442\u043e\u0438\u0442\");         log.info(\"\u041c\u0438\u0441\u0442\u0435\u0440 {} \u043a\u0443\u043f\u0438\u043b {} \u043a\u0443\u0441\u043a\u043e\u0432 \u043f\u0438\u0446\u0446\u044b \u0437\u0430 {} \u0434\u0435\u043d\u0435\u0433\",                 command.getUsername(),                 command.getCountOfPieces(),                 command.getPrice());         return of(SendPizzaNotification.builder()                 .username(command.getUsername())                 .build()); }  @Handler public List&lt;Command> handle(SendPizzaNotification command) {  log.info(\"\u041f\u0438\u0446\u0446\u0430 \u043f\u0438\u0446\u0446\u0430 \u0443 {} \u043f\u0438\u0446\u0446\u0430!!!\", command.getUsername());  return Collections.emptyList(); }  <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u0434\u0430\u0447\u0430, \u043a\u0430\u043a \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 handler\u2019\u044b \u0438 \u043f\u0440\u0438 \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0435 \u0435\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0441\u0435\u0440\u0432\u0438\u0441. \u0412 \u043d\u0435\u043c \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0434\u043b\u044f \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u0445 \u043a\u043e\u043c\u0430\u043d\u0434 \u0438 \u0434\u043b\u044f \u043d\u0435\u0443\u0441\u043f\u0435\u0448\u043d\u043e. \u0422\u0430\u043a\u0436\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043f\u043e\u0441\u043e\u0431 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u044b\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432. \u0412 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>addCommandHandler<\/code> \u0431\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0438\u0439 \u0432 \u0441\u0435\u0431\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u0443.<\/p>\n<pre><code>public interface Publisher {     void publish(Command message);     void publish(List&lt;Command> proceed);     void fail(Command message);     void fail(List&lt;Command> proceed);     void addCommandHandler(Class type, Consumer&lt;Command> consumer); }  <\/code><\/pre>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u0431\u0443\u0434\u0435\u0442 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0430 \u043d\u0430 \u043c\u0430\u043f\u0435: \u043a\u043b\u0430\u0441\u0441 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043f\u0440\u043e\u0442\u0438\u0432 \u0441\u043f\u0438\u0441\u043a\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432. \u041f\u0440\u0438 \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u0435\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0438 \u043f\u043e \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u0438\u0445 \u043a \u043a\u043e\u043c\u0430\u043d\u0434\u0435.<\/p>\n<pre><code>@Component @RequiredArgsConstructor public class PublisherImpl implements Publisher {      private final Map&lt;Class&lt;? extends Command>, Collection&lt;Consumer&lt;Command>>> consumerHandlerMap = new HashMap&lt;>();      private final CommandProcessor commandProcessor;      @Override     public void publish(Command command) {         if (command == null) return;         Collection&lt;Consumer&lt;Command>> handlers = consumerHandlerMap.get(command.getClass());         if (handlers != null &amp;&amp; !handlers.isEmpty()){             Command processedCommand = commandProcessor.processCommand(command);             handlers.forEach(x -> x.accept(processedCommand));         } else {             throw new IllegalStateException(format(\"No handlers exist for class %s\", command.getClass()));         }     } ...     @Override     public void addCommandHandler(Class type, Consumer&lt;Command> consumer) {         if (consumerHandlerMap.containsKey(type)) {             consumerHandlerMap.get(type).add(consumer);         } else {             List&lt;Consumer&lt;Command>> handlerList = new LinkedList();             handlerList.add(consumer);             consumerHandlerMap.put(type, handlerList);         }     } }  <\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u043f\u0438\u0448\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043a\u043e\u043c\u0430\u043d\u0434, \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0438\u0445 \u043d\u0430\u0434\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c. \u042d\u0442\u043e \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0432 \u043b\u0438\u0441\u0442\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>EventRepository<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043b\u0435\u043d\u044c \u043c\u043e\u0440\u043e\u0447\u0438\u0442\u044c\u0441\u044f \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code>@Component @RequiredArgsConstructor @Slf4j public class CommandProcessorImpl implements CommandProcessor {      private final EventRepository eventRepository;      @Override     public Command processCommand(Command command) {         if (command instanceof Event) {             eventRepository.save((Event)command);         }         return command;     }      @Override     public Command processFailure(Command command) {         log.info(\"\u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443\");         return command;     } }  <\/code><\/pre>\n<p>\u0412 \u0446\u0435\u043b\u043e\u043c \u043d\u0430 \u044d\u0442\u043e\u043c \u0441\u0430\u043c\u0430 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u043f\u0438\u0448\u0443\u0449\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0433\u043e\u0442\u043e\u0432\u0430, \u0438 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0430\u0431\u043b\u0438\u0448\u0435\u0440 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e. \u041a\u0430\u043a \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u043d\u043e \u0442\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u0445\u044d\u043d\u0434\u043b\u0435\u0440\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0435\u0433\u043e \u0432 \u043f\u0430\u0431\u043b\u0438\u0448\u0435\u0440 \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435, \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u043b\u0431\u0430\u0441\u0430 \u043a\u043e\u0434\u0430, \u0434\u0430 \u0438 \u0437\u0430\u0447\u0435\u043c, \u0435\u0441\u043b\u0438 \u0432\u0441\u0435\u043c \u044d\u0442\u0438\u043c \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c\u0441\u044f \u0441\u043f\u0440\u0438\u043d\u0433.<br \/> \u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435 \u0432\u0441\u0435 \u0445\u044d\u043d\u0434\u043b\u0435\u0440\u044b \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0435\u0439 <code>@Handler<\/code>. \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0431\u0438\u043d \u043f\u043e\u0441\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440, \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u0432\u0441\u0435 \u0445\u044d\u043d\u0434\u043b\u0435\u0440\u044b \u0438\u0437 \u0432\u0441\u0435\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u0438\u043c\u0438 publisher. \u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043f\u0430\u0434\u0430\u0435\u0442 \u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0434\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u043a\u0441\u0438.<\/p>\n<p>\u0412 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>postProcessBeforeInitialization<\/code> \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430. \u0421\u043e\u0431\u0435\u0440\u0435\u043c \u0432\u0441\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043e\u0442\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0435\u0439 <code>@Handler<\/code> \u0438 \u0437\u0430\u043f\u0438\u0448\u0435\u043c \u0438\u0445 \u0432 \u043c\u0430\u043f\u0443 \u043f\u0440\u043e\u0442\u0438\u0432 \u0438\u043c\u0435\u043d\u0438 \u0431\u0438\u043d\u0430. \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>postProcessAfterInitialization<\/code>. \u0412 \u043d\u0435\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u043e\u043a\u0441\u0438 \u0432\u0441\u0435\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432. \u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043a\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u0445\u044d\u043d\u0434\u043b\u0435\u0440\u044b \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u0445 \u0432 <code>Publisher<\/code>.<\/p>\n<p>\u0412\u0430\u0436\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441, \u043f\u043e\u0447\u0435\u043c\u0443 \u043d\u0430\u0434\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432\u0435\u0434\u044c \u043c\u043e\u0436\u043d\u043e \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u044b \u043d\u0430\u0448\u043b\u0438 \u043c\u0435\u0442\u043e\u0434 \u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435 \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u0435\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0432 <code>Publisher<\/code>. \u042d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432 \u043f\u0440\u043e\u043a\u0441\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438 \u0435\u0441\u043b\u0438 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0430 \u043d\u0435 \u043f\u0440\u043e\u043a\u0441\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0442\u0435\u0440\u044f\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u043f\u043e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 <code>@Transactional<\/code><\/p>\n<pre><code>@Component @RequiredArgsConstructor public class CommandHandlerBeanPostProcessor implements BeanPostProcessor {      private final Map&lt;String, List&lt;Method>> handlers = new HashMap&lt;>();     private final Publisher publisher;      @Override     public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {         List&lt;Method> handleMethods = Stream.of(bean.getClass().getMethods())                 .filter(x -> x.isAnnotationPresent(Handler.class))                 .collect(Collectors.toList());         if (!handleMethods.isEmpty()){             handlers.put(beanName, handleMethods);         }         return bean;     }      @Override     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {         if (handlers.containsKey(beanName)) {             handlers.get(beanName).forEach(x -> addWithInvoke(bean, x));         }         return bean;     }      private void addWithInvoke(@NonNull Object object, @NonNull Method handleMethod) {         Class[] classes = handleMethod.getParameterTypes();         if (classes.length != 1) throw new RuntimeException(\"handler mast contains in params only Command\");         publisher.addCommandHandler(classes[0], a -> invoke(object, handleMethod, a));     }      @SneakyThrows     private void invoke(@NonNull Object object, @NonNull Method handleMethod, Object param)     {         handleMethod.invoke(object, param);     }  } <\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0443\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>Publisher<\/code>. \u0418\u043d\u0436\u0435\u043a\u0442\u0438\u043c \u0435\u0433\u043e \u0433\u0434\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u0435\u0433\u043e \u043f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u044b. \u0412 \u0442\u0430\u043a\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u043d\u0435\u0437\u0430\u0447\u0435\u043c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043d\u0438\u0447\u0435\u0433\u043e \u0438\u0437 \u0445\u044d\u043d\u0434\u043b\u0435\u0440\u0430. \u041f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u0432 \u043a\u043e\u043d\u0446\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043b\u043e\u0433\u0438\u043a\u0438, \u0442\u0430\u043a \u043a\u0430\u043a \u0435\u0441\u043b\u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430, \u0442\u043e \u043d\u0438 \u043e\u0434\u043d\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u043b\u0430 \u0431\u044b\u0442\u044c \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043d\u0430. \u041f\u0443\u0441\u0442\u044c \u0432\u0441\u0435 \u043f\u043e\u0440\u043e\u0436\u0434\u0435\u043d\u043d\u044b\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0431\u0443\u0434\u0443\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u0438\u0437 \u0445\u044d\u043d\u0434\u043b\u0435\u0440\u0430 \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c\u0441\u044f.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u0434\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0430\u0441\u043f\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u043a\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u044b. \u0415\u0441\u043b\u0438 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u0445\u044d\u043d\u0434\u043b\u0435\u0440\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043e\u0448\u0438\u0431\u043a\u0438 \u0443 Publisher\u2019\u0430.<\/p>\n<pre><code>@Aspect @Component public class HandlerContextAspect {      @Autowired     private Publisher publisher;      @Pointcut(value = \"@annotation(ru.abdyabdya.es_cqrs.annotations.Handler)\")     public void callAtHandlerAnnotation() { }      @Around(value = \"callAtHandlerAnnotation()\")     @SneakyThrows     public Object aroundCallAt(ProceedingJoinPoint pjp) {         List&lt;Command> retVal = null;         try {             retVal = (List&lt;Command>)pjp.proceed();             publisher.publish(retVal);         } catch (CommandException commandException) {             publisher.fail(commandException.getCommand());         }         return retVal;     } } <\/code><\/pre>\n<p>\u041d\u0430 \u0441 \u043f\u0438\u0448\u0443\u0449\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e \u043c\u044b \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u043c. \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0435\u0449\u0435 \u043f\u0430\u0440\u0443 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0438 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u043e\u0432 \u0434\u043b\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 \u0434\u0435\u043d\u0435\u0433 \u0438 \u0441\u044a\u0435\u0434\u0430\u043d\u0438\u044f \u043a\u0443\u0441\u043e\u0447\u043a\u0430 \u043f\u0438\u0446\u0446\u044b \u0438 \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u044d\u0442\u0430\u043f\u0443.<\/p>\n<pre><code>@Data public class GiveMoneyBack extends Event {     private Integer amountOfMoney;     private String username;     private String targetUser; }  @Data @AllArgsConstructor @NoArgsConstructor public class TakePizzaPieces extends Event {    private String name; } @Handler public List&lt;Command> handle(TakePizzaPiece command) {      if (pizzaService.checkPieces(command.getEventId())) {         return of(PieceTaken.builder() .eventId(command.getEventId()) .username(command.getUsername()) .build());     } else {         log.info(\"\u0430 \u043a\u0443\u0441\u043e\u0447\u0435\u043a \u0432\u0437\u044f\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f\");         return null;     } }  @Handler public List&lt;Command> handle(GiveMoneyBack command) {     log.info(\"{} \u0432\u0435\u0440\u043d\u0443\u043b {}\", command.getUsername(), command.getAmountOfMoney());     return Collections.emptyList(); }  <\/code><\/pre>\n<h2>Read \u043c\u043e\u0434\u0435\u043b\u044c<\/h2>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c, \u043a\u0430\u043a\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u044b. \u041a\u0430\u043a \u0432\u044b \u0432\u0438\u0434\u0435\u043b\u0438, \u0434\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>canTakeOnePiece<\/code>. \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u043e\u043d\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043e\u0441\u0442\u0430\u043b\u0438\u0441\u044c \u043b\u0438 \u043a\u0443\u0441\u043e\u0447\u043a\u0438 \u043f\u0438\u0446\u0446\u044b, \u043d\u043e \u0434\u043b\u044f \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0445\u0441\u044f \u043a\u0443\u0441\u043e\u0447\u043a\u043e\u0432. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438 \u0438\u0432\u0435\u043d\u0442\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430 \u043d\u0435\u0433\u043e \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u0432\u043b\u0438\u044f\u0442\u044c \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043d\u043e\u0432\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435. \u041f\u0443\u0441\u0442\u044c \u043d\u0430 \u043f\u043e\u043a\u0443\u043f\u043a\u0443 \u043f\u0438\u0446\u0446\u044b \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0435\u0435 \u043a\u0443\u0441\u043e\u0447\u043a\u043e\u0432 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a \u0442\u0435\u043c, \u0447\u0442\u043e \u0431\u044b\u043b\u0438, \u0430 \u043f\u0440\u0438 \u043f\u043e\u0435\u0434\u0430\u043d\u0438\u0438 \u043a\u0443\u0441\u043e\u0447\u043a\u0430 \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u044d\u0442\u043e \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 <code>PizzaApplier<\/code>.<\/p>\n<pre><code>@Applier public class PizzaApplier extends ApplierContainer&lt;Integer> {    @Applier    public Integer apply(Integer countOfPieces, BuyPizza command){        if (countOfPieces==null) countOfPieces = 0l;        return countOfPieces + command.getCountOfPieces();    }    @Applier    public Integer apply(Integer countOfPieces, PieceTaken command){        return countOfPieces - 1;    } }  <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u044b\u043d\u044e \u0438\u0437 ifelse \u043d\u0430\u0434\u043e \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u0430\u043f\u043f\u043b\u0430\u0435\u0440\u044b \u0432 \u043c\u0430\u043f\u0443 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u0440\u043e\u0442\u0438\u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u044b, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043e\u043d \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442. \u041f\u0443\u0441\u0442\u044c \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 ApplierContainer. \u0412 \u043d\u0435\u0433\u043e \u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0438 \u043b\u043e\u0433\u0438\u043a\u0443 \u0441\u0431\u043e\u0440\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043f\u043e \u043a\u043e\u043c\u0430\u043d\u0434\u0430\u043c. \u0414\u0436\u0435\u043d\u0435\u0440\u0438\u043a\u043e\u043c \u0437\u0434\u0435\u0441\u044c \u0431\u0443\u0434\u0435\u0442 \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u0441\u043e\u0431\u044b\u0442\u0438\u0439. \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 Integer. \u0415\u0449\u0435 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u0430\u043f\u043f\u043b\u0430\u0435\u0440\u043e\u0432.<\/p>\n<pre><code>public abstract class ApplierContainer&lt;T> {      private Map &lt;Class, ApplierFunction&lt;T>> classConsumerMap = new HashMap &lt;>();      public T apply(T object, List &lt;? extends Command> commands){         for (Command command: commands) {             if (classConsumerMap.containsKey(command.getClass())) {                 object = classConsumerMap.get(command.getClass()).apply(object, command);             }         }         return object;     }      public T apply(List &lt;? extends Command> commands){         return apply(null, commands);     }      public void add(Class cls, ApplierFunction&lt;T> applierFunction){         classConsumerMap.put(cls, applierFunction);     }  }  public interface ApplierFunction&lt;T> {    public T apply(T object, Command event); } <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043a\u0430\u043a-\u0442\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u043a\u043b\u0430\u0441\u0441. \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0431\u0438\u043d \u043f\u043e\u0441\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440. \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u0432\u0441\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u0430 \u043f\u043e\u0441\u043b\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u044d\u0442\u0438 \u0436\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043d\u043e \u0443\u0436\u0435 \u0438\u0437 \u043f\u0440\u043e\u043a\u0441\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u0431\u0443\u0434\u0435\u043c \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u0431\u0438\u043d \u043f\u043e-\u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<pre><code>@Component public class CommandApplierBeanPostProcessor implements BeanPostProcessor {     private final Map&lt;String, List&lt;Method>> appliersNames = new HashMap&lt;>();      @Override     public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {         if (bean instanceof ApplierContainer) {             List&lt;Method> applierMethods = Stream.of(bean.getClass().getMethods())                     .filter(x -> x.isAnnotationPresent(Applier.class))                     .collect(Collectors.toList());             appliersNames.put(beanName, applierMethods);         }         return bean;     }      @Override     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {         if (appliersNames.containsKey(beanName)) {             if (!appliersNames.get(beanName).isEmpty()) {                 for (Method method : appliersNames.get(beanName)) {                     ((ApplierContainer&lt;?>) bean).add(                             method.getParameterTypes()[1],                             getApplierFunction(bean, method));                 }             }         }         return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);     }      private ApplierFunction getApplierFunction(@NonNull Object object, @NonNull Method applyMethod) {         return (obj, command) -> invokeApplierFunction(object, applyMethod, obj, command);     }      @SneakyThrows     private Object invokeApplierFunction(             Object bean,             Method applyMethod,             Object processingObject,             Command command) {         return applyMethod.invoke(bean, processingObject, command);     }  } <\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0430\u043f\u043f\u043b\u0430\u0435\u0440\u043e\u0432 \u0437\u0430\u043a\u043e\u043d\u0447\u0435\u043d\u0430. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043a\u0443\u0441\u043e\u0447\u043a\u043e\u0432 \u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 PizzaDto, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043f\u0438\u0448\u0435\u043c \u0438\u043c\u044f \u043a\u0443\u043f\u0438\u0432\u0448\u0435\u0433\u043e \u043f\u0438\u0446\u0446\u0443, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0445\u0441\u044f \u043a\u0443\u0441\u043a\u043e\u0432 \u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0438\u0446\u0446\u044b \u0438 \u043c\u0430\u043f\u0443 \u0434\u043e\u043b\u0436\u043d\u0438\u043a\u043e\u0432 \u0441 \u0434\u043e\u043b\u0433\u0430\u043c\u0438. \u041d\u0443 \u0438 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0430\u043f\u043f\u043b\u0430\u0435\u0440\u044b \u0434\u043b\u044f \u043d\u0435\u0433\u043e.<\/p>\n<pre><code>@Service @RequiredArgsConstructor public class PizzaService {     private final PizzaPieceApplier pizzaPieceApplier;     private final PizzaApplier pizzaApplier;     private final EventRepository eventRepository;      public boolean checkPieces(Long id){         return pizzaPieceApplier.apply(eventRepository.findById(id)) > 0;     }      public PizzaDto getById(Long id){         return pizzaApplier.apply(id);     } } <\/code><\/pre>\n<pre><code>@Applier public class PizzaPieceApplier extends ApplierContainer&lt;Integer> {      @Applier     public Integer apply(Integer countOfPieces, BuyPizza command){         if (countOfPieces==null) countOfPieces = 0;         return countOfPieces + command.getCountOfPieces();     }      @Applier     public Integer apply(Integer countOfPieces, PieceTaken command){         return countOfPieces - 1;     } }    @Applier @RequiredArgsConstructor public class PizzaApplier extends SnapshotApplierContainer&lt;PizzaDto, Long> {      private final PizzaRepository pizzaRepository;      @Applier     public PizzaDto apply(PizzaDto pizzaDto, BuyPizza command){         if (pizzaDto==null) pizzaDto = new PizzaDto();         if (pizzaDto.getUsername()!=null &amp;&amp; !pizzaDto.getUsername().equals(command.getUsername())){             throw new RuntimeException(\"\u0440\u0430\u0437\u043d\u044b\u0435 \u043b\u044e\u0434\u0438 \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043a\u0443\u043f\u0430\u0442\u044c \u043f\u0438\u0446\u0446\u0443 \u0441 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c\u0438 \u0430\u0439\u0434\u0438\");         }         pizzaDto.setId(command.getEventId());         pizzaDto.setLastDate(command.getCreateAt());         pizzaDto.setUsername(command.getUsername());         pizzaDto.setPiecePrice(command.getPrice()\/command.getCountOfPieces());         pizzaDto.setPrice(pizzaDto.getPrice()+command.getPrice());         pizzaDto.setPieceCount(pizzaDto.getPieceCount()+command.getCountOfPieces());          return pizzaDto;     }      @Applier     public PizzaDto apply(PizzaDto pizzaDto, PieceTaken command){         if (pizzaDto.getBorrowers().containsKey(command.getUsername())){             pizzaDto.getBorrowers().put(                     command.getUsername(),                     pizzaDto.getBorrowers().get(command.getUsername())+pizzaDto.getPiecePrice());         } else {             pizzaDto.getBorrowers().put(command.getUsername(), pizzaDto.getPiecePrice());         }         pizzaDto.setLastDate(command.getCreateAt());         return pizzaDto;     }      @Override     protected PizzaDto getApplyingObjectById(Long id) {         return pizzaRepository.findById(id);     }      @Override     protected void saveApplyingObject(PizzaDto applyingObject) {         pizzaRepository.save(applyingObject);     } } <\/code><\/pre>\n<p>\u041d\u0443 \u0438 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0442\u0435\u0441\u0442 \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0447\u0442\u043e \u0447\u0442\u043e-\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442<\/p>\n<pre><code>@SpringBootTest class EsCqrsApplicationTests {    @Autowired    private Publisher publisher;    @Autowired    private PizzaService pizzaService;    @Test    void contextLoads() {                publisher.publish(BuyPizza.builder()                 .eventId(1l)                 .price(100)                 .countOfPieces(2)                 .username(\"john\").build());         publisher.publish(TakePizzaPiece.builder().eventId(1l).username(\"jack\").build());         publisher.publish(TakePizzaPiece.builder().eventId(1l).username(\"jim\").build());         publisher.publish(TakePizzaPiece.builder().eventId(1l).username(\"jack\").build());    } }  <\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0442\u0430\u043a\u0438\u043c<\/p>\n<pre><code>2022-09-03 00:08:31.309  INFO 5764 --- [    Test worker] r.a.es_cqrs.handlers.PizzaHandler        : \u041c\u0438\u0441\u0442\u0435\u0440 john \u043a\u0443\u043f\u0438\u043b 2 \u043a\u0443\u0441\u043a\u043e\u0432 \u043f\u0438\u0446\u0446\u044b \u0437\u0430 100 \u0434\u0435\u043d\u0435\u0433 2022-09-03 00:08:31.312  INFO 5764 --- [    Test worker] r.a.es_cqrs.handlers.PizzaHandler        : \u041f\u0438\u0446\u0446\u0430 \u043f\u0438\u0446\u0446\u0430 \u0443 john \u043f\u0438\u0446\u0446\u0430!!! 2022-09-03 00:08:31.315  INFO 5764 --- [    Test worker] r.a.es_cqrs.handlers.PizzaHandler        : jack \u0432\u0437\u044f\u043b \u043a\u0443\u0441\u043e\u0447\u0435\u043a 2022-09-03 00:08:31.316  INFO 5764 --- [    Test worker] r.a.es_cqrs.handlers.PizzaHandler        : jim \u0432\u0437\u044f\u043b \u043a\u0443\u0441\u043e\u0447\u0435\u043a 2022-09-03 00:08:31.316  INFO 5764 --- [    Test worker] r.a.es_cqrs.handlers.PizzaHandler        : \u0430 \u043a\u0443\u0441\u043e\u0447\u0435\u043a \u0432\u0437\u044f\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f PizzaDto(id=1, lastDate=2022-09-02T21:08:31.316115Z, username=john, price=100, piecePrice=50, pieceCount=2, borrowers={jack=50, jim=50}) <\/code><\/pre>\n<h2>\u0421\u043d\u0430\u043f\u0448\u043e\u0442\u044b<\/h2>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u043e \u0447\u0435\u043c \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 &#8212; \u044d\u0442\u043e \u0441\u043d\u0430\u043f\u0448\u043e\u0442\u044b. \u042d\u0442\u043e \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0432 \u0440\u0438\u0434 \u043c\u043e\u0434\u0435\u043b\u0438. \u0412 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043c\u043e\u043c\u0435\u043d\u0442 \u043c\u043e\u0436\u0435\u0442 \u0442\u0430\u043a \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0438 \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u043f\u043e \u043d\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0442\u0440\u0430\u0442\u0438\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u0418\u043b\u0438 \u0435\u0441\u043b\u0438 \u0447\u0430\u0441\u0442\u043e \u0447\u0438\u0442\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f. \u0414\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u042d\u0442\u043e \u0438 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u043d\u0430\u043f\u0448\u043e\u0442\u043e\u043c.<\/p>\n<p>\u0423 \u0432\u0441\u0435\u0445 \u0441\u043d\u0430\u043f\u0448\u043e\u0442\u043e\u0432 \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0446\u0435\u043f\u043e\u0447\u043a\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043e\u043d \u0434\u043e \u044d\u0442\u043e\u0433\u043e \u0441\u0442\u0440\u043e\u0438\u043b\u0441\u044f \u0438 \u0434\u0430\u0442\u0430 \u0438 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043a \u043d\u0435\u043c\u0443 \u0431\u044b\u043b\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u043e.<\/p>\n<p>\u041f\u0443\u0441\u0442\u044c \u0432\u0441\u0435 \u0440\u0438\u0434 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 <code>ApplyingObject<\/code>, \u0433\u0434\u0435 I &#8212; \u0442\u0438\u043f \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430.<\/p>\n<pre><code>public interface ApplyingObject&lt;I> {    I getId();    Instant getLastDate(); } <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 <code>SnapshotApplierContainer<\/code> \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a \u043a\u043b\u0430\u0441\u0441\u0430 <code>ApplierContainer<\/code>. \u0415\u0449\u0435 \u0440\u0430\u0437 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>apply<\/code>. \u042d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0434\u043e\u043b\u0436\u0435\u043d \u0443\u043c\u0435\u0442\u044c \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u043f\u043e \u0435\u0433\u043e eventId. \u0412 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>apply<\/code> \u0431\u0443\u0434\u0435\u043c \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442, \u0434\u043e\u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u0435\u0433\u043e \u0434\u043e \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0435\u0433\u043e \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u0417\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0431\u0443\u0434\u0443\u0442 \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a\u0438 \u043a\u043b\u0430\u0441\u0441\u0430, \u0430 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>CommandService<\/code>.<\/p>\n<pre><code>@Component public abstract class SnapshotApplierContainer&lt;T extends ApplyingObject, I> extends ApplierContainer&lt;T> implements ApplicationListener&lt;TakeSnapshotApplicationEvent> {      @Autowired     private CommandService&lt;I> commandService;      protected abstract T getApplyingObjectById(I id);      protected abstract void saveApplyingObject(T applyingObject);      public T apply(I id) {         T applyingObject = getApplyingObjectById(id);         List&lt;? extends Command> commands = applyingObject == null ?                 commandService.getCommandsById(id) :                 commandService.getCommandsByIdAndDate(id, applyingObject.getLastDate());          if (!commands.isEmpty()) {             applyingObject = apply(applyingObject, commands);             saveApplyingObject(applyingObject);             return applyingObject;         } else {             return applyingObject;         }     } } <\/code><\/pre>\n<pre><code>public interface CommandService&lt;I> {     List&lt;? extends Command> getCommandsByIdAndDate(I id, Instant lastAppliedEventCreateDate);     List&lt;? extends Command> getCommandsById(I id); } <\/code><\/pre>\n<p>\u0418 \u0432\u043e\u0442 \u0443\u0436\u0435 \u043f\u0440\u0438 \u0447\u0442\u0435\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c\u0441\u044f. \u0422\u0435\u043f\u0435\u0440\u044c \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u0432\u0442\u043e\u0440\u043e\u0439 \u0440\u0430\u0437 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043a \u043d\u0435\u043c\u0443 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u0438 \u043c\u0435\u0436\u0434\u0443 \u0447\u0442\u0435\u043d\u0438\u044f\u043c\u0438. \u041d\u043e \u044d\u0442\u043e \u043d\u0430\u0441 \u043d\u0435 \u0441\u043f\u0430\u0441\u0435\u0442 \u043e\u0442 \u0441\u043b\u0443\u0447\u0430\u044f, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u043a\u043e\u043f\u0438\u043b\u043e\u0441\u044c 10\u041c \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0438 \u043a\u0442\u043e-\u0442\u043e \u0440\u0435\u0448\u0438\u043b \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 (\u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0431\u043e\u0433\u0430\u0447\u0438 \u043d\u0430\u043a\u0443\u043f\u0438\u043b \u043a\u0443\u0447\u0443 \u043f\u0438\u0446\u0446\u044b). \u0414\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043d\u0430\u043f\u0448\u043e\u0442\u0430 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u044f\u043c\u0438 \u0441\u043f\u0440\u0438\u043d\u0433\u0430. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u043e\u0431\u044b\u0442\u0438\u0435 <code>TakeSnapshotApplicationEvent<\/code>, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0435\u0435 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0446\u0435\u043f\u043e\u0447\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u0441\u043d\u0430\u043f\u0448\u043e\u0442\u0435.<\/p>\n<pre><code>public class TakeSnapshotApplicationEvent extends ApplicationEvent {     @Getter     private final Long id;      public TakeSnapshotApplicationEvent(Object source, Long id) {         super(source);         this.id = id;     } } <\/code><\/pre>\n<p>\u0412 \u0441\u0435\u0440\u0432\u0438\u0441\u0435 <code>SnapshotServiceImpl<\/code> \u0431\u0443\u0434\u0435\u043c \u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0438\u0432\u0435\u043d\u0442, \u0430 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0430\u043f\u043b\u0430\u0435\u0440\u0435 \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0435\u0433\u043e \u0438 \u043f\u043e id \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0441\u043d\u044d\u043f\u0448\u043e\u0442\u044b. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e SnapshotApplierContainer \u0434\u043e\u043b\u0436\u0435\u043d \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 <code>ApplicationListener<\/code>, \u0430 \u0437\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0438\u0432\u0435\u043d\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>onApplicationEvent<\/code><\/p>\n<pre><code>@Component @RequiredArgsConstructor public class SnapshotServiceImpl implements SnapshotService {      private final ApplicationEventPublisher applicationEventPublisher;      @Override     public void takeSnapshot(Long id) {         applicationEventPublisher.publishEvent(new TakeSnapshotApplicationEvent(this, id));     } } <\/code><\/pre>\n<pre><code> @Override     public void onApplicationEvent(TakeSnapshotApplicationEvent event) {         apply((I) event.getId());     } <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u043b\u043e\u0433\u0438\u043a\u0443 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043d\u0430\u043f\u0448\u043e\u0442\u043e\u0432, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0437 \u0432 \u0441\u0443\u0442\u043a\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0435\u0441\u0442\u044c \u043b\u0438 \u0446\u0435\u043f\u043e\u0447\u043a\u0438 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0447\u0435\u043c 20. \u041d\u043e \u0434\u043b\u044f \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 \u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0437\u043e\u0432 \u0432 \u0442\u0435\u0441\u0442\u0435.<\/p>\n<pre><code>@SpringBootTest class EsCqrsApplicationTests {     @Autowired    private Publisher publisher;    @Autowired    private PizzaService pizzaService;    @Autowired    private SnapshotService snapshotService;    @Autowired    private PizzaRepository pizzaRepository;    @Test    void contextLoads() {        publisher.publish(BuyPizza.builder()                .eventId(1l)                .price(100)                .countOfPieces(2)                .username(\"john\").build());        publisher.publish(TakePizzaPiece.builder().eventId(1l).username(\"jack\").build());  System.out.println(pizzaRepository.findById(1L)); snapshotService.takeSnapshot(1l); System.out.println(pizzaRepository.findById(1L));         publisher.publish(TakePizzaPiece.builder().eventId(1l).username(\"jim\").build());        publisher.publish(TakePizzaPiece.builder().eventId(1l).username(\"jack\").build());        System.out.println(pizzaService.getById(1l));    } } <\/code><\/pre>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u044c \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043b\u0435\u0433\u043a\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b Event sourcing + cqrs. \u041d\u0430 \u0440\u0430\u0431\u043e\u0447\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u043e\u043d \u043d\u0435 \u043f\u043e\u0434\u043e\u0448\u0435\u043b \u0431\u044b, \u0442\u0430\u043a \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0435\u0444\u043b\u0435\u043a\u0441\u0438\u0438 \u0437\u0430\u043c\u0435\u0434\u043b\u0438\u043b\u043e \u0431\u044b \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 \u043a\u043e\u043c\u0430\u043d\u0434\u0430\u043c\u0438, \u0430 \u0441\u0435\u0440\u0432\u0438\u0441 \u0432\u044b\u0441\u043e\u043a\u043e\u043d\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0439. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0437\u0430\u0434\u0430\u0447\u0430 \u043e\u043a\u0430\u0437\u0430\u043b\u0430\u0441\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0439 \u0438 \u043c\u043e\u0434\u0443\u043b\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0432 \u043d\u0435\u0432\u044b\u0441\u043e\u043a\u043e\u043d\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435.<br \/>\u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u0432\u043e\u0442 <a href=\"https:\/\/t.me\/avtarasov210\" rel=\"noopener noreferrer nofollow\">\u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c\u043c<\/a><\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/8de\/d1e\/058\/8ded1e058dd3f5a3c53675998efd8b32.png\" width=\"63\" height=\"63\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8de\/d1e\/058\/8ded1e058dd3f5a3c53675998efd8b32.png\"\/><figcaption><\/figcaption><\/figure>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/689468\/\"> https:\/\/habr.com\/ru\/post\/689468\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0448\u0435\u043b \u043a\u0430\u043a-\u0442\u043e \u043d\u0430 \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442, \u0430 \u0442\u0430\u043c Event sourcing+CQRS. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043b \u043a\u0430\u043a \u044d\u0442\u0430 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u0430 <a href=\"https:\/\/habr.com\/ru\/post\/146429\/\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a> \u0438 <a href=\"https:\/\/habr.com\/ru\/post\/149464\/\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a> \u0438 \u043e\u0447\u0435\u043d\u044c \u0432\u0434\u043e\u0445\u043d\u043e\u0432\u0438\u043b\u0441\u044f. \u041d\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0431\u044b\u043b\u0438 \u0434\u0436\u0430\u0432\u0430 \u0438 \u0441\u043f\u0440\u0438\u043d\u0433\u043e\u043c, \u043d\u043e \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u043b\u0430\u0441\u044c \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u0440\u0443\u0447\u043d\u0443\u044e.<\/p>\n<p>\u041a\u0430\u043a \u043c\u044b \u0437\u043d\u0430\u0435\u043c, \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u044d\u0442\u043e \u0445\u043e\u0440\u043e\u0448\u043e. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u043e\u0431\u0440\u0430\u0442\u044c handler\u2019\u044b \u0438 applier\u2019\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0445 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0439.<\/p>\n<p>\u0417\u0430\u0434\u0430\u0447\u0430 &#8212; \u043c\u043e\u0434\u0443\u043b\u044c \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u0430 \u0434\u0435\u043d\u0435\u0433 \u0437\u0430 \u043f\u0438\u0446\u0446\u0443. \u041a\u0442\u043e-\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043a\u0443\u043f\u0438\u0442\u044c \u043f\u0438\u0446\u0446\u0443, \u043a\u0442\u043e-\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u044a\u0435\u0441\u0442\u044c \u043a\u0443\u0441\u043e\u0447\u0435\u043a \u043f\u0438\u0446\u0446\u044b. \u041a\u0443\u043f\u0438\u0432\u0448\u0438\u0439 \u0441\u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438 \u043a\u0442\u043e \u0435\u043c\u0443 \u0434\u043e\u043b\u0436\u0435\u043d, \u0430 \u0434\u043e\u043b\u0436\u043d\u0438\u043a\u0438 \u043c\u043e\u0433\u0443\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u0435\u0440\u043d\u0443\u043b\u0438 \u0435\u043c\u0443 \u0434\u0435\u043d\u044c\u0433\u0438.<\/p>\n<h2>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/h2>\n<p>\u041f\u0440\u043e\u0435\u043a\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0432 \u0434\u0432\u0443\u0445 \u043c\u043e\u0434\u0443\u043b\u044f\u0445. \u041c\u043e\u0434\u0443\u043b\u044c event-sourcing \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u0430 handler\u2019\u043e\u0432 \u0438 applier\u2019\u043e\u0432. \u0410 \u0432 \u043c\u043e\u0434\u0443\u043b\u0435  api \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u044d\u0442\u0443 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443. \u041d\u0443 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0431\u0443\u0434\u0435\u043c \u0432 \u0442\u0435\u0441\u0442\u0435. \u0421\u0440\u0430\u0437\u0443 \u0432\u043e\u0442 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0430 <a href=\"https:\/\/github.com\/AVTarasov210\/es_cqrs\" rel=\"noopener noreferrer nofollow\">\u0433\u0438\u0442\u0435<\/a><\/p>\n<h2>Wright \u043c\u043e\u0434\u0435\u043b\u044c<\/h2>\n<p>\u0412 \u0440\u0430\u043c\u043a\u0430\u0445 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043e\u0431\u044b\u0442\u0438\u0439. \u0417\u0430 \u0441\u0447\u0435\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0438 \u043a\u043e\u043c\u0430\u043d\u0434 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e \u0431\u0438\u0437\u043d\u0435\u0441-\u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c. \u041a\u043e\u043c\u0430\u043d\u0434\u0430 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u044f. \u041a\u043e\u043c\u0430\u043d\u0434\u0430 \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u0443\u0435\u0442 \u043a\u0430\u043a\u0443\u044e-\u043b\u0438\u0431\u043e \u043b\u043e\u0433\u0438\u043a\u0443, \u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 &#8212; \u044d\u0442\u043e \u043a\u043e\u043c\u0430\u043d\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438 \u0432\u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0438 \u043f\u043e \u0441\u043f\u0438\u0441\u043a\u0443 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432 \u043c\u043e\u0434\u0443\u043b\u0435 event-sourcing. \u041e\u0442 \u043d\u0435\u0433\u043e \u043e \u0431\u0443\u0434\u0443\u0442 \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u0441\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u044b\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b. \u0410 \u043a\u043b\u0430\u0441\u0441 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u0442\u044c\u0441\u044f \u0432 \u043c\u043e\u0434\u0443\u043b\u0435 api. \u042d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043b\u043e\u0433\u0438\u043a\u0430 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u043e\u0439. \u041a\u043b\u0430\u0441\u0441 <code>Event<\/code> \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0438 id \u0441\u043e\u0431\u044b\u0442\u0438\u044f.<\/p>\n<pre><code>@SuperBuilder @AllArgsConstructor @NoArgsConstructor @Data public abstract class Event implements Command {     private UUID id;     private Long eventId;     @Builder.Default     private Instant createAt = Instant.now(); }  <\/code><\/pre>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a\u0443\u044e \u043f\u0438\u0446\u0446\u0443 \u043a\u0443\u043f\u0438\u043b \u0447\u0435\u043b\u043e\u0432\u0435\u043a. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0432\u0435\u043d\u0442 <code>BuyPizza<\/code> \u0441 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u043e \u0442\u043e\u043c \u043a\u0442\u043e \u043a\u0443\u043f\u0438\u043b, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0438\u0446\u0446\u0430 \u0441\u0442\u043e\u0438\u043b\u0430 \u0438 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u0443\u0441\u043a\u043e\u0432 \u0432 \u043d\u0435\u0439.<\/p>\n<pre><code>@Data @SuperBuilder public class BuyPizza extends Event {     private Integer countOfPieces;     private Integer price;     private String username; }  <\/code><\/pre>\n<p>\u0422\u0430\u043a \u0436\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u044f. \u041f\u0443\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043b\u0438\u0441\u0442 \u043f\u043e\u0440\u043e\u0436\u0434\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u043c\u0430\u043d\u0434.  \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c, \u0438 \u0443\u0432\u0435\u0434\u043e\u043c\u0438\u043c \u0432\u0441\u0435\u0445 \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u0438\u0446\u0446\u0430 \u043a\u0443\u043f\u043b\u0435\u043d\u0430. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u0435\u0440\u043d\u0435\u043c \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 <code>SendPizzaNotification<\/code> \u0438 \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u0442\u043e\u0436\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a.<\/p>\n<pre><code>@Handler public List&lt;Command> handle(BuyPizza command){         if (command.getUsername()!=null &amp;&amp; command.getUsername().isBlank())             throw new RuntimeException(\"\u0438\u043c\u044f \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043e\");         if (command.getCountOfPieces() == null || command.getCountOfPieces() &lt;= 0)             throw new RuntimeException(\"\u043a\u0443\u0441\u043e\u0447\u043a\u043e\u0432 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u0431\u044b\u0442\u044c\");         if (command.getPrice() == null || command.getPrice() &lt;= 0)             throw new RuntimeException(\"\u043f\u0438\u0446\u0446\u0430 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0447\u0435\u0433\u043e-\u0442\u043e \u0441\u0442\u043e\u0438\u0442\");         log.info(\"\u041c\u0438\u0441\u0442\u0435\u0440 {} \u043a\u0443\u043f\u0438\u043b {} \u043a\u0443\u0441\u043a\u043e\u0432 \u043f\u0438\u0446\u0446\u044b \u0437\u0430 {} \u0434\u0435\u043d\u0435\u0433\",                 command.getUsername(),                 command.getCountOfPieces(),                 command.getPrice());         return of(SendPizzaNotification.builder()                 .username(command.getUsername())                 .build()); }  @Handler public List&lt;Command> handle(SendPizzaNotification command) {  log.info(\"\u041f\u0438\u0446\u0446\u0430 \u043f\u0438\u0446\u0446\u0430 \u0443 {} \u043f\u0438\u0446\u0446\u0430!!!\", command.getUsername());  return Collections.emptyList(); }  <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u0434\u0430\u0447\u0430, \u043a\u0430\u043a \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 handler\u2019\u044b \u0438 \u043f\u0440\u0438 \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0435 \u0435\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0441\u0435\u0440\u0432\u0438\u0441. \u0412 \u043d\u0435\u043c \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0434\u043b\u044f \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u0445 \u043a\u043e\u043c\u0430\u043d\u0434 \u0438 \u0434\u043b\u044f \u043d\u0435\u0443\u0441\u043f\u0435\u0448\u043d\u043e. \u0422\u0430\u043a\u0436\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043f\u043e\u0441\u043e\u0431 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u044b\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432. \u0412 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>addCommandHandler<\/code> \u0431\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0438\u0439 \u0432 \u0441\u0435\u0431\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u0443.<\/p>\n<pre><code>public interface Publisher {     void publish(Command message);     void publish(List&lt;Command> proceed);     void fail(Command message);     void fail(List&lt;Command> proceed);     void addCommandHandler(Class type, Consumer&lt;Command> consumer); }  <\/code><\/pre>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u0431\u0443\u0434\u0435\u0442 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0430 \u043d\u0430 \u043c\u0430\u043f\u0435: \u043a\u043b\u0430\u0441\u0441 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043f\u0440\u043e\u0442\u0438\u0432 \u0441\u043f\u0438\u0441\u043a\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432. \u041f\u0440\u0438 \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u0435\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0438 \u043f\u043e \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u0438\u0445 \u043a \u043a\u043e\u043c\u0430\u043d\u0434\u0435.<\/p>\n<pre><code>@Component @RequiredArgsConstructor public class PublisherImpl implements Publisher {      private final Map&lt;Class&lt;? extends Command>, Collection&lt;Consumer&lt;Command>>> consumerHandlerMap = new HashMap&lt;>();      private final CommandProcessor commandProcessor;      @Override     public void publish(Command command) {         if (command == null) return;         Collection&lt;Consumer&lt;Command>> handlers = consumerHandlerMap.get(command.getClass());         if (handlers != null &amp;&amp; !handlers.isEmpty()){             Command processedCommand = commandProcessor.processCommand(command);             handlers.forEach(x -> x.accept(processedCommand));         } else {             throw new IllegalStateException(format(\"No handlers exist for class %s\", command.getClass()));         }     } ...     @Override     public void addCommandHandler(Class type, Consumer&lt;Command> consumer) {         if (consumerHandlerMap.containsKey(type)) {             consumerHandlerMap.get(type).add(consumer);         } else {             List&lt;Consumer&lt;Command>> handlerList = new LinkedList();             handlerList.add(consumer);             consumerHandlerMap.put(type, handlerList);         }     } }  <\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u043f\u0438\u0448\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043a\u043e\u043c\u0430\u043d\u0434, \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0438\u0445 \u043d\u0430\u0434\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c. \u042d\u0442\u043e \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0432 \u043b\u0438\u0441\u0442\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>EventRepository<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043b\u0435\u043d\u044c \u043c\u043e\u0440\u043e\u0447\u0438\u0442\u044c\u0441\u044f \u0441 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code>@Component @RequiredArgsConstructor @Slf4j public class CommandProcessorImpl implements CommandProcessor {      private final EventRepository eventRepository;      @Override     public Command processCommand(Command command) {         if (command instanceof Event) {             eventRepository.save((Event)command);         }         return command;     }      @Override     public Command processFailure(Command command) {         log.info(\"\u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443\");         return command;     } }  <\/code><\/pre>\n<p>\u0412 \u0446\u0435\u043b\u043e\u043c \u043d\u0430 \u044d\u0442\u043e\u043c \u0441\u0430\u043c\u0430 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u043f\u0438\u0448\u0443\u0449\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u0438 \u0433\u043e\u0442\u043e\u0432\u0430, \u0438 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0430\u0431\u043b\u0438\u0448\u0435\u0440 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e. \u041a\u0430\u043a \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u043d\u043e \u0442\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u0445\u044d\u043d\u0434\u043b\u0435\u0440\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0435\u0433\u043e \u0432 \u043f\u0430\u0431\u043b\u0438\u0448\u0435\u0440 \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435, \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u043b\u0431\u0430\u0441\u0430 \u043a\u043e\u0434\u0430, \u0434\u0430 \u0438 \u0437\u0430\u0447\u0435\u043c, \u0435\u0441\u043b\u0438 \u0432\u0441\u0435\u043c \u044d\u0442\u0438\u043c \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c\u0441\u044f \u0441\u043f\u0440\u0438\u043d\u0433.<br \/> \u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435 \u0432\u0441\u0435 \u0445\u044d\u043d\u0434\u043b\u0435\u0440\u044b \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u044b \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0435\u0439 <code>@Handler<\/code>. \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0431\u0438\u043d \u043f\u043e\u0441\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440, \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u0432\u0441\u0435 \u0445\u044d\u043d\u0434\u043b\u0435\u0440\u044b \u0438\u0437 \u0432\u0441\u0435\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u0438\u043c\u0438 publisher. \u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043f\u0430\u0434\u0430\u0435\u0442 \u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0434\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u043a\u0441\u0438.<\/p>\n<p>\u0412 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>postProcessBeforeInitialization<\/code> \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430. \u0421\u043e\u0431\u0435\u0440\u0435\u043c \u0432\u0441\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043e\u0442\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0435\u0439 <code>@Handler<\/code> \u0438 \u0437\u0430\u043f\u0438\u0448\u0435\u043c \u0438\u0445 \u0432 \u043c\u0430\u043f\u0443 \u043f\u0440\u043e\u0442\u0438\u0432 \u0438\u043c\u0435\u043d\u0438 \u0431\u0438\u043d\u0430. \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>postProcessAfterInitialization<\/code>. \u0412 \u043d\u0435\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u043e\u043a\u0441\u0438 \u0432\u0441\u0435\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432. \u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043a\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u0445\u044d\u043d\u0434\u043b\u0435\u0440\u044b \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0435 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u0445 \u0432 <code>Publisher<\/code>.<\/p>\n<p>\u0412\u0430\u0436\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441, \u043f\u043e\u0447\u0435\u043c\u0443 \u043d\u0430\u0434\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432\u0435\u0434\u044c \u043c\u043e\u0436\u043d\u043e \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u044b \u043d\u0430\u0448\u043b\u0438 \u043c\u0435\u0442\u043e\u0434 \u0432 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435 \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u0435\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0432 <code>Publisher<\/code>. \u042d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432 \u043f\u0440\u043e\u043a\u0441\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438 \u0435\u0441\u043b\u0438 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0430 \u043d\u0435 \u043f\u0440\u043e\u043a\u0441\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0442\u0435\u0440\u044f\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u043f\u043e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 <code>@Transactional<\/code><\/p>\n<pre><code>@Component @RequiredArgsConstructor public class CommandHandlerBeanPostProcessor implements BeanPostProcessor {      private final Map&lt;String, List&lt;Method>> handlers = new HashMap&lt;>();     private final Publisher publisher;      @Override     public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {         List&lt;Method> handleMethods = Stream.of(bean.getClass().getMethods())                 .filter(x -> x.isAnnotationPresent(Handler.class))                 .collect(Collectors.toList());         if (!handleMethods.isEmpty()){             handlers.put(beanName, handleMethods);         }         return bean;     }      @Override     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {         if (handlers.containsKey(beanName)) {             handlers.get(beanName).forEach(x -> addWithInvoke(bean, x));         }         return bean;     }      private void addWithInvoke(@NonNull Object object, @NonNull Method handleMethod) {         Class[] classes = handleMethod.getParameterTypes();         if (classes.length != 1) throw new RuntimeException(\"handler mast contains in params only Command\");         publisher.addCommandHandler(classes[0], a -> invoke(object, handleMethod, a));     }      @SneakyThrows     private void invoke(@NonNull Object object, @NonNull Method handleMethod, Object param)     {         handleMethod.invoke(object, param);     }  } <\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0443\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>Publisher<\/code>. \u0418\u043d\u0436\u0435\u043a\u0442\u0438\u043c \u0435\u0433\u043e \u0433\u0434\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u0435\u0433\u043e \u043f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u043c \u043a\u043e\u043c\u0430\u043d\u0434\u044b. \u0412 \u0442\u0430\u043a\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u043d\u0435\u0437\u0430\u0447\u0435\u043c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043d\u0438\u0447\u0435\u0433\u043e \u0438\u0437 \u0445\u044d\u043d\u0434\u043b\u0435\u0440\u0430. \u041f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u0432 \u043a\u043e\u043d\u0446\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043b\u043e\u0433\u0438\u043a\u0438, \u0442\u0430\u043a \u043a\u0430\u043a \u0435\u0441\u043b\u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430, \u0442\u043e \u043d\u0438 \u043e\u0434\u043d\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u043b\u0430 \u0431\u044b\u0442\u044c \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043d\u0430. \u041f\u0443\u0441\u0442\u044c \u0432\u0441\u0435 \u043f\u043e\u0440\u043e\u0436\u0434\u0435\u043d\u043d\u044b\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0431\u0443\u0434\u0443\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u0438\u0437 \u0445\u044d\u043d\u0434\u043b\u0435\u0440\u0430 \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c\u0441\u044f.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u0434\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0430\u0441\u043f\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u043a\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u044b. \u0415\u0441\u043b\u0438 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u0445\u044d\u043d\u0434\u043b\u0435\u0440\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0430, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043e\u0448\u0438\u0431\u043a\u0438 \u0443 Publisher\u2019\u0430.<\/p>\n<pre><code>@Aspect @Component public class HandlerContextAspect {      @Autowired     private Publisher publisher;      @Pointcut(value = \"@annotation(ru.abdyabdya.es_cqrs.annotations.Handler)\")     public void callAtHandlerAnnotation() { }      @Around(value = \"callAtHandlerAnnotation()\")     @SneakyThrows     public Object aroundCallAt(ProceedingJoinPoint pjp) {         List&lt;Command> retVal = null;         try {             retVal = (List&lt;Command>)pjp.proceed();             publisher.publish(retVal);         } catch (CommandException commandException) {             publisher.fail(commandException.getCommand());         }         return retVal;     } } <\/code><\/pre>\n<p>\u041d\u0430 \u0441 \u043f\u0438\u0448\u0443\u0449\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e \u043c\u044b<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-338574","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/338574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=338574"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/338574\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=338574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=338574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=338574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}