File tree Expand file tree Collapse file tree
BasicPubSub/src/main/java/pubsub
RawPubSub/src/main/java/rawpubsub Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2222import software .amazon .awssdk .iot .iotjobs .model .RejectedError ;
2323
2424import java .io .UnsupportedEncodingException ;
25+ import java .nio .charset .StandardCharsets ;
2526import java .util .UUID ;
2627import java .util .concurrent .CompletableFuture ;
28+ import java .util .concurrent .CountDownLatch ;
2729import java .util .concurrent .ExecutionException ;
2830
2931class PubSub {
@@ -291,13 +293,12 @@ public void onConnectionResumed(boolean sessionPresent) {
291293 throw new RuntimeException ("Exception occurred during connect" , ex );
292294 }
293295
296+ CountDownLatch countDownLatch = new CountDownLatch (messagesToPublish );
297+
294298 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 ();
301302 });
302303
303304 subscribed .get ();
@@ -308,6 +309,8 @@ public void onConnectionResumed(boolean sessionPresent) {
308309 published .get ();
309310 Thread .sleep (1000 );
310311 }
312+
313+ countDownLatch .await ();
311314
312315 CompletableFuture <Void > disconnected = connection .disconnect ();
313316 disconnected .get ();
Original file line number Diff line number Diff line change 1616import software .amazon .awssdk .iot .iotjobs .model .RejectedError ;
1717
1818import java .io .UnsupportedEncodingException ;
19+ import java .nio .charset .StandardCharsets ;
1920import java .util .Arrays ;
2021import java .util .List ;
2122import java .util .UUID ;
2223import java .util .concurrent .CompletableFuture ;
24+ import java .util .concurrent .CountDownLatch ;
2325import java .util .concurrent .ExecutionException ;
2426
2527class RawPubSub {
@@ -216,13 +218,12 @@ public void onConnectionResumed(boolean sessionPresent) {
216218 throw new RuntimeException ("Exception occurred during connect" , ex );
217219 }
218220
221+ CountDownLatch countDownLatch = new CountDownLatch (messagesToPublish );
222+
219223 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 ();
226227 });
227228
228229 subscribed .get ();
@@ -233,6 +234,8 @@ public void onConnectionResumed(boolean sessionPresent) {
233234 published .get ();
234235 Thread .sleep (1000 );
235236 }
237+
238+ countDownLatch .await ();
236239
237240 CompletableFuture <Void > disconnected = connection .disconnect ();
238241 disconnected .get ();
You can’t perform that action at this time.
0 commit comments