μ›Ήμ†ŒμΌ“ μ•ŒλžŒ κ°±μ‹  동기화

λŒ€λΆ€λΆ„μ˜ μ˜ˆμ œλŠ” μ±„νŒ…μœΌλ‘œ 곡유되고 μžˆμ§€λ§Œ 일반적인 μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œμ˜ μ›Ήμ†ŒμΌ“ κΈ°λŠ₯은 μ„œλ²„μ—μ„œ ν΄λΌμ΄μ–ΈνŠΈλ‘œμ˜ μ•ŒλžŒκ³Ό 이벀트λ₯Ό μ¦‰μ‹œ μ „λ‹¬ν•˜κΈ° μœ„ν•΄μ„œ μ‚¬μš©ν•˜κ³  μžˆμ„ 것이닀. 단일 μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œλŠ” κ³ λ €ν•˜μ§€ μ•Šμ•„λ„ 될 λΆ€λΆ„μ΄μ§€λ§Œ μŠ€μΌ€μΌ μ•„μ›ƒλ˜μ–΄ λΆ„μ‚° μ²˜λ¦¬λ˜λŠ” μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ— 각각 μ—°κ²°λœ μ›Ήμ†ŒμΌ“μœΌλ‘œ νŠΉμ • μ„œλ²„μ—μ„œ λ°œμƒν•˜μ—¬ λ§Œλ“€μ–΄μ§€λŠ” μ•ŒλžŒ 정보λ₯Ό μ¦‰μ‹œ μ „λ‹¬ν•˜κΈ° μœ„ν•΄μ„œλŠ” μ „νŒŒν•˜κ³  λ™κΈ°ν™”ν•˜λŠ” λ°©μ•ˆμ΄ ν•„μš”ν•˜λ‹€. Redis(Pub/Sub)둜 둜컬 μΊμ‹œ λ™κΈ°ν™”ν•˜κΈ°μ™€ λΉ„μŠ·ν•˜λ‹€.

@Slf4j
@SpringBootApplication
public class Application implements MessageListener {
    private static final ChannelTopic topic = ChannelTopic.of("NOTIFICATIONS");

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Autowired
    public StringRedisTemplate redisTemplate;

    @Bean
    public CommandLineRunner commandLineRunner() {
        return args -> redisTemplate.convertAndSend(topic.getTopic(), "REQUIRE.SYNC");
    }

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(this, topic);
        return container;
    }

    @Bean
    public MessageListenerAdapter listenerAdapter() {
        return new MessageListenerAdapter(this);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        log.info("receive: {}", message);
    }
}

ν™œμš© 클래슀 λͺ©λ‘

λ ˆλ””μŠ€μ— μ˜ν•œ Pub/Sub을 κ΅¬ν˜„ν•˜κΈ° μœ„ν•΄μ„œ μ‚¬μš©λ˜λŠ” μŠ€ν”„λ§ ν”„λ ˆμž„μ›Œν¬ ν΄λž˜μŠ€λ“€μ€ μ•„λž˜μ™€ κ°™λ‹€.

  • RedisMessageListenerContainer
  • RedisConnectionFactory
  • MessageListenerAdapter
  • MessageListener
  • StringRedisTemplate
  • ChannelTopic

참고 링크