21
21
import java .time .Duration ;
22
22
import java .time .ZonedDateTime ;
23
23
import java .util .ArrayList ;
24
+ import java .util .Arrays ;
25
+ import java .util .Collection ;
24
26
import java .util .List ;
25
27
import java .util .concurrent .CompletableFuture ;
26
28
import java .util .concurrent .ConcurrentHashMap ;
27
- import java .util .concurrent .ConcurrentMap ;
28
29
import java .util .concurrent .TimeUnit ;
29
30
import java .util .concurrent .locks .ReentrantLock ;
30
31
@@ -46,7 +47,7 @@ public class Service {
46
47
47
48
private final Connection conn ;
48
49
private final Duration drainTimeout ;
49
- private final ConcurrentMap <String , EndpointContext > serviceContexts ;
50
+ private final ConcurrentHashMap <String , EndpointContext > serviceContexts ;
50
51
private final List <EndpointContext > discoveryContexts ;
51
52
private final List <Dispatcher > dInternals ;
52
53
private final PingResponse pingResponse ;
@@ -70,9 +71,7 @@ public class Service {
70
71
// set up the service contexts
71
72
// ? do we need an internal dispatcher for any user endpoints !! addServiceEndpoint deals with it
72
73
serviceContexts = new ConcurrentHashMap <>();
73
- for (ServiceEndpoint se : b .serviceEndpoints .values ()) {
74
- addServiceEndpoint (se );
75
- }
74
+ addServiceEndpoints (b .serviceEndpoints .values ());
76
75
77
76
Dispatcher dTemp = null ;
78
77
if (b .pingDispatcher == null || b .infoDispatcher == null || b .statsDispatcher == null ) {
@@ -87,31 +86,41 @@ public class Service {
87
86
}
88
87
89
88
/**
90
- * Adds a service endpoint to the list of service contexts and starts it if the service is running.
91
- * @param se the service endpoint to be added
89
+ * Adds one or more service endpoint to the list of service contexts and starts it if the service is running.
90
+ * @param serviceEndpoints one or more service endpoints to be added
92
91
*/
93
- public void addServiceEndpoint (ServiceEndpoint se ) {
94
- // do this first so it's available on start
95
- infoResponse .addServiceEndpoint (se );
96
-
97
- EndpointContext ctx ;
98
- if (se .getDispatcher () == null ) {
99
- Dispatcher dTemp = dInternals .isEmpty () ? null : dInternals .get (0 );
100
- if (dTemp == null ) {
101
- dTemp = conn .createDispatcher ();
102
- dInternals .add (dTemp );
103
- }
104
- ctx = new EndpointContext (conn , dTemp , false , se );
105
- } else {
106
- ctx = new EndpointContext (conn , null , false , se );
107
- }
108
- serviceContexts .put (se .getName (), ctx );
92
+ public void addServiceEndpoints (ServiceEndpoint ... serviceEndpoints ) {
93
+ addServiceEndpoints (Arrays .asList (serviceEndpoints ));
94
+ }
109
95
110
- // if the service is already started, start the newly added context
96
+ /**
97
+ * Adds all service endpoints to the list of service contexts and starts it if the service is running.
98
+ * @param serviceEndpoints service endpoints to be added
99
+ */
100
+ public void addServiceEndpoints (Collection <ServiceEndpoint > serviceEndpoints ) {
111
101
startStopLock .lock ();
112
102
try {
113
- if (runningIndicator != null ) {
114
- ctx .start ();
103
+ // do this first so it's available on start
104
+ infoResponse .addServiceEndpoints (serviceEndpoints );
105
+ for (ServiceEndpoint se : serviceEndpoints ) {
106
+ EndpointContext ctx ;
107
+ if (se .getDispatcher () == null ) {
108
+ Dispatcher dTemp = dInternals .isEmpty () ? null : dInternals .get (0 );
109
+ if (dTemp == null ) {
110
+ dTemp = conn .createDispatcher ();
111
+ dInternals .add (dTemp );
112
+ }
113
+ ctx = new EndpointContext (conn , dTemp , false , se );
114
+ }
115
+ else {
116
+ ctx = new EndpointContext (conn , null , false , se );
117
+ }
118
+ serviceContexts .put (se .getName (), ctx );
119
+
120
+ // if the service is already started, start the newly added context
121
+ if (runningIndicator != null ) {
122
+ ctx .start ();
123
+ }
115
124
}
116
125
}
117
126
finally {
@@ -242,7 +251,7 @@ public void stop(boolean drain, Throwable t) {
242
251
for (EndpointContext c : serviceContexts .values ()) {
243
252
if (c .isNotInternalDispatcher ()) {
244
253
try {
245
- futures .add (c .getSub (). drain (drainTimeout ));
254
+ futures .add (c .drain (drainTimeout ));
246
255
}
247
256
catch (Exception e ) { /* nothing I can really do, we are stopping anyway */ }
248
257
}
@@ -251,7 +260,7 @@ public void stop(boolean drain, Throwable t) {
251
260
for (EndpointContext c : discoveryContexts ) {
252
261
if (c .isNotInternalDispatcher ()) {
253
262
try {
254
- futures .add (c .getSub (). drain (drainTimeout ));
263
+ futures .add (c .drain (drainTimeout ));
255
264
}
256
265
catch (Exception e ) { /* nothing I can really do, we are stopping anyway */ }
257
266
}
0 commit comments