Skip to content

Commit

Permalink
eventapis(#21) More Advanced Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zeldal committed Mar 6, 2018
1 parent 2a47c58 commit 1e042d7
Show file tree
Hide file tree
Showing 142 changed files with 2,547 additions and 170 deletions.
15 changes: 15 additions & 0 deletions samples/2-basic/order-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.kloia.test</groupId>
<artifactId>samples</artifactId>
<version>0.5.0-SNAPSHOT</version>
</parent>

<artifactId>order-service</artifactId>


</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Created by zeldalozdemir on 30/01/2017.
*/
@SpringBootApplication
@ComponentScan(basePackages = {"com.kloia.sample","com.kloia.eventapis"})
@ComponentScan(basePackages = {"com.kloia.sample", "com.kloia.eventapis"})
public class TestOrderServiceMain {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.kloia.eventapis.view.EntityFunctionSpec;
import com.kloia.sample.dto.command.CreateOrderCommandDto;
import com.kloia.sample.dto.event.OrderCreatedEvent;
import com.kloia.sample.model.Orders;
import com.kloia.sample.model.OrderState;
import com.kloia.sample.model.Orders;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -36,7 +36,7 @@ public CreateOrderCommand(EventRepository eventRepository) {
public EventKey execute(@RequestBody @Valid CreateOrderCommandDto dto) throws Exception {

OrderCreatedEvent orderCreatedEvent = new OrderCreatedEvent();
BeanUtils.copyProperties(dto,orderCreatedEvent);
BeanUtils.copyProperties(dto, orderCreatedEvent);

EventKey eventKey = eventRepository.recordAndPublish(orderCreatedEvent);
return eventKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import com.kloia.eventapis.view.EntityFunctionSpec;
import com.kloia.sample.dto.command.ProcessOrderCommandDto;
import com.kloia.sample.dto.event.ReserveStockEvent;
import com.kloia.sample.model.Orders;
import com.kloia.sample.model.OrderState;
import com.kloia.sample.model.Orders;
import com.kloia.sample.model.PaymentInformation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -51,7 +51,7 @@ public EventKey execute(@RequestBody ProcessOrderCommandDto dto) throws Exceptio
if (order.getState() == OrderState.INITIAL) {
ReserveStockEvent reserveStockEvent = new ReserveStockEvent(order.getStockId(), order.getOrderAmount(), dto.getPaymentInformation());
log.info("Template account saved: " + dto);
return eventRepository.recordAndPublish(order,reserveStockEvent);
return eventRepository.recordAndPublish(order, reserveStockEvent);
} else
throw new EventStoreException("Order state is not valid for this Operation: " + dto);
}
Expand All @@ -70,11 +70,12 @@ public ProcessOrderSpec() {
});
}
}

