38
38
import java .util .*;
39
39
import java .util .concurrent .*;
40
40
import java .util .concurrent .atomic .AtomicInteger ;
41
+ import java .util .function .Supplier ;
41
42
42
43
import static io .nats .client .support .Encoding .*;
43
44
import static io .nats .client .support .NatsConstants .*;
@@ -226,6 +227,11 @@ public class Options {
226
227
*/
227
228
public static final boolean DEFAULT_DISCARD_MESSAGES_WHEN_OUTGOING_QUEUE_FULL = false ;
228
229
230
+ /**
231
+ * Default supplier for creating a single-threaded executor service.
232
+ */
233
+ public static final Supplier <ExecutorService > DEFAULT_SINGLE_THREAD_EXECUTOR = Executors ::newSingleThreadExecutor ;
234
+
229
235
// ----------------------------------------------------------------------------------------------------
230
236
// ENVIRONMENT PROPERTIES
231
237
// ----------------------------------------------------------------------------------------------------
@@ -644,6 +650,8 @@ public class Options {
644
650
private final boolean traceConnection ;
645
651
646
652
private final ExecutorService executor ;
653
+ private final ThreadFactory connectThreadFactory ;
654
+ private final ThreadFactory callbackThreadFactory ;
647
655
private final ServerPool serverPool ;
648
656
private final DispatcherFactory dispatcherFactory ;
649
657
@@ -759,6 +767,8 @@ public static class Builder {
759
767
private StatisticsCollector statisticsCollector = null ;
760
768
private String dataPortType = DEFAULT_DATA_PORT_TYPE ;
761
769
private ExecutorService executor ;
770
+ private ThreadFactory connectThreadFactory ;
771
+ private ThreadFactory callbackThreadFactory ;
762
772
private List <java .util .function .Consumer <HttpRequest >> httpRequestInterceptors ;
763
773
private Proxy proxy ;
764
774
@@ -1553,6 +1563,28 @@ public Builder executor(ExecutorService executor) {
1553
1563
return this ;
1554
1564
}
1555
1565
1566
+ /**
1567
+ * Sets custom thread factory for the executor service
1568
+ *
1569
+ * @param threadFactory the thread factory to use for the executor service
1570
+ * @return the Builder for chaining
1571
+ */
1572
+ public Builder connectThreadFactory (ThreadFactory threadFactory ) {
1573
+ this .connectThreadFactory = threadFactory ;
1574
+ return this ;
1575
+ }
1576
+
1577
+ /**
1578
+ * Sets custom thread factory for the executor service
1579
+ *
1580
+ * @param threadFactory the thread factory to use for the executor service
1581
+ * @return the Builder for chaining
1582
+ */
1583
+ public Builder callbackThreadFactory (ThreadFactory threadFactory ) {
1584
+ this .callbackThreadFactory = threadFactory ;
1585
+ return this ;
1586
+ }
1587
+
1556
1588
/**
1557
1589
* Add an HttpRequest interceptor which can be used to modify the HTTP request when using websockets
1558
1590
*
@@ -1914,6 +1946,8 @@ public Builder(Options o) {
1914
1946
this .dataPortType = o .dataPortType ;
1915
1947
this .trackAdvancedStats = o .trackAdvancedStats ;
1916
1948
this .executor = o .executor ;
1949
+ this .callbackThreadFactory = o .callbackThreadFactory ;
1950
+ this .connectThreadFactory = o .connectThreadFactory ;
1917
1951
this .httpRequestInterceptors = o .httpRequestInterceptors ;
1918
1952
this .proxy = o .proxy ;
1919
1953
@@ -1979,6 +2013,8 @@ private Options(Builder b) {
1979
2013
this .dataPortType = b .dataPortType ;
1980
2014
this .trackAdvancedStats = b .trackAdvancedStats ;
1981
2015
this .executor = b .executor ;
2016
+ this .callbackThreadFactory = b .callbackThreadFactory ;
2017
+ this .connectThreadFactory = b .connectThreadFactory ;
1982
2018
this .httpRequestInterceptors = b .httpRequestInterceptors ;
1983
2019
this .proxy = b .proxy ;
1984
2020
@@ -2002,6 +2038,22 @@ public ExecutorService getExecutor() {
2002
2038
return this .executor ;
2003
2039
}
2004
2040
2041
+ /**
2042
+ * @return the callback executor, see {@link Builder#callbackThreadFactory(ThreadFactory) callbackThreadFactory()} in the builder doc
2043
+ */
2044
+ public ExecutorService getCallbackExecutor () {
2045
+ return this .callbackThreadFactory == null ?
2046
+ DEFAULT_SINGLE_THREAD_EXECUTOR .get () : Executors .newSingleThreadExecutor (this .callbackThreadFactory );
2047
+ }
2048
+
2049
+ /**
2050
+ * @return the connect executor, see {@link Builder#connectThreadFactory(ThreadFactory) connectThreadFactory()} in the builder doc
2051
+ */
2052
+ public ExecutorService getConnectExecutor () {
2053
+ return this .connectThreadFactory == null ?
2054
+ DEFAULT_SINGLE_THREAD_EXECUTOR .get () : Executors .newSingleThreadExecutor (this .connectThreadFactory );
2055
+ }
2056
+
2005
2057
/**
2006
2058
* @return the list of HttpRequest interceptors.
2007
2059
*/
0 commit comments