Skip to content

Commit

Permalink
chore: optimized map entry listener
Browse files Browse the repository at this point in the history
  • Loading branch information
mherwig committed May 15, 2024
1 parent f6945ff commit 154ec2e
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package de.telekom.eni.pandora.horizon.cache.listener;

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;

import java.util.Optional;

@Slf4j
public abstract class AbstractHazelcastJsonEntryMapEventBroadcaster<T extends AbstractHazelcastJsonEvent<?>> implements EntryAddedListener<String, HazelcastJsonValue>,
EntryRemovedListener<String, HazelcastJsonValue>,
EntryUpdatedListener<String, HazelcastJsonValue>,
EntryEvictedListener<String, HazelcastJsonValue> {

protected final ObjectMapper mapper;

private final ApplicationEventPublisher applicationEventPublisher;

public AbstractHazelcastJsonEntryMapEventBroadcaster(ObjectMapper mapper, ApplicationEventPublisher applicationEventPublisher) {
this.mapper = mapper;
this.applicationEventPublisher = applicationEventPublisher;
}

protected abstract Optional<T> map(EntryEvent<String, HazelcastJsonValue> entryEvent);

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

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

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

@Override
public void entryUpdated(EntryEvent<String, HazelcastJsonValue> entryEvent) {
map(entryEvent).ifPresent(applicationEventPublisher::publishEvent);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.telekom.eni.pandora.horizon.cache.service;
package de.telekom.eni.pandora.horizon.cache.listener;

import com.hazelcast.core.EntryEvent;
import com.hazelcast.core.HazelcastJsonValue;
Expand All @@ -9,8 +9,8 @@
@Getter
@Setter
@AllArgsConstructor
public class JsonCacheServiceEvent<T> {
private T newValue;
public abstract class AbstractHazelcastJsonEvent<T> {
private T value;

private T oldValue;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package de.telekom.eni.pandora.horizon.cache.listener;

import com.hazelcast.core.EntryEvent;
import com.hazelcast.core.HazelcastJsonValue;
import de.telekom.eni.pandora.horizon.kubernetes.resource.SubscriptionResource;

public class SubscriptionResourceEvent extends AbstractHazelcastJsonEvent<SubscriptionResource> {
public SubscriptionResourceEvent(SubscriptionResource value, SubscriptionResource oldValue, EntryEvent<String, HazelcastJsonValue> entryEvent) {
super(value, oldValue, entryEvent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package de.telekom.eni.pandora.horizon.cache.listener;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hazelcast.core.EntryEvent;
import com.hazelcast.core.HazelcastJsonValue;
import de.telekom.eni.pandora.horizon.kubernetes.resource.SubscriptionResource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;

import java.util.Optional;

@Slf4j
public class SubscriptionResourceEventBroadcaster extends AbstractHazelcastJsonEntryMapEventBroadcaster<SubscriptionResourceEvent> {

public SubscriptionResourceEventBroadcaster(ObjectMapper mapper, ApplicationEventPublisher applicationEventPublisher) {
super(mapper, applicationEventPublisher);
}

@Override
protected Optional<SubscriptionResourceEvent> map(EntryEvent<String, HazelcastJsonValue> entryEvent) {
try {
SubscriptionResource newValue = null;
SubscriptionResource oldValue = null;

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

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


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

return Optional.empty();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.hazelcast.core.HazelcastJsonValue;
import com.hazelcast.map.IMap;
import de.telekom.eni.pandora.horizon.cache.service.JsonCacheService;
import de.telekom.eni.pandora.horizon.cache.service.JsonCacheServiceEntryListener;
import de.telekom.eni.pandora.horizon.cache.listener.SubscriptionResourceEventBroadcaster;
import de.telekom.eni.pandora.horizon.kubernetes.resource.SubscriptionResource;
import de.telekom.jsonfilter.operator.Operator;
import de.telekom.jsonfilter.serde.OperatorDeserializer;
Expand All @@ -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<>(SubscriptionResource.class, mapper, applicationEventPublisher), true);
map.addEntryListener(new SubscriptionResourceEventBroadcaster(mapper, applicationEventPublisher), true);
return new JsonCacheService<>(SubscriptionResource.class, map, mapper);
}

Expand Down

0 comments on commit 154ec2e

Please sign in to comment.