@Bean public AbstractReplyProducingMessageHandler handler1() { AbstractReplyProducingMessageHandler handler = new RequestHeaderCopyingEchoHandler(); handler.setOutputChannel(pubSub()); return handler; }
@Bean public AbstractReplyProducingMessageHandler handler2() { AbstractReplyProducingMessageHandler handler = new RequestHeaderCopyingEchoHandler(); handler.setOutputChannel(out()); return handler; }
@Bean public AbstractReplyProducingMessageHandler handler3() { AbstractReplyProducingMessageHandler handler = new RequestHeaderCopyingEchoHandler(); handler.setOutputChannel(out()); return handler; }
@Test public void errorMessageShouldContainChannelName() { this.handler.setOutputChannel(this.channel); given(this.channel.send(this.message)).willReturn(false); given(this.channel.toString()).willReturn("testChannel"); try { this.handler.handleMessage(this.message); fail("Expected a MessagingException"); } catch (MessagingException e) { assertThat(e.getMessage(), containsString("'testChannel'")); } }
@Test public void testCustomAccessor() { QueueChannel outputChannel = new QueueChannel(); fooHandler.setOutputChannel(outputChannel); Foo foo = new Foo("baz"); fooHandler.handleMessage(new GenericMessage<Foo>(foo)); Message<?> reply = outputChannel.receive(0); assertNotNull(reply); assertTrue(reply.getPayload() instanceof String); assertEquals("baz", reply.getPayload()); assertEquals(3, TestUtils.getPropertyValue(this.evaluationContextFactoryBean, "propertyAccessors", Map.class).size()); }
@Test public void testGoodResultWithNoReplyChannelHeaderNoOutput() { this.whichTest = 0; this.handler.setOutputChannel(null); QueueChannel errorChannel = new QueueChannel(); Message<String> message = MessageBuilder.withPayload("foo").setErrorChannel(errorChannel).build(); this.handler.handleMessage(message); assertNull(this.output.receive(0)); this.latch.countDown(); Message<?> errorMessage = errorChannel.receive(10000); assertNotNull(errorMessage); assertThat(errorMessage.getPayload(), instanceOf(DestinationResolutionException.class)); assertEquals("no output-channel or replyChannel header available", ((Throwable) errorMessage.getPayload()).getMessage()); assertNull(((MessagingException) errorMessage.getPayload()).getFailedMessage()); assertNotNull(this.failedCallbackException); assertThat(this.failedCallbackException.getMessage(), containsString("or replyChannel header")); }
@Test public void testCustomFunction() { QueueChannel outputChannel = new QueueChannel(); barHandler.setOutputChannel(outputChannel); barHandler.handleMessage(new GenericMessage<String>("foo")); Message<?> reply = outputChannel.receive(0); assertNotNull(reply); assertEquals("bar", reply.getPayload()); }
@Test @SuppressWarnings("unchecked") public void testNotPropagateAddWhenNonExist() { AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override protected Object handleRequestMessage(Message<?> requestMessage) { return new GenericMessage<>("world", Collections.singletonMap("bar", "RAB")); } }; handler.addNotPropagatedHeaders("boom"); assertThat(handler.getNotPropagatedHeaders(), containsInAnyOrder("boom")); handler.setOutputChannel(this.channel); ArgumentCaptor<Message<?>> captor = ArgumentCaptor.forClass(Message.class); willReturn(true).given(this.channel).send(captor.capture()); handler.handleMessage(MessageBuilder.withPayload("hello") .setHeader("boom", "FOO") .setHeader("bar", "BAR") .setHeader("baz", "BAZ") .build()); Message<?> out = captor.getValue(); assertThat(out, notNullValue()); assertThat(out.getHeaders().get("boom"), nullValue()); assertThat(out.getHeaders().get("bar"), equalTo("RAB")); assertThat(out.getHeaders().get("baz"), equalTo("BAZ")); }
@Test public void defaultStatefulRetryRecoverAfterThirdTry() { final AtomicInteger counter = new AtomicInteger(3); AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override protected Object handleRequestMessage(Message<?> requestMessage) { if (counter.getAndDecrement() > 0) { throw new RuntimeException("foo"); } return "bar"; } }; QueueChannel replies = new QueueChannel(); handler.setOutputChannel(replies); RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice(); advice.setRetryStateGenerator(message -> new DefaultRetryState(message.getHeaders().getId())); defaultStatefulRetryRecoverAfterThirdTryGuts(counter, handler, replies, advice); }
@Test public void defaultStatefulRetryRecoverAfterThirdTrySpelState() { final AtomicInteger counter = new AtomicInteger(3); AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override protected Object handleRequestMessage(Message<?> requestMessage) { if (counter.getAndDecrement() > 0) { throw new RuntimeException("foo"); } return "bar"; } }; QueueChannel replies = new QueueChannel(); handler.setOutputChannel(replies); RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice(); advice.setRetryStateGenerator(new SpelExpressionRetryStateGenerator("headers['id']")); defaultStatefulRetryRecoverAfterThirdTryGuts(counter, handler, replies, advice); }
@Test public void defaultRetrySucceedOnThirdTry() { final AtomicInteger counter = new AtomicInteger(2); AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override protected Object handleRequestMessage(Message<?> requestMessage) { if (counter.getAndDecrement() > 0) { throw new RuntimeException("foo"); } return "bar"; } }; QueueChannel replies = new QueueChannel(); handler.setOutputChannel(replies); RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice(); List<Advice> adviceChain = new ArrayList<Advice>(); adviceChain.add(advice); handler.setAdviceChain(adviceChain); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); Message<String> message = new GenericMessage<>("Hello, world!"); handler.handleMessage(message); assertTrue(counter.get() == -1); Message<?> reply = replies.receive(10000); assertNotNull(reply); assertEquals("bar", reply.getPayload()); }
@Test @SuppressWarnings("unchecked") public void testNotPropagateAdd() { AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override protected Object handleRequestMessage(Message<?> requestMessage) { return new GenericMessage<>("world", Collections.singletonMap("bar", "RAB")); } }; assertThat(handler.getNotPropagatedHeaders(), emptyCollectionOf(String.class)); handler.setNotPropagatedHeaders("foo"); handler.addNotPropagatedHeaders("b*r"); handler.setOutputChannel(this.channel); assertThat(handler.getNotPropagatedHeaders(), containsInAnyOrder("foo", "b*r")); ArgumentCaptor<Message<?>> captor = ArgumentCaptor.forClass(Message.class); willReturn(true).given(this.channel).send(captor.capture()); handler.handleMessage( MessageBuilder.withPayload("hello") .setHeader("foo", "FOO") .setHeader("bar", "BAR") .setHeader("baz", "BAZ") .build()); Message<?> out = captor.getValue(); assertThat(out, notNullValue()); assertThat(out.getHeaders().get("foo"), nullValue()); assertThat(out.getHeaders().get("bar"), equalTo("RAB")); assertThat(out.getHeaders().get("baz"), equalTo("BAZ")); }
@Test @SuppressWarnings("unchecked") public void testNotPropagate() { AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override protected Object handleRequestMessage(Message<?> requestMessage) { return new GenericMessage<>("world", Collections.singletonMap("bar", "RAB")); } }; assertThat(handler.getNotPropagatedHeaders(), emptyCollectionOf(String.class)); handler.setNotPropagatedHeaders("f*", "*r"); handler.setOutputChannel(this.channel); assertThat(handler.getNotPropagatedHeaders(), containsInAnyOrder("f*", "*r")); ArgumentCaptor<Message<?>> captor = ArgumentCaptor.forClass(Message.class); willReturn(true).given(this.channel).send(captor.capture()); handler.handleMessage(MessageBuilder.withPayload("hello") .setHeader("foo", "FOO") .setHeader("bar", "BAR") .setHeader("baz", "BAZ") .build()); Message<?> out = captor.getValue(); assertThat(out, notNullValue()); assertThat(out.getHeaders().get("foo"), nullValue()); assertThat(out.getHeaders().get("bar"), equalTo("RAB")); assertThat(out.getHeaders().get("baz"), equalTo("BAZ")); }
@Test public void testGatewayWithException() throws Exception { this.whichTest = 0; GatewayProxyFactoryBean gpfb = new GatewayProxyFactoryBean(Foo.class); gpfb.setBeanFactory(mock(BeanFactory.class)); DirectChannel input = new DirectChannel(); gpfb.setDefaultRequestChannel(input); gpfb.setDefaultReplyTimeout(10000L); gpfb.afterPropertiesSet(); Foo foo = (Foo) gpfb.getObject(); this.handler.setOutputChannel(null); EventDrivenConsumer consumer = new EventDrivenConsumer(input, this.handler); consumer.afterPropertiesSet(); consumer.start(); this.latch.countDown(); try { foo.exchange("foo"); } catch (MessagingException e) { assertThat(e.getClass().getSimpleName(), equalTo("RuntimeException")); assertThat(e.getMessage(), equalTo("foo")); } }
@Test(expected = MessagingException.class) public void exceptionThrownFromRegisteredEndpoint() { AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @Override public Object handleRequestMessage(Message<?> message) { throw new RuntimeException("intentional test failure"); } }; handler.setOutputChannel(targetChannel); EventDrivenConsumer endpoint = new EventDrivenConsumer(sourceChannel, handler); this.context.registerEndpoint("testEndpoint", endpoint); this.context.refresh(); this.sourceChannel.send(new GenericMessage<>("foo")); }
handler.setOutputChannel(replies); RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice();
@Test public void testGoodResultWithReplyChannelHeader() { this.whichTest = 0; this.handler.setOutputChannel(null); QueueChannel replyChannel = new QueueChannel(); Message<?> message = MessageBuilder.withPayload("foo") .setReplyChannel(replyChannel) .build(); this.handler.handleMessage(message); assertNull(replyChannel.receive(0)); this.latch.countDown(); Message<?> received = replyChannel.receive(10000); assertNotNull(received); assertEquals("reply", received.getPayload()); assertNull(this.failedCallbackException); }
@Test public void testGateway() throws Exception { this.whichTest = 0; GatewayProxyFactoryBean gpfb = new GatewayProxyFactoryBean(Foo.class); gpfb.setBeanFactory(mock(BeanFactory.class)); DirectChannel input = new DirectChannel(); gpfb.setDefaultRequestChannel(input); gpfb.setDefaultReplyTimeout(10000L); gpfb.afterPropertiesSet(); Foo foo = (Foo) gpfb.getObject(); this.handler.setOutputChannel(null); EventDrivenConsumer consumer = new EventDrivenConsumer(input, this.handler); consumer.afterPropertiesSet(); consumer.start(); this.latch.countDown(); String result = foo.exchange("foo"); assertEquals("reply", result); }
context.registerChannel("output1", outputChannel1); context.registerChannel("output2", outputChannel2); handler1.setOutputChannel(outputChannel1); handler2.setOutputChannel(outputChannel2); EventDrivenConsumer endpoint1 = new EventDrivenConsumer(inputChannel, handler1); EventDrivenConsumer endpoint2 = new EventDrivenConsumer(inputChannel, handler2);
context.registerChannel("output1", outputChannel1); context.registerChannel("output2", outputChannel2); handler1.setOutputChannel(outputChannel1); handler2.setOutputChannel(outputChannel2); PollingConsumer endpoint1 = new PollingConsumer(inputChannel, handler1); endpoint1.setBeanFactory(mock(BeanFactory.class));