gpt4 book ai didi

java - 我们如何在 Android 应用程序中创建 Kafka 生产者?

转载 作者:行者123 更新时间:2023-11-29 22:52:18 25 4
gpt4 key购买 nike

我是 apache kafka 的新手,我正在尝试在 android studio 上使用它,以便使用 A simple Kafka Consumer and Producer example 中的代码向位于我电脑上的服务器生成数据。

渐变代码:

apply plugin: 'com.android.application'

android {
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
}
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 14
targetSdkVersion 29
versionCode 1
versionName "1.0"
multiDexEnabled true

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.android.support:multidex:1.0.1'
implementation 'io.socket:socket.io-client:0.2.1'
implementation 'org.apache.kafka:kafka-clients:0.10.0.0'
implementation 'org.apache.kafka:kafka-streams:0.10.0.0'




}

这是我的主要 Activity :

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.os.Looper;
import android.os.StrictMode;
import android.widget.Toast;


import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.LongSerializer;
import org.apache.kafka.common.serialization.StringSerializer;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.WeakHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, LongSerializer.class.getName());
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props);
for (int i = 0; i < 1000; i++) {
ProducerRecord<String, String> data;
if (i % 2 == 0) {
data = new ProducerRecord<String, String>("even", 0, Integer.toString(i), String.format("%d is even", i));
} else {
data = new ProducerRecord<String, String>("odd", 0, Integer.toString(i), String.format("%d is odd", i));
}
producer.send(data);
try {
Thread.sleep(1L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
producer.close();


}


}

这里是错误:

基本上错误清楚地出现在这些行中:

 java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;
at org.apache.kafka.common.utils.AppInfoParser.unregisterAppInfo(AppInfoParser.java:65)
at org.apache.kafka.clients.producer.KafkaProducer.close(KafkaProducer.java:699)
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:333)
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:188)

它总是立即崩溃并且无法识别问题。我试图将 multidex 设置为 true 或 false,但出现了同样的错误。我也尝试使用 maven 依赖项,但错误仍然存​​在。

我错过了什么?你能帮忙吗

最佳答案

你不会。这将是不安全的。您向 Web 服务发送请求,它会将数据发送到 Kafka。其他任何事情都需要您的 Kafka 服务器公开应用程序中的身份验证信息,然后基本上对任何人开放。

关于java - 我们如何在 Android 应用程序中创建 Kafka 生产者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57772783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com