1
1
package com .prashantchaubey .exceptionlessclient ;
2
2
3
+ import com .prashantchaubey .exceptionlessclient .configuration .Configuration ;
3
4
import com .prashantchaubey .exceptionlessclient .configuration .ConfigurationManager ;
4
5
import com .prashantchaubey .exceptionlessclient .models .Event ;
5
6
import com .prashantchaubey .exceptionlessclient .models .EventPluginContext ;
8
9
import com .prashantchaubey .exceptionlessclient .models .enums .EventPropertyKey ;
9
10
import com .prashantchaubey .exceptionlessclient .models .enums .EventType ;
10
11
import com .prashantchaubey .exceptionlessclient .models .submission .SubmissionResponse ;
11
- import com .prashantchaubey .exceptionlessclient .plugins .EventPluginManager ;
12
+ import com .prashantchaubey .exceptionlessclient .plugins .EventPluginRunner ;
12
13
import lombok .Builder ;
13
- import lombok .Getter ;
14
14
15
15
import java .time .LocalDate ;
16
16
import java .util .Timer ;
17
17
import java .util .TimerTask ;
18
- import java .util .function .Consumer ;
19
18
20
- @ Builder (builderClassName = "ExceptionlessClientInternalBuilder" )
21
- @ Getter
22
19
public class ExceptionlessClient {
23
20
private static final int UPDATE_SETTINGS_TIMER_INITIAL_DELAY = 5000 ;
24
21
25
22
private ConfigurationManager configurationManager ;
23
+ private EventPluginRunner eventPluginRunner ;
24
+ private Timer updateSettingsTimer ;
26
25
27
- // lombok ignored fields
28
- private EventPluginManager $eventPluginManager ;
29
- private Timer $updateSettingsTimer = new Timer ();
26
+ @ Builder
27
+ public ExceptionlessClient (ConfigurationManager configurationManager ) {
28
+ this .configurationManager = configurationManager ;
29
+ this .eventPluginRunner =
30
+ EventPluginRunner .builder ().configurationManager (this .configurationManager ).build ();
31
+ this .updateSettingsTimer = new Timer ();
32
+ init ();
33
+ }
34
+
35
+ private void init () {
36
+ updateSettingsTimer .schedule (
37
+ new TimerTask () {
38
+ @ Override
39
+ public void run () {
40
+ configurationManager .getSettingsManager ().updateSettingsThreadSafe ();
41
+ }
42
+ },
43
+ UPDATE_SETTINGS_TIMER_INITIAL_DELAY ,
44
+ configurationManager .getConfiguration ().getUpdateSettingsWhenIdleInterval ());
45
+
46
+ configurationManager .onChanged (
47
+ ignored -> configurationManager .getSettingsManager ().updateSettingsThreadSafe ());
48
+ configurationManager
49
+ .getQueue ()
50
+ .onEventsPosted (
51
+ (ignored1 , ignored2 ) ->
52
+ configurationManager .getSettingsManager ().updateSettingsThreadSafe ());
53
+ }
30
54
31
55
public static ExceptionlessClient from (String apiKey , String serverUrl ) {
32
56
return ExceptionlessClient .builder ()
33
- .configurationManager (ConfigurationManager .from (apiKey , serverUrl ))
57
+ .configurationManager (
58
+ ConfigurationManager .builder ()
59
+ .configuration (Configuration .builder ().apiKey (apiKey ).serverUrl (serverUrl ).build ())
60
+ .build ())
34
61
.build ();
35
62
}
36
63
37
- public void submitException (Exception exception , Consumer < EventPluginContext > handler ) {
64
+ public void submitException (Exception exception ) {
38
65
Event event = createException ().build ();
39
66
PluginContext pluginContext = PluginContext .builder ().exception (exception ).build ();
40
- submitEvent (EventPluginContext .builder ().event (event ).context (pluginContext ).build (), handler );
67
+ submitEvent (EventPluginContext .builder ().event (event ).context (pluginContext ).build ());
41
68
}
42
69
43
- private Event .EventBuilderImpl createException () {
70
+ private Event .EventBuilder createException () {
44
71
return createEvent ().type (EventType .ERROR .value ());
45
72
}
46
73
47
- public void submitUnhandledException (
48
- Exception exception , String submissionMethod , Consumer <EventPluginContext > handler ) {
74
+ public void submitUnhandledException (Exception exception , String submissionMethod ) {
49
75
Event event = createException ().build ();
50
76
PluginContext pluginContext =
51
77
PluginContext .builder ()
52
78
.exception (exception )
53
- .markAsUnhandledError ( )
79
+ .unhandledError ( true )
54
80
.submissionMethod (submissionMethod )
55
81
.build ();
56
- submitEvent (EventPluginContext .builder ().event (event ).context (pluginContext ).build (), handler );
82
+ submitEvent (EventPluginContext .builder ().event (event ).context (pluginContext ).build ());
57
83
}
58
84
59
- public void submitFeatureUsage (String feature , Consumer < EventPluginContext > handler ) {
85
+ public void submitFeatureUsage (String feature ) {
60
86
Event event = createFeatureUsage (feature ).build ();
61
- submitEvent (EventPluginContext .from (event ), handler );
87
+ submitEvent (EventPluginContext .from (event ));
62
88
}
63
89
64
- private Event .EventBuilderImpl createFeatureUsage (String feature ) {
90
+ private Event .EventBuilder createFeatureUsage (String feature ) {
65
91
return createEvent ().type (EventType .USAGE .value ()).source (feature );
66
92
}
67
93
68
- public void submitLog (String message , Consumer < EventPluginContext > handler ) {
69
- submitLog (message , null , null , handler );
94
+ public void submitLog (String message ) {
95
+ submitLog (message , null , null );
70
96
}
71
97
72
- public void submitLog (String message , String source , Consumer < EventPluginContext > handler ) {
73
- submitLog (message , source , null , handler );
98
+ public void submitLog (String message , String source ) {
99
+ submitLog (message , source , null );
74
100
}
75
101
76
- public void submitLog (
77
- String message , String source , String level , Consumer <EventPluginContext > handler ) {
102
+ public void submitLog (String message , String source , String level ) {
78
103
Event event = createLog (message , source , level ).build ();
79
- submitEvent (EventPluginContext .from (event ), handler );
104
+ submitEvent (EventPluginContext .from (event ));
80
105
}
81
106
82
- private Event .EventBuilderImpl createLog (String message , String source , String level ) {
107
+ private Event .EventBuilder createLog (String message , String source , String level ) {
83
108
if (source == null ) {
84
109
// Calling method
85
110
source = Thread .currentThread ().getStackTrace ()[2 ].getMethodName ();
86
111
}
87
112
88
- Event .EventBuilderImpl builder =
113
+ Event .EventBuilder builder =
89
114
createEvent ().type (EventType .LOG .value ()).source (source ).message (message );
90
115
if (level == null ) {
91
116
return builder ;
@@ -94,44 +119,33 @@ private Event.EventBuilderImpl createLog(String message, String source, String l
94
119
return builder .property (EventPropertyKey .LOG_LEVEL .value (), level );
95
120
}
96
121
97
- public void submitNotFound (String resource , Consumer < EventPluginContext > handler ) {
122
+ public void submitNotFound (String resource ) {
98
123
Event event = createNotFound (resource ).build ();
99
- submitEvent (EventPluginContext .from (event ), handler );
124
+ submitEvent (EventPluginContext .from (event ));
100
125
}
101
126
102
- private Event .EventBuilderImpl createNotFound (String resource ) {
127
+ private Event .EventBuilder createNotFound (String resource ) {
103
128
return createEvent ().type (EventType .NOT_FOUND .value ()).source (resource );
104
129
}
105
130
106
- public void submitSessionStart (Consumer < EventPluginContext > handler ) {
131
+ public void submitSessionStart () {
107
132
Event event = createSessionStart ().build ();
108
- submitEvent (EventPluginContext .from (event ), handler );
133
+ submitEvent (EventPluginContext .from (event ));
109
134
}
110
135
111
- private Event .EventBuilderImpl createSessionStart () {
136
+ private Event .EventBuilder createSessionStart () {
112
137
return createEvent ().type (EventType .SESSION .value ());
113
138
}
114
139
115
- private Event .EventBuilderImpl createEvent () {
116
- return Event .builder (configurationManager .getDataExclusions ()).date (LocalDate .now ());
140
+ private Event .EventBuilder createEvent () {
141
+ return Event .builder ()
142
+ .dataExclusions (configurationManager .getDataExclusions ())
143
+ .date (LocalDate .now ());
117
144
}
118
145
119
- private void submitEvent (
120
- EventPluginContext eventPluginContext , Consumer <EventPluginContext > handler ) {
121
- $eventPluginManager .run (
122
- eventPluginContext ,
123
- evc -> {
124
- if (evc .getContext ().isEventCancelled ()) {
125
- return ;
126
- }
127
- configurationManager .getQueue ().enqueue (evc .getEvent ());
128
- if (evc .getEvent ().getReferenceId () != null ) {
129
- configurationManager
130
- .getLastReferenceIdManager ()
131
- .setLast (evc .getEvent ().getReferenceId ());
132
- }
133
- handler .accept (evc );
134
- });
146
+ // todo this should be async
147
+ private void submitEvent (EventPluginContext eventPluginContext ) {
148
+ eventPluginRunner .run (eventPluginContext );
135
149
}
136
150
137
151
public void submitSessionEnd (String sessionOrUserId ) {
@@ -141,12 +155,8 @@ public void submitSessionEnd(String sessionOrUserId) {
141
155
configurationManager .getSubmissionClient ().sendHeartBeat (sessionOrUserId , true );
142
156
}
143
157
144
- public void submitSessionHeartbeat (String sessionOrUserId ) {
145
- configurationManager .submitSessionHeartbeat (sessionOrUserId );
146
- }
147
-
148
- public void updateEmailAndDescription (
149
- String referenceId , String email , String description , Consumer <SubmissionResponse > handler ) {
158
+ public SubmissionResponse updateEmailAndDescription (
159
+ String referenceId , String email , String description ) {
150
160
SubmissionResponse response =
151
161
configurationManager
152
162
.getSubmissionClient ()
@@ -160,38 +170,11 @@ public void updateEmailAndDescription(
160
170
String .format (
161
171
"Failed to submit user email and description for event: %s" , referenceId ));
162
172
}
163
- handler .accept (response );
173
+
174
+ return response ;
164
175
}
165
176
166
177
public String getLastReferenceId () {
167
178
return configurationManager .getLastReferenceIdManager ().getLast ();
168
179
}
169
-
170
- public static ExceptionlessClientBuilder builder () {
171
- return new ExceptionlessClientBuilder ();
172
- }
173
-
174
- public static class ExceptionlessClientBuilder extends ExceptionlessClientInternalBuilder {
175
- @ Override
176
- public ExceptionlessClient build () {
177
- ExceptionlessClient client = super .build ();
178
- client .init ();
179
-
180
- return client ;
181
- }
182
- }
183
-
184
- private void init () {
185
- $updateSettingsTimer .schedule (
186
- new TimerTask () {
187
- @ Override
188
- public void run () {
189
- configurationManager .getSettingsManager ().updateSettingsThreadSafe ();
190
- }
191
- },
192
- UPDATE_SETTINGS_TIMER_INITIAL_DELAY ,
193
- configurationManager .getConfiguration ().getUpdateSettingsWhenIdleInterval ());
194
- $eventPluginManager =
195
- EventPluginManager .builder ().configurationManager (configurationManager ).build ();
196
- }
197
180
}
0 commit comments