@Override protected void handleInboundMessage(Message<?> message) { StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); if (accessor != null && StompCommand.MESSAGE.equals(accessor.getCommand())) { String destination = accessor.getDestination(); if (destination == null) { if (logger.isDebugEnabled()) { logger.debug("Got message on \"system\" connection, with no destination: " + accessor.getDetailedLogMessage(message.getPayload())); } return; } if (!getSystemSubscriptions().containsKey(destination)) { if (logger.isDebugEnabled()) { logger.debug("Got message on \"system\" connection with no handler: " + accessor.getDetailedLogMessage(message.getPayload())); } return; } try { MessageHandler handler = getSystemSubscriptions().get(destination); handler.handleMessage(message); } catch (Throwable ex) { if (logger.isDebugEnabled()) { logger.debug("Error while handling message on \"system\" connection.", ex); } } } }
@Override public void run() { Message<?> message = this.inputMessage; try { message = applyBeforeHandle(message); if (message == null) { return; } this.messageHandler.handleMessage(message); triggerAfterMessageHandled(message, null); } catch (Exception ex) { triggerAfterMessageHandled(message, ex); if (ex instanceof MessagingException) { throw (MessagingException) ex; } String description = "Failed to handle " + message + " to " + this + " in " + this.messageHandler; throw new MessageDeliveryException(message, description, ex); } catch (Throwable err) { String description = "Failed to handle " + message + " to " + this + " in " + this.messageHandler; MessageDeliveryException ex2 = new MessageDeliveryException(message, description, err); triggerAfterMessageHandled(message, ex2); throw ex2; } }
@Test public void subscribeTwice() { assertThat(this.channel.subscribe(this.handler), equalTo(true)); assertThat(this.channel.subscribe(this.handler), equalTo(false)); this.channel.send(this.message); verify(this.handler, times(1)).handleMessage(this.message); }
@Test public void unsubscribeTwice() { this.channel.subscribe(this.handler); assertThat(this.channel.unsubscribe(this.handler), equalTo(true)); assertThat(this.channel.unsubscribe(this.handler), equalTo(false)); this.channel.send(this.message); verify(this.handler, never()).handleMessage(this.message); }
@Test public void sendWithExecutor() { BeforeHandleInterceptor interceptor = new BeforeHandleInterceptor(); TaskExecutor executor = mock(TaskExecutor.class); ExecutorSubscribableChannel testChannel = new ExecutorSubscribableChannel(executor); testChannel.addInterceptor(interceptor); testChannel.subscribe(this.handler); testChannel.send(this.message); verify(executor).execute(this.runnableCaptor.capture()); verify(this.handler, never()).handleMessage(this.message); this.runnableCaptor.getValue().run(); verify(this.handler).handleMessage(this.message); assertEquals(1, interceptor.getCounter().get()); assertTrue(interceptor.wasAfterHandledInvoked()); }
@Test public void concurrentModification() { this.channel.subscribe(message1 -> channel.unsubscribe(handler)); this.channel.subscribe(this.handler); this.channel.send(this.message); verify(this.handler).handleMessage(this.message); }
@Test public void interceptorWithException() { IllegalStateException expected = new IllegalStateException("Fake exception"); willThrow(expected).given(this.handler).handleMessage(this.message); BeforeHandleInterceptor interceptor = new BeforeHandleInterceptor(); this.channel.addInterceptor(interceptor); this.channel.subscribe(this.handler); try { this.channel.send(this.message); } catch (MessageDeliveryException actual) { assertSame(expected, actual.getCause()); } verify(this.handler).handleMessage(this.message); assertEquals(1, interceptor.getCounter().get()); assertTrue(interceptor.wasAfterHandledInvoked()); }
@Test public void failurePropagates() { RuntimeException ex = new RuntimeException(); willThrow(ex).given(this.handler).handleMessage(this.message); MessageHandler secondHandler = mock(MessageHandler.class); this.channel.subscribe(this.handler); this.channel.subscribe(secondHandler); try { this.channel.send(message); } catch (MessageDeliveryException actualException) { assertThat(actualException.getCause(), equalTo(ex)); } verifyZeroInteractions(secondHandler); }
@Test public void sendWithoutExecutor() { BeforeHandleInterceptor interceptor = new BeforeHandleInterceptor(); this.channel.addInterceptor(interceptor); this.channel.subscribe(this.handler); this.channel.send(this.message); verify(this.handler).handleMessage(this.message); assertEquals(1, interceptor.getCounter().get()); assertTrue(interceptor.wasAfterHandledInvoked()); }
@Test public void systemSubscription() throws Exception { MessageHandler handler = mock(MessageHandler.class); this.brokerRelay.setSystemSubscriptions(Collections.singletonMap("/topic/foo", handler)); this.brokerRelay.start(); StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.CONNECTED); accessor.setLeaveMutable(true); MessageHeaders headers = accessor.getMessageHeaders(); this.tcpClient.handleMessage(MessageBuilder.createMessage(new byte[0], headers)); assertEquals(2, this.tcpClient.getSentMessages().size()); assertEquals(StompCommand.CONNECT, this.tcpClient.getSentHeaders(0).getCommand()); assertEquals(StompCommand.SUBSCRIBE, this.tcpClient.getSentHeaders(1).getCommand()); assertEquals("/topic/foo", this.tcpClient.getSentHeaders(1).getDestination()); Message<byte[]> message = message(StompCommand.MESSAGE, null, null, "/topic/foo"); this.tcpClient.handleMessage(message); ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class); verify(handler).handleMessage(captor.capture()); assertSame(message, captor.getValue()); }
@Override protected Object handleRequestMessage(Message<?> requestMessage) { this.target.handleMessage(requestMessage); return null; }
@Test public void interceptorWithModifiedMessage() { Message<?> expected = mock(Message.class); BeforeHandleInterceptor interceptor = new BeforeHandleInterceptor(); interceptor.setMessageToReturn(expected); this.channel.addInterceptor(interceptor); this.channel.subscribe(this.handler); this.channel.send(this.message); verify(this.handler).handleMessage(expected); assertEquals(1, interceptor.getCounter().get()); assertTrue(interceptor.wasAfterHandledInvoked()); }
@Override protected void handleMessageInternal(Message<?> message) throws Exception { if (!this.initialized) { this.onInit(); } this.handlers.get(0).handleMessage(message); }
protected boolean tryOptimizedDispatch(Message<?> message) { MessageHandler handler = this.theOneHandler; if (handler != null) { try { handler.handleMessage(message); return true; } catch (Exception e) { throw IntegrationUtils.wrapInDeliveryExceptionIfNecessary(message, () -> "Dispatcher failed to deliver Message", e); } } return false; }
@Override public void handleMessage(Message<?> message) { messageHandler.handleMessage(message); output.send(message); } }
@Override public void run() { try { this.aggregator.handleMessage(message); } catch (Exception e) { this.exception = e; } finally { this.latch.countDown(); } }
private void sendAsynchronously(final MessageHandler handler, final Message<Object> message, final CountDownLatch start, final CountDownLatch sent, ExecutorService exec) { exec.execute(() -> { try { start.await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } handler.handleMessage(message); sent.countDown(); }); }
@Test public void anyMatcher_withVerifyArgumentMatcherAndEqualPayload_matching() throws Exception { handler.handleMessage(message); verify(handler).handleMessage(messageWithPayload(SOME_PAYLOAD)); verify(handler).handleMessage(messageWithPayload(is(instanceOf(Date.class)))); }
@Test(expected = ArgumentsAreDifferent.class) public void anyMatcher_withVerifyAndDifferentPayload_notMatching() throws Exception { handler.handleMessage(message); verify(handler).handleMessage(messageWithPayload(nullValue())); }
@Test public void testWithServiceToString() { ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("GatewayInterfaceTests-context.xml", getClass()); DirectChannel channel = ac.getBean("requestChannelBaz", DirectChannel.class); MessageHandler handler = mock(MessageHandler.class); channel.subscribe(handler); Bar bar = ac.getBean(Bar.class); bar.toString(); verify(handler, times(0)).handleMessage(Mockito.any(Message.class)); ac.close(); }