List of usage examples for org.springframework.integration.channel QueueChannel setBeanName
@Override public final void setBeanName(String beanName)
From source file:org.springframework.cloud.stream.binder.rabbit.RabbitBinderTests.java
@SuppressWarnings("unchecked") @Test// w w w .j a v a 2 s. com public void testBatchingAndCompression() throws Exception { RabbitTestBinder binder = getBinder(); ExtendedProducerProperties<RabbitProducerProperties> producerProperties = createProducerProperties(); producerProperties.getExtension().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT); producerProperties.getExtension().setBatchingEnabled(true); producerProperties.getExtension().setBatchSize(2); producerProperties.getExtension().setBatchBufferLimit(100000); producerProperties.getExtension().setBatchTimeout(30000); producerProperties.getExtension().setCompress(true); producerProperties.setRequiredGroups("default"); DirectChannel output = createBindableChannel("input", createProducerBindingProperties(producerProperties)); output.setBeanName("batchingProducer"); Binding<MessageChannel> producerBinding = binder.bindProducer("batching.0", output, producerProperties); Log logger = spy(TestUtils.getPropertyValue(binder, "binder.compressingPostProcessor.logger", Log.class)); new DirectFieldAccessor(TestUtils.getPropertyValue(binder, "binder.compressingPostProcessor")) .setPropertyValue("logger", logger); when(logger.isTraceEnabled()).thenReturn(true); assertThat(TestUtils.getPropertyValue(binder, "binder.compressingPostProcessor.level")) .isEqualTo(Deflater.BEST_SPEED); output.send(new GenericMessage<>("foo".getBytes())); output.send(new GenericMessage<>("bar".getBytes())); Object out = spyOn("batching.0.default").receive(false); assertThat(out).isInstanceOf(byte[].class); assertThat(new String((byte[]) out)).isEqualTo("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar"); ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); verify(logger).trace(captor.capture()); assertThat(captor.getValue().toString()).contains(("Compressed 14 to ")); QueueChannel input = new QueueChannel(); input.setBeanName("batchingConsumer"); Binding<MessageChannel> consumerBinding = binder.bindConsumer("batching.0", "test", input, createConsumerProperties()); output.send(new GenericMessage<>("foo".getBytes())); output.send(new GenericMessage<>("bar".getBytes())); Message<byte[]> in = (Message<byte[]>) input.receive(10000); assertThat(in).isNotNull(); assertThat(new String(in.getPayload())).isEqualTo("foo"); in = (Message<byte[]>) input.receive(10000); assertThat(in).isNotNull(); assertThat(new String(in.getPayload())).isEqualTo("bar"); assertThat(in.getHeaders().get(AmqpHeaders.DELIVERY_MODE)).isNull(); producerBinding.unbind(); consumerBinding.unbind(); }
From source file:org.springframework.integration.amqp.outbound.AsyncAmqpGatewayTests.java
@Test public void testConfirmsAndReturns() throws Exception { CachingConnectionFactory ccf = new CachingConnectionFactory("localhost"); ccf.setPublisherConfirms(true);/* w w w .j a va2 s. c o m*/ ccf.setPublisherReturns(true); RabbitTemplate template = new RabbitTemplate(ccf); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(ccf); container.setBeanName("replyContainer"); container.setQueueNames("asyncRQ1"); container.afterPropertiesSet(); container.start(); AsyncRabbitTemplate asyncTemplate = new AsyncRabbitTemplate(template, container); asyncTemplate.setEnableConfirms(true); asyncTemplate.setMandatory(true); SimpleMessageListenerContainer receiver = new SimpleMessageListenerContainer(ccf); receiver.setBeanName("receiver"); receiver.setQueueNames("asyncQ1"); final CountDownLatch waitForAckBeforeReplying = new CountDownLatch(1); MessageListenerAdapter messageListener = new MessageListenerAdapter( (ReplyingMessageListener<String, String>) foo -> { try { waitForAckBeforeReplying.await(10, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } return foo.toUpperCase(); }); receiver.setMessageListener(messageListener); receiver.afterPropertiesSet(); receiver.start(); AsyncAmqpOutboundGateway gateway = new AsyncAmqpOutboundGateway(asyncTemplate); Log logger = spy(TestUtils.getPropertyValue(gateway, "logger", Log.class)); given(logger.isDebugEnabled()).willReturn(true); final CountDownLatch replyTimeoutLatch = new CountDownLatch(1); willAnswer(invocation -> { invocation.callRealMethod(); replyTimeoutLatch.countDown(); return null; }).given(logger).debug(startsWith("Reply not required and async timeout for")); new DirectFieldAccessor(gateway).setPropertyValue("logger", logger); QueueChannel outputChannel = new QueueChannel(); outputChannel.setBeanName("output"); QueueChannel returnChannel = new QueueChannel(); returnChannel.setBeanName("returns"); QueueChannel ackChannel = new QueueChannel(); ackChannel.setBeanName("acks"); QueueChannel errorChannel = new QueueChannel(); errorChannel.setBeanName("errors"); gateway.setOutputChannel(outputChannel); gateway.setReturnChannel(returnChannel); gateway.setConfirmAckChannel(ackChannel); gateway.setConfirmNackChannel(ackChannel); gateway.setConfirmCorrelationExpressionString("#this"); gateway.setExchangeName(""); gateway.setRoutingKey("asyncQ1"); gateway.setBeanFactory(mock(BeanFactory.class)); gateway.afterPropertiesSet(); gateway.start(); Message<?> message = MessageBuilder.withPayload("foo").setErrorChannel(errorChannel).build(); gateway.handleMessage(message); Message<?> ack = ackChannel.receive(10000); assertNotNull(ack); assertEquals("foo", ack.getPayload()); assertEquals(true, ack.getHeaders().get(AmqpHeaders.PUBLISH_CONFIRM)); waitForAckBeforeReplying.countDown(); Message<?> received = outputChannel.receive(10000); assertNotNull(received); assertEquals("FOO", received.getPayload()); // timeout tests asyncTemplate.setReceiveTimeout(10); receiver.setMessageListener(message1 -> { }); // reply timeout with no requiresReply message = MessageBuilder.withPayload("bar").setErrorChannel(errorChannel).build(); gateway.handleMessage(message); assertTrue(replyTimeoutLatch.await(10, TimeUnit.SECONDS)); // reply timeout with requiresReply gateway.setRequiresReply(true); message = MessageBuilder.withPayload("baz").setErrorChannel(errorChannel).build(); gateway.handleMessage(message); received = errorChannel.receive(10000); assertThat(received, instanceOf(ErrorMessage.class)); ErrorMessage error = (ErrorMessage) received; assertThat(error.getPayload(), instanceOf(MessagingException.class)); assertThat(error.getPayload().getCause(), instanceOf(AmqpReplyTimeoutException.class)); asyncTemplate.setReceiveTimeout(30000); receiver.setMessageListener(messageListener); // error on sending result DirectChannel errorForce = new DirectChannel(); errorForce.setBeanName("errorForce"); errorForce.subscribe(message1 -> { throw new RuntimeException("intentional"); }); gateway.setOutputChannel(errorForce); message = MessageBuilder.withPayload("qux").setErrorChannel(errorChannel).build(); gateway.handleMessage(message); received = errorChannel.receive(10000); assertThat(received, instanceOf(ErrorMessage.class)); error = (ErrorMessage) received; assertThat(error.getPayload(), instanceOf(MessagingException.class)); assertEquals("QUX", ((MessagingException) error.getPayload()).getFailedMessage().getPayload()); gateway.setRoutingKey(UUID.randomUUID().toString()); message = MessageBuilder.withPayload("fiz").setErrorChannel(errorChannel).build(); gateway.handleMessage(message); Message<?> returned = returnChannel.receive(10000); assertNotNull(returned); assertEquals("fiz", returned.getPayload()); ackChannel.receive(10000); ackChannel.purge(null); asyncTemplate = mock(AsyncRabbitTemplate.class); RabbitMessageFuture future = asyncTemplate.new RabbitMessageFuture(null, null); willReturn(future).given(asyncTemplate).sendAndReceive(anyString(), anyString(), any(org.springframework.amqp.core.Message.class)); DirectFieldAccessor dfa = new DirectFieldAccessor(future); dfa.setPropertyValue("nackCause", "nacknack"); SettableListenableFuture<Boolean> confirmFuture = new SettableListenableFuture<Boolean>(); confirmFuture.set(false); dfa.setPropertyValue("confirm", confirmFuture); new DirectFieldAccessor(gateway).setPropertyValue("template", asyncTemplate); message = MessageBuilder.withPayload("buz").setErrorChannel(errorChannel).build(); gateway.handleMessage(message); ack = ackChannel.receive(10000); assertNotNull(ack); assertEquals("buz", ack.getPayload()); assertEquals("nacknack", ack.getHeaders().get(AmqpHeaders.PUBLISH_CONFIRM_NACK_CAUSE)); assertEquals(false, ack.getHeaders().get(AmqpHeaders.PUBLISH_CONFIRM)); asyncTemplate.stop(); receiver.stop(); ccf.destroy(); }
From source file:org.springframework.xd.dirt.integration.bus.rabbit.RabbitMessageBusTests.java
@SuppressWarnings("unchecked") @Test/*from w w w . ja va2 s .c om*/ public void testBatchingAndCompression() throws Exception { RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource()); MessageBus bus = getMessageBus(); Properties properties = new Properties(); properties.put("deliveryMode", "NON_PERSISTENT"); properties.put("batchingEnabled", "true"); properties.put("batchSize", "2"); properties.put("batchBufferLimit", "100000"); properties.put("batchTimeout", "30000"); properties.put("compress", "true"); DirectChannel output = new DirectChannel(); output.setBeanName("batchingProducer"); bus.bindProducer("batching.0", output, properties); while (template.receive("xdbus.batching.0") != null) { } Log logger = spy(TestUtils.getPropertyValue(bus, "messageBus.compressingPostProcessor.logger", Log.class)); new DirectFieldAccessor(TestUtils.getPropertyValue(bus, "messageBus.compressingPostProcessor")) .setPropertyValue("logger", logger); when(logger.isTraceEnabled()).thenReturn(true); assertEquals(Deflater.BEST_SPEED, TestUtils.getPropertyValue(bus, "messageBus.compressingPostProcessor.level")); output.send(new GenericMessage<>("foo".getBytes())); output.send(new GenericMessage<>("bar".getBytes())); Object out = spyOn("batching.0").receive(false); assertThat(out, instanceOf(byte[].class)); assertEquals("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar", new String((byte[]) out)); ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); verify(logger).trace(captor.capture()); assertThat(captor.getValue().toString(), containsString("Compressed 14 to ")); QueueChannel input = new QueueChannel(); input.setBeanName("batchingConsumer"); bus.bindConsumer("batching.0", input, null); output.send(new GenericMessage<>("foo".getBytes())); output.send(new GenericMessage<>("bar".getBytes())); Message<byte[]> in = (Message<byte[]>) input.receive(10000); assertNotNull(in); assertEquals("foo", new String(in.getPayload())); in = (Message<byte[]>) input.receive(10000); assertNotNull(in); assertEquals("bar", new String(in.getPayload())); assertNull(in.getHeaders().get(AmqpHeaders.DELIVERY_MODE)); bus.unbindProducers("batching.0"); bus.unbindConsumers("batching.0"); }