1919import java .time .LocalDate ;
2020import java .util .Timer ;
2121import java .util .TimerTask ;
22+ import java .util .concurrent .CompletableFuture ;
23+ import java .util .concurrent .ExecutorService ;
24+ import java .util .concurrent .Executors ;
2225
2326public class ExceptionlessClient {
2427 private static final Logger LOG = LoggerFactory .getLogger (ExceptionlessClient .class );
2528 private static final String UPDATE_SETTINGS_TIMER_NAME = "update-settings-timer" ;
2629 private static final int UPDATE_SETTINGS_TIMER_INITIAL_DELAY = 5000 ;
30+ private static final Integer DEFAULT_NTHREADS = 10 ;
2731
2832 @ Getter private final ConfigurationManager configurationManager ;
2933 private final EventPluginRunner eventPluginRunner ;
3034 private final Timer updateSettingsTimer ;
35+ private final ExecutorService executorService ;
3136
3237 @ 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 );
3943 }
4044
4145 @ VisibleForTesting
4246 ExceptionlessClient (
43- ConfigurationManager configurationManager , long updateSettingsTimerInitialDelay ) {
47+ ConfigurationManager configurationManager ,
48+ long updateSettingsTimerInitialDelay ,
49+ Integer nThreads ) {
4450 this .configurationManager = configurationManager ;
4551 this .eventPluginRunner =
4652 EventPluginRunner .builder ().configurationManager (this .configurationManager ).build ();
4753 this .updateSettingsTimer = new Timer (UPDATE_SETTINGS_TIMER_NAME );
54+ this .executorService = Executors .newFixedThreadPool (nThreads );
4855 init (updateSettingsTimerInitialDelay );
4956 }
5057
@@ -80,6 +87,10 @@ public static ExceptionlessClient from(String apiKey, String serverUrl) {
8087 .build ();
8188 }
8289
90+ public CompletableFuture <Void > submitExceptionAsync (Exception exception ) {
91+ return CompletableFuture .runAsync (() -> submitException (exception ), executorService );
92+ }
93+
8394 public void submitException (Exception exception ) {
8495 Event event = createException ().build ();
8596 PluginContext pluginContext = PluginContext .builder ().exception (exception ).build ();
@@ -90,6 +101,12 @@ public Event.EventBuilder createException() {
90101 return createEvent ().type (EventType .ERROR .value ());
91102 }
92103
104+ public CompletableFuture <Void > submitUnhandledExceptionAsync (
105+ Exception exception , String submissionMethod ) {
106+ return CompletableFuture .runAsync (
107+ () -> submitUnhandledException (exception , submissionMethod ), executorService );
108+ }
109+
93110 public void submitUnhandledException (Exception exception , String submissionMethod ) {
94111 Event event = createException ().build ();
95112 PluginContext pluginContext =
@@ -101,6 +118,10 @@ public void submitUnhandledException(Exception exception, String submissionMetho
101118 submitEvent (EventPluginContext .builder ().event (event ).context (pluginContext ).build ());
102119 }
103120
121+ public CompletableFuture <Void > submitFeatureUsageAsync (String feature ) {
122+ return CompletableFuture .runAsync (() -> submitFeatureUsage (feature ), executorService );
123+ }
124+
104125 public void submitFeatureUsage (String feature ) {
105126 Event event = createFeatureUsage (feature ).build ();
106127 submitEvent (EventPluginContext .from (event ));
@@ -110,14 +131,26 @@ public Event.EventBuilder createFeatureUsage(String feature) {
110131 return createEvent ().type (EventType .USAGE .value ()).source (feature );
111132 }
112133
134+ public CompletableFuture <Void > submitLogAsync (String message ) {
135+ return CompletableFuture .runAsync (() -> submitLog (message ), executorService );
136+ }
137+
113138 public void submitLog (String message ) {
114139 submitLog (message , null , null );
115140 }
116141
142+ public CompletableFuture <Void > submitLogAsync (String message , String source ) {
143+ return CompletableFuture .runAsync (() -> submitLog (message , source ), executorService );
144+ }
145+
117146 public void submitLog (String message , String source ) {
118147 submitLog (message , source , null );
119148 }
120149
150+ public CompletableFuture <Void > submitLogAsync (String message , String source , String level ) {
151+ return CompletableFuture .runAsync (() -> submitLog (message , source , level ), executorService );
152+ }
153+
121154 public void submitLog (String message , String source , String level ) {
122155 Event event = createLog (message , source , level ).build ();
123156 submitEvent (EventPluginContext .from (event ));
@@ -151,6 +184,10 @@ public Event.EventBuilder createLog(String message, String source, String level)
151184 return builder .property (EventPropertyKey .LOG_LEVEL .value (), level );
152185 }
153186
187+ public CompletableFuture <Void > submitNotFoundAsync (String resource ) {
188+ return CompletableFuture .runAsync (() -> submitNotFound (resource ), executorService );
189+ }
190+
154191 public void submitNotFound (String resource ) {
155192 Event event = createNotFound (resource ).build ();
156193 submitEvent (EventPluginContext .from (event ));
@@ -160,6 +197,10 @@ public Event.EventBuilder createNotFound(String resource) {
160197 return createEvent ().type (EventType .NOT_FOUND .value ()).source (resource );
161198 }
162199
200+ public CompletableFuture <Void > submitSessionStartAsync () {
201+ return CompletableFuture .runAsync (this ::submitSessionStart , executorService );
202+ }
203+
163204 public void submitSessionStart () {
164205 Event event = createSessionStart ().build ();
165206 submitEvent (EventPluginContext .from (event ));
@@ -175,16 +216,29 @@ public Event.EventBuilder createEvent() {
175216 .date (LocalDate .now ());
176217 }
177218
178- // todo this should be async
219+ public CompletableFuture <Void > submitEventAsync (EventPluginContext eventPluginContext ) {
220+ return CompletableFuture .runAsync (() -> submitEvent (eventPluginContext ), executorService );
221+ }
222+
179223 public void submitEvent (EventPluginContext eventPluginContext ) {
180224 eventPluginRunner .run (eventPluginContext );
181225 }
182226
227+ public CompletableFuture <Void > submitSessionEndAsync (String sessionOrUserId ) {
228+ return CompletableFuture .runAsync (() -> submitSessionEnd (sessionOrUserId ), executorService );
229+ }
230+
183231 public void submitSessionEnd (String sessionOrUserId ) {
184232 LOG .info (String .format ("Submitting session end: %s" , sessionOrUserId ));
185233 configurationManager .getSubmissionClient ().sendHeartBeat (sessionOrUserId , true );
186234 }
187235
236+ public CompletableFuture <SubmissionResponse > updateEmailAndDescriptionAsync (
237+ String referenceId , String email , String description ) {
238+ return CompletableFuture .supplyAsync (
239+ () -> updateEmailAndDescription (referenceId , email , description ), executorService );
240+ }
241+
188242 public SubmissionResponse updateEmailAndDescription (
189243 String referenceId , String email , String description ) {
190244 SubmissionResponse response =
0 commit comments