@Test public void handleMessage() throws Exception { TestSimpUser simpUser1 = new TestSimpUser("joe"); TestSimpUser simpUser2 = new TestSimpUser("jane"); simpUser1.addSessions(new TestSimpSession("123")); simpUser2.addSessions(new TestSimpSession("456")); HashSet<SimpUser> simpUsers = new HashSet<>(Arrays.asList(simpUser1, simpUser2)); SimpUserRegistry remoteUserRegistry = mock(SimpUserRegistry.class); when(remoteUserRegistry.getUserCount()).thenReturn(2); when(remoteUserRegistry.getUsers()).thenReturn(simpUsers); MultiServerUserRegistry remoteRegistry = new MultiServerUserRegistry(remoteUserRegistry); Message<?> message = this.converter.toMessage(remoteRegistry.getLocalRegistryDto(), null); this.handler.handleMessage(message); assertEquals(2, remoteRegistry.getUserCount()); assertNotNull(this.multiServerRegistry.getUser("joe")); assertNotNull(this.multiServerRegistry.getUser("jane")); }
@Test public void purgeExpiredRegistries() throws Exception { // Prepare broadcast message from remote server TestSimpUser testUser = new TestSimpUser("joe"); testUser.addSessions(new TestSimpSession("remote-sub")); SimpUserRegistry testRegistry = mock(SimpUserRegistry.class); when(testRegistry.getUsers()).thenReturn(Collections.singleton(testUser)); Object registryDto = new MultiServerUserRegistry(testRegistry).getLocalRegistryDto(); Message<?> message = this.converter.toMessage(registryDto, null); // Add remote registry this.registry.addRemoteRegistryDto(message, this.converter, -1); assertEquals(1, this.registry.getUserCount()); this.registry.purgeExpiredRegistries(); assertEquals(0, this.registry.getUserCount()); }
/** * Create an instance wrapping the local user registry. */ public MultiServerUserRegistry(SimpUserRegistry localRegistry) { Assert.notNull(localRegistry, "'localRegistry' is required."); this.id = generateId(); this.localRegistry = localRegistry; this.delegateApplicationEvents = this.localRegistry instanceof SmartApplicationListener; }
@Override public void run() { try { SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE); accessor.setHeader(SimpMessageHeaderAccessor.IGNORE_ERROR, true); accessor.setLeaveMutable(true); Object payload = userRegistry.getLocalRegistryDto(); brokerTemplate.convertAndSend(getBroadcastDestination(), payload, accessor.getMessageHeaders()); } finally { userRegistry.purgeExpiredRegistries(); } } }
@Test public void getUserFromLocalRegistry() throws Exception { SimpUser user = Mockito.mock(SimpUser.class); Set<SimpUser> users = Collections.singleton(user); when(this.localRegistry.getUsers()).thenReturn(users); when(this.localRegistry.getUserCount()).thenReturn(1); when(this.localRegistry.getUser("joe")).thenReturn(user); assertEquals(1, this.registry.getUserCount()); assertSame(user, this.registry.getUser("joe")); }
@Test public void findSubscriptionsFromRemoteRegistry() throws Exception { // Prepare broadcast message from remote server TestSimpUser user1 = new TestSimpUser("joe"); TestSimpUser user2 = new TestSimpUser("jane"); TestSimpUser user3 = new TestSimpUser("jack"); TestSimpSession session1 = new TestSimpSession("sess1"); TestSimpSession session2 = new TestSimpSession("sess2"); TestSimpSession session3 = new TestSimpSession("sess3"); session1.addSubscriptions(new TestSimpSubscription("sub1", "/match")); session2.addSubscriptions(new TestSimpSubscription("sub1", "/match")); session3.addSubscriptions(new TestSimpSubscription("sub1", "/not-a-match")); user1.addSessions(session1); user2.addSessions(session2); user3.addSessions(session3); SimpUserRegistry userRegistry = mock(SimpUserRegistry.class); when(userRegistry.getUsers()).thenReturn(new HashSet<>(Arrays.asList(user1, user2, user3))); Object registryDto = new MultiServerUserRegistry(userRegistry).getLocalRegistryDto(); Message<?> message = this.converter.toMessage(registryDto, null); // Add remote registry this.registry.addRemoteRegistryDto(message, this.converter, 20000); assertEquals(3, this.registry.getUserCount()); Set<SimpSubscription> matches = this.registry.findSubscriptions(s -> s.getDestination().equals("/match")); assertEquals(2, matches.size()); Iterator<SimpSubscription> iterator = matches.iterator(); Set<String> sessionIds = new HashSet<>(2); sessionIds.add(iterator.next().getSession().getId()); sessionIds.add(iterator.next().getSession().getId()); assertEquals(new HashSet<>(Arrays.asList("sess1", "sess2")), sessionIds); }
@Test public void handleMessageFromOwnBroadcast() throws Exception { TestSimpUser simpUser = new TestSimpUser("joe"); simpUser.addSessions(new TestSimpSession("123")); when(this.localRegistry.getUserCount()).thenReturn(1); when(this.localRegistry.getUsers()).thenReturn(Collections.singleton(simpUser)); assertEquals(1, this.multiServerRegistry.getUserCount()); Message<?> message = this.converter.toMessage(this.multiServerRegistry.getLocalRegistryDto(), null); this.multiServerRegistry.addRemoteRegistryDto(message, this.converter, 20000); assertEquals(1, this.multiServerRegistry.getUserCount()); }
@Bean @SuppressWarnings("deprecation") public SimpUserRegistry userRegistry() { SimpUserRegistry registry = createLocalUserRegistry(); if (registry == null) { registry = createLocalUserRegistry(getBrokerRegistry().getUserRegistryOrder()); } boolean broadcast = getBrokerRegistry().getUserRegistryBroadcast() != null; return (broadcast ? new MultiServerUserRegistry(registry) : registry); }
@Override public void handleMessage(Message<?> message) throws MessagingException { MessageConverter converter = this.brokerTemplate.getMessageConverter(); this.userRegistry.addRemoteRegistryDto(message, converter, getRegistryExpirationPeriod()); }
@Override public void run() { try { SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE); accessor.setHeader(SimpMessageHeaderAccessor.IGNORE_ERROR, true); accessor.setLeaveMutable(true); Object payload = userRegistry.getLocalRegistryDto(); brokerTemplate.convertAndSend(getBroadcastDestination(), payload, accessor.getMessageHeaders()); } finally { userRegistry.purgeExpiredRegistries(); } } }
@Before public void setup() throws Exception { this.localRegistry = Mockito.mock(SimpUserRegistry.class); this.registry = new MultiServerUserRegistry(this.localRegistry); this.converter = new MappingJackson2MessageConverter(); }
@Override public void handleMessage(Message<?> message) throws MessagingException { MessageConverter converter = this.brokerTemplate.getMessageConverter(); this.userRegistry.addRemoteRegistryDto(message, converter, getRegistryExpirationPeriod()); }
@Test public void broadcastRegistry() throws Exception { TestSimpUser simpUser1 = new TestSimpUser("joe"); TestSimpUser simpUser2 = new TestSimpUser("jane"); simpUser1.addSessions(new TestSimpSession("123")); simpUser1.addSessions(new TestSimpSession("456")); HashSet<SimpUser> simpUsers = new HashSet<>(Arrays.asList(simpUser1, simpUser2)); when(this.localRegistry.getUsers()).thenReturn(simpUsers); getUserRegistryTask().run(); ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class); verify(this.brokerChannel).send(captor.capture()); Message<?> message = captor.getValue(); assertNotNull(message); MessageHeaders headers = message.getHeaders(); assertEquals("/topic/simp-user-registry", SimpMessageHeaderAccessor.getDestination(headers)); MultiServerUserRegistry remoteRegistry = new MultiServerUserRegistry(mock(SimpUserRegistry.class)); remoteRegistry.addRemoteRegistryDto(message, this.converter, 20000); assertEquals(2, remoteRegistry.getUserCount()); assertNotNull(remoteRegistry.getUser("joe")); assertNotNull(remoteRegistry.getUser("jane")); }
@Override public void run() { try { SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE); accessor.setHeader(SimpMessageHeaderAccessor.IGNORE_ERROR, true); accessor.setLeaveMutable(true); Object payload = userRegistry.getLocalRegistryDto(); brokerTemplate.convertAndSend(getBroadcastDestination(), payload, accessor.getMessageHeaders()); } finally { userRegistry.purgeExpiredRegistries(); } } }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); when(this.brokerChannel.send(any())).thenReturn(true); this.converter = new MappingJackson2MessageConverter(); SimpMessagingTemplate brokerTemplate = new SimpMessagingTemplate(this.brokerChannel); brokerTemplate.setMessageConverter(this.converter); this.localRegistry = mock(SimpUserRegistry.class); this.multiServerRegistry = new MultiServerUserRegistry(this.localRegistry); this.handler = new UserRegistryMessageHandler(this.multiServerRegistry, brokerTemplate, "/topic/simp-user-registry", this.taskScheduler); }
@Override public void handleMessage(Message<?> message) throws MessagingException { MessageConverter converter = this.brokerTemplate.getMessageConverter(); this.userRegistry.addRemoteRegistryDto(message, converter, getRegistryExpirationPeriod()); }
/** * Create an instance wrapping the local user registry. */ public MultiServerUserRegistry(SimpUserRegistry localRegistry) { Assert.notNull(localRegistry, "'localRegistry' is required."); this.id = generateId(); this.localRegistry = localRegistry; this.delegateApplicationEvents = this.localRegistry instanceof SmartApplicationListener; }
@Test // SPR-13800 public void getSessionsWhenUserIsConnectedToMultipleServers() throws Exception { // Add user to local registry TestSimpUser localUser = new TestSimpUser("joe"); TestSimpSession localSession = new TestSimpSession("sess123"); localUser.addSessions(localSession); when(this.localRegistry.getUser("joe")).thenReturn(localUser); // Prepare broadcast message from remote server TestSimpUser remoteUser = new TestSimpUser("joe"); TestSimpSession remoteSession = new TestSimpSession("sess456"); remoteUser.addSessions(remoteSession); SimpUserRegistry remoteRegistry = mock(SimpUserRegistry.class); when(remoteRegistry.getUsers()).thenReturn(Collections.singleton(remoteUser)); Object remoteRegistryDto = new MultiServerUserRegistry(remoteRegistry).getLocalRegistryDto(); Message<?> message = this.converter.toMessage(remoteRegistryDto, null); // Add remote registry this.registry.addRemoteRegistryDto(message, this.converter, 20000); assertEquals(1, this.registry.getUserCount()); SimpUser user = this.registry.getUsers().iterator().next(); assertTrue(user.hasSessions()); assertEquals(2, user.getSessions().size()); assertThat(user.getSessions(), containsInAnyOrder(localSession, remoteSession)); assertSame(localSession, user.getSession("sess123")); assertEquals(remoteSession, user.getSession("sess456")); user = this.registry.getUser("joe"); assertEquals(2, user.getSessions().size()); assertThat(user.getSessions(), containsInAnyOrder(localSession, remoteSession)); assertSame(localSession, user.getSession("sess123")); assertEquals(remoteSession, user.getSession("sess456")); }
@Override public void run() { try { SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE); accessor.setHeader(SimpMessageHeaderAccessor.IGNORE_ERROR, true); accessor.setLeaveMutable(true); Object payload = userRegistry.getLocalRegistryDto(); brokerTemplate.convertAndSend(getBroadcastDestination(), payload, accessor.getMessageHeaders()); } finally { userRegistry.purgeExpiredRegistries(); } } }
@Bean @SuppressWarnings("deprecation") public SimpUserRegistry userRegistry() { SimpUserRegistry registry = createLocalUserRegistry(); if (registry == null) { registry = createLocalUserRegistry(getBrokerRegistry().getUserRegistryOrder()); } boolean broadcast = getBrokerRegistry().getUserRegistryBroadcast() != null; return (broadcast ? new MultiServerUserRegistry(registry) : registry); }