Java Code Examples for org.springframework.kafka.core.KafkaTemplate#send()
The following examples show how to use
org.springframework.kafka.core.KafkaTemplate#send() .
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: SpringKafkaTest.java From java-specialagent with Apache License 2.0 | 5 votes |
@Test public void test(final MockTracer tracer) { try (final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(KafkaConfiguration.class)) { final KafkaTemplate<Integer,String> kafkaTemplate = context.getBean(KafkaTemplate.class); kafkaTemplate.send("spring", "message"); await().atMost(15, TimeUnit.SECONDS).until(() -> tracer.finishedSpans().size(), equalTo(1)); assertEquals(1, counter.get()); assertEquals(1, tracer.finishedSpans().size()); } }
Example 2
Source File: KafkaStreamsBinderDestinationIsPatternTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@Test public void test() { SpringApplication app = new SpringApplication(ConsumingApplication.class); app.setWebApplicationType(WebApplicationType.NONE); ConfigurableApplicationContext context = app.run("--server.port=0", "--spring.cloud.stream.bindings.process-out-0.destination=out", "--spring.cloud.stream.bindings.process-in-0.destination=in.*", "--spring.cloud.stream.bindings.process-in-0.consumer.use-native-decoding=false", "--spring.cloud.stream.kafka.streams.bindings.process-in-0.consumer.destinationIsPattern=true", "--spring.cloud.stream.kafka.streams.binder.brokers=" + embeddedKafka.getBrokersAsString()); try { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> producerFactory = new DefaultKafkaProducerFactory<>( senderProps); KafkaTemplate<Integer, String> template = new KafkaTemplate<>(producerFactory, true); // send message to both topics that fit the pattern template.send("in.1", "foo1"); assertThat(KafkaTestUtils.getSingleRecord(consumer, "out").value()) .isEqualTo("foo1"); template.send("in.2", "foo2"); assertThat(KafkaTestUtils.getSingleRecord(consumer, "out").value()) .isEqualTo("foo2"); } finally { context.close(); } }
Example 3
Source File: ConsumerProducerTransactionTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@Bean public ApplicationRunner runner(KafkaTemplate<byte[], byte[]> template) { return args -> { template.send("consumer.producer.txIn", "one".getBytes()); template.send("consumer.producer.txIn", "two".getBytes()); template.send("consumer.producer.txIn", "three".getBytes()); }; }
Example 4
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testTopicPatterns() throws Exception { try (AdminClient admin = AdminClient.create( Collections.singletonMap(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, embeddedKafka.getEmbeddedKafka().getBrokersAsString()))) { admin.createTopics(Collections .singletonList(new NewTopic("topicPatterns.1", 1, (short) 1))).all() .get(); Binder binder = getBinder(); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProperties = createConsumerProperties(); consumerProperties.getExtension().setDestinationIsPattern(true); DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties)); final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<String> topic = new AtomicReference<>(); moduleInputChannel.subscribe(m -> { topic.set(m.getHeaders().get(KafkaHeaders.RECEIVED_TOPIC, String.class)); latch.countDown(); }); Binding<MessageChannel> consumerBinding = binder.bindConsumer( "topicPatterns\\..*", "testTopicPatterns", moduleInputChannel, consumerProperties); DefaultKafkaProducerFactory pf = new DefaultKafkaProducerFactory( KafkaTestUtils.producerProps(embeddedKafka.getEmbeddedKafka())); KafkaTemplate template = new KafkaTemplate(pf); template.send("topicPatterns.1", "foo"); assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); assertThat(topic.get()).isEqualTo("topicPatterns.1"); consumerBinding.unbind(); pf.destroy(); } }
Example 5
Source File: SpringKafkaMainTest.java From spring-kafka with MIT License | 5 votes |
@Test public void testReceive() throws Exception { String bootstrapServers = embeddedKafka.getBrokersAsString(); LOGGER.info("bootstrapServers='{}'", bootstrapServers); ContainerProperties containerProperties = new ContainerProperties(JAVA_TOPIC); Receiver receiver = new Receiver(); KafkaMessageListenerContainer<String, String> messageListenerContainer = ReceiverConfig.createMessageListenerContainer( containerProperties, bootstrapServers); messageListenerContainer.setupMessageListener(receiver); messageListenerContainer.start(); // wait a bit for the container to start Thread.sleep(1000); KafkaTemplate<String, String> kafkaTemplate = SenderConfig.createKafkaTemplate(bootstrapServers); kafkaTemplate.send(JAVA_TOPIC, "Hello Java!"); receiver.getLatch().await(10000, TimeUnit.MILLISECONDS); assertThat(receiver.getLatch().getCount()).isEqualTo(0); messageListenerContainer.stop(); }
Example 6
Source File: SpringBootDecisionTracingCollector.java From kogito-runtimes with Apache License 2.0 | 4 votes |
public SpringBootDecisionTracingCollector(BiFunction<String, String, org.kie.dmn.api.core.DMNModel> modelSupplier, KafkaTemplate<String, String> template, String kafkaTopicName) { collector = new DecisionTracingCollector((payload) -> template.send(kafkaTopicName, payload), modelSupplier); }
Example 7
Source File: KafkaRepository.java From kafka-service-broker with Apache License 2.0 | 4 votes |
public ListenableFuture<SendResult<Integer, String>> sendMessage(String message) throws ExecutionException, InterruptedException { KafkaTemplate<Integer, String> template = getTemplate(); ListenableFuture<SendResult<Integer, String>> future = template.send(info.getTopicName(), message); template.flush(); return future; }
Example 8
Source File: PerRecordAvroContentTypeTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@Test @Ignore public void testPerRecordAvroConentTypeAndVerifySerialization() throws Exception { SpringApplication app = new SpringApplication(SensorCountAvroApplication.class); app.setWebApplicationType(WebApplicationType.NONE); try (ConfigurableApplicationContext ignored = app.run("--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.input.consumer.useNativeDecoding=false", "--spring.cloud.stream.bindings.output.producer.useNativeEncoding=false", "--spring.cloud.stream.bindings.input.destination=sensors", "--spring.cloud.stream.bindings.output.destination=received-sensors", "--spring.cloud.stream.bindings.output.contentType=application/avro", "--spring.cloud.stream.kafka.streams.bindings.input.consumer.application-id=per-record-avro-contentType-test", "--spring.cloud.stream.kafka.streams.binder.configuration.commit.interval.ms=1000", "--spring.cloud.stream.kafka.streams.binder.brokers=" + embeddedKafka.getBrokersAsString())) { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); // Use a custom avro test serializer senderProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, TestAvroSerializer.class); DefaultKafkaProducerFactory<Integer, Sensor> pf = new DefaultKafkaProducerFactory<>( senderProps); try { KafkaTemplate<Integer, Sensor> template = new KafkaTemplate<>(pf, true); Random random = new Random(); Sensor sensor = new Sensor(); sensor.setId(UUID.randomUUID().toString() + "-v1"); sensor.setAcceleration(random.nextFloat() * 10); sensor.setVelocity(random.nextFloat() * 100); sensor.setTemperature(random.nextFloat() * 50); // Send with avro content type set. Message<?> message = MessageBuilder.withPayload(sensor) .setHeader("contentType", "application/avro").build(); template.setDefaultTopic("sensors"); template.send(message); // Serialized byte[] ^^ is received by the binding process and deserialzed // it using avro converter. // Then finally, the data will be output to a return topic as byte[] // (using the same avro converter). // Receive the byte[] from return topic ConsumerRecord<String, byte[]> cr = KafkaTestUtils .getSingleRecord(consumer, "received-sensors"); final byte[] value = cr.value(); // Convert the byte[] received back to avro object and verify that it is // the same as the one we sent ^^. AvroSchemaMessageConverter avroSchemaMessageConverter = new AvroSchemaMessageConverter(); Message<?> receivedMessage = MessageBuilder.withPayload(value) .setHeader("contentType", MimeTypeUtils.parseMimeType("application/avro")) .build(); Sensor messageConverted = (Sensor) avroSchemaMessageConverter .fromMessage(receivedMessage, Sensor.class); assertThat(messageConverted).isEqualTo(sensor); } finally { pf.destroy(); } } }
Example 9
Source File: KafkaStreamsBinderHealthIndicatorTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
private void receive(ConfigurableApplicationContext context, List<ProducerRecord<Integer, String>> records, Status expected, String... topics) throws Exception { Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("group-id0", "false", embeddedKafka); consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); DefaultKafkaConsumerFactory<String, String> cf = new DefaultKafkaConsumerFactory<>( consumerProps); Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>( senderProps); try (Consumer<String, String> consumer = cf.createConsumer()) { KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); CountDownLatch latch = new CountDownLatch(records.size()); for (ProducerRecord<Integer, String> record : records) { ListenableFuture<SendResult<Integer, String>> future = template .send(record); future.addCallback( new ListenableFutureCallback<SendResult<Integer, String>>() { @Override public void onFailure(Throwable ex) { Assert.fail(); } @Override public void onSuccess(SendResult<Integer, String> result) { latch.countDown(); } }); } latch.await(5, TimeUnit.SECONDS); embeddedKafka.consumeFromEmbeddedTopics(consumer, topics); KafkaTestUtils.getRecords(consumer, 1000); TimeUnit.SECONDS.sleep(2); checkHealth(context, expected); } finally { pf.destroy(); } }
Example 10
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testPolledConsumer() throws Exception { KafkaTestBinder binder = getBinder(); PollableSource<MessageHandler> inboundBindTarget = new DefaultPollableMessageSource( this.messageConverter); ExtendedConsumerProperties<KafkaConsumerProperties> consumerProps = createConsumerProperties(); consumerProps.setMultiplex(true); consumerProps.getExtension().setPollTimeout(1); Binding<PollableSource<MessageHandler>> binding = binder.bindPollableConsumer( "pollable,anotherOne", "group-polledConsumer", inboundBindTarget, consumerProps); Map<String, Object> producerProps = KafkaTestUtils .producerProps(embeddedKafka.getEmbeddedKafka()); KafkaTemplate template = new KafkaTemplate( new DefaultKafkaProducerFactory<>(producerProps)); template.send("pollable", "testPollable"); boolean polled = inboundBindTarget.poll(m -> { assertThat(m.getPayload()).isEqualTo("testPollable"); }); int n = 0; while (n++ < 100 && !polled) { polled = inboundBindTarget.poll(m -> { assertThat(m.getPayload()).isEqualTo("testPollable".getBytes()); }); Thread.sleep(100); } assertThat(polled).isTrue(); template.send("anotherOne", "testPollable2"); polled = inboundBindTarget.poll(m -> { assertThat(m.getPayload()).isEqualTo("testPollable2"); }); n = 0; while (n++ < 100 && !polled) { polled = inboundBindTarget.poll(m -> { assertThat(m.getPayload()).isEqualTo("testPollable2".getBytes()); }); Thread.sleep(100); } assertThat(polled).isTrue(); // Bind a second pollable consumer GH-521 consumerProps.getExtension().getConfiguration() .put(ConsumerConfig.CLIENT_ID_CONFIG, "pollable2"); PollableSource<MessageHandler> second = new DefaultPollableMessageSource( this.messageConverter); Binding<PollableSource<MessageHandler>> binding2 = binder.bindPollableConsumer( "pollable2", "group-polledConsumer2", second, consumerProps); second.poll(m -> { }); binding.unbind(); binding2.unbind(); }
Example 11
Source File: KafkaBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testPolledConsumerWithDlq() throws Exception { KafkaTestBinder binder = getBinder(); PollableSource<MessageHandler> inboundBindTarget = new DefaultPollableMessageSource( this.messageConverter); ExtendedConsumerProperties<KafkaConsumerProperties> properties = createConsumerProperties(); properties.getExtension().setPollTimeout(1); properties.setMaxAttempts(2); properties.setBackOffInitialInterval(0); properties.getExtension().setEnableDlq(true); Map<String, Object> producerProps = KafkaTestUtils .producerProps(embeddedKafka.getEmbeddedKafka()); Binding<PollableSource<MessageHandler>> binding = binder.bindPollableConsumer( "pollableDlq", "group-pcWithDlq", inboundBindTarget, properties); KafkaTemplate template = new KafkaTemplate( new DefaultKafkaProducerFactory<>(producerProps)); template.send("pollableDlq", "testPollableDLQ"); try { int n = 0; while (n++ < 100) { inboundBindTarget.poll(m -> { throw new RuntimeException("test DLQ"); }); Thread.sleep(100); } } catch (MessageHandlingException e) { assertThat(e.getCause().getMessage()).isEqualTo("test DLQ"); } Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("dlq", "false", embeddedKafka.getEmbeddedKafka()); consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); ConsumerFactory cf = new DefaultKafkaConsumerFactory<>(consumerProps); Consumer consumer = cf.createConsumer(); embeddedKafka.getEmbeddedKafka().consumeFromAnEmbeddedTopic(consumer, "error.pollableDlq.group-pcWithDlq"); ConsumerRecord deadLetter = KafkaTestUtils.getSingleRecord(consumer, "error.pollableDlq.group-pcWithDlq"); assertThat(deadLetter).isNotNull(); assertThat(deadLetter.value()).isEqualTo("testPollableDLQ"); binding.unbind(); consumer.close(); }