@Override public final void start() { synchronized (this.lifecycleMonitor) { this.clientInboundChannel.subscribe(this); this.running = true; } }
@Override public final void stop() { synchronized (this.lifecycleMonitor) { this.running = false; this.clientInboundChannel.unsubscribe(this); } }
@Test public void handleSubscribe() { given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true); this.handler.handleMessage(createWith(SimpMessageType.SUBSCRIBE, "joe", SESSION_ID, "/user/queue/foo")); ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class); Mockito.verify(this.brokerChannel).send(captor.capture()); Message message = captor.getValue(); assertEquals("/queue/foo-user123", SimpMessageHeaderAccessor.getDestination(message.getHeaders())); }
@Override @SuppressWarnings("unchecked") public void subscribe(Subscriber<? super Message<T>> subscriber) { Flux. <Message<?>>create(emitter -> { MessageHandler messageHandler = emitter::next; this.channel.subscribe(messageHandler); emitter.onCancel(() -> this.channel.unsubscribe(messageHandler)); }, FluxSink.OverflowStrategy.IGNORE) .subscribe((Subscriber<? super Message<?>>) subscriber); }
@Test public void testFindByEntityClass() throws Exception { this.handler = message -> { assertThat(message.getPayload(), Matchers.instanceOf(StudentDomain.class)); StudentDomain student = (StudentDomain) message.getPayload(); assertEquals("First One", student.getFirstName()); }; this.responseChannel.subscribe(this.handler); Message<Long> message = MessageBuilder.withPayload(1001L).build(); this.findByEntityClassChannel.send(message); }
@Test @SuppressWarnings("unchecked") public void testIgnoredHeader() { MessageHandler messageHandler = mock(MessageHandler.class); ((SubscribableChannel) this.errorChannel).subscribe(messageHandler); this.ignoredHeaderGateway.service("foo", "theHeaderValue"); ArgumentCaptor<Message<?>> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); verify(messageHandler).handleMessage(messageArgumentCaptor.capture()); Message<?> message = messageArgumentCaptor.getValue(); assertFalse(message.getHeaders().containsKey(IGNORE_HEADER)); ((SubscribableChannel) this.errorChannel).unsubscribe(messageHandler); }
@Test public void validateGatewayInTheChainViaChannel() { output.subscribe(message -> { Assert.assertEquals("echo:echo:echo:hello", message.getPayload()); Assert.assertEquals("foo", message.getHeaders().get("foo")); Assert.assertEquals("oleg", message.getHeaders().get("name")); }); channel.send(new GenericMessage<String>("hello")); }
@Test public void testOutboundDefaultInboundReplyTrueRequestFalse() { this.outboundGateway.setExtractRequestPayload(true); this.outboundGateway.setExtractReplyPayload(true); this.inboundGateway.setExtractReplyPayload(true); this.inboundGateway.setExtractRequestPayload(false); MessageHandler handler = unwrapTextMessageAndEchoHandler(); this.jmsInputChannel.subscribe(handler); this.outboundChannel.send(new GenericMessage<String>("Hello " + this.testName.getMethodName())); Message<?> replyMessage = this.replyChannel.receive(10000); assertThat(replyMessage.getPayload(), instanceOf(String.class)); this.jmsInputChannel.unsubscribe(handler); }
@Test public void testFindByPayloadType() throws Exception { this.handler = message -> { assertThat(message.getPayload(), Matchers.instanceOf(StudentDomain.class)); StudentDomain student = (StudentDomain) message.getPayload(); assertEquals("First Two", student.getFirstName()); }; this.responseChannel.subscribe(this.handler); StudentDomain payload = new StudentDomain(); payload.setRollNumber(1002L); Message<StudentDomain> message = MessageBuilder.withPayload(payload).build(); this.findByPayloadTypeChannel.send(message); }
@Override public final void stop() { synchronized (this.lifecycleMonitor) { this.running = false; this.clientInboundChannel.unsubscribe(this); this.brokerChannel.unsubscribe(this); } }
@Test public void handleUnsubscribe() { given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true); this.handler.handleMessage(createWith(SimpMessageType.UNSUBSCRIBE, "joe", "123", "/user/queue/foo")); ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class); Mockito.verify(this.brokerChannel).send(captor.capture()); Message message = captor.getValue(); assertEquals("/queue/foo-user123", SimpMessageHeaderAccessor.getDestination(message.getHeaders())); }
@Test public void testOutboundRequestFalseReplyTrueInboundDefault() { this.outboundGateway.setExtractRequestPayload(false); this.outboundGateway.setExtractReplyPayload(true); this.inboundGateway.setExtractReplyPayload(true); this.inboundGateway.setExtractRequestPayload(true); MessageHandler handler = echoInboundStringHandler(); this.jmsInputChannel.subscribe(handler); this.outboundChannel.send(new GenericMessage<String>("Hello " + this.testName.getMethodName())); Message<?> replyMessage = this.replyChannel.receive(10000); assertThat(replyMessage.getPayload(), instanceOf(String.class)); this.jmsInputChannel.unsubscribe(handler); }
@Override public final void start() { Assert.isTrue(this.defaultProtocolHandler != null || !this.protocolHandlers.isEmpty(), "No handlers"); synchronized (this.lifecycleMonitor) { this.clientOutboundChannel.subscribe(this); this.running = true; } }
/** * Sends a message with the payload as a integer representing the start number in the result * set and a header with value maxResults to get the max number of results * @throws Exception */ @Test public void retrieveFromSecondRecordAndMaximumOneRecord() throws Exception { this.handler = message -> { assertEquals(2, ((List<?>) message.getPayload()).size()); assertEquals(1, entityManager.createQuery("from Student").getResultList().size()); assertThat(message, HeaderMatcher.hasHeader("maxResults", "10")); }; this.responseChannel.subscribe(this.handler); Message<Integer> message = MessageBuilder .withPayload(1) .setHeader("maxResults", "10") .build(); this.requestChannel.send(message); }
@Override public void stop() { synchronized (this.lifecycleMonitor) { logger.info("Stopping..."); stopInternal(); this.clientInboundChannel.unsubscribe(this); this.brokerChannel.unsubscribe(this); if (this.clientInboundChannel instanceof InterceptableChannel) { ((InterceptableChannel) this.clientInboundChannel).removeInterceptor(this.unsentDisconnectInterceptor); } this.running = false; logger.info("Stopped."); } }
@Test public void handleMessageWithoutActiveSession() { this.handler.setBroadcastDestination("/topic/unresolved"); given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true); this.handler.handleMessage(createWith(SimpMessageType.MESSAGE, "joe", "123", "/user/joe/queue/foo")); ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class); Mockito.verify(this.brokerChannel).send(captor.capture()); Message message = captor.getValue(); SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(message); assertEquals("/topic/unresolved", accessor.getDestination()); assertEquals("/user/joe/queue/foo", accessor.getFirstNativeHeader(ORIGINAL_DESTINATION)); // Should ignore our own broadcast to brokerChannel this.handler.handleMessage(message); Mockito.verifyNoMoreInteractions(this.brokerChannel); }
@Test public void testOutboundDefaultInboundBothFalse() throws Exception { this.outboundGateway.setExtractRequestPayload(true); this.outboundGateway.setExtractReplyPayload(true); this.inboundGateway.setExtractReplyPayload(false); this.inboundGateway.setExtractRequestPayload(false); MessageHandler handler = unwrapTextMessageAndEchoHandler(); this.jmsInputChannel.subscribe(handler); this.outboundChannel.send(new GenericMessage<String>("Hello " + this.testName.getMethodName())); Message<?> replyMessage = this.replyChannel.receive(10000); assertThat(replyMessage.getPayload(), instanceOf(String.class)); this.jmsInputChannel.unsubscribe(handler); }
@Override public void start() { synchronized (this.lifecycleMonitor) { logger.info("Starting..."); this.clientInboundChannel.subscribe(this); this.brokerChannel.subscribe(this); if (this.clientInboundChannel instanceof InterceptableChannel) { ((InterceptableChannel) this.clientInboundChannel).addInterceptor(0, this.unsentDisconnectInterceptor); } startInternal(); this.running = true; logger.info("Started."); } }
@Override public final void stop() { synchronized (this.lifecycleMonitor) { this.running = false; this.clientOutboundChannel.unsubscribe(this); } // Proactively notify all active WebSocket sessions for (WebSocketSessionHolder holder : this.sessions.values()) { try { holder.getSession().close(CloseStatus.GOING_AWAY); } catch (Throwable ex) { if (logger.isWarnEnabled()) { logger.warn("Failed to close '" + holder.getSession() + "': " + ex); } } } }