Skip to content

Commit

Permalink
feat: improvements regarding cache map entry listener
Browse files Browse the repository at this point in the history
  • Loading branch information
mherwig committed May 15, 2024
1 parent 5b28feb commit f6945ff
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,41 +1,74 @@
package de.telekom.eni.pandora.horizon.cache.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hazelcast.core.EntryEvent;
import com.hazelcast.core.HazelcastJsonValue;
import com.hazelcast.map.listener.EntryAddedListener;
import com.hazelcast.map.listener.EntryEvictedListener;
import com.hazelcast.map.listener.EntryRemovedListener;
import com.hazelcast.map.listener.EntryUpdatedListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;

public class JsonCacheServiceEntryListener implements EntryAddedListener<String, HazelcastJsonValue>,
import java.util.Optional;

@Slf4j
public class JsonCacheServiceEntryListener<T> implements EntryAddedListener<String, HazelcastJsonValue>,
EntryRemovedListener<String, HazelcastJsonValue>,
EntryUpdatedListener<String, HazelcastJsonValue>,
EntryEvictedListener<String, HazelcastJsonValue> {

private final Class<T> mapClass;

private final ObjectMapper mapper;

private final ApplicationEventPublisher applicationEventPublisher;

public JsonCacheServiceEntryListener(ApplicationEventPublisher applicationEventPublisher) {
public JsonCacheServiceEntryListener(Class<T> mapClass, ObjectMapper mapper, ApplicationEventPublisher applicationEventPublisher) {
this.mapClass = mapClass;
this.mapper = mapper;
this.applicationEventPublisher = applicationEventPublisher;
}

private Optional<JsonCacheServiceEvent<T>> toJsonCacheServiceEvent(EntryEvent<String, HazelcastJsonValue> entryEvent) {
try {
T newValue = null;
T oldValue = null;

if (entryEvent.getValue() != null) {
newValue = mapper.readValue(entryEvent.getValue().getValue(), mapClass);
}

if (entryEvent.getOldValue() != null) {
oldValue = mapper.readValue(entryEvent.getOldValue().getValue(), mapClass);
}

return Optional.of(new JsonCacheServiceEvent<>(newValue, oldValue, entryEvent));
} catch (JsonProcessingException e) {
log.error(e.getMessage(), e);
}

return Optional.empty();
}

@Override
public void entryAdded(EntryEvent<String, HazelcastJsonValue> entryEvent) {
applicationEventPublisher.publishEvent(new JsonCacheServiceEvent(entryEvent));
toJsonCacheServiceEvent(entryEvent).ifPresent(applicationEventPublisher::publishEvent);
}

@Override
public void entryEvicted(EntryEvent<String, HazelcastJsonValue> entryEvent) {
applicationEventPublisher.publishEvent(new JsonCacheServiceEvent(entryEvent));
toJsonCacheServiceEvent(entryEvent).ifPresent(applicationEventPublisher::publishEvent);
}

@Override
public void entryRemoved(EntryEvent<String, HazelcastJsonValue> entryEvent) {
applicationEventPublisher.publishEvent(new JsonCacheServiceEvent(entryEvent));
toJsonCacheServiceEvent(entryEvent).ifPresent(applicationEventPublisher::publishEvent);
}

@Override
public void entryUpdated(EntryEvent<String, HazelcastJsonValue> entryEvent) {
applicationEventPublisher.publishEvent(new JsonCacheServiceEvent(entryEvent));
toJsonCacheServiceEvent(entryEvent).ifPresent(applicationEventPublisher::publishEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
@Getter
@Setter
@AllArgsConstructor
public class JsonCacheServiceEvent {
public class JsonCacheServiceEvent<T> {
private T newValue;

private T oldValue;

private EntryEvent<String, HazelcastJsonValue> entryEvent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public JsonCacheService<SubscriptionResource> subscriptionCache(HazelcastInstanc
mapper.registerModule(module);

IMap<String, HazelcastJsonValue> map = hazelcastInstance.getMap(SUBSCRIPTION_RESOURCE_V1);
map.addEntryListener(new JsonCacheServiceEntryListener(applicationEventPublisher), true);
map.addEntryListener(new JsonCacheServiceEntryListener<>(SubscriptionResource.class, mapper, applicationEventPublisher), true);
return new JsonCacheService<>(SubscriptionResource.class, map, mapper);
}

Expand Down

0 comments on commit f6945ff

Please sign in to comment.