2323import com .optimizely .ab .OptimizelyDecisionContext ;
2424import com .optimizely .ab .OptimizelyForcedDecision ;
2525import com .optimizely .ab .UnknownEventTypeException ;
26+ import com .optimizely .ab .android .event_handler .DefaultEventHandler ;
2627import com .optimizely .ab .android .sdk .OptimizelyClient ;
2728
2829import java .util .HashMap ;
3536import com .fasterxml .jackson .databind .ObjectMapper ;
3637import com .optimizely .ab .android .sdk .OptimizelyManager ;
3738import 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 ;
3842import com .optimizely .ab .event .LogEvent ;
3943import com .optimizely .ab .notification .DecisionNotification ;
44+ import com .optimizely .ab .notification .NotificationCenter ;
4045import com .optimizely .ab .notification .TrackNotification ;
4146import com .optimizely .ab .notification .UpdateConfigNotification ;
4247import com .optimizely .ab .optimizelyconfig .OptimizelyConfig ;
4954import java .util .Collections ;
5055import java .util .LinkedHashMap ;
5156import java .util .List ;
57+ import java .util .concurrent .ArrayBlockingQueue ;
5258import java .util .concurrent .TimeUnit ;
5359
5460public 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 );
0 commit comments