Skip to content

Instantly share code, notes, and snippets.

@Romeh
Created December 22, 2017 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Romeh/004cad80ac6b4a59766d63a86c981b95 to your computer and use it in GitHub Desktop.
Save Romeh/004cad80ac6b4a59766d63a86c981b95 to your computer and use it in GitHub Desktop.
@Override
// if you want to do atomic update over cache entry
public void updateAlertEntry(String serviceId, String serviceCode, AlertEntry alertEntry) {
//get the JSR cache reference
final Cache<String, List<AlertEntry>> alertsCache = getAlertsCache();
// then invoke atomic update on the cache entry
alertsCache.invoke(serviceId, (mutableEntry, objects) -> {
if (mutableEntry.exists() && mutableEntry.getValue() != null) {
logger.debug("updating alert entry into the cache store invoke: {},{}",serviceId,serviceCode);
final List<AlertEntry> alertEntries = mutableEntry.getValue();
// remove only if it has the error code
alertEntries.removeIf(alertEntry1 -> alertEntry1.getErrorCode().equals(serviceCode));
alertEntries.add(alertEntry);
mutableEntry.setValue(alertEntries);
}else{
throw new ResourceNotFoundException(String.format("Alert for %s with %s not found", serviceId,serviceCode));
}
// by api design nothing needed here
return null;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment