This code snippet is an implementation of the 'onMessage' method for a Redis message listener.

First, it converts the byte array 'pattern' to a 'String' representing the message channel.

Then, it deserializes the message body using the value serializer of the 'redisTemplate'. The deserialized object is logged. If the deserialized object is null, the method returns.

Next, it calculates an MD5 hash of the deserialized object as the key for a Redis key-value pair.

The method then tries to set the key in Redis with a timeout of 20 seconds using the 'setIfAbsent' method of the 'redisTemplate'. If the operation succeeds (returns 'true'), it means that the current service is the first one to process the message, and the message is processed. The result is logged.

If the operation fails (returns 'false'), it means that another service is already processing the message, and the method logs that the message is being processed by another service.


原文地址: https://www.cveoy.top/t/topic/pid4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录