Created
October 1, 2025 01:14
-
-
Save robsonkades/312e4bdabe258076566cbfe696246d56 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.cache.Cache; | |
| import org.springframework.cache.CacheManager; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.context.annotation.Configuration; | |
| import org.springframework.context.event.EventListener; | |
| import org.springframework.data.redis.connection.RedisConnectionFactory; | |
| import org.springframework.data.redis.listener.PatternTopic; | |
| import org.springframework.data.redis.listener.RedisMessageListenerContainer; | |
| import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; | |
| import org.springframework.stereotype.Component; | |
| import java.util.concurrent.Executors; | |
| @Configuration | |
| public class RedisPubSubConfig { | |
| private static final Logger LOG = LoggerFactory.getLogger(RedisPubSubConfig.class); | |
| @Autowired | |
| private CacheManager cacheManager; | |
| @Bean | |
| public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) { | |
| RedisMessageListenerContainer container = new RedisMessageListenerContainer(); | |
| container.setConnectionFactory(connectionFactory); | |
| container.setSubscriptionExecutor(Executors.newVirtualThreadPerTaskExecutor()); | |
| container.setTaskExecutor(Executors.newVirtualThreadPerTaskExecutor()); | |
| container.addMessageListener(listenerAdapter, new PatternTopic("my-channel:*")); | |
| return container; | |
| } | |
| @Bean | |
| public MessageListenerAdapter listenerAdapter(MessageSubscriber subscriber) { | |
| return new MessageListenerAdapter(subscriber, "handleMessage"); | |
| } | |
| @Component | |
| public static class MessageSubscriber { | |
| private final CacheManager cacheManager; | |
| public MessageSubscriber(CacheManager cacheManager) { | |
| this.cacheManager = cacheManager; | |
| } | |
| @EventListener | |
| public void handleMessage(String message) { | |
| Cache sefaz = cacheManager.getCache("app"); | |
| sefaz.evictIfPresent("test"); | |
| LOG.info("Received: {}", message); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment