@SuppressWarnings("unchecked") private void registerSagaBeanDefinitions(EventProcessingConfigurer configurer) { String[] sagas = beanFactory.getBeanNamesForAnnotation(Saga.class); for (String saga : sagas) { Saga sagaAnnotation = beanFactory.findAnnotationOnBean(saga, Saga.class); Class sagaType = beanFactory.getType(saga); ProcessingGroup processingGroupAnnotation = beanFactory.findAnnotationOnBean(saga, ProcessingGroup.class); if (processingGroupAnnotation != null && !"".equals(processingGroupAnnotation.value())) { configurer.assignHandlerTypesMatching(processingGroupAnnotation.value(), sagaType::equals); } configurer.registerSaga(sagaType, sagaConfigurer -> { if (sagaAnnotation != null && !"".equals(sagaAnnotation.sagaStore())) { sagaConfigurer.configureSagaStore(c -> beanFactory.getBean(sagaAnnotation.sagaStore(), SagaStore.class)); } }); } }
@Service @ProcessingGroup("commandPublishingEventHandlers") public class PortfolioManagementUserListener { private static final Logger logger = LoggerFactory.getLogger(PortfolioManagementUserListener.class); private final CommandGateway commandGateway; public PortfolioManagementUserListener(CommandGateway commandGateway) { this.commandGateway = commandGateway; } @EventHandler public void on(UserCreatedEvent event) { logger.debug("About to dispatch a new command to create a Portfolio for the new user {}", event.getUserId()); commandGateway.send(new CreatePortfolioCommand(new PortfolioId(), event.getUserId())); } }
/** * Listener that is used to create a new portfolio for each new user that is created. * TODO #28 the Portfolio aggregate should be instantiated from the Company aggregate, as is possible since axon 3.3 */ @Service @ProcessingGroup("commandPublishingEventHandlers") public class PortfolioManagementUserListener { private static final Logger logger = LoggerFactory.getLogger(PortfolioManagementUserListener.class); private final CommandGateway commandGateway; @Autowired public PortfolioManagementUserListener(CommandGateway commandGateway) { this.commandGateway = commandGateway; } @EventHandler public void on(UserCreatedEvent event) { logger.debug("About to dispatch a new command to create a Portfolio for the new user {}", event.getUserId()); commandGateway.send(new CreatePortfolioCommand(new PortfolioId(), event.getUserId())); } }
@Service @ProcessingGroup("commandPublishingEventHandlers") public class CompanyOrderBookListener { private static final Logger logger = LoggerFactory.getLogger(CompanyOrderBookListener.class); private final CommandGateway commandGateway; public CompanyOrderBookListener(CommandGateway commandGateway) { this.commandGateway = commandGateway; } @EventHandler public void on(CompanyCreatedEvent event) { logger.debug("About to dispatch a new command to create an OrderBook for the company {}", event.getCompanyId()); OrderBookId orderBookId = new OrderBookId(); commandGateway.send(new CreateOrderBookCommand(orderBookId)); commandGateway.send(new AddOrderBookToCompanyCommand(event.getCompanyId(), orderBookId)); } }
@Service @ProcessingGroup("userQueryModel") public class UserEventHandler { private final UserViewRepository userRepository; @Autowired public UserEventHandler(UserViewRepository userRepository) { this.userRepository = userRepository; } @EventHandler public void on(UserCreatedEvent event) { UserView userView = new UserView(); userView.setIdentifier(event.getUserId().getIdentifier()); userView.setName(event.getName()); userView.setUsername(event.getUsername()); userView.setPassword(event.getPassword()); userRepository.save(userView); } }
@Service @ProcessingGroup("queryModel") public class CompanyEventHandler { private final CompanyViewRepository companyRepository; @Autowired public CompanyEventHandler(CompanyViewRepository companyRepository) { this.companyRepository = companyRepository; } @EventHandler public void on(CompanyCreatedEvent event) { CompanyView companyView = new CompanyView(); companyView.setIdentifier(event.getCompanyId().getIdentifier()); companyView.setValue(event.getCompanyValue()); companyView.setAmountOfShares(event.getAmountOfShares()); companyView.setTradeStarted(true); companyView.setName(event.getCompanyName()); companyRepository.save(companyView); } }
/** * This listener is used to create order book instances when we have created a new company</p> * TODO #28 the OrderBook aggregate should be instantiated from the Company aggregate, as is possible since axon 3.3 **/ @Service @ProcessingGroup("commandPublishingEventHandlers") public class CompanyOrderBookListener { private static final Logger logger = LoggerFactory.getLogger(CompanyOrderBookListener.class); private final CommandGateway commandGateway; @Autowired public CompanyOrderBookListener(CommandGateway commandGateway) { this.commandGateway = commandGateway; } @EventHandler public void on(CompanyCreatedEvent event) { logger.debug("About to dispatch a new command to create an OrderBook for the company {}", event.getCompanyId()); OrderBookId orderBookId = new OrderBookId(); commandGateway.send(new CreateOrderBookCommand(orderBookId)); commandGateway.send(new AddOrderBookToCompanyCommand(event.getCompanyId(), orderBookId)); } }
@Service @ProcessingGroup("userQueryModel") public class UserEventHandler {
@Component @ProcessingGroup("amqpEvents") @AllArgsConstructor @FieldDefaults(level = PRIVATE, makeFinal = true) public class MenuItemAddedEventListener { @Autowired MenuItemRepository menu; @EventHandler public void on(MenuItemAddedEvent event) { Logger .getInstance(getClass()) .info(format("Handling event: %s", event)); menu.save(new MenuItem(event.getId(), event.getPrice())); } }
@Component @ProcessingGroup("amqpEvents") @AllArgsConstructor @FieldDefaults(level = PRIVATE, makeFinal = true) public class MenuItemRemovedEventListener { @Autowired MenuItemRepository menu; @EventHandler public void on(MenuItemRemovedEvent event) { Logger .getInstance(getClass()) .info(format("Handling event: %s", event)); menu.delete(event.getId()); } }
@Component @ProcessingGroup("amqpEvents") @AllArgsConstructor @FieldDefaults(level = PRIVATE, makeFinal = true) public class CustomerSignedUpEventListener { @Autowired CustomerRepository customers; @EventHandler public void on(CustomerSignedUpEvent event) { Logger .getInstance(getClass()) .info(format("Handling event: %s", event)); customers.save(new Customer(event.getId())); } }
@Service @ProcessingGroup("queryModel") public class PortfolioMoneyEventHandler {
@Service @ProcessingGroup("queryModel") public class PortfolioItemEventHandler {
@Component @ProcessingGroup("amqpEvents") @AllArgsConstructor @FieldDefaults(level = PRIVATE, makeFinal = true) public class ItemAddedToOrderEventListener { @Autowired ItemRepository items; @EventHandler public void on(ItemAddedToOrderEvent event) { Logger .getInstance(getClass()) .info(format("Handling event: %s", event)); Optional .ofNullable(items.findOne(event.getMenuItemId())) .map(Item::increasePopularity) .ifPresent(items::save); } }
@Service @ProcessingGroup("queryModel") public class OrderBookEventHandler {
@Component @ProcessingGroup("amqpEvents") @AllArgsConstructor @FieldDefaults(level = PRIVATE, makeFinal = true) public class AddressAddedEventListener { @Autowired CustomerRepository customers; @Autowired AddressRepository addresses; @EventHandler public void on(AddressAddedEvent event) { Logger .getInstance(getClass()) .info(format("Handling event: %s", event)); Optional .ofNullable(customers.findOne(event.getCustomerId())) .map(customer -> new Address( event.getAddressId(), event.getLocation(), customer)) .ifPresent(addresses::save); } }
@Service @ProcessingGroup("queryModel") public class TransactionEventHandler {
@Service @ProcessingGroup("trading") public class TransactionEventHandler {
@Service @ProcessingGroup("trading") public class PortfolioEventHandler {
@Service @ProcessingGroup("trading") public class OrderBookEventHandler {