File tree Expand file tree Collapse file tree 2 files changed +18
-12
lines changed
BasicPubSub/src/main/java/pubsub
RawPubSub/src/main/java/rawpubsub Expand file tree Collapse file tree 2 files changed +18
-12
lines changed Original file line number Diff line number Diff line change 22
22
import software .amazon .awssdk .iot .iotjobs .model .RejectedError ;
23
23
24
24
import java .io .UnsupportedEncodingException ;
25
+ import java .nio .charset .StandardCharsets ;
25
26
import java .util .UUID ;
26
27
import java .util .concurrent .CompletableFuture ;
28
+ import java .util .concurrent .CountDownLatch ;
27
29
import java .util .concurrent .ExecutionException ;
28
30
29
31
class PubSub {
@@ -291,13 +293,12 @@ public void onConnectionResumed(boolean sessionPresent) {
291
293
throw new RuntimeException ("Exception occurred during connect" , ex );
292
294
}
293
295
296
+ CountDownLatch countDownLatch = new CountDownLatch (messagesToPublish );
297
+
294
298
CompletableFuture <Integer > subscribed = connection .subscribe (topic , QualityOfService .AT_LEAST_ONCE , (message ) -> {
295
- try {
296
- String payload = new String (message .getPayload (), "UTF-8" );
297
- System .out .println ("MESSAGE: " + payload );
298
- } catch (UnsupportedEncodingException ex ) {
299
- System .out .println ("Unable to decode payload: " + ex .getMessage ());
300
- }
299
+ String payload = new String (message .getPayload (), StandardCharsets .UTF_8 );
300
+ System .out .println ("MESSAGE: " + payload );
301
+ countDownLatch .countDown ();
301
302
});
302
303
303
304
subscribed .get ();
@@ -308,6 +309,8 @@ public void onConnectionResumed(boolean sessionPresent) {
308
309
published .get ();
309
310
Thread .sleep (1000 );
310
311
}
312
+
313
+ countDownLatch .await ();
311
314
312
315
CompletableFuture <Void > disconnected = connection .disconnect ();
313
316
disconnected .get ();
Original file line number Diff line number Diff line change 16
16
import software .amazon .awssdk .iot .iotjobs .model .RejectedError ;
17
17
18
18
import java .io .UnsupportedEncodingException ;
19
+ import java .nio .charset .StandardCharsets ;
19
20
import java .util .Arrays ;
20
21
import java .util .List ;
21
22
import java .util .UUID ;
22
23
import java .util .concurrent .CompletableFuture ;
24
+ import java .util .concurrent .CountDownLatch ;
23
25
import java .util .concurrent .ExecutionException ;
24
26
25
27
class RawPubSub {
@@ -216,13 +218,12 @@ public void onConnectionResumed(boolean sessionPresent) {
216
218
throw new RuntimeException ("Exception occurred during connect" , ex );
217
219
}
218
220
221
+ CountDownLatch countDownLatch = new CountDownLatch (messagesToPublish );
222
+
219
223
CompletableFuture <Integer > subscribed = connection .subscribe (topic , QualityOfService .AT_LEAST_ONCE , (message ) -> {
220
- try {
221
- String payload = new String (message .getPayload (), "UTF-8" );
222
- System .out .println ("MESSAGE: " + payload );
223
- } catch (UnsupportedEncodingException ex ) {
224
- System .out .println ("Unable to decode payload: " + ex .getMessage ());
225
- }
224
+ String payload = new String (message .getPayload (), StandardCharsets .UTF_8 );
225
+ System .out .println ("MESSAGE: " + payload );
226
+ countDownLatch .countDown ();
226
227
});
227
228
228
229
subscribed .get ();
@@ -233,6 +234,8 @@ public void onConnectionResumed(boolean sessionPresent) {
233
234
published .get ();
234
235
Thread .sleep (1000 );
235
236
}
237
+
238
+ countDownLatch .await ();
236
239
237
240
CompletableFuture <Void > disconnected = connection .disconnect ();
238
241
disconnected .get ();
You can’t perform that action at this time.
0 commit comments