Skip to content

Commit 64f4c96

Browse files
committed
Adds support for the following client options: eventBatchSize, eventTimeInterval, eventMaxQueueSize, periodicDownloadInterval
1 parent 088fdd4 commit 64f4c96

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

android/src/main/java/com/optimizely/optimizely_flutter_sdk/OptimizelyFlutterClient.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.optimizely.ab.OptimizelyDecisionContext;
2424
import com.optimizely.ab.OptimizelyForcedDecision;
2525
import com.optimizely.ab.UnknownEventTypeException;
26+
import com.optimizely.ab.android.event_handler.DefaultEventHandler;
2627
import com.optimizely.ab.android.sdk.OptimizelyClient;
2728

2829
import java.util.HashMap;
@@ -35,8 +36,12 @@
3536
import com.fasterxml.jackson.databind.ObjectMapper;
3637
import com.optimizely.ab.android.sdk.OptimizelyManager;
3738
import com.optimizely.ab.error.RaiseExceptionErrorHandler;
39+
import com.optimizely.ab.event.BatchEventProcessor;
40+
import com.optimizely.ab.event.EventHandler;
41+
import com.optimizely.ab.event.EventProcessor;
3842
import com.optimizely.ab.event.LogEvent;
3943
import com.optimizely.ab.notification.DecisionNotification;
44+
import com.optimizely.ab.notification.NotificationCenter;
4045
import com.optimizely.ab.notification.TrackNotification;
4146
import com.optimizely.ab.notification.UpdateConfigNotification;
4247
import com.optimizely.ab.optimizelyconfig.OptimizelyConfig;
@@ -49,6 +54,7 @@
4954
import java.util.Collections;
5055
import java.util.LinkedHashMap;
5156
import java.util.List;
57+
import java.util.concurrent.ArrayBlockingQueue;
5258
import java.util.concurrent.TimeUnit;
5359

5460
public class OptimizelyFlutterClient {
@@ -66,15 +72,55 @@ protected void initializeOptimizely(@NonNull ArgumentsParser argumentsParser, @N
6672
result.success(createResponse(false, ErrorMessage.INVALID_PARAMS));
6773
return;
6874
}
75+
// EventDispatcher Default Values
76+
Integer batchSize = 10;
77+
Long timeInterval = TimeUnit.MINUTES.toMillis(1L); // Minutes
78+
Integer maxQueueSize = 10000;
79+
80+
if (argumentsParser.getEventBatchSize() != null) {
81+
batchSize = argumentsParser.getEventBatchSize();
82+
}
83+
if (argumentsParser.getEventTimeInterval() != null) {
84+
timeInterval = TimeUnit.SECONDS.toMillis(argumentsParser.getEventBatchSize());
85+
}
86+
if (argumentsParser.getEventMaxQueueSize() != null) {
87+
maxQueueSize = argumentsParser.getEventMaxQueueSize();
88+
}
89+
90+
DefaultEventHandler eventHandler = DefaultEventHandler.getInstance(context);
91+
eventHandler.setDispatchInterval(-1L);
92+
NotificationCenter notificationCenter = new NotificationCenter();
93+
// Here we are using the builder options to set batch size
94+
// to 5 events and flush interval to a minute.
95+
EventProcessor batchProcessor = BatchEventProcessor.builder()
96+
.withNotificationCenter(notificationCenter)
97+
.withEventHandler(eventHandler)
98+
.withBatchSize(batchSize)
99+
.withEventQueue(new ArrayBlockingQueue<>(maxQueueSize))
100+
.withFlushInterval(timeInterval)
101+
.build();
102+
103+
// Datafile Download Interval
104+
long periodicDownloadInterval = 10 * 60; // seconds
105+
106+
if (argumentsParser.getPeriodicDownloadInterval() != null) {
107+
periodicDownloadInterval = argumentsParser.getPeriodicDownloadInterval();
108+
}
69109
// Delete old user context
70110
userContextsTracker.remove(sdkKey);
111+
getOptimizelyClient(sdkKey).close();
112+
optimizelyManagerTracker.remove(sdkKey);
113+
71114
// Creating new instance
72115
OptimizelyManager optimizelyManager = OptimizelyManager.builder()
73-
.withEventDispatchInterval(60L, TimeUnit.SECONDS)
74-
.withDatafileDownloadInterval(15, TimeUnit.MINUTES)
116+
.withEventProcessor(batchProcessor)
117+
.withEventHandler(eventHandler)
118+
.withNotificationCenter(notificationCenter)
119+
.withDatafileDownloadInterval(periodicDownloadInterval, TimeUnit.SECONDS)
75120
.withErrorHandler(new RaiseExceptionErrorHandler())
76121
.withSDKKey(sdkKey)
77122
.build(context);
123+
78124
optimizelyManager.initialize(context, null, (OptimizelyClient client) -> {
79125
if (client.isValid()) {
80126
optimizelyManagerTracker.put(sdkKey, optimizelyManager);

android/src/main/java/com/optimizely/optimizely_flutter_sdk/helper_classes/ArgumentsParser.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,20 @@ public String getRuleKey() {
7474
public String getVariationKey() {
7575
return (String) arguments.get(Constants.RequestParameterKey.VARIATION_KEY);
7676
}
77+
78+
public Integer getEventBatchSize() {
79+
return (Integer) arguments.get(Constants.RequestParameterKey.EVENT_BATCH_SIZE);
80+
}
81+
82+
public Long getEventTimeInterval() {
83+
return (Long) arguments.get(Constants.RequestParameterKey.EVENT_TIME_INTERVAL);
84+
}
85+
86+
public Integer getEventMaxQueueSize() {
87+
return (Integer) arguments.get(Constants.RequestParameterKey.EVENT_MAX_QUEUE_SIZE);
88+
}
89+
90+
public Long getPeriodicDownloadInterval() {
91+
return (Long) arguments.get(Constants.RequestParameterKey.PERIODIC_DOWNLOAD_INTERVAL);
92+
}
7793
}

android/src/main/java/com/optimizely/optimizely_flutter_sdk/helper_classes/Constants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public static class RequestParameterKey {
4848
public static final String ATTRIBUTES = "attributes";
4949
public static final String DECIDE_KEYS = "keys";
5050
public static final String DECIDE_OPTIONS = "optimizelyDecideOption";
51+
public static final String EVENT_BATCH_SIZE = "eventBatchSize";
52+
public static final String EVENT_TIME_INTERVAL = "eventTimeInterval";
53+
public static final String EVENT_MAX_QUEUE_SIZE = "eventMaxQueueSize";
54+
public static final String PERIODIC_DOWNLOAD_INTERVAL = "periodicDownloadInterval";
5155
public static final String EVENT_KEY = "eventKey";
5256
public static final String EVENT_TAGS = "eventTags";
5357
public static final String FLAG_KEY = "flagKey";

0 commit comments

Comments
 (0)