org.springframework.kafka.test.rule.EmbeddedKafkaRule Java Examples
The following examples show how to use
org.springframework.kafka.test.rule.EmbeddedKafkaRule.
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: KafkaClientITest.java From java-specialagent with Apache License 2.0 | 6 votes |
private static void createConsumer(final EmbeddedKafkaRule embeddedKafkaRule, final CountDownLatch latch) { Executors.newSingleThreadExecutor().execute(new Runnable() { @Override public void run() { final Map<String,Object> consumerProps = KafkaTestUtils.consumerProps("sampleRawConsumer", "false", embeddedKafkaRule.getEmbeddedKafka()); consumerProps.put("auto.offset.reset", "earliest"); try (final Consumer<Long,String> consumer = new KafkaConsumer<>(consumerProps)) { consumer.subscribe(Collections.singletonList(TOPIC_NAME)); for (int i = 0; i < MESSAGE_COUNT;) { final int count = consumer.poll(100).count(); for (int j = 0; j < count; ++j, ++i) { consumer.commitSync(); } } } latch.countDown(); } }); }
Example #2
Source File: SpringMessagingITest.java From java-specialagent with Apache License 2.0 | 5 votes |
public static void main(final String[] args) throws Exception { final EmbeddedKafkaRule embeddedKafkaRule = TestUtil.retry(new Callable<EmbeddedKafkaRule>() { @Override public EmbeddedKafkaRule call() { final EmbeddedKafkaRule rule = new EmbeddedKafkaRule(1, true, 2); try { rule.before(); return rule; } catch (final Exception e) { rule.after(); throw e; } } }, 10); kafkaEmbedded = embeddedKafkaRule.getEmbeddedKafka(); System.setProperty("spring.kafka.bootstrap-servers", kafkaEmbedded.getBrokersAsString()); final CountDownLatch latch = TestUtil.initExpectedSpanLatch(5); try (final ConfigurableApplicationContext context = SpringApplication.run(SpringMessagingITest.class, args)) { TestUtil.checkSpan(true, latch, new ComponentSpanCount("spring-messaging", 2, true), new ComponentSpanCount("java-kafka", 2, true), new ComponentSpanCount("spring-kafka", 1)); } catch (final Throwable t) { t.printStackTrace(System.err); embeddedKafkaRule.after(); System.exit(1); } finally { embeddedKafkaRule.after(); System.exit(0); } }
Example #3
Source File: SpringKafkaITest.java From java-specialagent with Apache License 2.0 | 5 votes |
public static void main(final String[] args) throws Exception { final EmbeddedKafkaRule embeddedKafkaRule = TestUtil.retry(new Callable<EmbeddedKafkaRule>() { @Override public EmbeddedKafkaRule call() { final EmbeddedKafkaRule rule = new EmbeddedKafkaRule(1, true, 2, "users", "reply"); try { rule.before(); return rule; } catch (final Exception e) { rule.after(); throw e; } } }, 10); kafkaEmbedded = embeddedKafkaRule.getEmbeddedKafka(); final CountDownLatch latch = TestUtil.initExpectedSpanLatch(6); try (final ConfigurableApplicationContext context = SpringApplication.run(SpringKafkaITest.class, args)) { TestUtil.checkSpan(true, latch, new ComponentSpanCount("java-kafka", 4, true), new ComponentSpanCount("spring-kafka", 2, true)); } catch (final Throwable t) { t.printStackTrace(System.err); embeddedKafkaRule.after(); System.exit(1); } finally { embeddedKafkaRule.after(); System.exit(0); } }
Example #4
Source File: KafkaClientITest.java From java-specialagent with Apache License 2.0 | 4 votes |
public static void main(final String[] args) throws Exception { final EmbeddedKafkaRule embeddedKafkaRule = TestUtil.retry(new Callable<EmbeddedKafkaRule>() { @Override public EmbeddedKafkaRule call() { final EmbeddedKafkaRule rule = new EmbeddedKafkaRule(1, true, 1, "messages"); try { rule.before(); return rule; } catch (final Exception e) { rule.after(); throw e; } } }, 10); try (final Producer<Long,String> producer = createProducer(embeddedKafkaRule)) { for (int i = 0; i < MESSAGE_COUNT; ++i) { final ProducerRecord<Long,String> record = new ProducerRecord<>(TOPIC_NAME, "This is record " + i); producer.send(record, new Callback() { @Override public void onCompletion(final RecordMetadata metadata, final Exception exception) { TestUtil.checkActiveSpan(); } }); } final CountDownLatch latch = new CountDownLatch(1); createConsumer(embeddedKafkaRule, latch); latch.await(15, TimeUnit.SECONDS); TestUtil.checkSpan(new ComponentSpanCount("java-kafka", 10)); } catch (final Throwable t) { t.printStackTrace(System.err); embeddedKafkaRule.after(); System.exit(1); } finally { embeddedKafkaRule.after(); System.exit(0); } }
Example #5
Source File: KafkaClientITest.java From java-specialagent with Apache License 2.0 | 4 votes |
private static Producer<Long,String> createProducer(final EmbeddedKafkaRule embeddedKafkaRule) { final Map<String,Object> senderProps = KafkaTestUtils.producerProps(embeddedKafkaRule.getEmbeddedKafka()); return new KafkaProducer<>(senderProps); }
Example #6
Source File: KafkaStreamsITest.java From java-specialagent with Apache License 2.0 | 4 votes |
public static void main(final String[] args) throws Exception { final EmbeddedKafkaRule embeddedKafkaRule = TestUtil.retry(new Callable<EmbeddedKafkaRule>() { @Override public EmbeddedKafkaRule call() { final EmbeddedKafkaRule rule = new EmbeddedKafkaRule(1, true, 1, "stream-test"); try { rule.before(); return rule; } catch (final Exception e) { rule.after(); throw e; } } }, 10); final Map<String,Object> senderProps = KafkaTestUtils.producerProps(embeddedKafkaRule.getEmbeddedKafka()); try (final Producer<Integer,String> producer = new KafkaProducer<>(senderProps)) { final CountDownLatch latch = TestUtil.initExpectedSpanLatch(4); final Properties config = new Properties(); config.put(StreamsConfig.APPLICATION_ID_CONFIG, "stream-app"); config.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, senderProps.get("bootstrap.servers")); config.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.Integer().getClass()); config.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass()); final ProducerRecord<Integer,String> record = new ProducerRecord<>("stream-test", 1, "test"); producer.send(record); final Serde<String> stringSerde = Serdes.String(); final Serde<Integer> intSerde = Serdes.Integer(); final StreamsBuilder builder = new StreamsBuilder(); final KStream<Integer,String> kStream = builder.stream("stream-test"); kStream.map(new KeyValueMapper<Integer,String,KeyValue<Integer,String>>() { @Override public KeyValue<Integer,String> apply(final Integer key, final String value) { TestUtil.checkActiveSpan(); return new KeyValue<>(key, value + "map"); } }).to("stream-out", Produced.with(intSerde, stringSerde)); KafkaStreams streams = new KafkaStreams(builder.build(), config); streams.start(); TestUtil.checkSpan(true, latch, new ComponentSpanCount("java-kafka", 3), new ComponentSpanCount("kafka-streams", 1)); streams.close(); } catch (final Throwable t) { t.printStackTrace(System.err); embeddedKafkaRule.after(); System.exit(1); } finally { embeddedKafkaRule.after(); System.exit(0); } }