Created
July 19, 2019 12:37
-
-
Save hpgrahsl/97ac69bda40d6f4e3a83dc00db67dbf1 to your computer and use it in GitHub Desktop.
OrderUpsertedEvent Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OrderUpsertedEvent implements Outboxable { | |
private static ObjectMapper MAPPER = new ObjectMapper(); | |
private final Long id; | |
private final JsonNode payload; | |
private final Long timestamp; | |
static { | |
MAPPER.registerModule(new JavaTimeModule()); | |
} | |
private OrderUpsertedEvent(Long id, JsonNode payload) { | |
this.id = id; | |
this.payload = payload; | |
this.timestamp = Instant.now().getEpochSecond(); | |
} | |
public static OrderUpsertedEvent of(PurchaseOrder order) { | |
return new OrderUpsertedEvent(order.getId(), MAPPER.valueToTree(order)); | |
} | |
@Override | |
public String getAggregateId() { | |
return String.valueOf(id); | |
} | |
@Override | |
public String getAggregateType() { | |
return PurchaseOrder.class.getName(); | |
} | |
@Override | |
public String getType() { | |
return this.getClass().getName(); | |
} | |
@Override | |
public Long getTimestamp() { | |
return timestamp; | |
} | |
@Override | |
public String getPayload() { | |
try { | |
return MAPPER.writeValueAsString(payload); | |
} catch (JsonProcessingException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment