I am trying to set up an update cache mechanism for whenever a change happens in MongoDB. For this I have implemented a listener as so:
@Bean
public MessageListenerContainer messageListenerContainer(MongoTemplate template){
MessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer(template);
messageListenerContainer.start();
ChangeStreamRequest<BinDataItem> request = ChangeStreamRequest.builder()
.collection("BIN")
.publishTo(getBinListener())
.build();
messageListenerContainer.register(request, BinDataItem.class);
return messageListenerContainer;
}
@Bean
@Qualifier("BinDataUpdateListener")
public BinDataUpdateListener<ChangeStreamDocument<Document>, BinDataItem> getBinListener(){
return new BinDataUpdateListener<ChangeStreamDocument<Document>, BinDataItem>();
}
And the update the method is:
public class BinDataUpdateListener<C, T> implements MessageListener<ChangeStreamDocument<Document>, BinDataItem> {
@Autowired
@Qualifier("binDataLoader")
CacheManager cacheManager;
@Override
public void onMessage(Message<ChangeStreamDocument<Document>, BinDataItem> message) {
BinDataItem binDataItem = (BinDataItem)message.getBody(); // this value comes as null for a delete operation
String operationType = message.getRaw().getOperationType().getValue();
try {
cacheManager.updateCache(operationType, binDataItem, message.getRaw().getDocumentKey());
}
catch(RedisCommandTimeoutException ex){
addToSLogger(DomainConstants.GET_BIN_DATA_STRING, DomainConstants.ERR_CODE_MS1003, ex);
throw ex;
}
catch(RedisBusyException ex){
addToSLogger(DomainConstants.GET_BIN_DATA_STRING, DomainConstants.ERR_CODE_MS1002, ex);
throw ex;
}
catch(RedisConnectionFailureException ex){
addToSLogger(DomainConstants.GET_BIN_DATA_STRING, DomainConstants.ERR_CODE_MS1002, ex);
throw ex;
}
catch(Exception ex){
addToSLogger(DomainConstants.GET_BIN_DATA_STRING, DomainConstants.ERR_CODE_MS1001, ex);
throw ex;
}
}
private void addToSLogger(String messageName, String errorCode, Exception e){
Slogger.get()
.withTag(DomainConstants.MSG_ERROR)
.withField(DomainConstants.MSG_NAME, messageName)
.withField(DomainConstants.ERR_CODE, errorCode)
.withThrowable(e)
.info(e.getMessage());
}
}
The BinDataItem pojo is:
@Document(collection = "BIN")
@JsonIgnoreProperties(ignoreUnknown=true)
public class BinDataItem implements Serializable {
@BsonProperty(value = "_id")
private String id;
private String bin;
}
The problem is that whenever an event of type delete happens, the object deleted comes as null in the program. I need the field bin of the deleted object in mongo to delete it in the redis cache. I know how to get the objectId but all the other fields I don't.