java [Kakfka Producer]样本kafka制作人#kafka #sourcecode

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java [Kakfka Producer]样本kafka制作人#kafka #sourcecode相关的知识,希望对你有一定的参考价值。

/**
* This method or function return a new Kafka producer. This maybe
* called once in an application. For example, in a spring project
* this may produced using @Bean or in a java ee application using
* the @Produce or @ApplicationScoped
*/
public KafkaProducer getProducer(KafkaConfiguration kafkaConfiguration) {
	return new KafkaProducer(kafkaConfiguration.asMap());
}
/**
* Kafka Producer configuration. Shows the way and
* the parameters. In this case the fields are bind
* with a application.properties file, but may bind 
* also with other ways.
*/
@Configuration
@ConfigurationProperties("custom.kafka")
public static class KafkaConfiguration {

  	private String bootstrapServers;

    private String keySerializerClass;

	private String valueSerializerClass;
        
    private int retries;

	Map<String, ? extends Serializable> asMap() {
    	return HashMap.of(
            ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers,
			ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, keySerializerClass,
	        ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, valueSerializerClass,
	        ProducerConfig.RETRIES_CONFIG, retries
		).toJavaMap();
	}
}
/**
* This shows the way of using tha kafka producer. In this
* example a text message, "text message", is sent to the 
* a topic, "testTopic". The producer sends the message using
* the fire-and-forget method.
*/
@Autowired
private KafkaProducer kafkaProducer;

...

Try.of(() -> kafkaProducer.send(new ProducerRecord("testTopic", "text message")))
                    .onSuccess(future -> log.info("Message sent"))
                    .onFailure(Throwable::printStackTrace);

以上是关于java [Kakfka Producer]样本kafka制作人#kafka #sourcecode的主要内容,如果未能解决你的问题,请参考以下文章

spark streaming璇诲彇kakfka鏁版嵁鎵嬪姩缁存姢offset

小内存linux启动Kakfka报错: commit_memory(0x00000000c0000000, 1073741824, 0) failed ..解决方案

kafka 源码分析 3 : Producer

057 Java中kafka的Producer程序实现

rocketmq源码解析-namesrv与broker

3.5 样本分布K-S检验 ——python实战