本文介绍了我们如何在Android应用程序中创建Kafka生产者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是apache kafka的新手,我正尝试在android studio上使用它,以便使用一个简单的Kafka消费者和生产者示例

I'am new to apache kafka and I'am trying to use it on android studio in order to produce data to my server located on my pc using codes from 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'




}

这是我的主要活动:

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();


        }


    }

这是错误:

基本错误在这些行中清晰显示:

Basicly The Error Appear clearly in those lines :

 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依赖项,但错误仍然相同.

It keeps crashing instantly and incapable of identifying the issue. I tried to set multidex to true or false but the same error showed up. I also tried to use maven dependencies but the error remained the same.

我想念的是什么?你能帮忙吗

What is it that I'm missing? Can you please help

推荐答案

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

You wouldn't. This would be insecure. You'd send a request to a webservice, and it would send data to Kafka. Anything else would require your Kafka servers to be public with the authentication info in the app, which is basically then open to anyone.

这篇关于我们如何在Android应用程序中创建Kafka生产者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:17