【问题标题】:Kafka OSGI bundle - Producer initialization issue could not initialize class org.apache.kafka.clients.producer.ProducerConfigKafka OSGI 捆绑包 - 生产者初始化问题无法初始化类 org.apache.kafka.clients.producer.ProducerConfig
【发布时间】:2016-09-04 23:32:01
【问题描述】:

我正在尝试在 karaf 4.0.3 中创建一个 Kafka Producer。

ClassLoader currentLoader; 
try{
    currentLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(null);
    Properties props = new Properties();
    props.put("bootstrap.servers","localhost:9092");
    props.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");
    props.put("key.serializer","org.apache.kafka.common.serialization.IntegerSerializer");

    KafkaProducer<String,String> producer = new KafkaProducer<String,String>(props);
    Thread.currentThread().setContextClassLoader(currentLoader);
    producer.close();

我的pom.xml 包含,

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
    <version>0.9.0.0</version>
</dependency>   

在部署代码时,我收到以下错误,

2016-05-09 14:18:12,127 | ERROR | nsole user karaf | ShellUtil                        | 44 - org.apache.karaf.shell.core - 4.0.3 | Exception caught while executing command
org.apache.karaf.shell.support.MultiException: Error executing command on bundles:
Error starting bundle143: Activator start error in bundle KafkaArtifact [143].
at org.apache.karaf.shell.support.MultiException.throwIf(MultiException.java:61)
at org.apache.karaf.bundle.command.BundlesCommand.doExecute(BundlesCommand.java:69)[24:org.apache.karaf.bundle.core:4.0.3]
at org.apache.karaf.bundle.command.BundlesCommand.execute(BundlesCommand.java:54)[24:org.apache.karaf.bundle.core:4.0.3]
at org.apache.karaf.shell.impl.action.command.ActionCommand.execute(ActionCommand.java:83)[44:org.apache.karaf.shell.core:4.0.3]
at org.apache.karaf.shell.impl.console.osgi.secured.SecuredCommand.execute(SecuredCommand.java:67)[44:org.apache.karaf.shell.core:4.0.3]
at org.apache.karaf.shell.impl.console.osgi.secured.SecuredCommand.execute(SecuredCommand.java:87)[44:org.apache.karaf.shell.core:4.0.3]
at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:480)[44:org.apache.karaf.shell.core:4.0.3]
at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:406)[44:org.apache.karaf.shell.core:4.0.3]
at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)[44:org.apache.karaf.shell.core:4.0.3]
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:182)[44:org.apache.karaf.shell.core:4.0.3]
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:119)[44:org.apache.karaf.shell.core:4.0.3]
at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:94)[44:org.apache.karaf.shell.core:4.0.3]
at org.apache.karaf.shell.impl.console.ConsoleSessionImpl.run(ConsoleSessionImpl.java:270)[44:org.apache.karaf.shell.core:4.0.3]
at java.lang.Thread.run(Thread.java:745)[:1.8.0_66]
Caused by: java.lang.Exception: Error starting bundle143: Activator start error in bundle KafkaArtifact [143].
at org.apache.karaf.bundle.command.BundlesCommand.doExecute(BundlesCommand.java:66)[24:org.apache.karaf.bundle.core:4.0.3]
... 12 more
Caused by: org.osgi.framework.BundleException: Activator start error in bundle KafkaArtifact [143].
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2276)[org.apache.felix.framework-5.4.0.jar:]
at org.apache.felix.framework.Felix.startBundle(Felix.java:2144)[org.apache.felix.framework-5.4.0.jar:]
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998)[org.apache.felix.framework-5.4.0.jar:]
at org.apache.karaf.bundle.command.Start.executeOnBundle(Start.java:38)[24:org.apache.karaf.bundle.core:4.0.3]
at org.apache.karaf.bundle.command.BundlesCommand.doExecute(BundlesCommand.java:64)[24:org.apache.karaf.bundle.core:4.0.3]
... 12 more
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.apache.kafka.clients.producer.ProducerConfig
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:181)
at com.KafkaSample.KafkaArtifact.Producer.<init>(Producer.java:24)
at com.KafkaSample.KafkaArtifact.KafkaConsumerProducerDemo.start(KafkaConsumerProducerDemo.java:9)
at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:697)
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2226)
... 16 more

我也尝试过以下链接中的解决方法,

Karaf - Kafka OSGI bundle - Producer issue

但仍然出现同样的错误。

【问题讨论】:

    标签: apache-kafka apache-karaf karaf kafka-producer-api


    【解决方案1】:

    我知道这是一个 3 个月大的问题。即使我被这个制作人问题困住了。在挖掘了 2 天后,我偶然发现了一个 jira 问题,其中有人已经提到了这个问题并且该解决方案已被修补,但将在 2.18.0 版本的骆驼库中可用。然而,为了解决这个问题,我所做的是克隆 this camel repository 并为我的项目构建我想要的组件。这些组件目前是 2.18.0-SNAPSHOT 版本。在components文件夹下选择你想要的组件,执行maven clean package得到目标目录下生成的jar。

    另外一件事,当你编译 camel-core sub project 时。它执行了很多您可能不需要的测试用例,因此只需在最后一行属性中将“”替换为“org/apache/camel/**/*.java”即可。 希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-04-16
      • 2020-10-17
      • 2020-03-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-25
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      相关资源
      最近更新 更多