配置事件调度程序

云眼About 3 min

配置事件调度程序

本主题介绍如何为云眼灰度试验 Android SDK 中每次展示或转化发出的 HTTP 请求配置事件调度程序。

建议修改

云眼灰度试验 SDK 为每个触发的决策事件或转化事件发出 HTTP 请求。每个 SDK 都有一个内置的事件调度程序来处理这些事件,但我们建议根据环境的具体情况重写它。

Android SDK 有一个开箱即用的异步调度程序。建议自定义在生产中使用的事件调度程序,以确保以可扩展到应用程序处理的卷的方式对事件进行排队和发送。通过自定义事件调度程序,可以利用批处理等功能,从而更轻松地有效地处理大型事件量或在请求失败时实现重试逻辑。可以从头开始构建调度程序,也可以从提供的调度程序开始。

这些示例显示,要自定义事件调度程序,请使用事件调度程序实例初始化 Eyeofcloud 客户端(或管理器)。

科特林Java

// Using an anonymous class here to implement the EventHandler interface. // Feel free to create an explicit class that implements the interface instead. val eventHandler = object : EventHandler { override fun dispatchEvent(logEvent: LogEvent) { // Send event to our log endpoint as documented in // https://developers.eyeofcloud.com/x/events/api/index.html } } // Build a manager val eyeofcloudManager = EyeofcloudManager.builder() .withSDKKey("SDK_KEY_HERE") .withEventDispatchInterval(60, TimeUnit.SECONDS) .withEventHandler(eventHandler) .build(context) // With the new Android O differences, you need to register the // service for the intent filter you desire in code instead of in the manifest. val eventRescheduler = EventRescheduler() context.registerReceiver(eventRescheduler, IntentFilter(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION))

// Using an anonymous class here to implement the EventHandler interface. // Feel free to create an explicit class that implements the interface instead. EventHandler eventHandler = new EventHandler() { @Override public void dispatchEvent(LogEvent logEvent) throws Exception { // Send event to our log endpoint as documented in // https://developers.eyeofcloud.com/x/events/api/index.html } }; // Build a manager EyeofcloudManager eyeofcloudManager = EyeofcloudManager.builder() .withSDKKey("SDK_KEY_HERE") .withEventDispatchInterval(60, TimeUnit.SECONDS) .withEventHandler(eventHandler) .build(context); // With the new Android O differences, you need to register the // service for the intent filter you desire in code instead of in the manifest. EventRescheduler eventRescheduler = new EventRescheduler(); context.registerReceiver(eventRescheduler, new IntentFilter(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION));

事件调度程序应实现一个函数,该函数接受三个参数:、 和 ,所有这些都由内部类创建。在此函数中,应该使用 作为请求的主体(确保将其字符串化为 JSON)和标头中向给定的请求发送请求。dispatchEvent``httpVerb``url``params``EventBuilder``POST``url``params``{content-type: 'application/json'}

🚧 重要

如果使用的是自定义事件调度程序,请不要修改从云眼灰度试验返回的事件负载。修改此有效负载将更改结果。

包含的事件处理程序实现包括排队和刷新,因此即使应用处于脱机状态,它也能正常工作。事件处理程序使用 将要调度的事件排队。如果发送失败,它们将存储在数据库中,并计划稍后调度。如果要在 Wi-Fi 可用时或使用默认事件处理程序重新启动或重新安装后刷新事件,则需要扩充 AndroidManifest.xml以包含意图过滤器声明。IntentService``sqlite

Android

// Add these lines to your manifest if you want the event handler background services to schedule themselves again after a boot or package replace. <receiver android:name="com.eyeofcloud.ab.android.event_handler.EventRescheduler" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="android.intent.action.MY_PACKAGE_REPLACED" /> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" /> </intent-filter> </receiver>

需要实现事件处理程序才能使用自己的事件处理程序open in new window

📘 注意

要在 Android O 及更高版本中处理后台,还需要在代码中注册事件处理程序重新调度程序。请参阅上面的代码示例。

Last update:
Contributors: “zhangweixue”