จำได้ไหมว่า notification log มีอยู่สองแบบ นั่นคือ current log และ archived log ด้วยเหตุนี้ interface ของ NotificationService จึงมี query method สำหรับแต่ละแบบ:
public class NotificationService {
@Transactional(readOnly=true)
public NotificationLog currentNotificationLog() {
EventStore eventStore = EventStore.instance();
return this.findNotificationLog(
this.calculateCurrentNotificationLogId(eventStore),
eventStore);
}
@Transactional(readOnly=true)
public NotificationLog notificationLog(String aNotificationLogId) {
EventStore eventStore = EventStore.instance();
return this.findNotificationLog(
new NotificationLogId(aNotificationLogId),
eventStore);
}
...
}
ท้ายที่สุด ทั้งสอง method นี้ก็จะต้อง “find” NotificationLog ให้ได้ ซึ่งความหมายที่แท้จริงก็คือการค้นหาส่วนของ DomainEvent ที่ถูก serialize ไว้ใน Event Store โดยห่อหุ้มแต่ละตัวด้วย Notification แล้วรวบรวมทั้งหมดนั้นเข้าด้วยกันเป็น NotificationLog เมื่อ instance ของ NotificationLog ถูกสร้างขึ้นมาแล้ว มันก็จะถูกแสดงเป็น RESTful resource และส่งมอบให้กับ client ที่ร้องขอได้
เนื่องจาก current log อาจเป็นเป้าหมายที่เคลื่อนที่อยู่ตลอดเวลา identity ของมันจึงต้องถูกคำนวณใหม่ทุกครั้งที่มีการร้องขอ วิธีคำนวณมีดังนี้:
public class NotificationService {
...
protected NotificationLogId calculateCurrentNotificationLogId(
EventStore anEventStore) {
long count = anEventStore.countStoredEvents();
long remainder = count % LOG_NOTIFICATION_COUNT;
if (remainder == 0) {
remainder = LOG_NOTIFICATION_COUNT;
}
long low = count - remainder + 1;
// ensures a minted id value even though there may
// not be a full set of notifications at present
long high = low + LOG_NOTIFICATION_COUNT - 1;
return new NotificationLogId(low, high);
}
...
}
ในทางกลับกัน สำหรับ archived log สิ่งที่ต้องมีก็คือ NotificationLogId เพื่อครอบคลุมช่วง low และ high ของ identifier จำไว้ว่า identifier ถูก encode ออกมาเป็นข้อความที่แทน ช่วงค่าระหว่าง low กับ high เช่น 21–40 ด้วยเหตุนี้ constructor สำหรับ encoded identity จึงมีหน้าตาแบบนี้:
public class NotificationLogId {
...
public NotificationLogId(String aNotificationLogId) {
super();
String[] textIds = aNotificationLogId.split(",");
this.setLow(Long.parseLong(textIds[0]));
this.setHigh(Long.parseLong(textIds[1]));
}
...
}
ไม่ว่าจะเป็นการ query หา current log หรือ archived log ตอนนี้เราก็จะได้ NotificationLogId ซึ่งเอาไว้บอกว่า method findNotificationLog() จะต้องทำการ query หาอะไร:
public class NotificationService {
...
protected NotificationLog findNotificationLog(
NotificationLogId aNotificationLogId,
EventStore anEventStore) {
List<StoredEvent> storedEvents =
anEventStore.allStoredEventsBetween(
aNotificationLogId.low(),
aNotificationLogId.high());
long count = anEventStore.countStoredEvents();
boolean archivedIndicator = aNotificationLogId.high() < count;
NotificationLog notificationLog =
new NotificationLog(
aNotificationLogId.encoded(),
NotificationLogId.encoded(
aNotificationLogId.next(
LOG_NOTIFICATION_COUNT)),
NotificationLogId.encoded(
aNotificationLogId.previous(
LOG_NOTIFICATION_COUNT)),
this.notificationsFrom(storedEvents),
archivedIndicator);
return notificationLog;
}
...
protected List<Notification> notificationsFrom(
List<StoredEvent> aStoredEvents) {
List<Notification> notifications =
new ArrayList<Notification>(aStoredEvents.size());
for (StoredEvent storedEvent : aStoredEvents) {
DomainEvent domainEvent =
EventStore.toDomainEvent(storedEvent);
Notification notification =
new Notification(
domainEvent.getClass().getSimpleName(),
storedEvent.eventId(),
domainEvent.occurredOn(),
domainEvent);
notifications.add(notification);
}
return notifications;
}
...
}
เป็นเรื่องน่าสนใจทีเดียวที่จริงๆ แล้วเราไม่จำเป็นต้อง persist instance ของ Notification หรือ log ทั้งชุดไว้แต่อย่างใด เราสามารถสร้างมันขึ้นมาใหม่ได้ทุกครั้งที่ต้องการใช้งาน และด้วยเหตุนี้เอง การ cache resource ของ NotificationLog ณ จุดที่รับ request จึงช่วยเรื่อง performance และ scalability ได้อย่างเห็นได้ชัด
Method findNotificationLog() จะใช้ component EventStore เพื่อ query instance ของ StoredEvent ที่จำเป็นสำหรับ log หนึ่งๆ นี่คือวิธีที่ EventStore ใช้ค้นหา instance เหล่านั้น:
package com.saasovation.identityaccess.application.eventStore;
...
public class EventStore ... {
...
public List<StoredEvent> allStoredEventsBetween(
long aLowStoredEventId,
long aHighStoredEventId) {
Query query =
this.session().createQuery(
"from StoredEvent as _obj_ "
+ "where _obj_.eventId between ? and ? "
+ "order by _obj_.eventId");
query.setParameter(0, aLowStoredEventId);
query.setParameter(1, aHighStoredEventId);
List<StoredEvent> storedEvents = query.list();
return storedEvents;
}
...
}
สุดท้ายนี้ ที่ Web tier เราจะ publish current log และ archived log:
@Path("/notifications")
public class NotificationResource {
...
@GET
@Produces({ OvationsMediaType.NAME })
public Response getCurrentNotificationLog(
@Context UriInfo aUriInfo) {
NotificationLog currentNotificationLog =
this.notificationService()
.currentNotificationLog();
if (currentNotificationLog == null) {
throw new WebApplicationException(
Response.Status.NOT_FOUND);
}
Response response =
this.currentNotificationLogResponse(
currentNotificationLog,
aUriInfo);
return response;
}
@GET
@Path("{notificationId}")
@Produces({ OvationsMediaType.ID_OVATION_NAME })
public Response getNotificationLog(
@PathParam("notificationId") String aNotificationId,
@Context UriInfo aUriInfo) {
NotificationLog notificationLog =
this.notificationService()
.notificationLog(aNotificationId);
if (notificationLog == null) {
throw new WebApplicationException(
Response.Status.NOT_FOUND);
}
Response response =
this.notificationLogResponse(
notificationLog,
aUriInfo);
return response;
}
...
}
ทีมอาจเลือกใช้ MessageBodyWriter เพื่อสร้าง response ก็ได้ แต่ยังมีความซับซ้อนปลีกย่อยที่จำเป็นบางอย่าง ซึ่งถูกจัดการอยู่ใน response builder methods
เท่านี้ก็ครอบคลุมส่วนสำคัญทั้งหมดที่ใช้ publish ทั้ง current notification log และ archived notification log ให้กับ RESTful clients แล้ว