0

我用 Grape 加载了 kafka-clients 依赖项,并且可以实例化一个 StringSerializer,但是 Class.forName 失败了。为什么会这样?

$ groovysh
Groovy Shell (1.8.6, JVM: 1.8.0_131)
-------------------------------------------------------------------------
groovy:000> import static groovy.grape.Grape.grab
===> [import static groovy.grape.Grape.grab]
groovy:000> grab group:'org.apache.kafka', module:'kafka-clients', version:'0.11.0.0'
===> null
groovy:000> new org.apache.kafka.common.serialization.StringSerializer()
===> org.apache.kafka.common.serialization.StringSerializer@2f67b837
groovy:000> Class.forName "org.apache.kafka.common.serialization.StringSerializer"
ERROR java.lang.ClassNotFoundException:
org.apache.kafka.common.serialization.StringSerializer
        at java_lang_Class$forName.call (Unknown Source)
        at groovysh_evaluate.run (groovysh_evaluate:3)
        ...

这让我很沮丧,因为实例化 KafkaProducer 显然需要 Serializer 能够从其 String 类名加载,即使 Serializer 实例被传递到KafkaProducer 构造函数中。

groovy:000> ss = new org.apache.kafka.common.serialization.StringSerializer()
===> org.apache.kafka.common.serialization.StringSerializer@4331d187
groovy:000> producer = new KafkaProducer(props, ss, ss)
ERROR org.apache.kafka.common.config.ConfigException:
Invalid value org.apache.kafka.common.serialization.StringSerializer for configuration key.serializer: Class org.apache.kafka.common.serialization.StringSerializer could not be found.
        at org.apache.kafka.common.config.ConfigDef.parseType (ConfigDef.java:715)
        at org.apache.kafka.common.config.ConfigDef.parseValue (ConfigDef.java:460)
        at org.apache.kafka.common.config.ConfigDef.parse (ConfigDef.java:453)
        at org.apache.kafka.common.config.AbstractConfig.<init> (AbstractConfig.java:62)
        at org.apache.kafka.common.config.AbstractConfig.<init> (AbstractConfig.java:75)
        at org.apache.kafka.clients.producer.ProducerConfig.<init> (ProducerConfig.java:359)
        at org.apache.kafka.clients.producer.KafkaProducer.<init> (KafkaProducer.java:300)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance (DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance (Constructor.java:423)
        at org.codehaus.groovy.reflection.CachedConstructor.invoke (CachedConstructor.java:77)
        at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor (ConstructorSite.java:102)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor (CallSiteArray.java:54)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor (AbstractCallSite.java:182)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor (AbstractCallSite.java:198)
        at groovysh_evaluate$_run_closure1.doCall (groovysh_evaluate:7)
        at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke (Method.java:498)
        at org.codehaus.groovy.reflection.CachedMethod.invoke (CachedMethod.java:90)
        at groovy.lang.MetaMethod.doMethodInvoke (MetaMethod.java:233)
        at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod (ClosureMetaClass.java:272)
        at groovy.lang.MetaClassImpl.invokeMethod (MetaClassImpl.java:877)
        at groovy.lang.MetaClassImpl.invokePropertyOrMissing (MetaClassImpl.java:1093)
        at groovy.lang.MetaClassImpl.invokeMethod (MetaClassImpl.java:1049)
        at groovy.lang.MetaClassImpl.invokeMethod (MetaClassImpl.java:877)
        at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent (PogoMetaClassSite.java:66)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent (CallSiteArray.java:46)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent (AbstractCallSite.java:133)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent (AbstractCallSite.java:141)
        at groovysh_evaluate.run (groovysh_evaluate:4)
        ...

我正在寻找一种可以让 Class.forName 工作的解决方法,或者任何其他避免 ConfigException(由 ClassNotFoundException 引起)的方法。

4

0 回答 0