Java Code Examples for org.springframework.kafka.core.KafkaTemplate#setDefaultTopic()
The following examples show how to use
org.springframework.kafka.core.KafkaTemplate#setDefaultTopic() .
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: DeserializtionErrorHandlerByBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 7 votes |
@Test @Ignore public void test() { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>( senderProps); KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("foos"); template.sendDefault(1, 7, "hello"); Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("foobar", "false", embeddedKafka); consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); DefaultKafkaConsumerFactory<String, String> cf = new DefaultKafkaConsumerFactory<>( consumerProps); Consumer<String, String> consumer1 = cf.createConsumer(); embeddedKafka.consumeFromAnEmbeddedTopic(consumer1, "error.foos.foobar-group"); ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer1, "error.foos.foobar-group"); assertThat(cr.value()).isEqualTo("hello"); assertThat(cr.partition()).isEqualTo(0); // Ensuring that the deserialization was indeed done by the binder verify(conversionDelegate).deserializeOnInbound(any(Class.class), any(KStream.class)); }
Example 2
Source File: KafkaBoardClientEmbeddedKafkaTests.java From event-store-demo with GNU General Public License v3.0 | 6 votes |
private void receiveAndValidateBoard( ConfigurableApplicationContext context ) throws Exception { Map<String, Object> senderProps = KafkaTestUtils.producerProps(kafkaEmbedded); DefaultKafkaProducerFactory<String, String> pf = new DefaultKafkaProducerFactory<>( senderProps ); KafkaTemplate<String, String> template = new KafkaTemplate<>( pf, true ); template.setDefaultTopic( RECEIVER_TOPIC ); ObjectMapper mapper = context.getBean( ObjectMapper.class ); BoardClient boardClient = context.getBean( BoardClient.class ); UUID boardUuid = UUID.randomUUID(); BoardInitialized boardInitialized = createTestBoardInitializedEvent( boardUuid ); String event = mapper.writeValueAsString( boardInitialized ); template.sendDefault( event ); Thread.sleep( 1000 ); Board board = boardClient.find( boardUuid ); assertThat( board, is( notNullValue() ) ); assertThat( board.getBoardUuid(), is( equalTo( boardUuid ) ) ); assertThat( board.getName(), is( equalTo( "New Board" ) ) ); assertThat( board.getStories().isEmpty(), is( equalTo( true ) ) ); assertThat( board.changes(), hasSize( 0 ) ); }
Example 3
Source File: Producers.java From spring-cloud-stream-samples with Apache License 2.0 | 6 votes |
public static void main(String... args) { ObjectMapper mapper = new ObjectMapper(); Serde<DomainEvent> domainEventSerde = new JsonSerde<>(DomainEvent.class, mapper); Map<String, Object> props = new HashMap<>(); props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); props.put(ProducerConfig.RETRIES_CONFIG, 0); props.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384); props.put(ProducerConfig.LINGER_MS_CONFIG, 1); props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432); props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, domainEventSerde.serializer().getClass()); DomainEvent ddEvent = new DomainEvent(); ddEvent.setBoardUuid("12345"); ddEvent.setEventType("thisisanevent"); DefaultKafkaProducerFactory<String, DomainEvent> pf = new DefaultKafkaProducerFactory<>(props); KafkaTemplate<String, DomainEvent> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("foobar"); template.sendDefault("", ddEvent); }
Example 4
Source File: KafkaStreamsNativeEncodingDecodingTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 6 votes |
@Test public void test() throws Exception { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>( senderProps); KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("decode-words-1"); template.sendDefault("foobar"); ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer, "decode-counts-1"); assertThat(cr.value().equals("Count for foobar : 1")).isTrue(); verify(conversionDelegate, never()).serializeOnOutbound(any(KStream.class)); verify(conversionDelegate, never()).deserializeOnInbound(any(Class.class), any(KStream.class)); }
Example 5
Source File: WordCountMultipleBranchesIntegrationTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 6 votes |
private void receiveAndValidate(ConfigurableApplicationContext context) throws Exception { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>( senderProps); KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("words"); template.sendDefault("english"); ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer, "counts"); assertThat(cr.value().contains("\"word\":\"english\",\"count\":1")).isTrue(); template.sendDefault("french"); template.sendDefault("french"); cr = KafkaTestUtils.getSingleRecord(consumer, "foo"); assertThat(cr.value().contains("\"word\":\"french\",\"count\":2")).isTrue(); template.sendDefault("spanish"); template.sendDefault("spanish"); template.sendDefault("spanish"); cr = KafkaTestUtils.getSingleRecord(consumer, "bar"); assertThat(cr.value().contains("\"word\":\"spanish\",\"count\":3")).isTrue(); }
Example 6
Source File: KafkaStreamsBinderMultipleInputTopicsTest.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 6 votes |
private void receiveAndValidate() throws Exception { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>( senderProps); KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("words1"); template.sendDefault("foobar1"); template.setDefaultTopic("words2"); template.sendDefault("foobar2"); // Sleep a bit so that both the messages are processed before reading from the // output topic. // Else assertions might fail arbitrarily. Thread.sleep(5000); ConsumerRecords<String, String> received = KafkaTestUtils.getRecords(consumer); List<String> wordCounts = new ArrayList<>(2); received.records("counts") .forEach((consumerRecord) -> wordCounts.add((consumerRecord.value()))); System.out.println(wordCounts); assertThat(wordCounts.contains("{\"word\":\"foobar1\",\"count\":1}")).isTrue(); assertThat(wordCounts.contains("{\"word\":\"foobar2\",\"count\":1}")).isTrue(); }
Example 7
Source File: MultipleFunctionsInSameAppTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 6 votes |
private void receiveAndValidate(String in, String... out) throws InterruptedException { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps); try { KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic(in); template.sendDefault("coffee"); ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer, out[0]); assertThat(cr.value().contains("coffee")).isTrue(); template.sendDefault("electronics"); cr = KafkaTestUtils.getSingleRecord(consumer, out[1]); assertThat(cr.value().contains("electronics")).isTrue(); Assert.isTrue(countDownLatch.await(5, TimeUnit.SECONDS), "Analyze (BiConsumer) method didn't receive all the expected records"); } finally { pf.destroy(); } }
Example 8
Source File: SpringKafkaTest.java From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
@Test public void test() throws InterruptedException { log.info("Start auto"); ContainerProperties containerProps = new ContainerProperties("topic1", "topic2"); final CountDownLatch latch = new CountDownLatch(4); containerProps.setMessageListener((MessageListener<Integer, String>) message -> { log.info("received: " + message); latch.countDown(); }); KafkaMessageListenerContainer<Integer, String> container = createContainer(containerProps); container.setBeanName("testAuto"); container.start(); Thread.sleep(1000); // wait a bit for the container to start KafkaTemplate<Integer, String> template = createTemplate(); template.setDefaultTopic("zptest"); template.sendDefault(0, "foo"); template.sendDefault(2, "bar"); template.sendDefault(0, "baz"); template.sendDefault(2, "qux"); template.flush(); assertThat(latch.await(60, TimeUnit.SECONDS)).isTrue(); container.stop(); log.info("Stop auto"); }
Example 9
Source File: KafkaNativeSerializationApplicationTests.java From spring-cloud-stream-samples with Apache License 2.0 | 6 votes |
@Test public void testSendReceive() { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka.getEmbeddedKafka()); senderProps.put("value.serializer", StringSerializer.class); DefaultKafkaProducerFactory<byte[], String> pf = new DefaultKafkaProducerFactory<>(senderProps); KafkaTemplate<byte[], String> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic(INPUT_TOPIC); template.sendDefault("foo"); Map<String, Object> consumerProps = KafkaTestUtils.consumerProps(GROUP_NAME, "false", embeddedKafka.getEmbeddedKafka()); consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); consumerProps.put("value.deserializer", MyJsonDeserializer.class); DefaultKafkaConsumerFactory<byte[], Person> cf = new DefaultKafkaConsumerFactory<>(consumerProps); Consumer<byte[], Person> consumer = cf.createConsumer(); consumer.subscribe(Collections.singleton(OUTPUT_TOPIC)); ConsumerRecords<byte[], Person> records = consumer.poll(Duration.ofSeconds(10)); consumer.commitSync(); assertThat(records.count()).isEqualTo(1); assertThat(new String(records.iterator().next().value().getName())).isEqualTo("foo"); }
Example 10
Source File: DeserializationErrorHandlerByKafkaTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@Test public void test() { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>( senderProps); KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("word1"); template.sendDefault("foobar"); template.setDefaultTopic("word2"); template.sendDefault("foobar"); Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("foobarx", "false", embeddedKafka); consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); DefaultKafkaConsumerFactory<String, String> cf = new DefaultKafkaConsumerFactory<>( consumerProps); Consumer<String, String> consumer1 = cf.createConsumer(); embeddedKafka.consumeFromEmbeddedTopics(consumer1, "error.word1.groupx", "error.word2.groupx"); ConsumerRecord<String, String> cr1 = KafkaTestUtils.getSingleRecord(consumer1, "error.word1.groupx"); assertThat(cr1.value()).isEqualTo("foobar"); ConsumerRecord<String, String> cr2 = KafkaTestUtils.getSingleRecord(consumer1, "error.word2.groupx"); assertThat(cr2.value()).isEqualTo("foobar"); // Ensuring that the deserialization was indeed done by Kafka natively verify(conversionDelegate, never()).deserializeOnInbound(any(Class.class), any(KStream.class)); verify(conversionDelegate, never()).serializeOnOutbound(any(KStream.class)); }
Example 11
Source File: KafkaStreamsWordCountApplicationTests.java From spring-cloud-stream-samples with Apache License 2.0 | 5 votes |
@Test public void testKafkaStreamsWordCountProcessor() { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps); try { KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("words"); template.sendDefault("foobar"); ConsumerRecords<String, String> cr = KafkaTestUtils.getRecords(consumer); assertThat(cr.count()).isGreaterThanOrEqualTo(1); } finally { pf.destroy(); } }
Example 12
Source File: OutboundValueNullSkippedConversionTest.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@Test public void testOutboundNullValueIsHandledGracefully() throws Exception { SpringApplication app = new SpringApplication( OutboundNullApplication.class); app.setWebApplicationType(WebApplicationType.NONE); try (ConfigurableApplicationContext context = app.run("--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.input.destination=words", "--spring.cloud.stream.bindings.output.destination=counts", "--spring.cloud.stream.bindings.output.producer.useNativeEncoding=false", "--spring.cloud.stream.kafka.streams.default.consumer.application-id=testOutboundNullValueIsHandledGracefully", "--spring.cloud.stream.kafka.streams.binder.configuration.commit.interval.ms=1000", "--spring.cloud.stream.kafka.streams.binder.configuration.default.key.serde" + "=org.apache.kafka.common.serialization.Serdes$StringSerde", "--spring.cloud.stream.kafka.streams.binder.configuration.default.value.serde" + "=org.apache.kafka.common.serialization.Serdes$StringSerde", "--spring.cloud.stream.kafka.streams.timeWindow.length=5000", "--spring.cloud.stream.kafka.streams.timeWindow.advanceBy=0", "--spring.cloud.stream.kafka.binder.brokers=" + embeddedKafka.getBrokersAsString())) { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>( senderProps); try { KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("words"); template.sendDefault("foobar"); ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer, "counts"); assertThat(cr.value() == null).isTrue(); } finally { pf.destroy(); } } }
Example 13
Source File: KafkaStreamsFunctionStateStoreTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
private void receiveAndValidate(ConfigurableApplicationContext context) throws Exception { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps); try { KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("words"); template.sendDefault(1, "foobar"); Thread.sleep(2000L); StateStoreTestApplication processorApplication = context .getBean(StateStoreTestApplication.class); KeyValueStore<Long, Long> state1 = processorApplication.state1; assertThat(processorApplication.processed1).isTrue(); assertThat(state1 != null).isTrue(); assertThat(state1.name()).isEqualTo("my-store"); WindowStore<Long, Long> state2 = processorApplication.state2; assertThat(state2 != null).isTrue(); assertThat(state2.name()).isEqualTo("other-store"); assertThat(state2.persistent()).isTrue(); KeyValueStore<Long, Long> state3 = processorApplication.state1; assertThat(processorApplication.processed2).isTrue(); assertThat(state3 != null).isTrue(); assertThat(state3.name()).isEqualTo("my-store"); WindowStore<Long, Long> state4 = processorApplication.state2; assertThat(state4 != null).isTrue(); assertThat(state4.name()).isEqualTo("other-store"); assertThat(state4.persistent()).isTrue(); } finally { pf.destroy(); } }
Example 14
Source File: DeserializationErrorHandlerByKafkaTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@Test public void test() { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>( senderProps); KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("xyz-DeserializationErrorHandlerByKafkaTests-In"); template.sendDefault(1, null, "foobar"); Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("foobar", "false", embeddedKafka); consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); DefaultKafkaConsumerFactory<String, String> cf = new DefaultKafkaConsumerFactory<>( consumerProps); Consumer<String, String> consumer1 = cf.createConsumer(); embeddedKafka.consumeFromAnEmbeddedTopic(consumer1, "error.xyz-DeserializationErrorHandlerByKafkaTests-In.group"); ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer1, "error.xyz-DeserializationErrorHandlerByKafkaTests-In.group"); assertThat(cr.value()).isEqualTo("foobar"); assertThat(cr.partition()).isEqualTo(0); // custom partition function // Ensuring that the deserialization was indeed done by Kafka natively verify(conversionDelegate, never()).deserializeOnInbound(any(Class.class), any(KStream.class)); verify(conversionDelegate, never()).serializeOnOutbound(any(KStream.class)); }
Example 15
Source File: DeserializationErrorHandlerByKafkaTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@Test @Ignore public void test() { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>( senderProps); KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("abc-DeserializationErrorHandlerByKafkaTests-In"); template.sendDefault(1, null, "foobar"); Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("foobar", "false", embeddedKafka); consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); DefaultKafkaConsumerFactory<String, String> cf = new DefaultKafkaConsumerFactory<>( consumerProps); Consumer<String, String> consumer1 = cf.createConsumer(); embeddedKafka.consumeFromAnEmbeddedTopic(consumer1, "error.abc-DeserializationErrorHandlerByKafkaTests-In.group"); ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer1, "error.abc-DeserializationErrorHandlerByKafkaTests-In.group"); assertThat(cr.value()).isEqualTo("foobar"); assertThat(cr.partition()).isEqualTo(0); // custom partition function // Ensuring that the deserialization was indeed done by Kafka natively verify(conversionDelegate, never()).deserializeOnInbound(any(Class.class), any(KStream.class)); verify(conversionDelegate, never()).serializeOnOutbound(any(KStream.class)); }
Example 16
Source File: DeserializtionErrorHandlerByBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings("unchecked") public void test() { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>( senderProps); KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("foos1"); template.sendDefault("hello"); template.setDefaultTopic("foos2"); template.sendDefault("hello"); Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("foobar1", "false", embeddedKafka); consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); DefaultKafkaConsumerFactory<String, String> cf = new DefaultKafkaConsumerFactory<>( consumerProps); Consumer<String, String> consumer1 = cf.createConsumer(); embeddedKafka.consumeFromEmbeddedTopics(consumer1, "error.foos1.fooz-group", "error.foos2.fooz-group"); ConsumerRecord<String, String> cr1 = KafkaTestUtils.getSingleRecord(consumer1, "error.foos1.fooz-group"); assertThat(cr1.value().equals("hello")).isTrue(); ConsumerRecord<String, String> cr2 = KafkaTestUtils.getSingleRecord(consumer1, "error.foos2.fooz-group"); assertThat(cr2.value().equals("hello")).isTrue(); // Ensuring that the deserialization was indeed done by the binder verify(conversionDelegate).deserializeOnInbound(any(Class.class), any(KStream.class)); }
Example 17
Source File: DeserializtionErrorHandlerByBinderTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@Test public void test() { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>( senderProps); KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("goos"); template.sendDefault(1, 7, "hello"); Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("foobar", "false", embeddedKafka); consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); DefaultKafkaConsumerFactory<String, String> cf = new DefaultKafkaConsumerFactory<>( consumerProps); Consumer<String, String> consumer1 = cf.createConsumer(); embeddedKafka.consumeFromAnEmbeddedTopic(consumer1, "error.goos.foobar-group"); ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer1, "error.goos.foobar-group"); assertThat(cr.value()).isEqualTo("hello"); assertThat(cr.partition()).isEqualTo(0); // Ensuring that the deserialization was indeed done by the binder verify(conversionDelegate).deserializeOnInbound(any(Class.class), any(KStream.class)); }
Example 18
Source File: KafkaStreamsBinderWordCountFunctionTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
private void receiveAndValidate(String in, String out) { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps); try { KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic(in); template.sendDefault("foobar"); ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer, out); assertThat(cr.value().contains("\"word\":\"foobar\",\"count\":1")).isTrue(); } finally { pf.destroy(); } }
Example 19
Source File: KafkaStreamsAggregateSampleTests.java From spring-cloud-stream-samples with Apache License 2.0 | 5 votes |
@Test public void testKafkaStreamsWordCountProcessor() throws Exception { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); ObjectMapper mapper = new ObjectMapper(); Serde<DomainEvent> domainEventSerde = new JsonSerde<>(DomainEvent.class, mapper); senderProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); senderProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, domainEventSerde.serializer().getClass()); DefaultKafkaProducerFactory<String, DomainEvent> pf = new DefaultKafkaProducerFactory<>(senderProps); try { KafkaTemplate<String, DomainEvent> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("foobar"); DomainEvent ddEvent = new DomainEvent(); ddEvent.setBoardUuid("12345"); ddEvent.setEventType("create-domain-event"); template.sendDefault("", ddEvent); Thread.sleep(1000); RestTemplate restTemplate = new RestTemplate(); String fooResourceUrl = "http://localhost:" + randomServerPort + "/events"; ResponseEntity<String> response = restTemplate.getForEntity(fooResourceUrl, String.class); assertThat(response.getBody()).contains("create-domain-event"); } finally { pf.destroy(); } }
Example 20
Source File: KafkaStreamsBinderWordCountFunctionTests.java From spring-cloud-stream-binder-kafka with Apache License 2.0 | 5 votes |
@Test public void testKstreamWordCountFunctionWithCustomProducerStreamPartitioner() throws Exception { SpringApplication app = new SpringApplication(WordCountProcessorApplication.class); app.setWebApplicationType(WebApplicationType.NONE); try (ConfigurableApplicationContext context = app.run( "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.bindings.process-in-0.destination=words-2", "--spring.cloud.stream.bindings.process-out-0.destination=counts-2", "--spring.cloud.stream.bindings.process-out-0.producer.partitionCount=2", "--spring.cloud.stream.kafka.streams.bindings.process-out-0.producer.streamPartitionerBeanName" + "=streamPartitioner", "--spring.cloud.stream.kafka.streams.binder.configuration.commit.interval.ms=1000", "--spring.cloud.stream.kafka.streams.binder.configuration.default.key.serde" + "=org.apache.kafka.common.serialization.Serdes$StringSerde", "--spring.cloud.stream.kafka.streams.binder.configuration.default.value.serde" + "=org.apache.kafka.common.serialization.Serdes$StringSerde", "--spring.cloud.stream.kafka.streams.binder.brokers=" + embeddedKafka.getBrokersAsString())) { Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka); DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps); try { KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true); template.setDefaultTopic("words-2"); template.sendDefault("foo"); ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer, "counts-2"); assertThat(cr.value().contains("\"word\":\"foo\",\"count\":1")).isTrue(); assertThat(cr.partition() == 0) .isTrue(); template.sendDefault("bar"); cr = KafkaTestUtils.getSingleRecord(consumer, "counts-2"); assertThat(cr.value().contains("\"word\":\"bar\",\"count\":1")).isTrue(); assertThat(cr.partition() == 1) .isTrue(); } finally { pf.destroy(); } } }