void serializeRecordedEvents(List<RecordedEvent> events, String filePath) { try { JsonWriter writer = new JsonWriter(new FileWriter(filePath)); writer.beginObject(); writer.name("events"); writer.beginArray(); for (RecordedEvent event : events) { writer.beginObject(); writer.name("entityRef_ID").value(event.getEntityId()); writer.name("timestamp").value(event.getTimestamp()); writer.name("index").value(event.getIndex()); writer.name("event_class").value(event.getEvent().getClass().getName()); writer.name("event_data"); writer.beginObject(); writeSpecificEventData(writer, event.getEvent()); writer.endObject(); writer.endObject(); } writer.endArray(); writer.endObject(); writer.close(); } catch (Exception e) { logger.error("Error while serializing events:", e); } }
/** * Try to process recorded events for 'maxDuration' miliseconds. Events are only processed if the time is right. * @param maxDuration the amount of time in which this method will try to process recorded events in one go. */ private void processRecordedEventsBatch(long maxDuration) { long beginTime = System.currentTimeMillis(); for (RecordedEvent re = recordedEvents.peek(); re != null; re = recordedEvents.peek()) { long passedTime = System.currentTimeMillis() - this.replayEventsLoadTime; //Waits until the time of reproduction is right or until 'maxDuration' miliseconds have already passed since this method was called while (passedTime < re.getTimestamp()) { passedTime = System.currentTimeMillis() - this.replayEventsLoadTime; if ((System.currentTimeMillis() - beginTime) >= maxDuration) { return; } } recordedEvents.poll(); EntityRef entity = getEntityRef(re); // Sends recorded event to be processed if (re.getComponent() != null) { originalSend(entity, re.getEvent(), re.getComponent()); } else { originalSend(entity, re.getEvent()); } this.lastRecordedEventIndex = re.getIndex(); // Check if time is up. if ((System.currentTimeMillis() - beginTime) >= maxDuration) { return; } } }