Run safely with several instances: ShedLock jobs, Redis WebSocket fan-out
- Scheduled jobs (nightly cleanup, moderation, search dictionary rebuild) take a ShedLock lock in Redis, so only one instance runs each; the moderation run stops before its lock can expire. - STOMP events (chat messages, unread counters, transcoding status) are published to the Redis channel ws:broadcast and every instance hands them to its local simple broker, so subscribers on other instances receive them. Falls back to local delivery if Redis is down; app.websocket.cluster-broadcast switches it off. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package com.krylov.refound.config;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.data.redis.RedisConnectionFailureException;
|
||||
import org.springframework.data.redis.connection.DefaultMessage;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
|
||||
class ClusterMessagingTest {
|
||||
|
||||
private SimpMessagingTemplate messagingTemplate;
|
||||
private StringRedisTemplate redis;
|
||||
private ObjectMapper objectMapper;
|
||||
private ClusterMessaging messaging;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
messagingTemplate = mock(SimpMessagingTemplate.class);
|
||||
redis = mock(StringRedisTemplate.class);
|
||||
objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
|
||||
messaging = new ClusterMessaging(messagingTemplate, redis, objectMapper, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void publishesToRedisInsteadOfLocalBroker() {
|
||||
messaging.convertAndSend("/topic/chat/7", Map.of("content", "привет"));
|
||||
|
||||
ArgumentCaptor<String> json = ArgumentCaptor.forClass(String.class);
|
||||
verify(redis).convertAndSend(eq(ClusterMessaging.CHANNEL), json.capture());
|
||||
assertThat(json.getValue()).contains("/topic/chat/7").contains("привет");
|
||||
verify(messagingTemplate, never()).convertAndSend(anyString(), any(Object.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void messageFromRedisGoesToLocalBroker() throws Exception {
|
||||
String json = objectMapper.writeValueAsString(Map.of("destination", "/topic/chat/7",
|
||||
"payload", Map.of("content", "привет")));
|
||||
|
||||
messaging.onMessage(new DefaultMessage(ClusterMessaging.CHANNEL.getBytes(), json.getBytes(StandardCharsets.UTF_8)),
|
||||
null);
|
||||
|
||||
ArgumentCaptor<Object> payload = ArgumentCaptor.forClass(Object.class);
|
||||
verify(messagingTemplate).convertAndSend(eq("/topic/chat/7"), payload.capture());
|
||||
assertThat(((JsonNode) payload.getValue()).get("content").asText()).isEqualTo("привет");
|
||||
}
|
||||
|
||||
@Test
|
||||
void redisOutageFallsBackToLocalDelivery() {
|
||||
doThrow(new RedisConnectionFailureException("down")).when(redis).convertAndSend(anyString(), anyString());
|
||||
Map<String, String> payload = Map.of("content", "привет");
|
||||
|
||||
messaging.convertAndSend("/topic/chat/7", payload);
|
||||
|
||||
verify(messagingTemplate).convertAndSend("/topic/chat/7", (Object) payload);
|
||||
}
|
||||
|
||||
@Test
|
||||
void garbageFromRedisIsIgnored() {
|
||||
messaging.onMessage(new DefaultMessage(new byte[0], "not json".getBytes(StandardCharsets.UTF_8)), null);
|
||||
|
||||
verify(messagingTemplate, never()).convertAndSend(anyString(), any(Object.class));
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import com.krylov.refound.config.ClusterMessaging;
|
||||
|
||||
class ChatServiceTest {
|
||||
|
||||
@@ -41,7 +41,7 @@ class ChatServiceTest {
|
||||
userRepository = mock(UserRepository.class);
|
||||
postRepository = mock(PostRepository.class);
|
||||
messageRepository = mock(MessageRepository.class);
|
||||
chatService = new ChatService(chatRepository, messageRepository, mock(SimpMessagingTemplate.class),
|
||||
chatService = new ChatService(chatRepository, messageRepository, mock(ClusterMessaging.class),
|
||||
userRepository, postRepository);
|
||||
|
||||
when(userRepository.existsById(anyLong())).thenReturn(true);
|
||||
|
||||
Reference in New Issue
Block a user