Restart Message Listener after exception

Hi all,

I have a spring project that contains several message listeners. Each of these message listeeners are typed similiar to below

Class randomListener() : MessageListener<ChangeStreamDocument<Document>, RandomModel?> {

    override fun onMessage(event: Message<ChangeStreamDocument<Document>, RandomModel?>) {
        ...processing
    }
}

However if there’s an exception thrown, particularly when Mongo is mapping the document to my random model. The listener doesn’t restart or reconstruct itself. Which prevents it from continuing to listen to events moving forward. I can’t find any reliable documentation to resume to changestream or reconstruct it. I can’t access the event timestamp of course because the program throws an exception mapping the data.

An example of the exception thrown

2023-02-06 13:25:06.319  INFO 7 --- [cTaskExecutor-3] .a.p.s.s.m.s.RandomListener : ..random logs
2023-02-06 13:25:06.354 ERROR 7 --- [cTaskExecutor-3] rContainer$DecoratingLoggingErrorHandler : Unexpected error occurred while listening to MongoDB.

org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.Instant] for value '2022-10-26T14:31:24.629+00:00'; nested exception is java.time.format.DateTimeParseException: Text '2022-10-26T14:31:24.629+00:00' could not be parsed at index 23

Now of course I can resolve why the exception is thrown later, but that doesn’t resolve the issue with the listener, not continuing.

I’ve configured the MessageListeenerContainer as a configuration annotation in Spring with the appropriate beans to create subscriptions using the above listeners.

Would the simplest solution be here to just wrap the method in a try catch and register the change stream if an exception is thrown?