@@ -81,11 +81,12 @@ public void setup() {
8181 public void callBackOrchestrate () throws InterruptedException {
8282 List <String > receivedHashValues = new ArrayList <>();
8383
84+ final CountDownLatch completionTracker = new CountDownLatch (expectedHashValues .size ()); // used to keep track of the progress of the subsequent calls
85+
8486 userIdService .request ().accept (MediaType .APPLICATION_JSON ).async ().get (new InvocationCallback <List <Long >>() {
8587 @ Override
8688 public void completed (List <Long > employeeIds ) {
8789 logger .info ("[CallbackExample] id-service result: {}" , employeeIds );
88- CountDownLatch completionTracker = new CountDownLatch (employeeIds .size ()); // used to keep track of the progress of the subsequent calls
8990 employeeIds .forEach ((id ) -> {
9091 // for each employee ID, get the name
9192 nameService .resolveTemplate ("userId" , id ).request ().async ().get (new InvocationCallback <String >() {
@@ -94,39 +95,28 @@ public void completed(List<Long> employeeIds) {
9495 public void completed (String response ) {
9596 logger .info ("[CallbackExample] name-service result: {}" , response );
9697
97- completionTracker .countDown ();
9898 hashService .resolveTemplate ("rawValue" , response + id ).request ().async ().get (new InvocationCallback <String >() {
9999 @ Override
100100 public void completed (String response ) {
101101 logger .info ("[CallbackExample] hash-service result: {}" , response );
102102 receivedHashValues .add (response );
103+ completionTracker .countDown ();
103104 }
104105
105106 @ Override
106107 public void failed (Throwable throwable ) {
107- completionTracker .countDown ();
108108 logger .warn ("[CallbackExample] An error has occurred in the hashing request step!" , throwable );
109109 }
110110 });
111111 }
112112
113113 @ Override
114114 public void failed (Throwable throwable ) {
115- completionTracker .countDown ();
116115 logger .warn ("[CallbackExample] An error has occurred in the username request step!" , throwable );
117116 }
118117 });
119118 });
120119
121- try {
122- // wait for inner requests to complete in 10 seconds
123- if (!completionTracker .await (10 , TimeUnit .SECONDS )) {
124- logger .warn ("[CallbackExample] Some requests didn't complete within the timeout" );
125- }
126- } catch (InterruptedException e ) {
127- logger .error ("Interrupted!" , e );
128- }
129-
130120 }
131121
132122 @ Override
@@ -136,7 +126,14 @@ public void failed(Throwable throwable) {
136126 });
137127
138128 // wait for async calls to complete
139- Thread .sleep (1000 );
129+ try {
130+ // wait for inner requests to complete in 10 seconds
131+ if (!completionTracker .await (10 , TimeUnit .SECONDS )) {
132+ logger .warn ("[CallbackExample] Some requests didn't complete within the timeout" );
133+ }
134+ } catch (InterruptedException e ) {
135+ logger .error ("Interrupted!" , e );
136+ }
140137
141138 assertThat (receivedHashValues ).containsAll (expectedHashValues );
142139 }
@@ -145,6 +142,8 @@ public void failed(Throwable throwable) {
145142 public void rxOrchestrate () throws InterruptedException {
146143 List <String > receivedHashValues = new ArrayList <>();
147144
145+ final CountDownLatch completionTracker = new CountDownLatch (expectedHashValues .size ()); // used to keep track of the progress of the subsequent calls
146+
148147 CompletionStage <List <Long >> userIdStage = userIdService .request ().accept (MediaType .APPLICATION_JSON ).rx ().get (new GenericType <List <Long >>() {
149148 }).exceptionally ((throwable ) -> {
150149 logger .warn ("[CompletionStageExample] An error has occurred" );
@@ -161,8 +160,10 @@ public void rxOrchestrate() throws InterruptedException {
161160 hashService .resolveTemplate ("rawValue" , userName + id ).request ().rx ().get (String .class ).toCompletableFuture ().thenAcceptAsync (hashValue -> {
162161 logger .info ("[CompletionStageExample] hash-service result: {}" , hashValue );
163162 receivedHashValues .add (hashValue );
163+ completionTracker .countDown ();
164164 }).exceptionally ((throwable ) -> {
165165 logger .warn ("[CompletionStageExample] Hash computation failed for {}" , id );
166+ completionTracker .countDown ();
166167 return null ;
167168 });
168169
@@ -172,7 +173,14 @@ public void rxOrchestrate() throws InterruptedException {
172173 });
173174
174175 // wait for async calls to complete
175- Thread .sleep (1000 );
176+ try {
177+ // wait for inner requests to complete in 10 seconds
178+ if (!completionTracker .await (10 , TimeUnit .SECONDS )) {
179+ logger .warn ("[CallbackExample] Some requests didn't complete within the timeout" );
180+ }
181+ } catch (InterruptedException e ) {
182+ logger .error ("Interrupted!" , e );
183+ }
176184
177185 assertThat (receivedHashValues ).containsAll (expectedHashValues );
178186 }
@@ -181,13 +189,15 @@ public void rxOrchestrate() throws InterruptedException {
181189 public void observableJavaOrchestrate () throws InterruptedException {
182190 List <String > receivedHashValues = new ArrayList <>();
183191
192+ final CountDownLatch completionTracker = new CountDownLatch (expectedHashValues .size ()); // used to keep track of the progress of the subsequent calls
193+
184194 Observable <List <Long >> observableUserIdService = userIdService .register (RxObservableInvokerProvider .class ).request ().accept (MediaType .APPLICATION_JSON ).rx (RxObservableInvoker .class ).get (new GenericType <List <Long >>() {
185195 }).asObservable ();
186196
187197 observableUserIdService .subscribe ((List <Long > employeeIds ) -> {
188198 logger .info ("[ObservableExample] id-service result: {}" , employeeIds );
189199 Observable .from (employeeIds ).subscribe (id -> nameService .register (RxObservableInvokerProvider .class ).resolveTemplate ("userId" , id ).request ().rx (RxObservableInvoker .class ).get (String .class ).asObservable () // gotten the name for the given
190- // userId
200+ // userId
191201 .doOnError ((throwable ) -> {
192202 logger .warn ("[ObservableExample] An error has occurred in the username request step {}" , throwable .getMessage ());
193203 }).subscribe (userName -> {
@@ -197,14 +207,22 @@ public void observableJavaOrchestrate() throws InterruptedException {
197207 .doOnError ((throwable ) -> {
198208 logger .warn ("[ObservableExample] An error has occurred in the hashing request step {}" , throwable .getMessage ());
199209 }).subscribe (hashValue -> {
200- logger .info ("[ObservableExample] hash-service result: {}" , hashValue );
201- receivedHashValues .add (hashValue );
202- });
210+ logger .info ("[ObservableExample] hash-service result: {}" , hashValue );
211+ receivedHashValues .add (hashValue );
212+ completionTracker .countDown ();
213+ });
203214 }));
204215 });
205216
206217 // wait for async calls to complete
207- Thread .sleep (1000 );
218+ try {
219+ // wait for inner requests to complete in 10 seconds
220+ if (!completionTracker .await (10 , TimeUnit .SECONDS )) {
221+ logger .warn ("[CallbackExample] Some requests didn't complete within the timeout" );
222+ }
223+ } catch (InterruptedException e ) {
224+ logger .error ("Interrupted!" , e );
225+ }
208226
209227 assertThat (receivedHashValues ).containsAll (expectedHashValues );
210228 }
@@ -213,6 +231,8 @@ public void observableJavaOrchestrate() throws InterruptedException {
213231 public void flowableJavaOrchestrate () throws InterruptedException {
214232 List <String > receivedHashValues = new ArrayList <>();
215233
234+ final CountDownLatch completionTracker = new CountDownLatch (expectedHashValues .size ()); // used to keep track of the progress of the subsequent calls
235+
216236 Flowable <List <Long >> userIdFlowable = userIdService .register (RxFlowableInvokerProvider .class ).request ().rx (RxFlowableInvoker .class ).get (new GenericType <List <Long >>() {
217237 });
218238
@@ -223,20 +243,28 @@ public void flowableJavaOrchestrate() throws InterruptedException {
223243 .doOnError ((throwable ) -> {
224244 logger .warn ("[FlowableExample] An error has occurred in the username request step {}" , throwable .getMessage ());
225245 }).subscribe (userName -> {
226- logger .info ("[FlowableExample] name-service result: {}" , userName );
227- hashService .register (RxFlowableInvokerProvider .class ).resolveTemplate ("rawValue" , userName + id ).request ().rx (RxFlowableInvoker .class ).get (String .class ) // gotten the hash value for userId+username
228- .doOnError ((throwable ) -> {
229- logger .warn (" [FlowableExample] An error has occurred in the hashing request step!" , throwable );
230- }).subscribe (hashValue -> {
231- logger .info ("[FlowableExample] hash-service result: {}" , hashValue );
232- receivedHashValues .add (hashValue );
233- });
234- });
246+ logger .info ("[FlowableExample] name-service result: {}" , userName );
247+ hashService .register (RxFlowableInvokerProvider .class ).resolveTemplate ("rawValue" , userName + id ).request ().rx (RxFlowableInvoker .class ).get (String .class ) // gotten the hash value for userId+username
248+ .doOnError ((throwable ) -> {
249+ logger .warn (" [FlowableExample] An error has occurred in the hashing request step!" , throwable );
250+ }).subscribe (hashValue -> {
251+ logger .info ("[FlowableExample] hash-service result: {}" , hashValue );
252+ receivedHashValues .add (hashValue );
253+ completionTracker .countDown ();
254+ });
255+ });
235256 });
236257 });
237258
238259 // wait for async calls to complete
239- Thread .sleep (1000 );
260+ try {
261+ // wait for inner requests to complete in 10 seconds
262+ if (!completionTracker .await (10 , TimeUnit .SECONDS )) {
263+ logger .warn ("[CallbackExample] Some requests didn't complete within the timeout" );
264+ }
265+ } catch (InterruptedException e ) {
266+ logger .error ("Interrupted!" , e );
267+ }
240268
241269 assertThat (receivedHashValues ).containsAll (expectedHashValues );
242270 }
0 commit comments