Code, JavaEE

Event Store – JSON Files

This is just me playing around with code to store events in a json form and file.  The hard problem I need to solve

now is how to make this work either using the Rolling log file appender which would mean converting all output

to a logging statement such that LOG.info(…) would contain the JSON entry for each entry.  A Rolling file

appender in LOG4J will automatically handle (based on configuration) creating a new file once the file size

has been met.  Now the real hard problem for me is to implement this functionality myself and continue to

use the format that I have in place.  The Java API for appending to a file uses a java.io.Writer passing a true

to its constructor (FileWriter(file, true)) to state that the file is to be appended to.  Then, there is the other

way of doing this like I have where I use Gson and Jackson to convert the Event object to JSON then

serialize it to the file system. What I really want to do is keep the current format and figure out how

to implement file rolling using a single file source that gets rolled once reaching a threshold file size.

Event Store Test


package principles;

import org.joda.time.DateTime;
import org.junit.Test;

import java.util.List;
import java.util.UUID;

/**
* EventStoreTest
*
* @author david.king
*         Date: 5/23/13
*
*/
public class EventStoreTest {

@Test
public void testWriteToAndReadFromEventStore() {
EventStore eventStore = new EventStore();
String description = "create event";
DateTime timestamp = new DateTime();
String ipaddress = "123.123.123.123";
String id = UUID.randomUUID().toString().replaceAll("-","");

Event event = new Event(id, description, timestamp, ipaddress);
eventStore.writeEvent(event);

Event eventRead = eventStore.getEventInMemory(id);
System.out.format("Event Read from store: %s", eventRead);
}

}

Event


package principles;

import org.joda.time.DateTime;

import java.io.Serializable;

/**
* Event
*
* @author david.king
*         Date: 5/23/13
*
*/
public class Event implements Serializable {
private String id;
private String description;
private DateTime timestamp;
private String ipaddress;

public Event(String id, String description, DateTime timestamp, String ipaddress) {
this.id = id;
this.description = description;
this.timestamp = timestamp;
this.ipaddress = ipaddress;
}

public String getId() {
return id;
}

public String getDescription() {
return description;
}

public DateTime getTimestamp() {
return timestamp;
}

public String getIpaddress() {
return ipaddress;
}

@Override
public String toString() {
StringBuilder json = new StringBuilder();
json.append("{")
.append("\"id\"").append(":\"").append(id).append(",")
.append("\"description\"").append(":\"").append(description).append("\"")
.append("}");
return json.toString();
}
}

Event Store


package principles;

import com.google.gson.Gson;
import com.google.gson.stream.JsonWriter;
import org.codehaus.jackson.map.ObjectMapper;
import org.joda.time.DateTime;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.*;

/**
* EventStore
*
* @author david.king
*         Date: 5/23/13
*
*/
public class EventStore {
private static final String EVENT_STORE_PATH = "/temp/";
private static final String EVENT_STORE_FILE = String.format("%seventstore-%s.json", EVENT_STORE_PATH, UUID.randomUUID());
private static final int EVENT_STORE_MAX_FILE_SIZE = 5242880;
private Gson gson = new Gson();
private ObjectMapper objectMapper = new ObjectMapper();
private Map<String, Event> events = new LinkedHashMap<>();

public EventStore() {

}

public void writeEvent(Event event) {
// convert to json
String json = gson.toJson(event);

// store in event store - in memory
events.put(event.getId(), event);

try {
// write to file
final String EVENT_STORE_FILE_JSON = String.format("%seventstore-%s.json", EVENT_STORE_PATH, event.getId());
System.out.format("%s - Event Stord in File", EVENT_STORE_FILE_JSON);
objectMapper.writeValue(new File(EVENT_STORE_FILE_JSON), event);
} catch (IOException e) {
e.printStackTrace();
}

}

public Event getEventInMemory(String eventId) {
Event event = null;
if (events.containsKey(eventId)) {
event = events.get(eventId);
}
return event;
}

public Event getEventFromFile(String eventId) {
final String EVENT_STORE_FILE_JSON = String.format("%seventstore-%s.json", EVENT_STORE_PATH, eventId);
try {
Event event = objectMapper.readValue(new File(EVENT_STORE_FILE_JSON), Event.class);
return event;
} catch (IOException e) {
e.printStackTrace();
}

return null;
}
}

Sample file: eventstore-e671a6059e4e47719c81630f759bd5d3.json

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s