简体   繁体   English

如何将数据从 android java 发送到 azure 事件中心

[英]How are send data from android java to azure event hubs

Code:代码:



    public void eventHubs() throws EventHubException, ExecutionException, InterruptedException, IOException {
        final ConnectionStringBuilder connStr = new ConnectionStringBuilder()
                .setNamespaceName("---value---")
                .setEventHubName("---value---")
                .setSasKeyName("---value---")
                .setSasKey("---value---");
        final Gson gson = new GsonBuilder().create();

        // The Executor handles all asynchronous tasks and this is passed to the EventHubClient instance.
        // This enables the user to segregate their thread pool based on the work load.
        // This pool can then be shared across multiple EventHubClient instances.
        // The following sample uses a single thread executor, as there is only one EventHubClient instance,
        // handling different flavors of ingestion to Event Hubs here.
        final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);

        // Each EventHubClient instance spins up a new TCP/SSL connection, which is expensive.
        // It is always a best practice to reuse these instances. The following sample shows this.
        final EventHubClient ehClient = EventHubClient.createSync(connStr.toString(), executorService);


        try {
            String data = 0.259848484 + "|" + 0.99999999999 + "|" + "2019/20/18T15:30";
            System.out.println(data);
            byte[] payloadBytes = gson.toJson(data).getBytes(Charset.defaultCharset());
            EventData sendEvent = EventData.create(payloadBytes);
            ehClient.sendSync(sendEvent);
            System.out.println(Instant.now() + ": Send Complete...");
            System.out.println("Press Enter to stop.");
        } finally {
            ehClient.closeSync();
            executorService.shutdown();
        }
    }

Error:错误:

I/c.iy.event_hub: NativeAlloc concurrent copying GC freed 1143(135KB) AllocSpace objects, 1(20KB) LOS objects, 52% free, 1380KB/2916KB, paused 894us total 321.619ms
D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 1 0
I/Choreographer: Skipped 382 frames!  The application may be doing too much work on its main thread.
W/System.err: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
W/System.err: SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
W/System.err: com.microsoft.azure.eventhubs.EventHubException: Operation not permitted
        at com.microsoft.azure.eventhubs.impl.ExceptionUtil.toException(ExceptionUtil.java:60)
        at com.microsoft.azure.eventhubs.impl.MessagingFactory.onConnectionError(MessagingFactory.java:253)
        at com.microsoft.azure.eventhubs.impl.ConnectionHandler.onTransportError(ConnectionHandler.java:178)
        at org.apache.qpid.proton.engine.BaseHandler.handle(BaseHandler.java:191)
W/System.err:     at org.apache.qpid.proton.engine.impl.EventImpl.dispatch(EventImpl.java:108)
        at org.apache.qpid.proton.reactor.impl.ReactorImpl.dispatch(ReactorImpl.java:324)
        at org.apache.qpid.proton.reactor.impl.ReactorImpl.process(ReactorImpl.java:291)
        at com.microsoft.azure.eventhubs.impl.MessagingFactory$RunReactor.run(MessagingFactory.java:507)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)
I/Choreographer: Skipped 42 frames!  The application may be doing too much work on its main thread.
D/EGL_emulation: eglMakeCurrent: 0xf627eec0: ver 3 0 (tinfo 0xebbe5180)
D/EGL_emulation: eglMakeCurrent: 0xf627eec0: ver 3 0 (tinfo 0xebbe5180)

Here is your problem:这是你的问题:

I/Choreographer: Skipped 382 frames!我/编舞:跳过了 382 帧! The application may be doing too much work on its main thread.应用程序可能在其主线程上做了太多工作。

You can check this answer: The application may be doing too much work on its main thread您可以查看这个答案: 应用程序可能在其主线程上做了太多工作

So you need to put your code to other thread.所以你需要把你的代码放到其他线程中。 You can use AsyncTask, Threads, Handlers, Executors or a lot of other instruments.您可以使用 AsyncTask、线程、处理程序、执行程序或许多其他工具。 To check this just try要检查这个只是尝试

 new Thread(new Runnable() {
            @Override
            public void run() {
                //put your code here
            }
        }).start();

As to second problem with logger: if it will be exist in future add this to app gradle:关于记录器的第二个问题:如果将来存在,请将其添加到应用程序gradle:

 implementation 'log4j:log4j:1.2.17'
 implementation 'de.mindpipe.android:android-logging-log4j:1.0.3'

Create logger class:创建记录器类:

class Logger {
    static org.apache.log4j.Logger getLogger(Class clazz) {
        final LogConfigurator logConfigurator = new LogConfigurator();
        logConfigurator.setFileName(Environment.getExternalStorageDirectory().toString() + File.separator + "log/file.log");
        logConfigurator.setRootLevel(Level.ALL);
        logConfigurator.setLevel("org.apache", Level.ALL);
        logConfigurator.setUseFileAppender(true);
        logConfigurator.setFilePattern("%d %-5p [%c{2}]-[%L] %m%n");
        logConfigurator.setMaxFileSize(1024 * 1024 * 5);
        logConfigurator.setImmediateFlush(true);
        logConfigurator.configure();
        Logger log = Logger.getLogger(clazz);
        return log;
    }
}

And before creating azure client add this:在创建 azure 客户端之前添加:

ALogger.getLogger("YOUR_CLASS_NAME".class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为Android中的Azure事件中心生成SAS令牌 - How to generate SAS Token for Azure Event Hubs in Android 将事件发送到 Azure 事件中心 -> 不允许操作 - Send events to Azure Event Hubs -> Operation not permitted 有没有办法使用 Java 中的 Azure Functions 将带有标头数据的事件输出到 Azure 事件中心? - Is there a way to output events with header data to Azure Event Hubs using Azure Functions in Java? 适用于 Android 的 Azure 通知中心:如何使用后台服务处理数据消息? - Azure Notification Hubs for Android: How do I handle Data-Messages with a Background-Service? 如何将数据从C ++发送到Java-Android - How to send data from c++ to java - android 如何将数据从Android发送到服务器上的Java应用程序 - How to send data from android to java application on server 如何将数据从Android应用程序发送到Java Server(Apache tomcat)? - How to send data from Android application to Java Server(Apache tomcat)? 如何将数据从Java Servlet发送到Android客户端 - How to send data from java servlet to android client 如何将数据从php发送和检索到Java / Android? - How to send and retrive data from php to java/android? 如何将数据从线程发送到主要活动 - how to send data from thread to main activity java android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM