Run YTTV manager initialization in a separate thread

This commit is contained in:
Alejandro 2020-02-27 19:48:17 +02:00
parent 202c1b655d
commit 518e12c6ad

View File

@ -77,42 +77,44 @@ public class YoutubeManager {
long millis = System.currentTimeMillis(); long millis = System.currentTimeMillis();
ExecutorService youtubeDataLoaderPool = Executors.newFixedThreadPool(10); Emulator.getThreading().run(() -> {
ExecutorService youtubeDataLoaderPool = Executors.newFixedThreadPool(10);
Emulator.getLogging().logStart("YouTube Manager -> Loading..."); Emulator.getLogging().logStart("YouTube Manager -> Loading...");
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM youtube_playlists")) { try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM youtube_playlists")) {
try (ResultSet set = statement.executeQuery()) { try (ResultSet set = statement.executeQuery()) {
while (set.next()) { while (set.next()) {
final int itemId = set.getInt("item_id"); final int itemId = set.getInt("item_id");
final String playlistId = set.getString("playlist_id"); final String playlistId = set.getString("playlist_id");
youtubeDataLoaderPool.submit(() -> { youtubeDataLoaderPool.submit(() -> {
ArrayList<YoutubePlaylist> playlists = this.playlists.getOrDefault(itemId, new ArrayList<>()); ArrayList<YoutubePlaylist> playlists = this.playlists.getOrDefault(itemId, new ArrayList<>());
YoutubePlaylist playlist = this.getPlaylistDataById(playlistId); YoutubePlaylist playlist = this.getPlaylistDataById(playlistId);
if (playlist != null) { if (playlist != null) {
playlists.add(playlist); playlists.add(playlist);
} else { } else {
Emulator.getLogging().logErrorLine("Failed to load YouTube playlist: " + playlistId); Emulator.getLogging().logErrorLine("Failed to load YouTube playlist: " + playlistId);
} }
this.playlists.put(itemId, playlists); this.playlists.put(itemId, playlists);
}); });
}
} }
} catch (SQLException e) {
Emulator.getLogging().logSQLException(e);
} }
} catch (SQLException e) {
Emulator.getLogging().logSQLException(e);
}
youtubeDataLoaderPool.shutdown(); youtubeDataLoaderPool.shutdown();
try { try {
youtubeDataLoaderPool.awaitTermination(60, TimeUnit.SECONDS); youtubeDataLoaderPool.awaitTermination(60, TimeUnit.SECONDS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
Emulator.getLogging().logStart("YouTube Manager -> Loaded! (" + (System.currentTimeMillis() - millis) + " MS)"); Emulator.getLogging().logStart("YouTube Manager -> Loaded! (" + (System.currentTimeMillis() - millis) + " MS)");
});
} }
public YoutubePlaylist getPlaylistDataById(String playlistId) { public YoutubePlaylist getPlaylistDataById(String playlistId) {