Java Code Examples for org.springframework.kafka.core.DefaultKafkaProducerFactory#destroy()

The following examples show how to use org.springframework.kafka.core.DefaultKafkaProducerFactory#destroy() . 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: KafkaStreamsBinderWordCountIntegrationTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
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 2
Source File: KafkaStreamsBinderWordCountIntegrationTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
private void sendTombStoneRecordsAndVerifyGracefulHandling() 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-1");
		template.sendDefault(null);
		ConsumerRecords<String, String> received = consumer
				.poll(Duration.ofMillis(5000));
		// By asserting that the received record is empty, we are ensuring that the
		// tombstone record
		// was handled by the binder gracefully.
		assertThat(received.isEmpty()).isTrue();
	}
	finally {
		pf.destroy();
	}
}
 
Example 3
Source File: MultipleFunctionsInSameAppTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
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 4
Source File: KafkaStreamsBranchingSampleTests.java    From spring-cloud-stream-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testKafkaStreamsWordCountProcessor() 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("words");
		template.sendDefault("english");
		template.sendDefault("french");
		template.sendDefault("spanish");
		Thread.sleep(2000);
		ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer, "english-counts", 5000);
		assertThat(cr.value().contains("english")).isTrue();
		cr = KafkaTestUtils.getSingleRecord(consumer, "french-counts", 5000);
		assertThat(cr.value().contains("french")).isTrue();
		cr = KafkaTestUtils.getSingleRecord(consumer, "spanish-counts", 5000);
		assertThat(cr.value().contains("spanish")).isTrue();
	}
	finally {
		pf.destroy();
	}
}
 
Example 5
Source File: OutboundValueNullSkippedConversionTest.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@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 6
Source File: KafkaStreamsBinderWordCountFunctionTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@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();
		}
	}
}
 
Example 7
Source File: KafkaStreamsBinderWordCountFunctionTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
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 8
Source File: KafkaStreamsFunctionStateStoreTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
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 9
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: KafkaStreamsWordCountApplicationTests.java    From spring-cloud-stream-samples with Apache License 2.0 5 votes vote down vote up
@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 11
Source File: KafkaStreamsDlqExampleTests.java    From spring-cloud-stream-samples with Apache License 2.0 5 votes vote down vote up
@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: KafkaStreamsAggregateSampleTests.java    From spring-cloud-stream-samples with Apache License 2.0 5 votes vote down vote up
@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 13
Source File: PerRecordAvroContentTypeTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
@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 14
Source File: KafkaStreamsBinderHealthIndicatorTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
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();
	}
}