0

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.

Fernando
  • 381
  • 1
  • 5
  • 20
  • See https://stackoverflow.com/questions/57703023/mongodb-change-stream-can-i-get-value-before-update-delete, I believe what you are asking for is not currently possible. – D. SM Dec 14 '20 at 21:14
  • What is the best alternative approach I can take here?. I can either get the entire data set in memory and then try to find it by objectedId or I can just reload the entire cache by calling the mongodb. – Fernando Dec 14 '20 at 22:07
  • I believe you would need to store the data you need in another collection and consequently some mechanism to clean up the orphan references once the documents in main collection get deleted. – D. SM Dec 14 '20 at 22:13
  • Update and Delete will always return the same ObjectId as in the old document right? – Fernando Dec 14 '20 at 22:18
  • I expect _id value to be referencing the value on the existing document if this is what you are referring to. – D. SM Dec 14 '20 at 22:27
  • ok and are Ids unique across collections? – Fernando Dec 14 '20 at 22:57
  • They would be if you make them so. – D. SM Dec 14 '20 at 23:32

0 Answers0