[英]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.