// 설정container.addMessageListener(listener, new ChannelTopic("user.notification"));// 메시지 발행redisTemplate.convertAndSend("user.notification", "새로운 알림");
패턴 방식 구독
패턴을 추가하는 방식
// 설정 - 여러 채널을 와일드카드로 구독container.addMessageListener(listener, new PatternTopic("user.*"));// 메시지 발행 - 모두 수신됨redisTemplate.convertAndSend("user.login", "로그인 알림");redisTemplate.convertAndSend("user.logout", "로그아웃 알림");redisTemplate.convertAndSend("user.notification", "일반 알림");
패턴 구독에서 pattern 파라미터 사용
public void onMessage(Message message, byte[] pattern) { String channel = new String(message.getChannel()); String patternStr = pattern != null ? new String(pattern) : "직접 구독"; log.info("패턴: {}, 실제 채널: {}", patternStr, channel); // 패턴별 분기 처리 if ("user.*".equals(patternStr)) { handleUserEvent(channel, message); } else if ("order.*".equals(patternStr)) { handleOrderEvent(channel, message); }}