λ λμ€ μΉμμΌ μλ λκΈ°ν
μΉμμΌ μλ κ°±μ λκΈ°ν
λλΆλΆμ μμ λ μ±ν μΌλ‘ 곡μ λκ³ μμ§λ§ μΌλ°μ μΈ μΉ μ ν리μΌμ΄μ μμμ μΉμμΌ κΈ°λ₯μ μλ²μμ ν΄λΌμ΄μΈνΈλ‘μ μλκ³Ό μ΄λ²€νΈλ₯Ό μ¦μ μ λ¬νκΈ° μν΄μ μ¬μ©νκ³ μμ κ²μ΄λ€. λ¨μΌ μ ν리μΌμ΄μ μμλ κ³ λ €νμ§ μμλ λ λΆλΆμ΄μ§λ§ μ€μΌμΌ μμλμ΄ λΆμ° μ²λ¦¬λλ μ ν리μΌμ΄μ μ κ°κ° μ°κ²°λ μΉμμΌμΌλ‘ νΉμ μλ²μμ λ°μνμ¬ λ§λ€μ΄μ§λ μλ μ 보λ₯Ό μ¦μ μ λ¬νκΈ° μν΄μλ μ ννκ³ λκΈ°ννλ λ°©μμ΄ νμνλ€. 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