@Component
public static class ProcessOrderRollback implements RollbackSpec<ReserveStockEvent>{
public static class ProcessOrderRollback implements RollbackSpec<ReserveStockEvent> {
@Override
public void rollback(ReserveStockEvent event) {
log.warn("ProcessOrderRollback for :"+event);
log.warn("ProcessOrderRollback for :" + event);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ ViewQuery<Orders> orderViewRepository(List<EntityFunctionSpec<Orders, ?>> functi
}

@Bean
EventRecorder orderPersistentEventRepository(EventApisConfiguration eventApisConfiguration,IUserContext userContext) {
EventRecorder orderPersistentEventRepository(EventApisConfiguration eventApisConfiguration, IUserContext userContext) {
return new CassandraEventRecorder(eventApisConfiguration.getTableNameForEvents("order"), cassandraSession, operationContext, userContext, new ObjectMapper());
}

Expand All @@ -66,5 +66,4 @@ EventRepository orderEventRepository(EventRecorder orderEventRecorder, IOperatio
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import com.kloia.eventapis.view.EntityFunctionSpec;
import com.kloia.sample.dto.event.OrderPaidEvent;
import com.kloia.sample.dto.event.PaymentSuccessEvent;
import com.kloia.sample.model.Orders;
import com.kloia.sample.model.OrderState;
import com.kloia.sample.model.Orders;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
Expand All @@ -35,12 +35,12 @@ public PaymentSuccessEventHandler(EventRepository eventRepository, ViewQuery<Ord

@Override
@KafkaListener(topics = "PaymentSuccessEvent", containerFactory = "eventsKafkaListenerContainerFactory")
public EventKey execute(PaymentSuccessEvent dto) throws EventStoreException, EventPulisherException, ConcurrentEventException {
public EventKey execute(PaymentSuccessEvent dto) throws EventStoreException, ConcurrentEventException {
Orders order = orderQuery.queryEntity(dto.getOrderId());

if (order.getState() == OrderState.PAYMENT_READY) {
log.info("Payment is processing : " + dto);
return eventRepository.recordAndPublish(order.getEventKey(),new OrderPaidEvent(dto.getPaymentId()));
return eventRepository.recordAndPublish(order.getEventKey(), new OrderPaidEvent(dto.getPaymentId()));
} else
throw new EventStoreException("Order state is not valid for this Operation: " + dto);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import com.kloia.eventapis.view.EntityFunctionSpec;
import com.kloia.sample.dto.event.PaymentProcessEvent;
import com.kloia.sample.dto.event.StockReservedEvent;
import com.kloia.sample.model.Orders;
import com.kloia.sample.model.OrderState;
import com.kloia.sample.model.Orders;
import com.kloia.sample.model.PaymentInformation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -24,9 +24,7 @@
*/
@Slf4j
@Controller
public class StockReservedEventHandler implements EventHandler< StockReservedEvent> {
private final static String name = "PROCESS_ORDER";
private final static String CREATED = "CREATED";
public class StockReservedEventHandler implements EventHandler<StockReservedEvent> {
private final EventRepository eventRepository;
private final ViewQuery<Orders> orderQuery;

Expand All @@ -38,13 +36,16 @@ public StockReservedEventHandler(EventRepository eventRepository, ViewQuery<Orde

@Override
@KafkaListener(topics = "StockReservedEvent", containerFactory = "eventsKafkaListenerContainerFactory")
public EventKey execute(StockReservedEvent dto) throws EventStoreException, EventPulisherException, ConcurrentEventException {
public EventKey execute(StockReservedEvent dto) throws EventStoreException, ConcurrentEventException {
Orders order = orderQuery.queryEntity(dto.getOrderId());

if (order.getState() == OrderState.RESERVING_STOCK) {
PaymentProcessEvent paymentProcessEvent = new PaymentProcessEvent(order.getId(),new PaymentInformation(order.getPaymentAddress(),order.getAmount(),order.getCardInformation()));
PaymentProcessEvent paymentProcessEvent = new PaymentProcessEvent(
order.getId(),
dto.getSender().getVersion(),
new PaymentInformation(order.getPaymentAddress(), order.getAmount(), order.getCardInformation()));
log.info("Payment is processing : " + dto);
return eventRepository.recordAndPublish(order,paymentProcessEvent);
return eventRepository.recordAndPublish(order, paymentProcessEvent);
} else
throw new EventStoreException("Order state is not valid for this Operation: " + dto);
}
Expand All @@ -54,6 +55,7 @@ public static class PaymentProcessSpec extends EntityFunctionSpec<Orders, Paymen
public PaymentProcessSpec() {
super((order, event) -> {
PaymentProcessEvent eventData = event.getEventData();
order.setReservedStockVersion(eventData.getReservedStockVersion());
order.setState(OrderState.PAYMENT_READY);
return order;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
@NoArgsConstructor
public class CreateOrderCommandDto {
private String stockId;
private int orderAmount ;
private int orderAmount;
private String description;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.kloia.sample.dto.event;

import com.kloia.eventapis.common.EventType;
import com.kloia.eventapis.common.PublishedEvent;
import com.kloia.eventapis.common.PublishableEvent;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrderCreatedEvent extends PublishedEvent {
public class OrderCreatedEvent extends PublishableEvent {
private String stockId;
private int orderAmount ;
private int orderAmount;
private String description;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.kloia.sample.dto.event;

import com.kloia.eventapis.common.EventType;
import com.kloia.eventapis.common.PublishedEvent;
import com.kloia.eventapis.common.PublishableEvent;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -12,8 +12,9 @@
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrderPaidEvent extends PublishedEvent{
public class OrderPaidEvent extends PublishableEvent {
private String paymentId;

@Override
public EventType getEventType() {
return EventType.OP_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.kloia.sample.dto.event;

import com.kloia.eventapis.common.EventType;
import com.kloia.eventapis.common.PublishedEvent;
import com.kloia.eventapis.common.PublishableEvent;
import com.kloia.sample.model.PaymentInformation;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand All @@ -13,9 +13,11 @@
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PaymentProcessEvent extends PublishedEvent{
public class PaymentProcessEvent extends PublishableEvent {
private String orderId;
private int reservedStockVersion;
private PaymentInformation paymentInformation;

@Override
public EventType getEventType() {
return EventType.EVENT;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kloia.sample.dto.event;

import com.kloia.eventapis.common.PublishedEvent;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Created by zeldalozdemir on 31/01/2017.
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PaymentSuccessEvent extends PublishedEvent {
private String orderId;
private String paymentId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.annotation.JsonView;
import com.kloia.eventapis.api.Views;
import com.kloia.eventapis.common.EventType;
import com.kloia.eventapis.common.PublishedEvent;
import com.kloia.eventapis.common.PublishableEvent;
import com.kloia.sample.model.PaymentInformation;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand All @@ -12,11 +12,12 @@
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ReserveStockEvent extends PublishedEvent {
public class ReserveStockEvent extends PublishableEvent {
private String stockId;
private long numberOfItemsSold;
@JsonView(Views.RecordedOnly.class)
private PaymentInformation paymentInformation;

@Override
public EventType getEventType() {
return EventType.OP_START;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kloia.sample.dto.event;

import com.kloia.eventapis.common.PublishedEvent;
import lombok.Data;

@Data
public class StockReservedEvent extends PublishedEvent {
private String stockId;
private String orderId;
private long numberOfItemsSold;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.kloia.sample.model;

public enum OrderState {
INITIAL, RESERVING_STOCK, PAYMENT_READY, PAID, RELEASING_STOCK, PROCESSING_FAILED, CANCELLED, SHIPPED, RETURNED
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.kloia.sample.model;

import com.kloia.eventapis.spring.model.JpaEntity;
import com.kloia.eventapis.view.Entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.Id;

/**
* Created by zeldalozdemir on 17/02/2017.
*/
Expand All @@ -20,7 +17,8 @@
public class Orders extends JpaEntity {
private long price;
private String stockId;
private int orderAmount ;
private int reservedStockVersion;
private int orderAmount;
private String paymentAddress;
private float amount;
private String cardInformation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ spring:
log-validation-errors: true
jpa:
hibernate:
ddl-auto: update
ddl-auto: create
show-sql: true

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--drop table if exists test.OrderEvents ;
CREATE TABLE test.OrderEvents (
entityId ASCII,
version INT,
eventType ASCII,
opId ASCII,
opDate TIMESTAMP,
status ASCII,
auditinfo ASCII,
eventData VARCHAR,
PRIMARY KEY (entityId, version)
);


CREATE INDEX OrderEvents_opId
ON test.OrderEvents (opId);
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.kloia.test</groupId>
<artifactId>samples</artifactId>
<version>0.4.1-SNAPSHOT</version>
<version>0.5.0-SNAPSHOT</version>
</parent>
<artifactId>payment-service</artifactId>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Created by zeldalozdemir on 30/01/2017.
*/
@SpringBootApplication
@ComponentScan(basePackages = {"com.kloia.sample","com.kloia.eventapis"})
@ComponentScan(basePackages = {"com.kloia.sample", "com.kloia.eventapis"})
public class TestPaymentServiceMain {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public class Components {
@Bean
AggregateListener snapshotRecorder(ViewQuery<Payment> paymentViewRepository, EventRepository paymentEventRepository, PaymentRepository paymentRepository,
Optional<List<RollbackSpec>> rollbackSpecs) {
return new AggregateListener(paymentViewRepository, paymentEventRepository, paymentRepository, rollbackSpecs.orElseGet(ArrayList::new),objectMapper);
return new AggregateListener(paymentViewRepository, paymentEventRepository, paymentRepository, rollbackSpecs.orElseGet(ArrayList::new), objectMapper);
}

@Bean
ViewQuery<Payment> paymentViewRepository(List<EntityFunctionSpec<Payment, ?>> functionSpecs,EventApisConfiguration eventApisConfiguration) {
ViewQuery<Payment> paymentViewRepository(List<EntityFunctionSpec<Payment, ?>> functionSpecs, EventApisConfiguration eventApisConfiguration) {
return new CassandraViewQuery<>(
eventApisConfiguration.getTableNameForEvents("payment"),
cassandraSession, objectMapper, functionSpecs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public class PaymentRestController {

@Autowired
private ViewQuery<Payment> orderEventRepository;
private ViewQuery<Payment> paymentEventRepository;

@Autowired
private PaymentRepository paymentRepository;
Expand All @@ -39,7 +39,7 @@ public ResponseEntity<?> getPayment(@PathVariable("paymentId") String paymentId)

@RequestMapping(value = "/{paymentId}/{version}", method = RequestMethod.GET)
public ResponseEntity<?> getPaymentWithVersion(@PathVariable("paymentId") String paymentId, @PathVariable("version") Integer version) throws IOException, EventStoreException {
return new ResponseEntity<Object>(orderEventRepository.queryEntity(paymentId,version), HttpStatus.CREATED);
return new ResponseEntity<Object>(paymentEventRepository.queryEntity(paymentId, version), HttpStatus.CREATED);
}

}
Expand Down
Loading

0 comments on commit 1e042d7

Please sign in to comment.