24
24
import com .optimizely .ab .config .parser .ConfigParseException ;
25
25
import com .optimizely .ab .error .ErrorHandler ;
26
26
import com .optimizely .ab .error .NoOpErrorHandler ;
27
- import com .optimizely .ab .event .EventHandler ;
28
- import com .optimizely .ab .event .LogEvent ;
29
- import com .optimizely .ab .event .NoopEventHandler ;
27
+ import com .optimizely .ab .event .*;
30
28
import com .optimizely .ab .event .internal .*;
31
29
import com .optimizely .ab .event .internal .payload .EventBatch ;
32
30
import com .optimizely .ab .notification .*;
@@ -78,6 +76,8 @@ public class Optimizely implements AutoCloseable {
78
76
@ VisibleForTesting
79
77
final EventHandler eventHandler ;
80
78
@ VisibleForTesting
79
+ final EventProcessor eventProcessor ;
80
+ @ VisibleForTesting
81
81
final ErrorHandler errorHandler ;
82
82
83
83
private final ProjectConfigManager projectConfigManager ;
@@ -89,13 +89,15 @@ public class Optimizely implements AutoCloseable {
89
89
private final UserProfileService userProfileService ;
90
90
91
91
private Optimizely (@ Nonnull EventHandler eventHandler ,
92
+ @ Nonnull EventProcessor eventProcessor ,
92
93
@ Nonnull ErrorHandler errorHandler ,
93
94
@ Nonnull DecisionService decisionService ,
94
95
@ Nullable UserProfileService userProfileService ,
95
96
@ Nonnull ProjectConfigManager projectConfigManager ,
96
97
@ Nonnull NotificationCenter notificationCenter
97
98
) {
98
99
this .eventHandler = eventHandler ;
100
+ this .eventProcessor = eventProcessor ;
99
101
this .errorHandler = errorHandler ;
100
102
this .decisionService = decisionService ;
101
103
this .userProfileService = userProfileService ;
@@ -137,6 +139,7 @@ private void tryClose(Object obj) {
137
139
*/
138
140
@ Override
139
141
public void close () {
142
+ tryClose (eventProcessor );
140
143
tryClose (eventHandler );
141
144
tryClose (projectConfigManager );
142
145
}
@@ -230,28 +233,25 @@ private void sendImpression(@Nonnull ProjectConfig projectConfig,
230
233
return ;
231
234
}
232
235
233
- logger .info ("Activating user \" {}\" in experiment \" {}\" ." , userId , experiment .getKey ());
234
236
UserEvent userEvent = UserEventFactory .createImpressionEvent (
235
237
projectConfig ,
236
238
experiment ,
237
239
variation ,
238
240
userId ,
239
241
filteredAttributes );
240
242
241
- LogEvent impressionEvent = EventFactory .createLogEvent (userEvent );
242
-
243
- try {
244
- eventHandler .dispatchEvent (impressionEvent );
245
- } catch (Exception e ) {
246
- logger .error ("Unexpected exception in event dispatcher" , e );
247
- }
243
+ eventProcessor .process (userEvent );
244
+ logger .info ("Activating user \" {}\" in experiment \" {}\" ." , userId , experiment .getKey ());
248
245
249
246
// Kept For backwards compatibility.
250
247
// This notification is deprecated and the new DecisionNotifications
251
248
// are sent via their respective method calls.
252
- ActivateNotification activateNotification = new ActivateNotification (
253
- experiment , userId , filteredAttributes , variation , impressionEvent );
254
- notificationCenter .send (activateNotification );
249
+ if (notificationCenter .getNotificationManager (ActivateNotification .class ).size () > 0 ) {
250
+ LogEvent impressionEvent = EventFactory .createLogEvent (userEvent );
251
+ ActivateNotification activateNotification = new ActivateNotification (
252
+ experiment , userId , filteredAttributes , variation , impressionEvent );
253
+ notificationCenter .send (activateNotification );
254
+ }
255
255
}
256
256
257
257
//======== track calls ========//
@@ -309,20 +309,17 @@ public void track(@Nonnull String eventName,
309
309
copiedAttributes ,
310
310
eventTags );
311
311
312
- // create the conversion event request parameters, then dispatch
313
- LogEvent conversionEvent = EventFactory .createLogEvent (userEvent );
312
+ eventProcessor .process (userEvent );
314
313
logger .info ("Tracking event \" {}\" for user \" {}\" ." , eventName , userId );
315
314
316
- try {
317
- eventHandler .dispatchEvent (conversionEvent );
318
- } catch (Exception e ) {
319
- logger .error ("Unexpected exception in event dispatcher" , e );
320
- }
321
-
322
- TrackNotification notification = new TrackNotification (eventName , userId ,
323
- copiedAttributes , eventTags , conversionEvent );
315
+ if (notificationCenter .getNotificationManager (TrackNotification .class ).size () > 0 ) {
316
+ // create the conversion event request parameters, then dispatch
317
+ LogEvent conversionEvent = EventFactory .createLogEvent (userEvent );
318
+ TrackNotification notification = new TrackNotification (eventName , userId ,
319
+ copiedAttributes , eventTags , conversionEvent );
324
320
325
- notificationCenter .send (notification );
321
+ notificationCenter .send (notification );
322
+ }
326
323
}
327
324
328
325
//======== FeatureFlag APIs ========//
@@ -1000,6 +997,7 @@ public static class Builder {
1000
997
private DecisionService decisionService ;
1001
998
private ErrorHandler errorHandler ;
1002
999
private EventHandler eventHandler ;
1000
+ private EventProcessor eventProcessor ;
1003
1001
private ProjectConfig projectConfig ;
1004
1002
private ProjectConfigManager projectConfigManager ;
1005
1003
private UserProfileService userProfileService ;
@@ -1027,6 +1025,11 @@ public Builder withEventHandler(EventHandler eventHandler) {
1027
1025
return this ;
1028
1026
}
1029
1027
1028
+ public Builder withEventProcessor (EventProcessor eventProcessor ) {
1029
+ this .eventProcessor = eventProcessor ;
1030
+ return this ;
1031
+ }
1032
+
1030
1033
public Builder withUserProfileService (UserProfileService userProfileService ) {
1031
1034
this .userProfileService = userProfileService ;
1032
1035
return this ;
@@ -1094,6 +1097,11 @@ public Optimizely build() {
1094
1097
decisionService = new DecisionService (bucketer , errorHandler , userProfileService );
1095
1098
}
1096
1099
1100
+ // For backwards compatibility
1101
+ if (eventProcessor == null ) {
1102
+ eventProcessor = new ForwardingEventProcessor (eventHandler );
1103
+ }
1104
+
1097
1105
if (projectConfig == null && datafile != null && !datafile .isEmpty ()) {
1098
1106
try {
1099
1107
projectConfig = new DatafileProjectConfig .Builder ().withDatafile (datafile ).build ();
@@ -1117,7 +1125,7 @@ public Optimizely build() {
1117
1125
notificationCenter = new NotificationCenter ();
1118
1126
}
1119
1127
1120
- return new Optimizely (eventHandler , errorHandler , decisionService , userProfileService , projectConfigManager , notificationCenter );
1128
+ return new Optimizely (eventHandler , eventProcessor , errorHandler , decisionService , userProfileService , projectConfigManager , notificationCenter );
1121
1129
}
1122
1130
}
1123
1131
}
0 commit comments