我测试kafka的时候, 使用 eclipse 在win 7 上或者 linux 上可以正常编译运行, 在 android studo上也可以正常编译, 运行的时候出现 FC.
代码如下:
public class Producer extends Thread {
private final KafkaProducer<Integer, String> producer;
private final String topic;
private final Boolean isAsync;
public Producer(String topic, Boolean isAsync) {
Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, KafkaProperties.KAFKA_SERVER_URL + ":" + KafkaProperties.KAFKA_SERVER_PORT);
props.put(ProducerConfig.CLIENT_ID_CONFIG, "DemoProducer");
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class.getName());
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
producer = new KafkaProducer<>(props);
this.topic = topic;
this.isAsync = isAsync;
}
public void run() {
int messageNo = 1;
while (true) {
String messageStr = "Message_" + messageNo;
long startTime = System.currentTimeMillis();
if (isAsync) { // Send asynchronously
producer.send(new ProducerRecord<>(topic,
messageNo,
messageStr), new DemoCallBack(startTime, messageNo, messageStr));
} else { // Send synchronously
try {
producer.send(new ProducerRecord<>(topic,
messageNo,
messageStr)).get();
System.out.println("Sent message: (" + messageNo + ", " + messageStr + ")");
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
++messageNo;
}
}
}
class DemoCallBack implements Callback {
private final long startTime;
private final int key;
private final String message;
public DemoCallBack(long startTime, int key, String message) {
this.startTime = startTime;
this.key = key;
this.message = message;
}
/**
* A callback method the user can implement to provide asynchronous handling of request completion. This method will
* be called when the record sent to the server has been acknowledged. Exactly one of the arguments will be
* non-null.
*
* @param metadata The metadata for the record that was sent (i.e. the partition and offset). Null if an error
* occurred.
* @param exception The exception thrown during processing of this record. Null if no error occurred.
*/
public void onCompletion(RecordMetadata metadata, Exception exception) {
long elapsedTime = System.currentTimeMillis() - startTime;
if (metadata != null) {
System.out.println(
"message(" + key + ", " + message + ") sent to partition(" + metadata.partition() +
"), " +
"offset(" + metadata.offset() + ") in " + elapsedTime + " ms");
} else {
exception.printStackTrace();
}
}
}
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;
at org.apache.kafka.common.utils.AppInfoParser.unregisterAppInfo(AppInfoParser.java:71)
at org.apache.kafka.clients.producer.KafkaProducer.close(KafkaProducer.java:1180)
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:429)
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:301)
at com.exe.kafkademo.Producer.<init>(Producer.java:41)
at com.exe.kafkademo.MainActivity.onCreate(MainActivity.java:13)
Caused by: java.lang.ClassNotFoundException: **Didn't find class "java.lang.management.ManagementFactory" on path: DexPathList**
[[zip file "/data/app/com.exe.kafkademo-6sa65kcj31aVWV2MfeY-Ag==/base.apk"],nativeLibraryDirectories=[/data/app/com.exe.kafkademo-6sa65kcj31aVWV2MfeY-Ag==/lib/arm64, /system/lib64, /system/vendor/lib64]]
网上搜索发现 java/lang/management/ManagementFactory
在rt.jar
,把这个jar 加入到libs中, 提示缺少libmanagement.so
。
多番尝试,无法解决问题
1.网上搜索 发现
https://stackoverflow.com/questions/19595814/android-add-java-lang-management-api
That API is not a part of Android, and is not compatible with Android.
The java.lang.management API is for managing and monitoring the Java VM. Android's Dalvik VM is not a Java VM.
看这个意思是 kafka目前不兼容android, 请问 android 客户端和 java客户端应该是类似的角色, android 有办法使用 kafka吗? 或者说 android 适合使用kafka 吗?
多谢了
嗯,另外kafka是长连接,不适用。走个api吧。
https://github.com/confluentinc/kafka-rest
多谢,多谢
你的答案