generated from telekom/reuse-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improvements regarding cache map entry listener
- Loading branch information
Showing
3 changed files
with
44 additions
and
8 deletions.
There are no files selected for viewing
45 changes: 39 additions & 6 deletions
45
...main/java/de/telekom/eni/pandora/horizon/cache/service/JsonCacheServiceEntryListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters