19
19
import java .time .LocalDate ;
20
20
import java .util .Timer ;
21
21
import java .util .TimerTask ;
22
+ import java .util .concurrent .CompletableFuture ;
23
+ import java .util .concurrent .ExecutorService ;
24
+ import java .util .concurrent .Executors ;
22
25
23
26
public class ExceptionlessClient {
24
27
private static final Logger LOG = LoggerFactory .getLogger (ExceptionlessClient .class );
25
28
private static final String UPDATE_SETTINGS_TIMER_NAME = "update-settings-timer" ;
26
29
private static final int UPDATE_SETTINGS_TIMER_INITIAL_DELAY = 5000 ;
30
+ private static final Integer DEFAULT_NTHREADS = 10 ;
27
31
28
32
@ Getter private final ConfigurationManager configurationManager ;
29
33
private final EventPluginRunner eventPluginRunner ;
30
34
private final Timer updateSettingsTimer ;
35
+ private final ExecutorService executorService ;
31
36
32
37
@ Builder
33
- public ExceptionlessClient (ConfigurationManager configurationManager ) {
34
- this .configurationManager = configurationManager ;
35
- this .eventPluginRunner =
36
- EventPluginRunner .builder ().configurationManager (this .configurationManager ).build ();
37
- this .updateSettingsTimer = new Timer (UPDATE_SETTINGS_TIMER_NAME );
38
- init (UPDATE_SETTINGS_TIMER_INITIAL_DELAY );
38
+ public ExceptionlessClient (ConfigurationManager configurationManager , Integer nThreads ) {
39
+ this (
40
+ configurationManager ,
41
+ UPDATE_SETTINGS_TIMER_INITIAL_DELAY ,
42
+ nThreads == null ? DEFAULT_NTHREADS : nThreads );
39
43
}
40
44
41
45
@ VisibleForTesting
42
46
ExceptionlessClient (
43
- ConfigurationManager configurationManager , long updateSettingsTimerInitialDelay ) {
47
+ ConfigurationManager configurationManager ,
48
+ long updateSettingsTimerInitialDelay ,
49
+ Integer nThreads ) {
44
50
this .configurationManager = configurationManager ;
45
51
this .eventPluginRunner =
46
52
EventPluginRunner .builder ().configurationManager (this .configurationManager ).build ();
47
53
this .updateSettingsTimer = new Timer (UPDATE_SETTINGS_TIMER_NAME );
54
+ this .executorService = Executors .newFixedThreadPool (nThreads );
48
55
init (updateSettingsTimerInitialDelay );
49
56
}
50
57
@@ -80,6 +87,10 @@ public static ExceptionlessClient from(String apiKey, String serverUrl) {
80
87
.build ();
81
88
}
82
89
90
+ public CompletableFuture <Void > submitExceptionAsync (Exception exception ) {
91
+ return CompletableFuture .runAsync (() -> submitException (exception ), executorService );
92
+ }
93
+
83
94
public void submitException (Exception exception ) {
84
95
Event event = createException ().build ();
85
96
PluginContext pluginContext = PluginContext .builder ().exception (exception ).build ();
@@ -90,6 +101,12 @@ public Event.EventBuilder createException() {
90
101
return createEvent ().type (EventType .ERROR .value ());
91
102
}
92
103
104
+ public CompletableFuture <Void > submitUnhandledExceptionAsync (
105
+ Exception exception , String submissionMethod ) {
106
+ return CompletableFuture .runAsync (
107
+ () -> submitUnhandledException (exception , submissionMethod ), executorService );
108
+ }
109
+
93
110
public void submitUnhandledException (Exception exception , String submissionMethod ) {
94
111
Event event = createException ().build ();
95
112
PluginContext pluginContext =
@@ -101,6 +118,10 @@ public void submitUnhandledException(Exception exception, String submissionMetho
101
118
submitEvent (EventPluginContext .builder ().event (event ).context (pluginContext ).build ());
102
119
}
103
120
121
+ public CompletableFuture <Void > submitFeatureUsageAsync (String feature ) {
122
+ return CompletableFuture .runAsync (() -> submitFeatureUsage (feature ), executorService );
123
+ }
124
+
104
125
public void submitFeatureUsage (String feature ) {
105
126
Event event = createFeatureUsage (feature ).build ();
106
127
submitEvent (EventPluginContext .from (event ));
@@ -110,14 +131,26 @@ public Event.EventBuilder createFeatureUsage(String feature) {
110
131
return createEvent ().type (EventType .USAGE .value ()).source (feature );
111
132
}
112
133
134
+ public CompletableFuture <Void > submitLogAsync (String message ) {
135
+ return CompletableFuture .runAsync (() -> submitLog (message ), executorService );
136
+ }
137
+
113
138
public void submitLog (String message ) {
114
139
submitLog (message , null , null );
115
140
}
116
141
142
+ public CompletableFuture <Void > submitLogAsync (String message , String source ) {
143
+ return CompletableFuture .runAsync (() -> submitLog (message , source ), executorService );
144
+ }
145
+
117
146
public void submitLog (String message , String source ) {
118
147
submitLog (message , source , null );
119
148
}
120
149
150
+ public CompletableFuture <Void > submitLogAsync (String message , String source , String level ) {
151
+ return CompletableFuture .runAsync (() -> submitLog (message , source , level ), executorService );
152
+ }
153
+
121
154
public void submitLog (String message , String source , String level ) {
122
155
Event event = createLog (message , source , level ).build ();
123
156
submitEvent (EventPluginContext .from (event ));
@@ -151,6 +184,10 @@ public Event.EventBuilder createLog(String message, String source, String level)
151
184
return builder .property (EventPropertyKey .LOG_LEVEL .value (), level );
152
185
}
153
186
187
+ public CompletableFuture <Void > submitNotFoundAsync (String resource ) {
188
+ return CompletableFuture .runAsync (() -> submitNotFound (resource ), executorService );
189
+ }
190
+
154
191
public void submitNotFound (String resource ) {
155
192
Event event = createNotFound (resource ).build ();
156
193
submitEvent (EventPluginContext .from (event ));
@@ -160,6 +197,10 @@ public Event.EventBuilder createNotFound(String resource) {
160
197
return createEvent ().type (EventType .NOT_FOUND .value ()).source (resource );
161
198
}
162
199
200
+ public CompletableFuture <Void > submitSessionStartAsync () {
201
+ return CompletableFuture .runAsync (this ::submitSessionStart , executorService );
202
+ }
203
+
163
204
public void submitSessionStart () {
164
205
Event event = createSessionStart ().build ();
165
206
submitEvent (EventPluginContext .from (event ));
@@ -175,16 +216,29 @@ public Event.EventBuilder createEvent() {
175
216
.date (LocalDate .now ());
176
217
}
177
218
178
- // todo this should be async
219
+ public CompletableFuture <Void > submitEventAsync (EventPluginContext eventPluginContext ) {
220
+ return CompletableFuture .runAsync (() -> submitEvent (eventPluginContext ), executorService );
221
+ }
222
+
179
223
public void submitEvent (EventPluginContext eventPluginContext ) {
180
224
eventPluginRunner .run (eventPluginContext );
181
225
}
182
226
227
+ public CompletableFuture <Void > submitSessionEndAsync (String sessionOrUserId ) {
228
+ return CompletableFuture .runAsync (() -> submitSessionEnd (sessionOrUserId ), executorService );
229
+ }
230
+
183
231
public void submitSessionEnd (String sessionOrUserId ) {
184
232
LOG .info (String .format ("Submitting session end: %s" , sessionOrUserId ));
185
233
configurationManager .getSubmissionClient ().sendHeartBeat (sessionOrUserId , true );
186
234
}
187
235
236
+ public CompletableFuture <SubmissionResponse > updateEmailAndDescriptionAsync (
237
+ String referenceId , String email , String description ) {
238
+ return CompletableFuture .supplyAsync (
239
+ () -> updateEmailAndDescription (referenceId , email , description ), executorService );
240
+ }
241
+
188
242
public SubmissionResponse updateEmailAndDescription (
189
243
String referenceId , String email , String description ) {
190
244
SubmissionResponse response =
0 commit comments