Java Code Examples for org.springframework.integration.channel.QueueChannel#receive()
The following examples show how to use
org.springframework.integration.channel.QueueChannel#receive() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageConverterConfigurerTests.java From spring-cloud-stream with Apache License 2.0 | 6 votes |
public void testConfigureOutputChannelWithBadContentType() { BindingServiceProperties props = new BindingServiceProperties(); BindingProperties bindingProps = new BindingProperties(); bindingProps.setContentType("application/json"); props.setBindings(Collections.singletonMap("foo", bindingProps)); CompositeMessageConverterFactory converterFactory = new CompositeMessageConverterFactory( Collections.<MessageConverter>emptyList(), null); MessageConverterConfigurer configurer = new MessageConverterConfigurer(props, converterFactory.getMessageConverterForAllRegistered()); QueueChannel out = new QueueChannel(); configurer.configureOutputChannel(out, "foo"); out.send(new GenericMessage<Foo>(new Foo(), Collections .<String, Object>singletonMap(MessageHeaders.CONTENT_TYPE, "bad/ct"))); Message<?> received = out.receive(0); assertThat(received).isNotNull(); assertThat(received.getPayload()).isInstanceOf(Foo.class); }
Example 2
Source File: Consumer.java From spring-kafka-demo with Apache License 2.0 | 5 votes |
@SuppressWarnings({ "unchecked", "unchecked", "rawtypes" }) public static void main(String[] args) { ch.qos.logback.classic.Logger rootLogger = (ch.qos.logback.classic.Logger)LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME); rootLogger.setLevel(Level.toLevel("info")); final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(CONFIG, Consumer.class); ctx.start(); final QueueChannel channel = ctx.getBean("inputFromKafka", QueueChannel.class); Message msg; while((msg = channel.receive()) != null) { HashMap map = (HashMap)msg.getPayload(); Set<Map.Entry> set = map.entrySet(); for (Map.Entry entry : set) { String topic = (String)entry.getKey(); System.out.println("Topic:" + topic); ConcurrentHashMap<Integer,List<byte[]>> messages = (ConcurrentHashMap<Integer,List<byte[]>>)entry.getValue(); Collection<List<byte[]>> values = messages.values(); for (Iterator<List<byte[]>> iterator = values.iterator(); iterator.hasNext();) { List<byte[]> list = iterator.next(); for (byte[] object : list) { String message = new String(object); System.out.println("\tMessage: " + message); } } } } try { Thread.sleep(100000); } catch (InterruptedException e) { e.printStackTrace(); } ctx.close(); }
Example 3
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testDlqWithNativeDecodingOnConsumerButMissingSerializerOnDlqProducer() throws Exception { Binder binder = getBinder(); ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties(); // Native serialization for producer producerProperties.setUseNativeEncoding(true); Map<String, String> producerConfig = new HashMap<>(); producerConfig.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); producerProperties.getExtension().setConfiguration(producerConfig); BindingProperties outputBindingProperties = createProducerBindingProperties( producerProperties); DirectChannel moduleOutputChannel = createBindableChannel("output", outputBindingProperties); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); // Native Deserialization for consumer consumerProperties.setUseNativeDecoding(true); Map<String, String> consumerConfig = new HashMap<>(); consumerConfig.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); // No Dlq producer properties set on the consumer with a native serializer. // This should cause an error for DLQ sending. consumerProperties.getExtension().setConfiguration(consumerConfig); consumerProperties.getExtension().setEnableDlq(true); DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); Binding<MessageChannel> producerBinding = binder.bindProducer("foo.bar", moduleOutputChannel, outputBindingProperties.getProducer()); Binding<MessageChannel> consumerBinding = binder.bindConsumer("foo.bar", "testDlqWithNativeEncoding-2", moduleInputChannel, consumerProperties); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); FailingInvocationCountingMessageHandler handler = new FailingInvocationCountingMessageHandler(); moduleInputChannel.subscribe(handler); // Consumer for the DLQ destination QueueChannel dlqChannel = new QueueChannel(); ExtendedConsumerProperties<KafkaConsumerProperties> dlqConsumerProperties = createConsumerProperties(); dlqConsumerProperties.setMaxAttempts(1); Binding<MessageChannel> dlqConsumerBinding = binder.bindConsumer( "error.foo.bar." + "testDlqWithNativeEncoding-2", null, dlqChannel, dlqConsumerProperties); binderBindUnbindLatency(); Message<?> message = org.springframework.integration.support.MessageBuilder .withPayload("foo").build(); moduleOutputChannel.send(message); Message<?> receivedMessage = dlqChannel.receive(5000); // Ensure that we didn't receive anything on DLQ because of serializer config // missing on dlq producer while native Decoding is enabled. assertThat(receivedMessage).isNull(); binderBindUnbindLatency(); dlqConsumerBinding.unbind(); producerBinding.unbind(); consumerBinding.unbind(); }
Example 4
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testDlqWithProducerPropertiesSetAtBinderLevel() throws Exception { KafkaBinderConfigurationProperties binderConfiguration = createConfigurationProperties(); Map<String, String> consumerProps = new HashMap<>(); consumerProps.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); binderConfiguration.setConsumerProperties(consumerProps); Map<String, String> producerProps = new HashMap<>(); producerProps.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); binderConfiguration.setProducerProperties(producerProps); Binder binder = getBinder(binderConfiguration); ExtendedProducerProperties<KafkaProducerProperties> producerProperties = createProducerProperties(); producerProperties.setUseNativeEncoding(true); BindingProperties outputBindingProperties = createProducerBindingProperties( producerProperties); DirectChannel moduleOutputChannel = createBindableChannel("output", outputBindingProperties); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.setUseNativeDecoding(true); consumerProperties.getExtension().setEnableDlq(true); DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); Binding<MessageChannel> producerBinding = binder.bindProducer("foo.bar", moduleOutputChannel, outputBindingProperties.getProducer()); Binding<MessageChannel> consumerBinding = binder.bindConsumer("foo.bar", "tdwcapsabl", moduleInputChannel, consumerProperties); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); FailingInvocationCountingMessageHandler handler = new FailingInvocationCountingMessageHandler(); moduleInputChannel.subscribe(handler); // Consumer for the DLQ destination QueueChannel dlqChannel = new QueueChannel(); ExtendedConsumerProperties<KafkaConsumerProperties> dlqConsumerProperties = createConsumerProperties(); dlqConsumerProperties.setMaxAttempts(1); Binding<MessageChannel> dlqConsumerBinding = binder.bindConsumer( "error.foo.bar." + "tdwcapsabl", null, dlqChannel, dlqConsumerProperties); binderBindUnbindLatency(); Message<?> message = org.springframework.integration.support.MessageBuilder .withPayload("foo").build(); moduleOutputChannel.send(message); Message<?> receivedMessage = dlqChannel.receive(5000); assertThat(receivedMessage).isNotNull(); assertThat(receivedMessage.getPayload()).isEqualTo("foo"); binderBindUnbindLatency(); dlqConsumerBinding.unbind(); producerBinding.unbind(); consumerBinding.unbind(); }
Example 5
Source File: RabbitBinderTests.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") @Test 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("output", 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(); }
Example 6
Source File: RabbitBinderTests.java From spring-cloud-stream-binder-rabbit with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") @Test public void testConsumerBatching() 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); DirectChannel output = createBindableChannel("output", createProducerBindingProperties(producerProperties)); output.setBeanName("consumerBatchingProducer"); Binding<MessageChannel> producerBinding = binder.bindProducer("c.batching.0", output, producerProperties); QueueChannel input = new QueueChannel(); input.setBeanName("batchingConsumer"); ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.setBatchMode(true); consumerProperties.getExtension().setBatchSize(2); Binding<MessageChannel> consumerBinding = binder.bindConsumer("c.batching.0", "consumerBatching", input, consumerProperties); output.send(new GenericMessage<>("foo".getBytes())); output.send(new GenericMessage<>("bar".getBytes())); Message<?> in = input.receive(10000); assertThat(in).isNotNull(); assertThat(in.getPayload()).isInstanceOf(List.class); List<byte[]> payload = (List<byte[]>) in.getPayload(); assertThat(payload).hasSize(2); assertThat(payload.get(0)).isEqualTo("foo".getBytes()); assertThat(payload.get(1)).isEqualTo("bar".getBytes()); producerBinding.unbind(); consumerBinding.unbind(); }
Example 7
Source File: AbstractBinderTests.java From spring-cloud-stream with Apache License 2.0 | 4 votes |
@SuppressWarnings("rawtypes") @Test public void testSendPojoReceivePojoWithStreamListenerDefaultContentType() throws Exception { StreamListenerMessageHandler handler = this.buildStreamListener( AbstractBinderTests.class, "echoStation", Station.class); Binder binder = getBinder(); BindingProperties producerBindingProperties = createProducerBindingProperties( createProducerProperties()); DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties); BindingProperties consumerBindingProperties = createConsumerBindingProperties( createConsumerProperties()); DirectChannel moduleInputChannel = createBindableChannel("input", consumerBindingProperties); Binding<MessageChannel> producerBinding = binder.bindProducer( String.format("bad%s0a", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer()); Binding<MessageChannel> consumerBinding = binder.bindConsumer( String.format("bad%s0a", getDestinationNameDelimiter()), "test-1", moduleInputChannel, consumerBindingProperties.getConsumer()); Station station = new Station(); Message<?> message = MessageBuilder.withPayload(station).build(); moduleInputChannel.subscribe(handler); moduleOutputChannel.send(message); QueueChannel replyChannel = (QueueChannel) handler.getOutputChannel(); Message<?> replyMessage = replyChannel.receive(5000); assertThat(replyMessage.getPayload() instanceof Station).isTrue(); producerBinding.unbind(); consumerBinding.unbind(); }
Example 8
Source File: AbstractBinderTests.java From spring-cloud-stream with Apache License 2.0 | 4 votes |
@SuppressWarnings("rawtypes") @Test public void testSendJsonReceivePojoWithStreamListener() throws Exception { StreamListenerMessageHandler handler = this.buildStreamListener( AbstractBinderTests.class, "echoStation", Station.class); Binder binder = getBinder(); BindingProperties producerBindingProperties = createProducerBindingProperties( createProducerProperties()); DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties); BindingProperties consumerBindingProperties = createConsumerBindingProperties( createConsumerProperties()); DirectChannel moduleInputChannel = createBindableChannel("input", consumerBindingProperties); Binding<MessageChannel> producerBinding = binder.bindProducer( String.format("bad%s0d", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer()); Binding<MessageChannel> consumerBinding = binder.bindConsumer( String.format("bad%s0d", getDestinationNameDelimiter()), "test-4", moduleInputChannel, consumerBindingProperties.getConsumer()); String value = "{\"readings\":[{\"stationid\":\"fgh\"," + "\"customerid\":\"12345\",\"timestamp\":null}," + "{\"stationid\":\"hjk\",\"customerid\":\"222\",\"timestamp\":null}]}"; Message<?> message = MessageBuilder.withPayload(value) .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON) .build(); moduleInputChannel.subscribe(handler); moduleOutputChannel.send(message); QueueChannel channel = (QueueChannel) handler.getOutputChannel(); Message<Station> reply = (Message<Station>) channel.receive(5000); assertThat(reply).isNotNull(); assertThat(reply.getPayload() instanceof Station).isTrue(); producerBinding.unbind(); consumerBinding.unbind(); }
Example 9
Source File: AbstractBinderTests.java From spring-cloud-stream with Apache License 2.0 | 4 votes |
@SuppressWarnings("rawtypes") @Test public void testSendJsonReceiveJsonWithStreamListener() throws Exception { StreamListenerMessageHandler handler = this.buildStreamListener( AbstractBinderTests.class, "echoStationString", String.class); Binder binder = getBinder(); BindingProperties producerBindingProperties = createProducerBindingProperties( createProducerProperties()); DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties); BindingProperties consumerBindingProperties = createConsumerBindingProperties( createConsumerProperties()); DirectChannel moduleInputChannel = createBindableChannel("input", consumerBindingProperties); Binding<MessageChannel> producerBinding = binder.bindProducer( String.format("bad%s0e", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer()); Binding<MessageChannel> consumerBinding = binder.bindConsumer( String.format("bad%s0e", getDestinationNameDelimiter()), "test-5", moduleInputChannel, consumerBindingProperties.getConsumer()); String value = "{\"readings\":[{\"stationid\":\"fgh\"," + "\"customerid\":\"12345\",\"timestamp\":null}," + "{\"stationid\":\"hjk\",\"customerid\":\"222\",\"timestamp\":null}]}"; Message<?> message = MessageBuilder.withPayload(value) .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON) .build(); moduleInputChannel.subscribe(handler); moduleOutputChannel.send(message); QueueChannel channel = (QueueChannel) handler.getOutputChannel(); Message<String> reply = (Message<String>) channel.receive(5000); assertThat(reply).isNotNull(); assertThat(reply.getPayload() instanceof String).isTrue(); producerBinding.unbind(); consumerBinding.unbind(); }
Example 10
Source File: AbstractBinderTests.java From spring-cloud-stream with Apache License 2.0 | 4 votes |
@SuppressWarnings("rawtypes") @Test public void testSendPojoReceivePojoWithStreamListener() throws Exception { StreamListenerMessageHandler handler = this.buildStreamListener( AbstractBinderTests.class, "echoStation", Station.class); Binder binder = getBinder(); BindingProperties producerBindingProperties = createProducerBindingProperties( createProducerProperties()); DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties); BindingProperties consumerBindingProperties = createConsumerBindingProperties( createConsumerProperties()); DirectChannel moduleInputChannel = createBindableChannel("input", consumerBindingProperties); Binding<MessageChannel> producerBinding = binder.bindProducer( String.format("bad%s0f", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer()); Binding<MessageChannel> consumerBinding = binder.bindConsumer( String.format("bad%s0f", getDestinationNameDelimiter()), "test-6", moduleInputChannel, consumerBindingProperties.getConsumer()); Readings r1 = new Readings(); r1.setCustomerid("123"); r1.setStationid("XYZ"); Readings r2 = new Readings(); r2.setCustomerid("546"); r2.setStationid("ABC"); Station station = new Station(); station.setReadings(Arrays.asList(r1, r2)); Message<?> message = MessageBuilder.withPayload(station) .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON) .build(); moduleInputChannel.subscribe(handler); moduleOutputChannel.send(message); QueueChannel channel = (QueueChannel) handler.getOutputChannel(); Message<Station> reply = (Message<Station>) channel.receive(5000); assertThat(reply).isNotNull(); assertThat(reply.getPayload() instanceof Station).isTrue(); producerBinding.unbind(); consumerBinding.unbind(); }