Skip to content

Commit

Permalink
Simplify payload definition in ClusterEvent
Browse files Browse the repository at this point in the history
Jackson doesn't really care if it has to serialize/deserialize an Object or
Map<String, Object>, so we go with the simpler definition.
  • Loading branch information
Jochen Schalanda committed Apr 9, 2015
1 parent 6a4f787 commit f892368
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
Expand Up @@ -59,7 +59,7 @@ public abstract class ClusterEvent {

@JsonProperty
@Nullable
public abstract Map<String, Object> payload();
public abstract Object payload();


@JsonCreator
Expand All @@ -68,13 +68,13 @@ public static ClusterEvent create(@Id @ObjectId @JsonProperty("_id") @Nullable S
@JsonProperty("producer") @Nullable String producer,
@JsonProperty("consumers") @Nullable Set<String> consumers,
@JsonProperty("event_class") @Nullable String eventClass,
@JsonProperty("payload") @Nullable Map<String, Object> payload) {
@JsonProperty("payload") @Nullable Object payload) {
return new AutoValue_ClusterEvent(id, date, producer, consumers, eventClass, payload);
}

public static ClusterEvent create(@NotEmpty String producer,
@NotEmpty String eventClass,
@NotEmpty Map<String, Object> payload) {
@NotEmpty Object payload) {
return new AutoValue_ClusterEvent(null,
DateTime.now(DateTimeZone.UTC),
producer,
Expand Down
Expand Up @@ -136,7 +136,7 @@ private void updateConsumers(final String eventId, final NodeId nodeId) {
final WriteResult<ClusterEvent, String> writeResult = dbCollection.updateById(eventId, DBUpdate.addToSet("consumers", nodeId.toString()));
}

private Object extractPayload(Map<String, Object> payload, String eventClass) {
private Object extractPayload(Object payload, String eventClass) {
try {
final Class<?> clazz = Class.forName(eventClass);
return objectMapper.convertValue(payload, clazz);
Expand Down Expand Up @@ -182,9 +182,7 @@ protected void run() throws Exception {
@Subscribe
public void publishClusterEvent(Object event) {
final String className = event.getClass().getCanonicalName();
final Map<String, Object> payload = objectMapper.convertValue(event, new TypeReference<Map<String, Object>>() {
});
final ClusterEvent clusterEvent = ClusterEvent.create(nodeId.toString(), className, payload);
final ClusterEvent clusterEvent = ClusterEvent.create(nodeId.toString(), className, event);
final String id = dbCollection.save(clusterEvent).getSavedId();
LOG.debug("Published cluster event with ID <{}> and type <{}>", id, className);
}
Expand Down

0 comments on commit f892368

Please sign in to comment.