Open TheBestOrNothing opened 1 year ago
The error occur when kafka java client running on the android.
Packages required in the build.gradle.kts
implementation("org.slf4j:slf4j-nop:2.0.3") implementation("org.apache.kafka:kafka-clients:3.3.1") implementation("com.fasterxml.jackson.core:jackson-databind:2.15.2")
The producer code are showed as following.
`public class ProducerExample {
public static void produce( ) throws IOException {
Properties p = new Properties();
p.setProperty("bootstrap.servers", "pkc-lzvrd.us-west4.gcp.confluent.cloud:9092");
p.setProperty("security.protocol", "SASL_SSL");
p.setProperty("sasl.mechanism", "PLAIN");
p.setProperty("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username='L6SNLCZPXP2XHFWA' password='Cqmw7vbw2FvZ9WQpsSkDEq9yLubqQwcjE1RrsSoUYQwf5B+8vuZf6HLfnkzSdL6X' ;");
p.setProperty("client.dns.lookup", "use_all_dns_ips");
p.setProperty("session.timeout.ms", "45000");
p.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
p.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
p.setProperty(ProducerConfig.ACKS_CONFIG, "all");
final String topic = "topic_kafka";
String[] users = {"eabara", "jsmith", "sgarcia", "jbernard", "htanaka", "awalther"};
String[] items = {"book", "alarm clock", "t-shirts", "gift card", "batteries"};
try (final Producer<String, String> producer = new KafkaProducer<>(p)) {
final Random rnd = new Random();
final Long numMessages = 10L;
for (Long i = 0L; i < numMessages; i++) {
String user = users[rnd.nextInt(users.length)];
String item = items[rnd.nextInt(items.length)];
producer.send(
new ProducerRecord<>(topic, user, item),
(event, ex) -> {
if (ex != null)
ex.printStackTrace();
else
System.out.printf("Produced event to topic %s: key = %-10s value = %s%n", topic, user, item);
});
}
System.out.printf("%s events were produced to topic %s%n", numMessages, topic);
}
}`
The error you're encountering when running your app on Android indicates that the java.lang.management.ManagementFactory class is not available in the Android runtime. This is expected because Android uses a different runtime environment than standard Java SE, and Android does not include the full set of Java SE APIs, including JMX (Java Management Extensions) classes.
In Android, you cannot directly use classes and features that are not part of the Android runtime. Therefore, you won't be able to use Apache Kafka's features that rely on JMX, such as ManagementFactory, in an Android app.
If you need to use Kafka in an Android application, you may need to look for a Kafka client library or framework that is specifically designed for Android and doesn't have dependencies on Java SE features like JMX.
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory; at org.apache.kafka.common.utils.AppInfoParser.unregisterAppInfo(AppInfoParser.java:73) at org.apache.kafka.clients.producer.KafkaProducer.close(KafkaProducer.java:1342) at org.apache.kafka.clients.producer.KafkaProducer.(KafkaProducer.java:466)
at org.apache.kafka.clients.producer.KafkaProducer.(KafkaProducer.java:291)
at org.apache.kafka.clients.producer.KafkaProducer.(KafkaProducer.java:318)
at org.apache.kafka.clients.producer.KafkaProducer.(KafkaProducer.java:303)
at com.example.marsphotos.network.ProducerExample.produce(ProducerExample.java:33)