简体   繁体   English

如何调用Properties 方法并将其设置到主类中?

[英]How to call the Properties Method and set it into the main class?

There's no error already, but when I run the program it fails to recognize the serializer which is inside the other class.已经没有错误,但是当我运行程序时,它无法识别另一个类中的序列化程序。 Here's how it looks like.这是它的样子。

public class Producer{

    private static final Logger logger = LogManager.getLogger(Producer.class);
    public static void main(String[] args) {
        logger.info("Creating Kafka Producer...");

        KafkaProducer<Integer, String> producer = new KafkaProducer<>(PropConfigs.prodProps());
        // The code above should run and call the PropConfigs (properties of kafka)

        logger.info("Start sending messages...");

        for (int i = 1; i <= AppConfigs.numEvents; i++) {
            producer.send(new ProducerRecord<>(AppConfigs.topicName, i, "Message " + i + " Test"), new Callback() {
                @Override
                public void onCompletion(RecordMetadata recordMetadata, Exception e) {
                    if(e == null){
                        logger.info("\nReceived metadata" + " Topic:" + recordMetadata.topic() + " Partition: " + recordMetadata.partition() + " Offset: " + recordMetadata.offset() + " Time: " + recordMetadata.timestamp() + "\n");
                    } else {
                        logger.error("Error", e);
                        e.printStackTrace();
                    }
                }
            });
        }

        logger.info("Finished - Closing Kafka Producer.");
        producer.flush();
        producer.close();

    }
}

Here's the PropConfigs class, where in I put the properties to be called from the main class Producer.这是 PropConfigs 类,我在其中放置了要从主类 Producer 调用的属性。

public class PropConfigs {


    public static Properties prodProps(){
        Properties props = new Properties();
        props.setProperty(ProducerConfig.CLIENT_ID_CONFIG, AppConfigs.applicationID);
        props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, AppConfigs.bootstrapServers);
        props.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class.getName());
        props.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.setProperty(ProducerConfig.ACKS_CONFIG, "all");
        props.setProperty(ProducerConfig.RETRIES_CONFIG, "3");
        props.setProperty(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION, "5");

        return new Properties(props);
    }

}

Whenever I run it, like I said it fails to recognize the properties that I set.每当我运行它时,就像我说的那样,它无法识别我设置的属性。

You mean it fails to recognize the prodProps method ?你的意思是它无法识别prodProps方法 (You have no custom serializer here). (这里没有自定义序列化程序)。 Note: You can have this return props注意:你可以有这个return props

You've not shown your import statements, but make sure the classes are in the same package (and not the default package, or no package)你没有显示你的导入语句,但确保这些类在同一个包中(而不是默认包,或者没有包)


If you really want to externalize the properties, Spring-Boot / Spring-Kafka are commonly used to do so如果你真的想外部化属性,通常使用 Spring-Boot / Spring-Kafka

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM