How can we create Kafka producer in Android applic

2020-04-27 07:51发布

问题:

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

Gradle code :

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'




}

And This My Main 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();


        }


    }

Here is the Error :

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)

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

回答1:

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.



回答2:

I figured out that we cannot create kafka in android. It is better to create an application for kafka that exposes REST end points, which can be later called in android app. You can create it in any language or framework, I have done it in spring boot and it is working fine.

https://github.com/vpkeerthan/Kafka/tree/master/KafkaRestService

Android app using RESTful endpoints exposed for kafka services https://github.com/vpkeerthan/Android-Tutorial/tree/master/Kafka-REST-Android