11package data .artists ;
22
3- import com .neovisionaries .i18n .CountryCode ;
43import com .wrapper .spotify .SpotifyApi ;
54import com .wrapper .spotify .exceptions .SpotifyWebApiException ;
65import com .wrapper .spotify .model_objects .specification .AlbumSimplified ;
76import com .wrapper .spotify .model_objects .specification .Paging ;
87import com .wrapper .spotify .requests .data .artists .GetArtistsAlbumsRequest ;
98
109import java .io .IOException ;
11- import java .util .concurrent .ExecutionException ;
12- import java .util .concurrent .Future ;
10+ import java .util .concurrent .CancellationException ;
11+ import java .util .concurrent .CompletableFuture ;
12+ import java .util .concurrent .CompletionException ;
1313
1414public class GetArtistsAlbumsExample {
1515 private static final String accessToken = "taHZ2SdB-bPA3FsK3D7ZN5npZS47cMy-IEySVEGttOhXmqaVAIo0ESvTCLjLBifhHOHOIuhFUKPW1WMDP7w6dj3MAZdWT8CLI2MkZaXbYLTeoDvXesf2eeiLYPBGdx8tIwQJKgV8XdnzH_DONk" ;
@@ -37,15 +37,18 @@ public static void getArtistsAlbums_Sync() {
3737
3838 public static void getArtistsAlbums_Async () {
3939 try {
40- final Future <Paging <AlbumSimplified >> pagingFuture = getArtistsAlbumsRequest .executeAsync ();
40+ final CompletableFuture <Paging <AlbumSimplified >> pagingFuture = getArtistsAlbumsRequest .executeAsync ();
4141
42- // ...
42+ // Thread free to do other tasks ...
4343
44- final Paging <AlbumSimplified > albumSimplifiedPaging = pagingFuture .get ();
44+ // Example Only. Never block in production code.
45+ final Paging <AlbumSimplified > albumSimplifiedPaging = pagingFuture .join ();
4546
4647 System .out .println ("Total: " + albumSimplifiedPaging .getTotal ());
47- } catch (InterruptedException | ExecutionException e ) {
48+ } catch (CompletionException e ) {
4849 System .out .println ("Error: " + e .getCause ().getMessage ());
50+ } catch (CancellationException e ) {
51+ System .out .println ("Async operation cancelled." );
4952 }
5053 }
5154}
0 commit comments